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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int fwrite(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ my_printf(char *, ...);
int /*<<< orphan*/ set_step(char *);
int /*<<< orphan*/ set_step_progress(int);
int flash_ext4_kernel(char *device, char *filename, off_t kernel_file_size,
                      int quiet, int no_write) {
  char buffer[512];
  FILE *kernel_file;
  kernel_file = fopen(filename, "rb");
  if (kernel_file == NULL) {
    my_printf("Error while opening kernel file %s\n", filename);
    return 0;
  }
  FILE *kernel_dev;
  kernel_dev = fopen(device, "wb");
  if (kernel_dev == NULL) {
    my_printf("Error while opening kernel device %s\n", device);
    return 0;
  }
  set_step("Writing ext4 kernel");
  int ret;
  long long readBytes = 0;
  int current_percent = 0;
  int new_percent = 0;
  while (!feof(kernel_file)) {
    ret = fread(buffer, 1, sizeof(buffer), kernel_file);
    if (ret == 0) {
      if (feof(kernel_file))
        continue;
      my_printf("Error reading kernel file.\n");
      fclose(kernel_file);
      fclose(kernel_dev);
      return 0;
    }
    readBytes += ret;
    new_percent = readBytes * 100 / kernel_file_size;
    if (current_percent < new_percent) {
      set_step_progress(new_percent);
      current_percent = new_percent;
    }
    if (!no_write) {
      ret = fwrite(buffer, ret, 1, kernel_dev);
      if (ret != 1) {
        my_printf("Error writing kernel file to kernel device.\n");
        fclose(kernel_file);
        fclose(kernel_dev);
        return 0;
      }
    }
  }
  fclose(kernel_file);
  fclose(kernel_dev);
  return 1;
}


x86 assembly (clang, -O0):
<flash_ext4_kernel>:
push   %rbp
mov    %rsp,%rbp
sub    $0x260,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    %r8d,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe57(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x238(%rbp)
cmpq   $0x0,-0x238(%rbp)
jne    11e1 <flash_ext4_kernel+0x61>
mov    -0x18(%rbp),%rsi
lea    0xe35(%rip),%rdi
mov    $0x0,%al
call   1050 <my_printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    13d6 <flash_ext4_kernel+0x256>
mov    -0x10(%rbp),%rdi
lea    0xe3b(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x240(%rbp)
cmpq   $0x0,-0x240(%rbp)
jne    1224 <flash_ext4_kernel+0xa4>
mov    -0x10(%rbp),%rsi
lea    0xe19(%rip),%rdi
mov    $0x0,%al
call   1050 <my_printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    13d6 <flash_ext4_kernel+0x256>
lea    0xe25(%rip),%rdi
call   1080 <set_step@plt>
movq   $0x0,-0x250(%rbp)
movl   $0x0,-0x254(%rbp)
movl   $0x0,-0x258(%rbp)
mov    -0x238(%rbp),%rdi
call   1060 <feof@plt>
cmp    $0x0,%rax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1271 <flash_ext4_kernel+0xf1>
jmp    13b7 <flash_ext4_kernel+0x237>
lea    -0x230(%rbp),%rdi
mov    -0x238(%rbp),%rcx
mov    $0x1,%esi
mov    $0x200,%edx
call   1030 <fread@plt>
mov    %eax,-0x244(%rbp)
cmpl   $0x0,-0x244(%rbp)
jne    12ee <flash_ext4_kernel+0x16e>
mov    -0x238(%rbp),%rdi
call   1060 <feof@plt>
cmp    $0x0,%rax
je     12bc <flash_ext4_kernel+0x13c>
jmp    124f <flash_ext4_kernel+0xcf>
lea    0xda1(%rip),%rdi
mov    $0x0,%al
call   1050 <my_printf@plt>
mov    -0x238(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x240(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    13d6 <flash_ext4_kernel+0x256>
movslq -0x244(%rbp),%rax
add    -0x250(%rbp),%rax
mov    %rax,-0x250(%rbp)
imul   $0x64,-0x250(%rbp),%rax
64
cqto
idivq  -0x20(%rbp)
mov    %eax,-0x258(%rbp)
mov    -0x254(%rbp),%eax
cmp    -0x258(%rbp),%eax
jge    1340 <flash_ext4_kernel+0x1c0>
mov    -0x258(%rbp),%edi
call   1070 <set_step_progress@plt>
mov    -0x258(%rbp),%eax
mov    %eax,-0x254(%rbp)
cmpl   $0x0,-0x28(%rbp)
jne    13b2 <flash_ext4_kernel+0x232>
lea    -0x230(%rbp),%rdi
mov    -0x244(%rbp),%esi
mov    -0x240(%rbp),%rcx
mov    $0x1,%edx
call   10a0 <fwrite@plt>
mov    %eax,-0x244(%rbp)
cmpl   $0x1,-0x244(%rbp)
je     13ad <flash_ext4_kernel+0x22d>
lea    0xcfe(%rip),%rdi
mov    $0x0,%al
call   1050 <my_printf@plt>
mov    -0x238(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x240(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    13d6 <flash_ext4_kernel+0x256>
jmp    13b2 <flash_ext4_kernel+0x232>
jmp    124f <flash_ext4_kernel+0xcf>
mov    -0x238(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x240(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x260,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<flash_ext4_kernel>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x218,%rsp
mov    %r8d,%r15d
mov    %rdx,%r12
mov    %rsi,%rbp
mov    %rdi,%r13
lea    0xe5c(%rip),%rsi
mov    %rbp,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     12c0 <flash_ext4_kernel+0x140>
mov    %rax,%rbx
lea    0xe68(%rip),%rsi
mov    %r13,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     12cf <flash_ext4_kernel+0x14f>
mov    %rax,%r14
lea    0xe76(%rip),%rdi
call   1080 <set_step@plt>
xor    %eax,%eax
mov    %rax,0x8(%rsp)
lea    0x10(%rsp),%r13
movl   $0x0,(%rsp)
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1060 <feof@plt>
test   %rax,%rax
jne    12e5 <flash_ext4_kernel+0x165>
mov    %r13,%rdi
mov    $0x1,%esi
mov    $0x200,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
test   %eax,%eax
jne    123c <flash_ext4_kernel+0xbc>
mov    %rbx,%rdi
call   1060 <feof@plt>
test   %rax,%rax
jne    1200 <flash_ext4_kernel+0x80>
jmp    12fd <flash_ext4_kernel+0x17d>
mov    %eax,%ebp
mov    %r15d,0x4(%rsp)
cltq
mov    0x8(%rsp),%rcx
add    %rax,%rcx
mov    %rcx,%rax
mov    %rcx,0x8(%rsp)
imul   $0x64,%rcx,%rax
mov    %rax,%rcx
or     %r12,%rcx
shr    $0x20,%rcx
je     12b0 <flash_ext4_kernel+0x130>
cqto
idiv   %r12
mov    %rax,%r15
cmp    %r15d,(%rsp)
jge    1282 <flash_ext4_kernel+0x102>
mov    %r15d,%edi
call   1070 <set_step_progress@plt>
mov    %r15d,%eax
mov    %r15d,(%rsp)
mov    0x4(%rsp),%r15d
test   %r15d,%r15d
jne    1200 <flash_ext4_kernel+0x80>
mov    %r13,%rdi
mov    %ebp,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   10a0 <fwrite@plt>
cmp    $0x1,%eax
je     1200 <flash_ext4_kernel+0x80>
jmp    1333 <flash_ext4_kernel+0x1b3>
xor    %edx,%edx
div    %r12d
mov    %eax,%r15d
cmp    %r15d,(%rsp)
jl     1273 <flash_ext4_kernel+0xf3>
jmp    1282 <flash_ext4_kernel+0x102>
lea    0xd3c(%rip),%rdi
xor    %r15d,%r15d
mov    %rbp,%rsi
jmp    12dc <flash_ext4_kernel+0x15c>
lea    0xd54(%rip),%rdi
xor    %r15d,%r15d
mov    %r13,%rsi
xor    %eax,%eax
call   1050 <my_printf@plt>
jmp    131e <flash_ext4_kernel+0x19e>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    $0x1,%r15d
jmp    131e <flash_ext4_kernel+0x19e>
lea    0xd60(%rip),%rdi
xor    %r15d,%r15d
xor    %eax,%eax
call   1050 <my_printf@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    %r15d,%eax
add    $0x218,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd46(%rip),%rdi
jmp    1304 <flash_ext4_kernel+0x184>

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [36 x i8] c"Error while opening kernel file %s\0A\00"
@.str.2 = [3 x i8] c"wb\00"
@.str.3 = [38 x i8] c"Error while opening kernel device %s\0A\00"
@.str.4 = [20 x i8] c"Writing ext4 kernel\00"
@.str.5 = [28 x i8] c"Error reading kernel file.\0A\00"
@.str.6 = [45 x i8] c"Error writing kernel file to kernel device.\0A\00"
define i32 @flash_ext4_kernel(i8* %0, i8* %1, i64 %2, i32 %3, i32 %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
%11 = alloca i32
%12 = alloca [512 x i8]
%13 = alloca i32*
%14 = alloca i32*
%15 = alloca i32
%16 = alloca i64
%17 = alloca i32
%18 = alloca i32
store i8* %0, i8** %7
store i8* %1, i8** %8
store i64 %2, i64* %9
store i32 %3, i32* %10
store i32 %4, i32* %11
%19 = load i8*, i8** %8
%20 = call i32* @fopen(i8* %19, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %20, i32** %13
%21 = load i32*, i32** %13
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %26
23: ; preds = %5
%24 = load i8*, i8** %8
%25 = call i32 (i8*, ...) @my_printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i8* %24)
store i32 0, i32* %6
br label %98
26: ; preds = %5
%27 = load i8*, i8** %7
%28 = call i32* @fopen(i8* %27, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %28, i32** %14
%29 = load i32*, i32** %14
%30 = icmp eq i32* %29, null
br i1 %30, label %31, label %34
31: ; preds = %26
%32 = load i8*, i8** %7
%33 = call i32 (i8*, ...) @my_printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i8* %32)
store i32 0, i32* %6
br label %98
34: ; preds = %26
%35 = call i32 @set_step(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
store i64 0, i64* %16
store i32 0, i32* %17
store i32 0, i32* %18
br label %36
36: ; preds = %92, %51, %34
%37 = load i32*, i32** %13
%38 = call i64 @feof(i32* %37)
%39 = icmp ne i64 %38, 0
%40 = xor i1 %39, true
br i1 %40, label %41, label %93
41: ; preds = %36
%42 = getelementptr inbounds [512 x i8], [512 x i8]* %12, i64 0, i64 0
%43 = load i32*, i32** %13
%44 = call i32 @fread(i8* %42, i32 1, i32 512, i32* %43)
store i32 %44, i32* %15
%45 = load i32, i32* %15
%46 = icmp eq i32 %45, 0
br i1 %46, label %47, label %58
47: ; preds = %41
%48 = load i32*, i32** %13
%49 = call i64 @feof(i32* %48)
%50 = icmp ne i64 %49, 0
br i1 %50, label %51, label %52
51: ; preds = %47
br label %36
52: ; preds = %47
%53 = call i32 (i8*, ...) @my_printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0))
%54 = load i32*, i32** %13
%55 = call i32 @fclose(i32* %54)
%56 = load i32*, i32** %14
%57 = call i32 @fclose(i32* %56)
store i32 0, i32* %6
br label %98
58: ; preds = %41
%59 = load i32, i32* %15
%60 = sext i32 %59 to i64
%61 = load i64, i64* %16
%62 = add nsw i64 %61, %60
store i64 %62, i64* %16
%63 = load i64, i64* %16
%64 = mul nsw i64 %63, 100
%65 = load i64, i64* %9
%66 = sdiv i64 %64, %65
%67 = trunc i64 %66 to i32
store i32 %67, i32* %18
%68 = load i32, i32* %17
%69 = load i32, i32* %18
%70 = icmp slt i32 %68, %69
br i1 %70, label %71, label %75
71: ; preds = %58
%72 = load i32, i32* %18
%73 = call i32 @set_step_progress(i32 %72)
%74 = load i32, i32* %18
store i32 %74, i32* %17
br label %75
75: ; preds = %71, %58
%76 = load i32, i32* %11
%77 = icmp ne i32 %76, 0
br i1 %77, label %92, label %78
78: ; preds = %75
%79 = getelementptr inbounds [512 x i8], [512 x i8]* %12, i64 0, i64 0
%80 = load i32, i32* %15
%81 = load i32*, i32** %14
%82 = call i32 @fwrite(i8* %79, i32 %80, i32 1, i32* %81)
store i32 %82, i32* %15
%83 = load i32, i32* %15
%84 = icmp ne i32 %83, 1
br i1 %84, label %85, label %91
85: ; preds = %78
%86 = call i32 (i8*, ...) @my_printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.6, i64 0, i64 0))
%87 = load i32*, i32** %13
%88 = call i32 @fclose(i32* %87)
%89 = load i32*, i32** %14
%90 = call i32 @fclose(i32* %89)
store i32 0, i32* %6
br label %98
91: ; preds = %78
br label %92
92: ; preds = %91, %75
br label %36
93: ; preds = %36
%94 = load i32*, i32** %13
%95 = call i32 @fclose(i32* %94)
%96 = load i32*, i32** %14
%97 = call i32 @fclose(i32* %96)
store i32 1, i32* %6
br label %98
98: ; preds = %93, %85, %52, %31, %23
%99 = load i32, i32* %6
ret i32 %99
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [36 x i8] c"Error while opening kernel file %s\0A\00"
@.str.2 = [3 x i8] c"wb\00"
@.str.3 = [38 x i8] c"Error while opening kernel device %s\0A\00"
@.str.4 = [20 x i8] c"Writing ext4 kernel\00"
@.str.5 = [28 x i8] c"Error reading kernel file.\0A\00"
@.str.6 = [45 x i8] c"Error writing kernel file to kernel device.\0A\00"
define i32 @flash_ext4_kernel(i8* %0, i8* %1, i64 %2, i32 %3, i32 %4) {
%6 = alloca [512 x i8]
%7 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%8 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %12
10: ; preds = %5
%11 = tail call i32 (i8*, ...) @my_printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i8* %1)
br label %58
12: ; preds = %5
%13 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = tail call i32 (i8*, ...) @my_printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br label %58
17: ; preds = %12
%18 = tail call i32 @set_step(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
%19 = icmp eq i32 %4, 0
br label %20
20: ; preds = %50, %17
%21 = phi i64 [ 0, %17 ], [ %38, %50 ]
%22 = phi i32 [ 0, %17 ], [ %46, %50 ]
br label %23
23: ; preds = %20, %29
%24 = tail call i64 @feof(i32* %8)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %55
26: ; preds = %23
%27 = call i32 @fread(i8* %7, i32 1, i32 512, i32* %8)
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %36
29: ; preds = %26
%30 = tail call i64 @feof(i32* %8)
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %23
32: ; preds = %29
%33 = tail call i32 (i8*, ...) @my_printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0))
%34 = tail call i32 @fclose(i32* %8)
%35 = tail call i32 @fclose(i32* %13)
br label %58
36: ; preds = %26
%37 = sext i32 %27 to i64
%38 = add nsw i64 %21, %37
%39 = mul nsw i64 %38, 100
%40 = sdiv i64 %39, %2
%41 = trunc i64 %40 to i32
%42 = icmp slt i32 %22, %41
br i1 %42, label %43, label %45
43: ; preds = %36
%44 = tail call i32 @set_step_progress(i32 %41)
br label %45
45: ; preds = %43, %36
%46 = phi i32 [ %41, %43 ], [ %22, %36 ]
br i1 %19, label %47, label %50
47: ; preds = %45
%48 = call i32 @fwrite(i8* %7, i32 %27, i32 1, i32* %13)
%49 = icmp eq i32 %48, 1
br i1 %49, label %50, label %51
50: ; preds = %47, %45
br label %20
51: ; preds = %47
%52 = tail call i32 (i8*, ...) @my_printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.6, i64 0, i64 0))
%53 = tail call i32 @fclose(i32* %8)
%54 = tail call i32 @fclose(i32* %13)
br label %58
55: ; preds = %23
%56 = tail call i32 @fclose(i32* %8)
%57 = tail call i32 @fclose(i32* %13)
br label %58
58: ; preds = %15, %55, %51, %32, %10
%59 = phi i32 [ 0, %10 ], [ 0, %15 ], [ 0, %32 ], [ 0, %51 ], [ 1, %55 ]
ret i32 %59
}