#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*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int strcmp(char *, char *);
int main(int argc, char **argv) {
  FILE *file;
  char buf[64];
  int res;
  int y;
  if (2 != argc) {
    fprintf(stderr, "Usage: sel_ldr test_rewind.nexe "
                    "path/to/test_rewind_data\n");
    return 1;
  }
  file = fopen(argv[1], "r");
  assert(NULL != file);
  for (y = 0; y < 8; ++y) {
    res = fread(buf, 1, 64, file);
    assert(0 < res);
    buf[res] = '\0';
    res = strcmp(buf, "test");
    assert(0 == res);
    rewind(file);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x2,%eax
cmp    -0x8(%rbp),%eax
je     11a5 <main+0x45>
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
lea    0xe6c(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1259 <main+0xf9>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe86(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
xor    %eax,%eax
cmp    -0x18(%rbp),%rax
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
movl   $0x0,-0x68(%rbp)
cmpl   $0x8,-0x68(%rbp)
jge    1252 <main+0xf2>
lea    -0x60(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
mov    $0x40,%edx
call   1030 <fread@plt>
mov    %eax,-0x64(%rbp)
xor    %eax,%eax
cmp    -0x64(%rbp),%eax
setl   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
movslq -0x64(%rbp),%rax
movb   $0x0,-0x60(%rbp,%rax,1)
lea    -0x60(%rbp),%rdi
lea    0xe1b(%rip),%rsi
call   1050 <strcmp@plt>
mov    %eax,-0x64(%rbp)
xor    %eax,%eax
cmp    -0x64(%rbp),%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x18(%rbp),%rdi
call   1040 <rewind@plt>
mov    -0x68(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x68(%rbp)
jmp    11d7 <main+0x77>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x40,%rsp
cmp    $0x2,%edi
jne    11f4 <main+0xa4>
mov    0x8(%rsi),%rdi
lea    0xeca(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%r15
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   1050 <assert@plt>
mov    $0x8,%ebx
mov    %rsp,%r14
mov    $0x74736574,%r12d
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0x40,%edx
mov    %r15,%rcx
call   1030 <fread@plt>
mov    %eax,%ebp
xor    %edi,%edi
test   %eax,%eax
setg   %dil
call   1050 <assert@plt>
movslq %ebp,%rax
movb   $0x0,(%rsp,%rax,1)
mov    (%rsp),%eax
xor    %r12d,%eax
movzbl 0x4(%rsp),%ecx
xor    %edi,%edi
or     %eax,%ecx
sete   %dil
call   1050 <assert@plt>
mov    %r15,%rdi
call   1040 <rewind@plt>
add    $0xffffffff,%ebx
jne    11a0 <main+0x50>
xor    %eax,%eax
jmp    120e <main+0xbe>
mov    0x2de5(%rip),%rax
mov    (%rax),%edi
lea    0xdfc(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [58 x i8] c"Usage: sel_ldr test_rewind.nexe path/to/test_rewind_data\0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [5 x i8] c"test\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca [64 x i8]
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 2, %10
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %50
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = call i32* @fopen(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %19, i32** %6
%20 = load i32*, i32** %6
%21 = icmp ne i32* null, %20
%22 = zext i1 %21 to i32
%23 = call i32 @assert(i32 %22)
store i32 0, i32* %9
br label %24
24: ; preds = %46, %15
%25 = load i32, i32* %9
%26 = icmp slt i32 %25, 8
br i1 %26, label %27, label %49
27: ; preds = %24
%28 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%29 = load i32*, i32** %6
%30 = call i32 @fread(i8* %28, i32 1, i32 64, i32* %29)
store i32 %30, i32* %8
%31 = load i32, i32* %8
%32 = icmp slt i32 0, %31
%33 = zext i1 %32 to i32
%34 = call i32 @assert(i32 %33)
%35 = load i32, i32* %8
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 %36
store i8 0, i8* %37
%38 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%39 = call i32 @strcmp(i8* %38, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
store i32 %39, i32* %8
%40 = load i32, i32* %8
%41 = icmp eq i32 0, %40
%42 = zext i1 %41 to i32
%43 = call i32 @assert(i32 %42)
%44 = load i32*, i32** %6
%45 = call i32 @rewind(i32* %44)
br label %46
46: ; preds = %27
%47 = load i32, i32* %9
%48 = add nsw i32 %47, 1
store i32 %48, i32* %9
br label %24
49: ; preds = %24
store i32 0, i32* %3
br label %50
50: ; preds = %49, %12
%51 = load i32, i32* %3
ret i32 %51
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [58 x i8] c"Usage: sel_ldr test_rewind.nexe path/to/test_rewind_data\0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [5 x i8] c"test\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [64 x i8]
%4 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%5 = icmp eq i32 %0, 2
br i1 %5, label %9, label %6
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str, i64 0, i64 0))
br label %31
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = tail call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%13 = icmp ne i32* %12, null
%14 = zext i1 %13 to i32
%15 = tail call i32 @assert(i32 %14)
br label %16
16: ; preds = %9, %16
%17 = phi i32 [ 0, %9 ], [ %29, %16 ]
%18 = call i32 @fread(i8* %4, i32 1, i32 64, i32* %12)
%19 = icmp sgt i32 %18, 0
%20 = zext i1 %19 to i32
%21 = tail call i32 @assert(i32 %20)
%22 = sext i32 %18 to i64
%23 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 %22
store i8 0, i8* %23
%24 = call i32 @bcmp(i8* dereferenceable(5) %4, i8* dereferenceable(5) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i64 5)
%25 = icmp eq i32 %24, 0
%26 = zext i1 %25 to i32
%27 = tail call i32 @assert(i32 %26)
%28 = tail call i32 @rewind(i32* %12)
%29 = add nuw nsw i32 %17, 1
%30 = icmp eq i32 %29, 8
br i1 %30, label %31, label %16
31: ; preds = %16, %6
%32 = phi i32 [ 1, %6 ], [ 0, %16 ]
ret i32 %32
}