#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*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int fseek(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ parser_boots(char *);
int /*<<< orphan*/ parser_rootdir(char *);
int /*<<< orphan*/ printf(char *, ...);
int main_proc(const char *imgfile) {
  FILE *pin;
  long file_size;
  char buf[513];
  int read_cnt;
  unsigned long cnt_in;
  cnt_in = 0;
  pin = fopen(imgfile, "rb");
  if (pin == NULL) {
    return -1;
  }
  read_cnt = fseek(pin, 0, SEEK_END);
  if (read_cnt != 0) {
    printf("fseek end error\n");
    return -1;
  }
  file_size = ftell(pin);
  printf("image file's size:%d\n", file_size);
  read_cnt = fseek(pin, 0, SEEK_SET);
  if (read_cnt != 0) {
    printf("fseek begin error\n");
    return -1;
  }
  if (file_size == 0 || file_size % 512 != 0) {
    printf("512 error\n");
    return -1;
  }
  printf("/512:%d(=%d)\n", file_size / 512, 80 * 2 * 18);
  while (!feof(pin)) {
    memset(buf, 0, sizeof(buf));
    read_cnt = fread(buf, sizeof(char), 512, pin);
    if (read_cnt != 512) {
      break;
    }
    cnt_in++;
    if (cnt_in == 1) {
      parser_boots(buf);
    }
    if (cnt_in == 20) {
      parser_rootdir(buf);
    }
  }
  if (pin != NULL) {
    fclose(pin);
    pin = NULL;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main_proc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x240,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x240(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe3b(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11e5 <main_proc+0x45>
movl   $0xffffffff,-0x4(%rbp)
jmp    13bb <main_proc+0x21b>
mov    -0x18(%rbp),%rdi
mov    0x2df8(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10b0 <fseek@plt>
mov    %eax,-0x234(%rbp)
cmpl   $0x0,-0x234(%rbp)
je     1226 <main_proc+0x86>
lea    0xdf0(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13bb <main_proc+0x21b>
mov    -0x18(%rbp),%rdi
call   1080 <ftell@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rsi
lea    0xdd6(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    0x2d88(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10b0 <fseek@plt>
mov    %eax,-0x234(%rbp)
cmpl   $0x0,-0x234(%rbp)
je     1286 <main_proc+0xe6>
lea    0xdb7(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13bb <main_proc+0x21b>
cmpq   $0x0,-0x20(%rbp)
je     12a9 <main_proc+0x109>
mov    -0x20(%rbp),%rax
mov    $0x200,%ecx
cqto
idiv   %rcx
cmp    $0x0,%rdx
je     12c3 <main_proc+0x123>
lea    0xd8d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13bb <main_proc+0x21b>
mov    -0x20(%rbp),%rax
mov    $0x200,%ecx
cqto
idiv   %rcx
mov    %rax,%rsi
lea    0xd6d(%rip),%rdi
mov    $0xb40,%edx
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1090 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1305 <main_proc+0x165>
jmp    1398 <main_proc+0x1f8>
lea    -0x230(%rbp),%rdi
xor    %esi,%esi
mov    $0x201,%edx
call   1060 <memset@plt>
lea    -0x230(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
mov    $0x200,%edx
call   1030 <fread@plt>
mov    %eax,-0x234(%rbp)
cmpl   $0x200,-0x234(%rbp)
je     134d <main_proc+0x1ad>
jmp    1398 <main_proc+0x1f8>
mov    -0x240(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x240(%rbp)
cmpq   $0x1,-0x240(%rbp)
01
jne    1379 <main_proc+0x1d9>
lea    -0x230(%rbp),%rdi
call   1070 <parser_boots@plt>
cmpq   $0x14,-0x240(%rbp)
14
jne    1393 <main_proc+0x1f3>
lea    -0x230(%rbp),%rdi
call   10a0 <parser_rootdir@plt>
jmp    12e7 <main_proc+0x147>
cmpq   $0x0,-0x18(%rbp)
je     13b4 <main_proc+0x214>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
movq   $0x0,-0x18(%rbp)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x240,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main_proc>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x208,%rsp
lea    0xe3c(%rip),%rsi
call   10d0 <fopen@plt>
mov    $0xffffffff,%r15d
test   %rax,%rax
je     1295 <main_proc+0xe5>
mov    %rax,%rbx
mov    0x2e06(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10c0 <fseek@plt>
test   %eax,%eax
je     11fe <main_proc+0x4e>
lea    0xe4a(%rip),%rdi
jmp    1290 <main_proc+0xe0>
mov    %rbx,%rdi
call   1090 <ftell@plt>
mov    %rax,%rbp
lea    0xdf3(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2db7(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10c0 <fseek@plt>
test   %eax,%eax
je     123a <main_proc+0x8a>
lea    0xdf9(%rip),%rdi
jmp    1290 <main_proc+0xe0>
test   %rbp,%rbp
je     1289 <main_proc+0xd9>
mov    %ebp,%eax
and    $0x1ff,%eax
test   %rax,%rax
jne    1289 <main_proc+0xd9>
lea    0x1ff(%rbp),%rsi
test   %rbp,%rbp
cmovns %rbp,%rsi
sar    $0x9,%rsi
lea    0xdb5(%rip),%rdi
xor    %r15d,%r15d
mov    $0xb40,%edx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
call   10a0 <feof@plt>
test   %eax,%eax
je     12a6 <main_proc+0xf6>
mov    %rbx,%rdi
call   1050 <fclose@plt>
jmp    1295 <main_proc+0xe5>
lea    0xd97(%rip),%rdi
call   1030 <puts@plt>
mov    %r15d,%eax
add    $0x208,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x13,%ebp
mov    %rsp,%r14
jmp    12c8 <main_proc+0x118>
mov    %r14,%rdi
call   1080 <parser_boots@plt>
mov    %rbx,%rdi
call   10a0 <feof@plt>
add    $0xffffffffffffffff,%rbp
test   %eax,%eax
jne    127f <main_proc+0xcf>
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x201,%edx
call   1070 <memset@plt>
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0x200,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
cmp    $0x200,%eax
jne    127f <main_proc+0xcf>
cmp    $0x13,%rbp
je     12b0 <main_proc+0x100>
test   %rbp,%rbp
jne    12b8 <main_proc+0x108>
mov    %r14,%rdi
call   10b0 <parser_rootdir@plt>
jmp    12b8 <main_proc+0x108>

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@.str.1 = [17 x i8] c"fseek end error\0A\00"
@.str.2 = [22 x i8] c"image file's size:%d\0A\00"
@SEEK_SET = global i32 0
@.str.3 = [19 x i8] c"fseek begin error\0A\00"
@.str.4 = [11 x i8] c"512 error\0A\00"
@.str.5 = [14 x i8] c"/512:%d(=%d)\0A\00"
define i32 @main_proc(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i64
%6 = alloca [513 x i8]
%7 = alloca i32
%8 = alloca i64
store i8* %0, i8** %3
store i64 0, i64* %8
%9 = load i8*, i8** %3
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %4
%11 = load i32*, i32** %4
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %14
13: ; preds = %1
store i32 -1, i32* %2
br label %83
14: ; preds = %1
%15 = load i32*, i32** %4
%16 = load i32, i32* @SEEK_END
%17 = call i32 @fseek(i32* %15, i32 0, i32 %16)
store i32 %17, i32* %7
%18 = load i32, i32* %7
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %14
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %83
22: ; preds = %14
%23 = load i32*, i32** %4
%24 = call i64 @ftell(i32* %23)
store i64 %24, i64* %5
%25 = load i64, i64* %5
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i64 %25)
%27 = load i32*, i32** %4
%28 = load i32, i32* @SEEK_SET
%29 = call i32 @fseek(i32* %27, i32 0, i32 %28)
store i32 %29, i32* %7
%30 = load i32, i32* %7
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %22
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %2
br label %83
34: ; preds = %22
%35 = load i64, i64* %5
%36 = icmp eq i64 %35, 0
br i1 %36, label %41, label %37
37: ; preds = %34
%38 = load i64, i64* %5
%39 = srem i64 %38, 512
%40 = icmp ne i64 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %37, %34
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %2
br label %83
43: ; preds = %37
%44 = load i64, i64* %5
%45 = sdiv i64 %44, 512
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i64 %45, i32 2880)
br label %47
47: ; preds = %75, %43
%48 = load i32*, i32** %4
%49 = call i32 @feof(i32* %48)
%50 = icmp ne i32 %49, 0
%51 = xor i1 %50, true
br i1 %51, label %52, label %76
52: ; preds = %47
%53 = getelementptr inbounds [513 x i8], [513 x i8]* %6, i64 0, i64 0
%54 = call i32 @memset(i8* %53, i32 0, i32 513)
%55 = getelementptr inbounds [513 x i8], [513 x i8]* %6, i64 0, i64 0
%56 = load i32*, i32** %4
%57 = call i32 @fread(i8* %55, i32 1, i32 512, i32* %56)
store i32 %57, i32* %7
%58 = load i32, i32* %7
%59 = icmp ne i32 %58, 512
br i1 %59, label %60, label %61
60: ; preds = %52
br label %76
61: ; preds = %52
%62 = load i64, i64* %8
%63 = add i64 %62, 1
store i64 %63, i64* %8
%64 = load i64, i64* %8
%65 = icmp eq i64 %64, 1
br i1 %65, label %66, label %69
66: ; preds = %61
%67 = getelementptr inbounds [513 x i8], [513 x i8]* %6, i64 0, i64 0
%68 = call i32 @parser_boots(i8* %67)
br label %69
69: ; preds = %66, %61
%70 = load i64, i64* %8
%71 = icmp eq i64 %70, 20
br i1 %71, label %72, label %75
72: ; preds = %69
%73 = getelementptr inbounds [513 x i8], [513 x i8]* %6, i64 0, i64 0
%74 = call i32 @parser_rootdir(i8* %73)
br label %75
75: ; preds = %72, %69
br label %47
76: ; preds = %60, %47
%77 = load i32*, i32** %4
%78 = icmp ne i32* %77, null
br i1 %78, label %79, label %82
79: ; preds = %76
%80 = load i32*, i32** %4
%81 = call i32 @fclose(i32* %80)
store i32* null, i32** %4
br label %82
82: ; preds = %79, %76
store i32 0, i32* %2
br label %83
83: ; preds = %82, %41, %32, %20, %13
%84 = load i32, i32* %2
ret i32 %84
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@.str.2 = [22 x i8] c"image file's size:%d\0A\00"
@SEEK_SET = global i32 0
@.str.5 = [14 x i8] c"/512:%d(=%d)\0A\00"
@str = [10 x i8] c"512 error\00"
@str.6 = [18 x i8] c"fseek begin error\00"
@str.7 = [16 x i8] c"fseek end error\00"
define i32 @main_proc(i8* nocapture %0) {
%2 = alloca [513 x i8]
%3 = getelementptr inbounds [513 x i8], [513 x i8]* %2, i64 0, i64 0
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %51, label %6
6: ; preds = %1
%7 = load i32, i32* @SEEK_END
%8 = tail call i32 @fseek(i32* %4, i32 0, i32 %7)
%9 = icmp eq i32 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %6
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.7, i64 0, i64 0))
br label %51
12: ; preds = %6
%13 = tail call i64 @ftell(i32* %4)
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i64 %13)
%15 = load i32, i32* @SEEK_SET
%16 = tail call i32 @fseek(i32* %4, i32 0, i32 %15)
%17 = icmp eq i32 %16, 0
br i1 %17, label %20, label %18
18: ; preds = %12
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.6, i64 0, i64 0))
br label %51
20: ; preds = %12
%21 = icmp ne i64 %13, 0
%22 = and i64 %13, 511
%23 = icmp eq i64 %22, 0
%24 = and i1 %21, %23
br i1 %24, label %27, label %25
25: ; preds = %20
%26 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str, i64 0, i64 0))
br label %51
27: ; preds = %20
%28 = sdiv i64 %13, 512
%29 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i64 %28, i32 2880)
%30 = call i32 @feof(i32* %4)
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %49
32: ; preds = %27, %46
%33 = phi i64 [ %38, %46 ], [ 0, %27 ]
%34 = call i32 @memset(i8* %3, i32 0, i32 513)
%35 = call i32 @fread(i8* %3, i32 1, i32 512, i32* %4)
%36 = icmp eq i32 %35, 512
br i1 %36, label %37, label %49
37: ; preds = %32
%38 = add i64 %33, 1
%39 = icmp eq i64 %33, 0
br i1 %39, label %40, label %42
40: ; preds = %37
%41 = call i32 @parser_boots(i8* %3)
br label %46
42: ; preds = %37
%43 = icmp eq i64 %38, 20
br i1 %43, label %44, label %46
44: ; preds = %42
%45 = call i32 @parser_rootdir(i8* %3)
br label %46
46: ; preds = %40, %44, %42
%47 = call i32 @feof(i32* %4)
%48 = icmp eq i32 %47, 0
br i1 %48, label %32, label %49
49: ; preds = %32, %46, %27
%50 = call i32 @fclose(i32* %4)
br label %51
51: ; preds = %1, %49, %25, %18, %10
%52 = phi i32 [ -1, %10 ], [ -1, %18 ], [ -1, %25 ], [ 0, %49 ], [ -1, %1 ]
ret i32 %52
}