#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
int /*<<< orphan*/ PROT_NONE;
int /*<<< orphan*/ _SC_PAGESIZE;
int /*<<< orphan*/ abort();
char *aligned_alloc(long, int);
int lib_memcmp(char *, char *, size_t);
int lib_strncmp(char *, char *, size_t);
int /*<<< orphan*/ memset(char *, char, size_t);
int mprotect(char *, long, int /*<<< orphan*/);
void stub1(char *, char *, int);
void stub2(char *, char *, int);
long sysconf(int /*<<< orphan*/);
void test_driver_strncmp(void(test_strncmp)(const char *, const char *, int),
                         void(test_memcmp)(const void *, const void *, int),
                         size_t sz) {
  long pgsz = sysconf(_SC_PAGESIZE);
  char buf1[sz + 1];
  char *buf2 = aligned_alloc(pgsz, 2 * pgsz);
  char *p2;
  int r, i, e;
  r = mprotect(buf2 + pgsz, pgsz, PROT_NONE);
  if (r < 0)
    abort();
  memset(buf1, 'A', sz);
  for (i = 10; i >= 0; i--) {
    p2 = buf2 + pgsz - sz - i;
    memset(p2, 'A', sz);
    e = lib_strncmp(buf1, p2, sz);
    (*test_strncmp)(buf1, p2, e);
    e = lib_memcmp(buf1, p2, sz);
    (*test_memcmp)(buf1, p2, e);
  }
}


x86 assembly (clang, -O0):
<test_driver_strncmp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
call   1080 <sysconf@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdx
mov    %rdx,%rax
add    $0x1,%rax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
add    $0x10,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x58(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x20(%rbp),%rax
shl    $0x1,%rax
mov    %eax,%esi
call   1090 <aligned_alloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
add    -0x20(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    0x2e05(%rip),%rax
mov    (%rax),%edx
call   1070 <mprotect@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jge    1206 <test_driver_strncmp+0x96>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x58(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x41,%esi
call   1060 <memset@plt>
movl   $0xa,-0x48(%rbp)
cmpl   $0x0,-0x48(%rbp)
jl     12b4 <test_driver_strncmp+0x144>
mov    -0x38(%rbp),%rax
add    -0x20(%rbp),%rax
xor    %ecx,%ecx
sub    -0x18(%rbp),%rcx
add    %rcx,%rax
movslq -0x48(%rbp),%rdx
xor    %ecx,%ecx
sub    %rdx,%rcx
add    %rcx,%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x41,%esi
call   1060 <memset@plt>
mov    -0x58(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <lib_strncmp@plt>
mov    -0x58(%rbp),%rdi
mov    %eax,-0x4c(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x40(%rbp),%rsi
mov    -0x4c(%rbp),%edx
call   *%rax
mov    -0x58(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1050 <lib_memcmp@plt>
mov    -0x58(%rbp),%rdi
mov    %eax,-0x4c(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x40(%rbp),%rsi
mov    -0x4c(%rbp),%edx
call   *%rax
mov    -0x48(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x48(%rbp)
jmp    121f <test_driver_strncmp+0xaf>
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_driver_strncmp>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r12
mov    %rsi,-0x40(%rbp)
mov    %rdi,-0x30(%rbp)
mov    0x2e4d(%rip),%rax
mov    (%rax),%edi
call   1080 <sysconf@plt>
mov    %rax,%rbx
mov    %rsp,%r13
lea    0x10(%r12),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r13
mov    %r13,%rsp
lea    (%rbx,%rbx,1),%esi
mov    %rbx,%rdi
call   1090 <aligned_alloc@plt>
mov    %rax,%r14
lea    (%rax,%rbx,1),%rdi
mov    0x2e28(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rsi
call   1070 <mprotect@plt>
test   %eax,%eax
js     1263 <test_driver_strncmp+0xf3>
mov    %r13,%rdi
mov    $0x41,%esi
mov    %r12,%rdx
call   1060 <memset@plt>
sub    %r12,%rbx
lea    (%rbx,%r14,1),%rax
add    $0xfffffffffffffff6,%rax
mov    %rax,-0x38(%rbp)
xor    %r15d,%r15d
mov    -0x30(%rbp),%r14
mov    -0x38(%rbp),%rax
lea    (%rax,%r15,1),%rbx
mov    %rbx,%rdi
mov    $0x41,%esi
mov    %r12,%rdx
call   1060 <memset@plt>
mov    %r13,%rdi
mov    %rbx,%rsi
mov    %r12,%rdx
call   1040 <lib_strncmp@plt>
mov    %r13,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   *%r14
mov    %r13,%rdi
mov    %rbx,%rsi
mov    %r12,%rdx
call   1050 <lib_memcmp@plt>
mov    %r13,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   *-0x40(%rbp)
add    $0x1,%r15
cmp    $0xb,%r15
jne    1200 <test_driver_strncmp+0x90>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
@_SC_PAGESIZE = global i32 0
@PROT_NONE = global i32 0
define void @test_driver_strncmp(void (i8*, i8*, i32)* %0, void (i8*, i8*, i32)* %1, i64 %2) {
%4 = alloca void (i8*, i8*, i32)*
%5 = alloca void (i8*, i8*, i32)*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
store void (i8*, i8*, i32)* %0, void (i8*, i8*, i32)** %4
store void (i8*, i8*, i32)* %1, void (i8*, i8*, i32)** %5
store i64 %2, i64* %6
%15 = load i32, i32* @_SC_PAGESIZE
%16 = call i64 @sysconf(i32 %15)
store i64 %16, i64* %7
%17 = load i64, i64* %6
%18 = add i64 %17, 1
%19 = call i8* @llvm.stacksave()
store i8* %19, i8** %8
%20 = alloca i8, i64 %18
store i64 %18, i64* %9
%21 = load i64, i64* %7
%22 = load i64, i64* %7
%23 = mul nsw i64 2, %22
%24 = trunc i64 %23 to i32
%25 = call i8* @aligned_alloc(i64 %21, i32 %24)
store i8* %25, i8** %10
%26 = load i8*, i8** %10
%27 = load i64, i64* %7
%28 = getelementptr inbounds i8, i8* %26, i64 %27
%29 = load i64, i64* %7
%30 = load i32, i32* @PROT_NONE
%31 = call i32 @mprotect(i8* %28, i64 %29, i32 %30)
store i32 %31, i32* %12
%32 = load i32, i32* %12
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %3
%35 = call i32 (...) @abort()
unreachable
36: ; preds = %3
%37 = load i64, i64* %6
%38 = call i32 @memset(i8* %20, i8 signext 65, i64 %37)
store i32 10, i32* %13
br label %39
39: ; preds = %68, %36
%40 = load i32, i32* %13
%41 = icmp sge i32 %40, 0
br i1 %41, label %42, label %71
42: ; preds = %39
%43 = load i8*, i8** %10
%44 = load i64, i64* %7
%45 = getelementptr inbounds i8, i8* %43, i64 %44
%46 = load i64, i64* %6
%47 = sub i64 0, %46
%48 = getelementptr inbounds i8, i8* %45, i64 %47
%49 = load i32, i32* %13
%50 = sext i32 %49 to i64
%51 = sub i64 0, %50
%52 = getelementptr inbounds i8, i8* %48, i64 %51
store i8* %52, i8** %11
%53 = load i8*, i8** %11
%54 = load i64, i64* %6
%55 = call i32 @memset(i8* %53, i8 signext 65, i64 %54)
%56 = load i8*, i8** %11
%57 = load i64, i64* %6
%58 = call i32 @lib_strncmp(i8* %20, i8* %56, i64 %57)
store i32 %58, i32* %14
%59 = load void (i8*, i8*, i32)*, void (i8*, i8*, i32)** %4
%60 = load i8*, i8** %11
%61 = load i32, i32* %14
%62 = load i8*, i8** %11
%63 = load i64, i64* %6
%64 = call i32 @lib_memcmp(i8* %20, i8* %62, i64 %63)
store i32 %64, i32* %14
%65 = load void (i8*, i8*, i32)*, void (i8*, i8*, i32)** %5
%66 = load i8*, i8** %11
%67 = load i32, i32* %14
br label %68
68: ; preds = %42
%69 = load i32, i32* %13
%70 = add nsw i32 %69, -1
store i32 %70, i32* %13
br label %39
71: ; preds = %39
%72 = load i8*, i8** %8
ret void
}

LLVM IR (-O2):
@_SC_PAGESIZE = global i32 0
@PROT_NONE = global i32 0
define void @test_driver_strncmp(void (i8*, i8*, i32)* nocapture %0, void (i8*, i8*, i32)* nocapture %1, i64 %2) {
%4 = load i32, i32* @_SC_PAGESIZE
%5 = tail call i64 @sysconf(i32 %4)
%6 = add i64 %2, 1
%7 = alloca i8, i64 %6
%8 = trunc i64 %5 to i32
%9 = shl i32 %8, 1
%10 = tail call i8* @aligned_alloc(i64 %5, i32 %9)
%11 = getelementptr inbounds i8, i8* %10, i64 %5
%12 = load i32, i32* @PROT_NONE
%13 = tail call i32 @mprotect(i8* %11, i64 %5, i32 %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %3
%16 = tail call i32 (...) @abort()
unreachable
17: ; preds = %3
%18 = call i32 @memset(i8* %7, i8 signext 65, i64 %2)
%19 = sub i64 0, %2
%20 = getelementptr inbounds i8, i8* %11, i64 %19
br label %21
21: ; preds = %17, %21
%22 = phi i64 [ 10, %17 ], [ %28, %21 ]
%23 = sub nsw i64 0, %22
%24 = getelementptr inbounds i8, i8* %20, i64 %23
%25 = call i32 @memset(i8* %24, i8 signext 65, i64 %2)
%26 = call i32 @lib_strncmp(i8* %7, i8* %24, i64 %2)
%27 = call i32 @lib_memcmp(i8* %7, i8* %24, i64 %2)
%28 = add nsw i64 %22, -1
%29 = icmp eq i64 %22, 0
br i1 %29, label %30, label %21
30: ; preds = %21
ret void
}