#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 */
scalar_t__ posix_memalign(void **, int, int);
int /*<<< orphan*/ printf(char *, ...);
char rand();
int xor_check(int, int, void **);
int /*<<< orphan*/ xor_gen(int, int, void **);
int main(int argc, char *argv[]) {
  int i, j, should_pass, should_fail;
  void *buffs[16 + 1];
  printf("XOR example\n");
  for (i = 0; i < 16 + 1; i++) {
    void *buf;
    if (posix_memalign(&buf, 32, 16 * 1024)) {
      printf("alloc error: Fail");
      return 1;
    }
    buffs[i] = buf;
  }
  printf("Make random data\n");
  for (i = 0; i < 16 + 1; i++)
    for (j = 0; j < 16 * 1024; j++)
      ((char *)buffs[i])[j] = rand();
  printf("Generate xor parity\n");
  xor_gen(16 + 1, 16 * 1024, buffs);
  printf("Check parity: ");
  should_pass = xor_check(16 + 1, 16 * 1024, buffs);
  printf("%s\n", should_pass == 0 ? "Pass" : "Fail");
  printf("Find corruption: ");
  ((char *)buffs[16 / 2])[16 * 1024 / 2] ^= 1;
  should_fail = xor_check(16 + 1, 16 * 1024, buffs);
  printf("%s\n", should_fail != 0 ? "Pass" : "Fail");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe90(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x14(%rbp)
cmpl   $0x11,-0x14(%rbp)
jge    11e3 <main+0x93>
lea    -0xb8(%rbp),%rdi
mov    $0x20,%esi
mov    $0x4000,%edx
call   1050 <posix_memalign@plt>
cmp    $0x0,%rax
je     11c2 <main+0x72>
lea    0xe5e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    132c <main+0x1dc>
mov    -0xb8(%rbp),%rcx
movslq -0x14(%rbp),%rax
mov    %rcx,-0xb0(%rbp,%rax,8)
ff
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    117e <main+0x2e>
lea    0xe35(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x14(%rbp)
cmpl   $0x11,-0x14(%rbp)
jge    1253 <main+0x103>
movl   $0x0,-0x18(%rbp)
cmpl   $0x4000,-0x18(%rbp)
jge    1240 <main+0xf0>
mov    $0x0,%al
call   1070 <rand@plt>
mov    %al,%dl
movslq -0x14(%rbp),%rax
mov    -0xb0(%rbp,%rax,8),%rax
ff
movslq -0x18(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1209 <main+0xb9>
jmp    1245 <main+0xf5>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11f8 <main+0xa8>
lea    0xdd7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0xb0(%rbp),%rdx
mov    $0x11,%edi
mov    $0x4000,%esi
call   1060 <xor_gen@plt>
lea    0xdc8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0xb0(%rbp),%rdx
mov    $0x11,%edi
mov    $0x4000,%esi
call   1030 <xor_check@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%ecx
lea    0xd72(%rip),%rsi
lea    0xdaa(%rip),%rax
cmp    $0x0,%ecx
cmove  %rax,%rsi
lea    0xd98(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xd93(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x70(%rbp),%rax
movsbl 0x2000(%rax),%ecx
xor    $0x1,%ecx
mov    %cl,0x2000(%rax)
lea    -0xb0(%rbp),%rdx
mov    $0x11,%edi
mov    $0x4000,%esi
call   1030 <xor_check@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%ecx
lea    0xd11(%rip),%rsi
lea    0xd49(%rip),%rax
cmp    $0x0,%ecx
cmovne %rax,%rsi
lea    0xd37(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x98,%rsp
lea    0xec4(%rip),%rdi
call   1030 <puts@plt>
xor    %ebp,%ebp
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x20,%esi
mov    $0x4000,%edx
call   1060 <posix_memalign@plt>
test   %rax,%rax
jne    1293 <main+0x133>
mov    0x8(%rsp),%rax
mov    %rax,0x10(%rsp,%rbp,8)
add    $0x1,%rbp
cmp    $0x11,%rbp
jne    1180 <main+0x20>
lea    0xe8e(%rip),%rdi
call   1030 <puts@plt>
xor    %ebx,%ebx
nopl   (%rax)
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
xor    %eax,%eax
call   1080 <rand@plt>
mov    0x10(%rsp,%rbx,8),%rcx
mov    %al,(%rcx,%rbp,1)
add    $0x1,%rbp
cmp    $0x4000,%rbp
jne    11d0 <main+0x70>
add    $0x1,%rbx
cmp    $0x11,%rbx
jne    11c0 <main+0x60>
lea    0xe58(%rip),%rdi
call   1030 <puts@plt>
lea    0x10(%rsp),%r14
mov    $0x11,%edi
mov    $0x4000,%esi
mov    %r14,%rdx
call   1070 <xor_gen@plt>
lea    0xdf2(%rip),%rdi
xor    %ebp,%ebp
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x11,%edi
mov    $0x4000,%esi
mov    %r14,%rdx
call   1040 <xor_check@plt>
test   %eax,%eax
lea    0xddd(%rip),%rbx
lea    0xdc2(%rip),%r15
mov    %r15,%rdi
cmove  %rbx,%rdi
call   1030 <puts@plt>
lea    0xdc8(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x50(%rsp),%rax
xorb   $0x1,0x2000(%rax)
mov    $0x11,%edi
mov    $0x4000,%esi
mov    %r14,%rdx
call   1040 <xor_check@plt>
test   %eax,%eax
cmove  %r15,%rbx
mov    %rbx,%rdi
call   1030 <puts@plt>
jmp    12a6 <main+0x146>
lea    0xd66(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x98,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"XOR example\0A\00"
@.str.1 = [18 x i8] c"alloc error: Fail\00"
@.str.2 = [18 x i8] c"Make random data\0A\00"
@.str.3 = [21 x i8] c"Generate xor parity\0A\00"
@.str.4 = [15 x i8] c"Check parity: \00"
@.str.5 = [4 x i8] c"%s\0A\00"
@.str.6 = [5 x i8] c"Pass\00"
@.str.7 = [5 x i8] c"Fail\00"
@.str.8 = [18 x i8] c"Find corruption: \00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca [17 x i8*]
%11 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %6
br label %13
13: ; preds = %26, %2
%14 = load i32, i32* %6
%15 = icmp slt i32 %14, 17
br i1 %15, label %16, label %29
16: ; preds = %13
%17 = call i64 @posix_memalign(i8** %11, i32 32, i32 16384)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %16
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %81
21: ; preds = %16
%22 = load i8*, i8** %11
%23 = load i32, i32* %6
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds [17 x i8*], [17 x i8*]* %10, i64 0, i64 %24
store i8* %22, i8** %25
br label %26
26: ; preds = %21
%27 = load i32, i32* %6
%28 = add nsw i32 %27, 1
store i32 %28, i32* %6
br label %13
29: ; preds = %13
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %6
br label %31
31: ; preds = %51, %29
%32 = load i32, i32* %6
%33 = icmp slt i32 %32, 17
br i1 %33, label %34, label %54
34: ; preds = %31
store i32 0, i32* %7
br label %35
35: ; preds = %47, %34
%36 = load i32, i32* %7
%37 = icmp slt i32 %36, 16384
br i1 %37, label %38, label %50
38: ; preds = %35
%39 = call signext i8 (...) @rand()
%40 = load i32, i32* %6
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds [17 x i8*], [17 x i8*]* %10, i64 0, i64 %41
%43 = load i8*, i8** %42
%44 = load i32, i32* %7
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i8, i8* %43, i64 %45
store i8 %39, i8* %46
br label %47
47: ; preds = %38
%48 = load i32, i32* %7
%49 = add nsw i32 %48, 1
store i32 %49, i32* %7
br label %35
50: ; preds = %35
br label %51
51: ; preds = %50
%52 = load i32, i32* %6
%53 = add nsw i32 %52, 1
store i32 %53, i32* %6
br label %31
54: ; preds = %31
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%56 = getelementptr inbounds [17 x i8*], [17 x i8*]* %10, i64 0, i64 0
%57 = call i32 @xor_gen(i32 17, i32 16384, i8** %56)
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
%59 = getelementptr inbounds [17 x i8*], [17 x i8*]* %10, i64 0, i64 0
%60 = call i32 @xor_check(i32 17, i32 16384, i8** %59)
store i32 %60, i32* %8
%61 = load i32, i32* %8
%62 = icmp eq i32 %61, 0
%63 = zext i1 %62 to i64
%64 = select i1 %62, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0)
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %64)
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.8, i64 0, i64 0))
%67 = getelementptr inbounds [17 x i8*], [17 x i8*]* %10, i64 0, i64 8
%68 = load i8*, i8** %67
%69 = getelementptr inbounds i8, i8* %68, i64 8192
%70 = load i8, i8* %69
%71 = sext i8 %70 to i32
%72 = xor i32 %71, 1
%73 = trunc i32 %72 to i8
store i8 %73, i8* %69
%74 = getelementptr inbounds [17 x i8*], [17 x i8*]* %10, i64 0, i64 0
%75 = call i32 @xor_check(i32 17, i32 16384, i8** %74)
store i32 %75, i32* %9
%76 = load i32, i32* %9
%77 = icmp ne i32 %76, 0
%78 = zext i1 %77 to i64
%79 = select i1 %77, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0)
%80 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %79)
store i32 0, i32* %3
br label %81
81: ; preds = %54, %19
%82 = load i32, i32* %3
ret i32 %82
}

LLVM IR (-O2):
@.str.1 = [18 x i8] c"alloc error: Fail\00"
@.str.4 = [15 x i8] c"Check parity: \00"
@.str.6 = [5 x i8] c"Pass\00"
@.str.7 = [5 x i8] c"Fail\00"
@.str.8 = [18 x i8] c"Find corruption: \00"
@str = [12 x i8] c"XOR example\00"
@str.9 = [17 x i8] c"Make random data\00"
@str.10 = [20 x i8] c"Generate xor parity\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [17 x i8*]
%4 = alloca i8*
%5 = bitcast [17 x i8*]* %3 to i8*
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
%7 = bitcast i8** %4 to i8*
br label %8
8: ; preds = %2, %14
%9 = phi i64 [ 0, %2 ], [ %17, %14 ]
%10 = call i64 @posix_memalign(i8** %4, i32 32, i32 16384)
%11 = icmp eq i64 %10, 0
br i1 %11, label %14, label %12
12: ; preds = %8
%13 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %53
14: ; preds = %8
%15 = load i8*, i8** %4
%16 = getelementptr inbounds [17 x i8*], [17 x i8*]* %3, i64 0, i64 %9
store i8* %15, i8** %16
%17 = add nuw nsw i64 %9, 1
%18 = icmp eq i64 %17, 17
br i1 %18, label %19, label %8
19: ; preds = %14
%20 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.9, i64 0, i64 0))
br label %21
21: ; preds = %19, %31
%22 = phi i64 [ 0, %19 ], [ %32, %31 ]
%23 = getelementptr inbounds [17 x i8*], [17 x i8*]* %3, i64 0, i64 %22
br label %24
24: ; preds = %21, %24
%25 = phi i64 [ 0, %21 ], [ %29, %24 ]
%26 = call signext i8 (...) @rand()
%27 = load i8*, i8** %23
%28 = getelementptr inbounds i8, i8* %27, i64 %25
store i8 %26, i8* %28
%29 = add nuw nsw i64 %25, 1
%30 = icmp eq i64 %29, 16384
br i1 %30, label %31, label %24
31: ; preds = %24
%32 = add nuw nsw i64 %22, 1
%33 = icmp eq i64 %32, 17
br i1 %33, label %34, label %21
34: ; preds = %31
%35 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.10, i64 0, i64 0))
%36 = getelementptr inbounds [17 x i8*], [17 x i8*]* %3, i64 0, i64 0
%37 = call i32 @xor_gen(i32 17, i32 16384, i8** %36)
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
%39 = call i32 @xor_check(i32 17, i32 16384, i8** %36)
%40 = icmp eq i32 %39, 0
%41 = select i1 %40, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0)
%42 = call i32 @puts(i8* dereferenceable(1) %41)
%43 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.8, i64 0, i64 0))
%44 = getelementptr inbounds [17 x i8*], [17 x i8*]* %3, i64 0, i64 8
%45 = load i8*, i8** %44
%46 = getelementptr inbounds i8, i8* %45, i64 8192
%47 = load i8, i8* %46
%48 = xor i8 %47, 1
store i8 %48, i8* %46
%49 = call i32 @xor_check(i32 17, i32 16384, i8** %36)
%50 = icmp eq i32 %49, 0
%51 = select i1 %50, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0)
%52 = call i32 @puts(i8* dereferenceable(1) %51)
br label %53
53: ; preds = %12, %34
%54 = phi i32 [ 1, %12 ], [ 0, %34 ]
ret i32 %54
}