#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 EINVAL;
int ENOMEM;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(char *);
int posix_memalign(void **, int, int);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  char *outp;
  size_t align_to_test[] = {4, 8, 16, 32, 64, 128, 256, 512, 1024, 0};
  size_t sizes_to_test[] = {1, 2, 4, 8, 10, 16, 32, 64, 128, 256, 512, 1024, 0};
  int i = 0;
  int j = 0;
  while (align_to_test[i] != 0) {
    j = 0;
    while (sizes_to_test[j] != 0) {
      int err = posix_memalign((void **)&outp, align_to_test[i],
                               sizes_to_test[j] * sizeof(char));
      if (err) {
        fprintf(stderr,
                "posix_memalign failed with err %d (EINVAL=%d,ENOMEM=%d)\n",
                err, EINVAL, ENOMEM);
        fprintf(stderr, "Input params were align=%d, size=%d\n",
                align_to_test[i], sizes_to_test[j]);
        return 1;
      }
      if ((size_t)outp % align_to_test[i] != 0) {
        fprintf(stderr, "posix_memalign failed to align to %zu: ptr=%p\n",
                align_to_test[i], outp);
        return 1;
      }
      free(outp);
      j++;
    }
    i++;
  }
  if (posix_memalign((void **)&outp, 7, 20 * sizeof(char)) != EINVAL) {
    fprintf(stderr, "posix_memaligned failed to return EINVAL for non-pow2!\n");
    return 1;
  }
  if (posix_memalign((void **)&outp, sizeof(void *) - 1, 20 * sizeof(char)) !=
      EINVAL) {
    fprintf(stderr, "posix_memaligned failed to return EINVAL for non-pow2!\n");
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xf0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x70(%rbp),%rdi
lea    0xe9c(%rip),%rsi
mov    $0x50,%edx
call   1050 <memcpy@plt>
lea    -0xe0(%rbp),%rdi
lea    0xed4(%rip),%rsi
mov    $0x68,%edx
call   1050 <memcpy@plt>
movl   $0x0,-0xe4(%rbp)
movl   $0x0,-0xe8(%rbp)
movslq -0xe4(%rbp),%rax
cmpq   $0x0,-0x70(%rbp,%rax,8)
je     12fa <main+0x1ba>
movl   $0x0,-0xe8(%rbp)
movslq -0xe8(%rbp),%rax
cmpq   $0x0,-0xe0(%rbp,%rax,8)
je     12e6 <main+0x1a6>
movslq -0xe4(%rbp),%rax
mov    -0x70(%rbp,%rax,8),%rax
mov    %eax,%esi
movslq -0xe8(%rbp),%rax
mov    -0xe0(%rbp,%rax,8),%rax
ff
shl    $0x0,%rax
mov    %eax,%edx
lea    -0x18(%rbp),%rdi
call   1060 <posix_memalign@plt>
mov    %eax,-0xec(%rbp)
cmpl   $0x0,-0xec(%rbp)
je     127a <main+0x13a>
mov    0x2dc5(%rip),%rax
mov    (%rax),%edi
mov    -0xec(%rbp),%edx
mov    0x2dae(%rip),%rax
mov    (%rax),%ecx
mov    0x2db5(%rip),%rax
mov    (%rax),%r8d
lea    0xe83(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    0x2d95(%rip),%rax
mov    (%rax),%edi
movslq -0xe4(%rbp),%rax
mov    -0x70(%rbp,%rax,8),%rdx
movslq -0xe8(%rbp),%rax
mov    -0xe0(%rbp,%rax,8),%rcx
ff
lea    0xe8a(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    138b <main+0x24b>
mov    -0x18(%rbp),%rax
movslq -0xe4(%rbp),%rcx
xor    %edx,%edx
divq   -0x70(%rbp,%rcx,8)
cmp    $0x0,%rdx
je     12c9 <main+0x189>
mov    0x2d3b(%rip),%rax
mov    (%rax),%edi
movslq -0xe4(%rbp),%rax
mov    -0x70(%rbp,%rax,8),%rdx
mov    -0x18(%rbp),%rcx
lea    0xe60(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    138b <main+0x24b>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0xe8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xe8(%rbp)
jmp    11b7 <main+0x77>
mov    -0xe4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xe4(%rbp)
jmp    119a <main+0x5a>
lea    -0x18(%rbp),%rdi
mov    $0x7,%esi
mov    $0x14,%edx
call   1060 <posix_memalign@plt>
mov    0x2cbc(%rip),%rcx
cmp    (%rcx),%eax
je     133f <main+0x1ff>
mov    0x2cb5(%rip),%rax
mov    (%rax),%edi
lea    0xe19(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    138b <main+0x24b>
lea    -0x18(%rbp),%rdi
mov    $0x7,%esi
mov    $0x14,%edx
call   1060 <posix_memalign@plt>
mov    0x2c77(%rip),%rcx
cmp    (%rcx),%eax
je     1384 <main+0x244>
mov    0x2c70(%rip),%rax
mov    (%rax),%edi
lea    0xdd4(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    138b <main+0x24b>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xf0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xebe(%rip),%rax
xor    %r13d,%r13d
lea    0xf04(%rip),%rbp
mov    %rsp,%r14
mov    (%rax),%r15
xor    %r12d,%r12d
cs nopw 0x0(%rax,%rax,1)
nop
mov    (%r12,%rbp,1),%rbx
mov    %r14,%rdi
mov    %r15d,%esi
mov    %ebx,%edx
call   1050 <posix_memalign@plt>
test   %eax,%eax
jne    122f <main+0xff>
mov    (%rsp),%rcx
mov    %rcx,%rax
or     %r15,%rax
shr    $0x20,%rax
je     11a0 <main+0x70>
mov    %rcx,%rax
xor    %edx,%edx
div    %r15
test   %rdx,%rdx
je     11b0 <main+0x80>
jmp    126e <main+0x13e>
nopl   0x0(%rax,%rax,1)
mov    %ecx,%eax
xor    %edx,%edx
div    %r15d
test   %rdx,%rdx
jne    126e <main+0x13e>
mov    %rcx,%rdi
call   1030 <free@plt>
add    $0x8,%r12
cmp    $0x60,%r12
jne    1160 <main+0x30>
lea    0xe37(%rip),%rax
lea    (%rax,%r13,8),%rax
add    $0x8,%rax
add    $0x1,%r13
cmp    $0x9,%r13
jne    114f <main+0x1f>
mov    %rsp,%rdi
mov    $0x7,%esi
mov    $0x14,%edx
call   1050 <posix_memalign@plt>
mov    0x2dd8(%rip),%rbx
cmp    (%rbx),%eax
jne    1216 <main+0xe6>
mov    %rsp,%rdi
mov    $0x7,%esi
mov    $0x14,%edx
call   1050 <posix_memalign@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    (%rbx),%ecx
je     128d <main+0x15d>
mov    0x2dbb(%rip),%rax
mov    (%rax),%edi
lea    0xf1f(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
jmp    1288 <main+0x158>
mov    0x2da2(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2d90(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2d97(%rip),%rdx
mov    (%rdx),%r8d
lea    0xe65(%rip),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    0x0(%rbp),%edi
lea    0xe8b(%rip),%rsi
mov    %r15,%rdx
mov    %rbx,%rcx
jmp    1281 <main+0x151>
mov    0x2d63(%rip),%rax
mov    (%rax),%edi
lea    0xe98(%rip),%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.main.align_to_test = [10 x i64] [i64 4, i64 8, i64 16, i64 32, i64 64, i64 128, i64 256, i64 512, i64 1024, i64 0]
@__const.main.sizes_to_test = [13 x i64] [i64 1, i64 2, i64 4, i64 8, i64 10, i64 16, i64 32, i64 64, i64 128, i64 256, i64 512, i64 1024, i64 0]
@stderr = global i32 0
@.str = [57 x i8] c"posix_memalign failed with err %d (EINVAL=%d,ENOMEM=%d)\0A\00"
@EINVAL = global i32 0
@ENOMEM = global i32 0
@.str.1 = [37 x i8] c"Input params were align=%d, size=%d\0A\00"
@.str.2 = [47 x i8] c"posix_memalign failed to align to %zu: ptr=%p\0A\00"
@.str.3 = [56 x i8] c"posix_memaligned failed to return EINVAL for non-pow2!\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca [10 x i64]
%8 = alloca [13 x i64]
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = bitcast [10 x i64]* %7 to i8*
%13 = bitcast [13 x i64]* %8 to i8*
store i32 0, i32* %9
store i32 0, i32* %10
br label %14
14: ; preds = %80, %2
%15 = load i32, i32* %9
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds [10 x i64], [10 x i64]* %7, i64 0, i64 %16
%18 = load i64, i64* %17
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %83
20: ; preds = %14
store i32 0, i32* %10
br label %21
21: ; preds = %75, %20
%22 = load i32, i32* %10
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds [13 x i64], [13 x i64]* %8, i64 0, i64 %23
%25 = load i64, i64* %24
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %80
27: ; preds = %21
%28 = load i32, i32* %9
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds [10 x i64], [10 x i64]* %7, i64 0, i64 %29
%31 = load i64, i64* %30
%32 = trunc i64 %31 to i32
%33 = load i32, i32* %10
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds [13 x i64], [13 x i64]* %8, i64 0, i64 %34
%36 = load i64, i64* %35
%37 = mul i64 %36, 1
%38 = trunc i64 %37 to i32
%39 = call i32 @posix_memalign(i8** %6, i32 %32, i32 %38)
store i32 %39, i32* %11
%40 = load i32, i32* %11
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %58
42: ; preds = %27
%43 = load i32, i32* @stderr
%44 = load i32, i32* %11
%45 = load i32, i32* @EINVAL
%46 = load i32, i32* @ENOMEM
%47 = call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str, i64 0, i64 0), i32 %44, i32 %45, i32 %46)
%48 = load i32, i32* @stderr
%49 = load i32, i32* %9
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds [10 x i64], [10 x i64]* %7, i64 0, i64 %50
%52 = load i64, i64* %51
%53 = load i32, i32* %10
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds [13 x i64], [13 x i64]* %8, i64 0, i64 %54
%56 = load i64, i64* %55
%57 = call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i64 %52, i64 %56)
store i32 1, i32* %3
br label %98
58: ; preds = %27
%59 = load i8*, i8** %6
%60 = ptrtoint i8* %59 to i64
%61 = load i32, i32* %9
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds [10 x i64], [10 x i64]* %7, i64 0, i64 %62
%64 = load i64, i64* %63
%65 = urem i64 %60, %64
%66 = icmp ne i64 %65, 0
br i1 %66, label %67, label %75
67: ; preds = %58
%68 = load i32, i32* @stderr
%69 = load i32, i32* %9
%70 = sext i32 %69 to i64
%71 = getelementptr inbounds [10 x i64], [10 x i64]* %7, i64 0, i64 %70
%72 = load i64, i64* %71
%73 = load i8*, i8** %6
%74 = call i32 (i32, i8*, ...) @fprintf(i32 %68, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0), i64 %72, i8* %73)
store i32 1, i32* %3
br label %98
75: ; preds = %58
%76 = load i8*, i8** %6
%77 = call i32 @free(i8* %76)
%78 = load i32, i32* %10
%79 = add nsw i32 %78, 1
store i32 %79, i32* %10
br label %21
80: ; preds = %21
%81 = load i32, i32* %9
%82 = add nsw i32 %81, 1
store i32 %82, i32* %9
br label %14
83: ; preds = %14
%84 = call i32 @posix_memalign(i8** %6, i32 7, i32 20)
%85 = load i32, i32* @EINVAL
%86 = icmp ne i32 %84, %85
br i1 %86, label %87, label %90
87: ; preds = %83
%88 = load i32, i32* @stderr
%89 = call i32 (i32, i8*, ...) @fprintf(i32 %88, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %98
90: ; preds = %83
%91 = call i32 @posix_memalign(i8** %6, i32 7, i32 20)
%92 = load i32, i32* @EINVAL
%93 = icmp ne i32 %91, %92
br i1 %93, label %94, label %97
94: ; preds = %90
%95 = load i32, i32* @stderr
%96 = call i32 (i32, i8*, ...) @fprintf(i32 %95, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %98
97: ; preds = %90
store i32 0, i32* %3
br label %98
98: ; preds = %97, %94, %87, %67, %42
%99 = load i32, i32* %3
ret i32 %99
}

LLVM IR (-O2):
@__const.main.align_to_test = [10 x i64] [i64 4, i64 8, i64 16, i64 32, i64 64, i64 128, i64 256, i64 512, i64 1024, i64 0]
@__const.main.sizes_to_test = [13 x i64] [i64 1, i64 2, i64 4, i64 8, i64 10, i64 16, i64 32, i64 64, i64 128, i64 256, i64 512, i64 1024, i64 0]
@stderr = global i32 0
@.str = [57 x i8] c"posix_memalign failed with err %d (EINVAL=%d,ENOMEM=%d)\0A\00"
@EINVAL = global i32 0
@ENOMEM = global i32 0
@.str.1 = [37 x i8] c"Input params were align=%d, size=%d\0A\00"
@.str.2 = [47 x i8] c"posix_memalign failed to align to %zu: ptr=%p\0A\00"
@.str.3 = [56 x i8] c"posix_memaligned failed to return EINVAL for non-pow2!\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
br label %5
5: ; preds = %2, %36
%6 = phi i64 [ 0, %2 ], [ %37, %36 ]
%7 = phi i64* [ getelementptr inbounds ([10 x i64], [10 x i64]* @__const.main.align_to_test, i64 0, i64 0), %2 ], [ %38, %36 ]
%8 = load i64, i64* %7
%9 = trunc i64 %8 to i32
br label %10
10: ; preds = %5, %32
%11 = phi i64 [ 0, %5 ], [ %34, %32 ]
%12 = getelementptr inbounds [13 x i64], [13 x i64]* @__const.main.sizes_to_test, i64 0, i64 %11
%13 = load i64, i64* %12
%14 = trunc i64 %13 to i32
%15 = call i32 @posix_memalign(i8** %3, i32 %9, i32 %14)
%16 = icmp eq i32 %15, 0
br i1 %16, label %24, label %17
17: ; preds = %10
%18 = load i32, i32* @stderr
%19 = load i32, i32* @EINVAL
%20 = load i32, i32* @ENOMEM
%21 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str, i64 0, i64 0), i32 %15, i32 %19, i32 %20)
%22 = load i32, i32* @stderr
%23 = call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i64 %8, i64 %13)
br label %54
24: ; preds = %10
%25 = load i8*, i8** %3
%26 = ptrtoint i8* %25 to i64
%27 = urem i64 %26, %8
%28 = icmp eq i64 %27, 0
br i1 %28, label %32, label %29
29: ; preds = %24
%30 = load i32, i32* @stderr
%31 = call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0), i64 %8, i8* %25)
br label %54
32: ; preds = %24
%33 = call i32 @free(i8* %25)
%34 = add nuw nsw i64 %11, 1
%35 = icmp eq i64 %34, 12
br i1 %35, label %36, label %10
36: ; preds = %32
%37 = add nuw nsw i64 %6, 1
%38 = getelementptr inbounds [10 x i64], [10 x i64]* @__const.main.align_to_test, i64 0, i64 %37
%39 = icmp eq i64 %37, 9
br i1 %39, label %40, label %5
40: ; preds = %36
%41 = call i32 @posix_memalign(i8** %3, i32 7, i32 20)
%42 = load i32, i32* @EINVAL
%43 = icmp eq i32 %41, %42
br i1 %43, label %47, label %44
44: ; preds = %40
%45 = load i32, i32* @stderr
%46 = call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.3, i64 0, i64 0))
br label %54
47: ; preds = %40
%48 = call i32 @posix_memalign(i8** %3, i32 7, i32 20)
%49 = load i32, i32* @EINVAL
%50 = icmp eq i32 %48, %49
br i1 %50, label %54, label %51
51: ; preds = %47
%52 = load i32, i32* @stderr
%53 = call i32 (i32, i8*, ...) @fprintf(i32 %52, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.3, i64 0, i64 0))
br label %54
54: ; preds = %29, %17, %47, %51, %44
%55 = phi i32 [ 1, %44 ], [ 1, %51 ], [ 0, %47 ], [ 1, %17 ], [ 1, %29 ]
ret i32 %55
}