/* Test memcmp/strncmp builtin expansion for compilation and proper execution.  */
/* { dg-do run } */
/* { dg-options "-O2" } */
/* { dg-require-effective-target ptr32plus } */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>

int lib_memcmp(const void *a, const void *b, size_t n) asm("memcmp");
int lib_strncmp(const char *a, const char *b, size_t n) asm("strncmp");

#ifndef NRAND
#define NRAND 10000
#endif
#define MAX_SZ 200

static void test_driver_memcmp (void (test_memcmp)(const char *, const char *, int),
				void (test_strncmp)(const char *, const char *, int),
				size_t sz, int align)
{
  char buf1[MAX_SZ*2+10],buf2[MAX_SZ*2+10];
  size_t test_sz = (sz<MAX_SZ)?sz:MAX_SZ;
  size_t diff_pos, zero_pos;
  uint32_t e;
  int i,j,l;
  for(l=0;l<sz;l++) {
    for(i=0;i<NRAND/sz;i++) {
      for(j=0;j<l;j++) {
	buf1[j] = rand() & 0xff;
	buf2[j] = buf1[j];
      }
      for(j=l;j<sz;j++) {
	buf1[j] = rand() & 0xff;
	buf2[j] = rand() & 0xff;
      }
    }
    e = lib_memcmp(buf1,buf2,sz);
    (*test_memcmp)(buf1,buf2,e);
    e = lib_strncmp(buf1,buf2,sz);
    (*test_strncmp)(buf1,buf2,e);
  }
  for(diff_pos = ((test_sz>10)?(test_sz-10):0); diff_pos < test_sz+10; diff_pos++)
    for(zero_pos = ((test_sz>10)?(test_sz-10):0); zero_pos < test_sz+10; zero_pos++)
      {
	memset(buf1, 'A', 2*test_sz);
	memset(buf2, 'A', 2*test_sz);
	buf2[diff_pos] = 'B';
	buf1[zero_pos] = 0;
	buf2[zero_pos] = 0;
	e = lib_memcmp(buf1,buf2,sz);
	(*test_memcmp)(buf1,buf2,e);
	(*test_memcmp)(buf2,buf1,-e);
	(*test_memcmp)(buf2,buf2,0);
	e = lib_strncmp(buf1,buf2,sz);
	(*test_strncmp)(buf1,buf2,e);
	(*test_strncmp)(buf2,buf1,-e);
	(*test_strncmp)(buf2,buf2,0);
	/* differing length: */
	buf2[diff_pos] = 0;
	e = lib_memcmp(buf1,buf2,sz);
	(*test_memcmp)(buf1,buf2,e);
	e = lib_strncmp(buf1,buf2,sz);
	(*test_strncmp)(buf1,buf2,e);
	memset(buf2+diff_pos,'B',sizeof(buf2)-diff_pos);
	buf2[zero_pos] = 0;
	e = lib_memcmp(buf1,buf2,sz);
	(*test_memcmp)(buf1,buf2,e);
	(*test_memcmp)(buf2,buf1,-e);
	e = lib_strncmp(buf1,buf2,sz);
	(*test_strncmp)(buf1,buf2,e);
	(*test_strncmp)(buf2,buf1,-e);
      }
}

#define RUN_TEST(SZ, ALIGN) test_driver_memcmp (test_memcmp_ ## SZ ## _ ## ALIGN, test_strncmp_ ## SZ ## _ ## ALIGN, SZ, ALIGN);

#define DEF_TEST(SZ, ALIGN)						\
  static void test_memcmp_ ## SZ ## _ ## ALIGN (const char *str1, const char *str2, int expect)	\
{									\
  char three[8192] __attribute__ ((aligned (4096)));			\
  char four[8192] __attribute__ ((aligned (4096)));			\
  char *a, *b;								\
  int i,j,r;								\
  for (j = 0; j < 2; j++)						\
    {									\
      for (i = 0; i < 2; i++)						\
	{								\
	  a = three+i*ALIGN+j*(4096-2*i*ALIGN);				\
	  b = four+i*ALIGN+j*(4096-2*i*ALIGN);				\
	  memcpy(a,str1,SZ);						\
	  memcpy(b,str2,SZ);						\
	  r = memcmp(a,b,SZ);						\
	  if ( r < 0 && !(expect < 0) ) abort();			\
	  if ( r > 0 && !(expect > 0) )	abort();			\
	  if ( r == 0 && !(expect == 0) ) abort();			\
	}								\
    }									\
}									\
static void test_strncmp_ ## SZ ## _ ## ALIGN (const char *str1, const char *str2, int expect)	 \
{									\
  char three[8192] __attribute__ ((aligned (4096)));			\
  char four[8192] __attribute__ ((aligned (4096)));			\
  char *a, *b;								\
  int i,j,r;								\
  for (j = 0; j < 2; j++)						\
    {									\
      for (i = 0; i < 2; i++)						\
	{								\
	  a = three+i*ALIGN+j*(4096-2*i*ALIGN);				\
	  b = four+i*ALIGN+j*(4096-2*i*ALIGN);				\
	  strcpy(a,str1);						\
	  strcpy(b,str2);						\
	  r = strncmp(a,b,SZ);						\
	  if ( r < 0 && !(expect < 0) ) abort();			\
	  if ( r > 0 && !(expect > 0) )	abort();			\
	  if ( r == 0 && !(expect == 0) ) abort();			\
	}								\
    }									\
}

#ifdef TEST_ALL
DEF_TEST(1,1)
DEF_TEST(1,2)
DEF_TEST(1,4)
DEF_TEST(1,8)
DEF_TEST(1,16)
DEF_TEST(2,1)
DEF_TEST(2,2)
DEF_TEST(2,4)
DEF_TEST(2,8)
DEF_TEST(2,16)
DEF_TEST(3,1)
DEF_TEST(3,2)
DEF_TEST(3,4)
DEF_TEST(3,8)
DEF_TEST(3,16)
DEF_TEST(4,1)
DEF_TEST(4,2)
DEF_TEST(4,4)
DEF_TEST(4,8)
DEF_TEST(4,16)
DEF_TEST(5,1)
DEF_TEST(5,2)
DEF_TEST(5,4)
DEF_TEST(5,8)
DEF_TEST(5,16)
DEF_TEST(6,1)
DEF_TEST(6,2)
DEF_TEST(6,4)
DEF_TEST(6,8)
DEF_TEST(6,16)
DEF_TEST(7,1)
DEF_TEST(7,2)
DEF_TEST(7,4)
DEF_TEST(7,8)
DEF_TEST(7,16)
DEF_TEST(8,1)
DEF_TEST(8,2)
DEF_TEST(8,4)
DEF_TEST(8,8)
DEF_TEST(8,16)
DEF_TEST(9,1)
DEF_TEST(9,2)
DEF_TEST(9,4)
DEF_TEST(9,8)
DEF_TEST(9,16)
DEF_TEST(10,1)
DEF_TEST(10,2)
DEF_TEST(10,4)
DEF_TEST(10,8)
DEF_TEST(10,16)
DEF_TEST(11,1)
DEF_TEST(11,2)
DEF_TEST(11,4)
DEF_TEST(11,8)
DEF_TEST(11,16)
DEF_TEST(12,1)
DEF_TEST(12,2)
DEF_TEST(12,4)
DEF_TEST(12,8)
DEF_TEST(12,16)
DEF_TEST(13,1)
DEF_TEST(13,2)
DEF_TEST(13,4)
DEF_TEST(13,8)
DEF_TEST(13,16)
DEF_TEST(14,1)
DEF_TEST(14,2)
DEF_TEST(14,4)
DEF_TEST(14,8)
DEF_TEST(14,16)
DEF_TEST(15,1)
DEF_TEST(15,2)
DEF_TEST(15,4)
DEF_TEST(15,8)
DEF_TEST(15,16)
DEF_TEST(16,1)
DEF_TEST(16,2)
DEF_TEST(16,4)
DEF_TEST(16,8)
DEF_TEST(16,16)
DEF_TEST(17,1)
DEF_TEST(17,2)
DEF_TEST(17,4)
DEF_TEST(17,8)
DEF_TEST(17,16)
DEF_TEST(18,1)
DEF_TEST(18,2)
DEF_TEST(18,4)
DEF_TEST(18,8)
DEF_TEST(18,16)
DEF_TEST(19,1)
DEF_TEST(19,2)
DEF_TEST(19,4)
DEF_TEST(19,8)
DEF_TEST(19,16)
DEF_TEST(20,1)
DEF_TEST(20,2)
DEF_TEST(20,4)
DEF_TEST(20,8)
DEF_TEST(20,16)
DEF_TEST(21,1)
DEF_TEST(21,2)
DEF_TEST(21,4)
DEF_TEST(21,8)
DEF_TEST(21,16)
DEF_TEST(22,1)
DEF_TEST(22,2)
DEF_TEST(22,4)
DEF_TEST(22,8)
DEF_TEST(22,16)
DEF_TEST(23,1)
DEF_TEST(23,2)
DEF_TEST(23,4)
DEF_TEST(23,8)
DEF_TEST(23,16)
DEF_TEST(24,1)
DEF_TEST(24,2)
DEF_TEST(24,4)
DEF_TEST(24,8)
DEF_TEST(24,16)
DEF_TEST(25,1)
DEF_TEST(25,2)
DEF_TEST(25,4)
DEF_TEST(25,8)
DEF_TEST(25,16)
DEF_TEST(26,1)
DEF_TEST(26,2)
DEF_TEST(26,4)
DEF_TEST(26,8)
DEF_TEST(26,16)
DEF_TEST(27,1)
DEF_TEST(27,2)
DEF_TEST(27,4)
DEF_TEST(27,8)
DEF_TEST(27,16)
DEF_TEST(28,1)
DEF_TEST(28,2)
DEF_TEST(28,4)
DEF_TEST(28,8)
DEF_TEST(28,16)
DEF_TEST(29,1)
DEF_TEST(29,2)
DEF_TEST(29,4)
DEF_TEST(29,8)
DEF_TEST(29,16)
DEF_TEST(30,1)
DEF_TEST(30,2)
DEF_TEST(30,4)
DEF_TEST(30,8)
DEF_TEST(30,16)
DEF_TEST(31,1)
DEF_TEST(31,2)
DEF_TEST(31,4)
DEF_TEST(31,8)
DEF_TEST(31,16)
DEF_TEST(32,1)
DEF_TEST(32,2)
DEF_TEST(32,4)
DEF_TEST(32,8)
DEF_TEST(32,16)
DEF_TEST(33,1)
DEF_TEST(33,2)
DEF_TEST(33,4)
DEF_TEST(33,8)
DEF_TEST(33,16)
DEF_TEST(34,1)
DEF_TEST(34,2)
DEF_TEST(34,4)
DEF_TEST(34,8)
DEF_TEST(34,16)
DEF_TEST(35,1)
DEF_TEST(35,2)
DEF_TEST(35,4)
DEF_TEST(35,8)
DEF_TEST(35,16)
DEF_TEST(36,1)
DEF_TEST(36,2)
DEF_TEST(36,4)
DEF_TEST(36,8)
DEF_TEST(36,16)
DEF_TEST(37,1)
DEF_TEST(37,2)
DEF_TEST(37,4)
DEF_TEST(37,8)
DEF_TEST(37,16)
DEF_TEST(38,1)
DEF_TEST(38,2)
DEF_TEST(38,4)
DEF_TEST(38,8)
DEF_TEST(38,16)
DEF_TEST(39,1)
DEF_TEST(39,2)
DEF_TEST(39,4)
DEF_TEST(39,8)
DEF_TEST(39,16)
DEF_TEST(40,1)
DEF_TEST(40,2)
DEF_TEST(40,4)
DEF_TEST(40,8)
DEF_TEST(40,16)
DEF_TEST(41,1)
DEF_TEST(41,2)
DEF_TEST(41,4)
DEF_TEST(41,8)
DEF_TEST(41,16)
DEF_TEST(42,1)
DEF_TEST(42,2)
DEF_TEST(42,4)
DEF_TEST(42,8)
DEF_TEST(42,16)
DEF_TEST(43,1)
DEF_TEST(43,2)
DEF_TEST(43,4)
DEF_TEST(43,8)
DEF_TEST(43,16)
DEF_TEST(44,1)
DEF_TEST(44,2)
DEF_TEST(44,4)
DEF_TEST(44,8)
DEF_TEST(44,16)
DEF_TEST(45,1)
DEF_TEST(45,2)
DEF_TEST(45,4)
DEF_TEST(45,8)
DEF_TEST(45,16)
DEF_TEST(46,1)
DEF_TEST(46,2)
DEF_TEST(46,4)
DEF_TEST(46,8)
DEF_TEST(46,16)
DEF_TEST(47,1)
DEF_TEST(47,2)
DEF_TEST(47,4)
DEF_TEST(47,8)
DEF_TEST(47,16)
DEF_TEST(48,1)
DEF_TEST(48,2)
DEF_TEST(48,4)
DEF_TEST(48,8)
DEF_TEST(48,16)
DEF_TEST(49,1)
DEF_TEST(49,2)
DEF_TEST(49,4)
DEF_TEST(49,8)
DEF_TEST(49,16)
DEF_TEST(100,1)
DEF_TEST(100,2)
DEF_TEST(100,4)
DEF_TEST(100,8)
DEF_TEST(100,16)
#else
DEF_TEST(3,1)
DEF_TEST(4,1)
DEF_TEST(5,1)
DEF_TEST(5,8)
DEF_TEST(6,1)
DEF_TEST(6,4)
DEF_TEST(6,8)
DEF_TEST(7,1)
DEF_TEST(7,2)
DEF_TEST(7,4)
DEF_TEST(7,8)
DEF_TEST(8,1)
DEF_TEST(9,1)
DEF_TEST(16,1)
DEF_TEST(32,1)
DEF_TEST(100,1)
DEF_TEST(100,8)
#endif

int
main(int argc, char **argv)
{
#ifdef TEST_ALL
  RUN_TEST(1,1)
    RUN_TEST(1,2)
    RUN_TEST(1,4)
    RUN_TEST(1,8)
    RUN_TEST(1,16)
    RUN_TEST(2,1)
    RUN_TEST(2,2)
    RUN_TEST(2,4)
    RUN_TEST(2,8)
    RUN_TEST(2,16)
    RUN_TEST(3,1)
    RUN_TEST(3,2)
    RUN_TEST(3,4)
    RUN_TEST(3,8)
    RUN_TEST(3,16)
    RUN_TEST(4,1)
    RUN_TEST(4,2)
    RUN_TEST(4,4)
    RUN_TEST(4,8)
    RUN_TEST(4,16)
    RUN_TEST(5,1)
    RUN_TEST(5,2)
    RUN_TEST(5,4)
    RUN_TEST(5,8)
    RUN_TEST(5,16)
    RUN_TEST(6,1)
    RUN_TEST(6,2)
    RUN_TEST(6,4)
    RUN_TEST(6,8)
    RUN_TEST(6,16)
    RUN_TEST(7,1)
    RUN_TEST(7,2)
    RUN_TEST(7,4)
    RUN_TEST(7,8)
    RUN_TEST(7,16)
    RUN_TEST(8,1)
    RUN_TEST(8,2)
    RUN_TEST(8,4)
    RUN_TEST(8,8)
    RUN_TEST(8,16)
    RUN_TEST(9,1)
    RUN_TEST(9,2)
    RUN_TEST(9,4)
    RUN_TEST(9,8)
    RUN_TEST(9,16)
    RUN_TEST(10,1)
    RUN_TEST(10,2)
    RUN_TEST(10,4)
    RUN_TEST(10,8)
    RUN_TEST(10,16)
    RUN_TEST(11,1)
    RUN_TEST(11,2)
    RUN_TEST(11,4)
    RUN_TEST(11,8)
    RUN_TEST(11,16)
    RUN_TEST(12,1)
    RUN_TEST(12,2)
    RUN_TEST(12,4)
    RUN_TEST(12,8)
    RUN_TEST(12,16)
    RUN_TEST(13,1)
    RUN_TEST(13,2)
    RUN_TEST(13,4)
    RUN_TEST(13,8)
    RUN_TEST(13,16)
    RUN_TEST(14,1)
    RUN_TEST(14,2)
    RUN_TEST(14,4)
    RUN_TEST(14,8)
    RUN_TEST(14,16)
    RUN_TEST(15,1)
    RUN_TEST(15,2)
    RUN_TEST(15,4)
    RUN_TEST(15,8)
    RUN_TEST(15,16)
    RUN_TEST(16,1)
    RUN_TEST(16,2)
    RUN_TEST(16,4)
    RUN_TEST(16,8)
    RUN_TEST(16,16)
    RUN_TEST(17,1)
    RUN_TEST(17,2)
    RUN_TEST(17,4)
    RUN_TEST(17,8)
    RUN_TEST(17,16)
    RUN_TEST(18,1)
    RUN_TEST(18,2)
    RUN_TEST(18,4)
    RUN_TEST(18,8)
    RUN_TEST(18,16)
    RUN_TEST(19,1)
    RUN_TEST(19,2)
    RUN_TEST(19,4)
    RUN_TEST(19,8)
    RUN_TEST(19,16)
    RUN_TEST(20,1)
    RUN_TEST(20,2)
    RUN_TEST(20,4)
    RUN_TEST(20,8)
    RUN_TEST(20,16)
    RUN_TEST(21,1)
    RUN_TEST(21,2)
    RUN_TEST(21,4)
    RUN_TEST(21,8)
    RUN_TEST(21,16)
    RUN_TEST(22,1)
    RUN_TEST(22,2)
    RUN_TEST(22,4)
    RUN_TEST(22,8)
    RUN_TEST(22,16)
    RUN_TEST(23,1)
    RUN_TEST(23,2)
    RUN_TEST(23,4)
    RUN_TEST(23,8)
    RUN_TEST(23,16)
    RUN_TEST(24,1)
    RUN_TEST(24,2)
    RUN_TEST(24,4)
    RUN_TEST(24,8)
    RUN_TEST(24,16)
    RUN_TEST(25,1)
    RUN_TEST(25,2)
    RUN_TEST(25,4)
    RUN_TEST(25,8)
    RUN_TEST(25,16)
    RUN_TEST(26,1)
    RUN_TEST(26,2)
    RUN_TEST(26,4)
    RUN_TEST(26,8)
    RUN_TEST(26,16)
    RUN_TEST(27,1)
    RUN_TEST(27,2)
    RUN_TEST(27,4)
    RUN_TEST(27,8)
    RUN_TEST(27,16)
    RUN_TEST(28,1)
    RUN_TEST(28,2)
    RUN_TEST(28,4)
    RUN_TEST(28,8)
    RUN_TEST(28,16)
    RUN_TEST(29,1)
    RUN_TEST(29,2)
    RUN_TEST(29,4)
    RUN_TEST(29,8)
    RUN_TEST(29,16)
    RUN_TEST(30,1)
    RUN_TEST(30,2)
    RUN_TEST(30,4)
    RUN_TEST(30,8)
    RUN_TEST(30,16)
    RUN_TEST(31,1)
    RUN_TEST(31,2)
    RUN_TEST(31,4)
    RUN_TEST(31,8)
    RUN_TEST(31,16)
    RUN_TEST(32,1)
    RUN_TEST(32,2)
    RUN_TEST(32,4)
    RUN_TEST(32,8)
    RUN_TEST(32,16)
    RUN_TEST(33,1)
    RUN_TEST(33,2)
    RUN_TEST(33,4)
    RUN_TEST(33,8)
    RUN_TEST(33,16)
    RUN_TEST(34,1)
    RUN_TEST(34,2)
    RUN_TEST(34,4)
    RUN_TEST(34,8)
    RUN_TEST(34,16)
    RUN_TEST(35,1)
    RUN_TEST(35,2)
    RUN_TEST(35,4)
    RUN_TEST(35,8)
    RUN_TEST(35,16)
    RUN_TEST(36,1)
    RUN_TEST(36,2)
    RUN_TEST(36,4)
    RUN_TEST(36,8)
    RUN_TEST(36,16)
    RUN_TEST(37,1)
    RUN_TEST(37,2)
    RUN_TEST(37,4)
    RUN_TEST(37,8)
    RUN_TEST(37,16)
    RUN_TEST(38,1)
    RUN_TEST(38,2)
    RUN_TEST(38,4)
    RUN_TEST(38,8)
    RUN_TEST(38,16)
    RUN_TEST(39,1)
    RUN_TEST(39,2)
    RUN_TEST(39,4)
    RUN_TEST(39,8)
    RUN_TEST(39,16)
    RUN_TEST(40,1)
    RUN_TEST(40,2)
    RUN_TEST(40,4)
    RUN_TEST(40,8)
    RUN_TEST(40,16)
    RUN_TEST(41,1)
    RUN_TEST(41,2)
    RUN_TEST(41,4)
    RUN_TEST(41,8)
    RUN_TEST(41,16)
    RUN_TEST(42,1)
    RUN_TEST(42,2)
    RUN_TEST(42,4)
    RUN_TEST(42,8)
    RUN_TEST(42,16)
    RUN_TEST(43,1)
    RUN_TEST(43,2)
    RUN_TEST(43,4)
    RUN_TEST(43,8)
    RUN_TEST(43,16)
    RUN_TEST(44,1)
    RUN_TEST(44,2)
    RUN_TEST(44,4)
    RUN_TEST(44,8)
    RUN_TEST(44,16)
    RUN_TEST(45,1)
    RUN_TEST(45,2)
    RUN_TEST(45,4)
    RUN_TEST(45,8)
    RUN_TEST(45,16)
    RUN_TEST(46,1)
    RUN_TEST(46,2)
    RUN_TEST(46,4)
    RUN_TEST(46,8)
    RUN_TEST(46,16)
    RUN_TEST(47,1)
    RUN_TEST(47,2)
    RUN_TEST(47,4)
    RUN_TEST(47,8)
    RUN_TEST(47,16)
    RUN_TEST(48,1)
    RUN_TEST(48,2)
    RUN_TEST(48,4)
    RUN_TEST(48,8)
    RUN_TEST(48,16)
    RUN_TEST(49,1)
    RUN_TEST(49,2)
    RUN_TEST(49,4)
    RUN_TEST(49,8)
    RUN_TEST(49,16)
    RUN_TEST(100,1)
    RUN_TEST(100,2)
    RUN_TEST(100,4)
    RUN_TEST(100,8)
    RUN_TEST(100,16)
#else
    RUN_TEST(3,1)
    RUN_TEST(4,1)
    RUN_TEST(5,1)
    RUN_TEST(5,8)
    RUN_TEST(6,1)
    RUN_TEST(6,4)
    RUN_TEST(6,8)
    RUN_TEST(7,1)
    RUN_TEST(7,2)
    RUN_TEST(7,4)
    RUN_TEST(7,8)
    RUN_TEST(8,1)
    RUN_TEST(9,1)
    RUN_TEST(16,1)
    RUN_TEST(32,1)
    RUN_TEST(100,1)
    RUN_TEST(100,8)
#endif
}
