#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 */
typedef int /*<<< orphan*/ uint32_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ findkeys(int /*<<< orphan*/ *, int);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *);
int main(int argc, char **argv) {
  FILE *fp = fopen(argv[1], "rb");
  if (NULL == fp) {
    printf("File open error!\n");
    return -1;
  }
  uint32_t *input = (uint32_t *)malloc(1024 * sizeof(uint32_t));
  uint32_t stored;
  int first = 1, numRead;
  for (;;) {
    if (first) {
      numRead = fread(input, sizeof(uint32_t), 1024, fp);
    } else {
      numRead = fread(input, sizeof(uint32_t), 1024 - 1, fp);
      input--;
    }
    if (numRead == 0) {
      break;
    } else if (numRead <= 1024 - 1) {
      if (first)
        stored = findkeys(input, numRead);
      else
        stored = findkeys(input, numRead + 1);
      break;
    } else {
      stored = findkeys(input, 1024);
      input[0] = stored;
      input++;
    }
    if (first)
      first = 0;
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe8b(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
xor    %eax,%eax
cmp    -0x18(%rbp),%rax
jne    11a2 <main+0x52>
lea    0xe72(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1298 <main+0x148>
mov    $0x1000,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
movl   $0x1,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     11e0 <main+0x90>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x4,%esi
mov    $0x400,%edx
call   1030 <fread@plt>
mov    %eax,-0x2c(%rbp)
jmp    1206 <main+0xb6>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x4,%esi
mov    $0x3ff,%edx
call   1030 <fread@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x20(%rbp),%rax
add    $0xfffffffffffffffc,%rax
mov    %rax,-0x20(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jne    1215 <main+0xc5>
jmp    1298 <main+0x148>
cmpl   $0x3ff,-0x2c(%rbp)
jg     1257 <main+0x107>
cmpl   $0x0,-0x28(%rbp)
je     1240 <main+0xf0>
mov    -0x20(%rbp),%rdi
mov    -0x2c(%rbp),%esi
call   1040 <findkeys@plt>
mov    %eax,-0x24(%rbp)
jmp    1252 <main+0x102>
mov    -0x20(%rbp),%rdi
mov    -0x2c(%rbp),%esi
add    $0x1,%esi
call   1040 <findkeys@plt>
mov    %eax,-0x24(%rbp)
jmp    1298 <main+0x148>
mov    -0x20(%rbp),%rdi
mov    $0x400,%esi
call   1040 <findkeys@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x20(%rbp)
jmp    1282 <main+0x132>
cmpl   $0x0,-0x28(%rbp)
je     1293 <main+0x143>
movl   $0x0,-0x28(%rbp)
jmp    11b7 <main+0x67>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x8(%rsi),%rdi
lea    0xe9e(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11f0 <main+0xa0>
mov    %rax,%r14
mov    $0x1000,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
xor    %r15d,%r15d
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
mov    $0x4,%esi
test   $0x1,%r15b
je     11c0 <main+0x70>
mov    $0x3ff,%edx
mov    %r14,%rcx
call   1040 <fread@plt>
add    $0xfffffffffffffffc,%rbx
test   %eax,%eax
jne    11d1 <main+0x81>
jmp    1216 <main+0xc6>
cs nopw 0x0(%rax,%rax,1)
nop
mov    $0x400,%edx
mov    %r14,%rcx
call   1040 <fread@plt>
test   %eax,%eax
je     1216 <main+0xc6>
cmp    $0x3ff,%eax
jle    1203 <main+0xb3>
mov    %rbx,%rdi
mov    $0x400,%esi
call   1050 <findkeys@plt>
mov    %eax,(%rbx)
add    $0x4,%rbx
mov    $0x1,%r15b
jmp    1190 <main+0x40>
lea    0xe0c(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebp
jmp    1216 <main+0xc6>
test   $0x1,%r15b
je     120c <main+0xbc>
add    $0x1,%eax
mov    %rbx,%rdi
mov    %eax,%esi
call   1050 <findkeys@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [18 x i8] c"File open error!\0A\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 i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i8**, i8*** %5
%12 = getelementptr inbounds i8*, i8** %11, i64 1
%13 = load i8*, i8** %12
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %14, i32** %6
%15 = load i32*, i32** %6
%16 = icmp eq i32* null, %15
br i1 %16, label %17, label %19
17: ; preds = %2
%18 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %69
19: ; preds = %2
%20 = call i64 @malloc(i32 4096)
%21 = inttoptr i64 %20 to i32*
store i32* %21, i32** %7
store i32 1, i32* %9
br label %22
22: ; preds = %68, %19
%23 = load i32, i32* %9
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %22
%26 = load i32*, i32** %7
%27 = load i32*, i32** %6
%28 = call i32 @fread(i32* %26, i32 4, i32 1024, i32* %27)
store i32 %28, i32* %10
br label %35
29: ; preds = %22
%30 = load i32*, i32** %7
%31 = load i32*, i32** %6
%32 = call i32 @fread(i32* %30, i32 4, i32 1023, i32* %31)
store i32 %32, i32* %10
%33 = load i32*, i32** %7
%34 = getelementptr inbounds i32, i32* %33, i32 -1
store i32* %34, i32** %7
br label %35
35: ; preds = %29, %25
%36 = load i32, i32* %10
%37 = icmp eq i32 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %35
br label %69
39: ; preds = %35
%40 = load i32, i32* %10
%41 = icmp sle i32 %40, 1023
br i1 %41, label %42, label %55
42: ; preds = %39
%43 = load i32, i32* %9
%44 = icmp ne i32 %43, 0
br i1 %44, label %45, label %49
45: ; preds = %42
%46 = load i32*, i32** %7
%47 = load i32, i32* %10
%48 = call i32 @findkeys(i32* %46, i32 %47)
store i32 %48, i32* %8
br label %54
49: ; preds = %42
%50 = load i32*, i32** %7
%51 = load i32, i32* %10
%52 = add nsw i32 %51, 1
%53 = call i32 @findkeys(i32* %50, i32 %52)
store i32 %53, i32* %8
br label %54
54: ; preds = %49, %45
br label %69
55: ; preds = %39
%56 = load i32*, i32** %7
%57 = call i32 @findkeys(i32* %56, i32 1024)
store i32 %57, i32* %8
%58 = load i32, i32* %8
%59 = load i32*, i32** %7
%60 = getelementptr inbounds i32, i32* %59, i64 0
store i32 %58, i32* %60
%61 = load i32*, i32** %7
%62 = getelementptr inbounds i32, i32* %61, i32 1
store i32* %62, i32** %7
br label %63
63: ; preds = %55
br label %64
64: ; preds = %63
%65 = load i32, i32* %9
%66 = icmp ne i32 %65, 0
br i1 %66, label %67, label %68
67: ; preds = %64
store i32 0, i32* %9
br label %68
68: ; preds = %67, %64
br label %22
69: ; preds = %17, %54, %38
%70 = load i32, i32* %3
ret i32 %70
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@str = [17 x i8] c"File open error!\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = getelementptr inbounds i8*, i8** %1, i64 1
%4 = load i8*, i8** %3
%5 = tail call i32* @fopen(i8* %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
br label %35
9: ; preds = %2
%10 = tail call i64 @malloc(i32 4096)
%11 = inttoptr i64 %10 to i32*
br label %12
12: ; preds = %32, %9
%13 = phi i32* [ %11, %9 ], [ %34, %32 ]
%14 = phi i1 [ false, %9 ], [ true, %32 ]
br i1 %14, label %17, label %15
15: ; preds = %12
%16 = tail call i32 @fread(i32* %13, i32 4, i32 1024, i32* %5)
br label %20
17: ; preds = %12
%18 = tail call i32 @fread(i32* %13, i32 4, i32 1023, i32* %5)
%19 = getelementptr inbounds i32, i32* %13, i64 -1
br label %20
20: ; preds = %17, %15
%21 = phi i32* [ %13, %15 ], [ %19, %17 ]
%22 = phi i32 [ %16, %15 ], [ %18, %17 ]
%23 = icmp eq i32 %22, 0
br i1 %23, label %35, label %24
24: ; preds = %20
%25 = icmp slt i32 %22, 1024
br i1 %25, label %26, label %32
26: ; preds = %24
br i1 %14, label %29, label %27
27: ; preds = %26
%28 = tail call i32 @findkeys(i32* %21, i32 %22)
br label %35
29: ; preds = %26
%30 = add nsw i32 %22, 1
%31 = tail call i32 @findkeys(i32* %21, i32 %30)
br label %35
32: ; preds = %24
%33 = tail call i32 @findkeys(i32* %21, i32 1024)
store i32 %33, i32* %21
%34 = getelementptr inbounds i32, i32* %21, i64 1
br label %12
35: ; preds = %20, %29, %27, %7
%36 = phi i32 [ -1, %7 ], [ 0, %27 ], [ 0, %29 ], [ 0, %20 ]
ret i32 %36
}