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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, int, char);
int main01(void) {
  FILE *p = fopen("D:\\temp\\bin-file.exe", "rb");
  while (!feof(p)) {
    char buf[1024] = {0};
    int res = fread(buf, sizeof(char), sizeof(buf), p);
    int i;
    for (i = 0; i < res; i++) {
      printf("buf[%d] = %x\n", i, buf[i]);
    }
  }
  fclose(p);
  return 0;
}


x86 assembly (clang, -O0):
<main01>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
lea    0xe8e(%rip),%rdi
lea    0xe9c(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1070 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11a0 <main01+0x40>
jmp    1229 <main01+0xc9>
lea    -0x410(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
lea    -0x410(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
mov    $0x400,%edx
call   1030 <fread@plt>
mov    %eax,-0x414(%rbp)
movl   $0x0,-0x418(%rbp)
mov    -0x418(%rbp),%eax
cmp    -0x414(%rbp),%eax
jge    1224 <main01+0xc4>
mov    -0x418(%rbp),%esi
movslq -0x418(%rbp),%rax
lea    0xe15(%rip),%rdi
movsbl -0x410(%rbp,%rax,1),%edx
ff
call   1050 <printf@plt>
mov    -0x418(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x418(%rbp)
jmp    11dd <main01+0x7d>
jmp    1182 <main01+0x22>
mov    -0x8(%rbp),%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main01>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x400,%rsp
lea    0xe89(%rip),%rdi
lea    0xe97(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%r15
mov    %rax,%rdi
call   1070 <feof@plt>
test   %eax,%eax
je     11ad <main01+0x4d>
mov    %r15,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
mov    %rsp,%r14
lea    0xe61(%rip),%r12
jmp    11cc <main01+0x6c>
nopl   0x0(%rax)
mov    %r15,%rdi
call   1070 <feof@plt>
test   %eax,%eax
jne    1192 <main01+0x32>
mov    $0x400,%edx
mov    %r14,%rdi
xor    %esi,%esi
call   1060 <memset@plt>
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0x400,%edx
mov    %r15,%rcx
call   1030 <fread@plt>
test   %eax,%eax
jle    11c0 <main01+0x60>
mov    %eax,%r13d
xor    %ebx,%ebx
nopl   0x0(%rax)
movsbl (%rsp,%rbx,1),%edx
mov    %r12,%rdi
mov    %ebx,%esi
call   1050 <printf@plt>
add    $0x1,%rbx
cmp    %rbx,%r13
jne    1200 <main01+0xa0>
jmp    11c0 <main01+0x60>

LLVM IR (-O0):
@.str = [21 x i8] c"D:\\temp\\bin-file.exe\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [14 x i8] c"buf[%d] = %x\0A\00"
define i32 @main01() {
%1 = alloca i32*
%2 = alloca [1024 x i8]
%3 = alloca i32
%4 = alloca i32
%5 = call i32* @fopen(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %5, i32** %1
br label %6
6: ; preds = %30, %0
%7 = load i32*, i32** %1
%8 = call i32 @feof(i32* %7)
%9 = icmp ne i32 %8, 0
%10 = xor i1 %9, true
br i1 %10, label %11, label %31
11: ; preds = %6
%12 = bitcast [1024 x i8]* %2 to i8*
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%14 = load i32*, i32** %1
%15 = call i32 @fread(i8* %13, i32 1, i32 1024, i32* %14)
store i32 %15, i32* %3
store i32 0, i32* %4
br label %16
16: ; preds = %27, %11
%17 = load i32, i32* %4
%18 = load i32, i32* %3
%19 = icmp slt i32 %17, %18
br i1 %19, label %20, label %30
20: ; preds = %16
%21 = load i32, i32* %4
%22 = load i32, i32* %4
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 %23
%25 = load i8, i8* %24
%26 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %21, i8 signext %25)
br label %27
27: ; preds = %20
%28 = load i32, i32* %4
%29 = add nsw i32 %28, 1
store i32 %29, i32* %4
br label %16
30: ; preds = %16
br label %6
31: ; preds = %6
%32 = load i32*, i32** %1
%33 = call i32 @fclose(i32* %32)
ret i32 0
}

LLVM IR (-O2):
@.str = [21 x i8] c"D:\\temp\\bin-file.exe\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [14 x i8] c"buf[%d] = %x\0A\00"
define i32 @main01() {
%1 = alloca [1024 x i8]
%2 = tail call i32* @fopen(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%3 = tail call i32 @feof(i32* %2)
%4 = icmp eq i32 %3, 0
br i1 %4, label %5, label %23
5: ; preds = %0
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
br label %7
7: ; preds = %5, %20
%8 = call i32 @fread(i8* %6, i32 1, i32 1024, i32* %2)
%9 = icmp sgt i32 %8, 0
br i1 %9, label %10, label %20
10: ; preds = %7
%11 = zext i32 %8 to i64
br label %12
12: ; preds = %10, %12
%13 = phi i64 [ 0, %10 ], [ %18, %12 ]
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 %13
%15 = load i8, i8* %14
%16 = trunc i64 %13 to i32
%17 = tail call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %16, i8 signext %15)
%18 = add nuw nsw i64 %13, 1
%19 = icmp eq i64 %18, %11
br i1 %19, label %20, label %12
20: ; preds = %12, %7
%21 = tail call i32 @feof(i32* %2)
%22 = icmp eq i32 %21, 0
br i1 %22, label %7, label %23
23: ; preds = %20, %0
%24 = tail call i32 @fclose(i32* %2)
ret i32 0
}