#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(unsigned char *);
int /*<<< orphan*/ fwrite(unsigned char *, int, int, int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memset(unsigned char *, int, int);
int /*<<< orphan*/ perror(char *);
int write_image(const char *image_path, int partition_size, FILE *ofp) {
  FILE *fp = fopen(image_path, "rb");
  if (fp == NULL) {
    perror("fopen");
    return -1;
  }
  unsigned char buf[(4 * 1024)];
  int bytes_read = 0, total_bytes_written = 0;
  while (!feof(fp)) {
    bytes_read = fread(buf, 1, (4 * 1024), fp);
    fwrite(buf, 1, bytes_read, ofp);
    total_bytes_written += bytes_read;
  }
  if (total_bytes_written < partition_size) {
    int bytes_left = partition_size - total_bytes_written;
    unsigned char *pad_buf = (unsigned char *)malloc(bytes_left);
    memset(pad_buf, 0xFF, bytes_left);
    fwrite(pad_buf, 1, bytes_left, ofp);
    free(pad_buf);
  }
  fclose(fp);
}


x86 assembly (clang, -O0):
<write_image>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1050,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe4f(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11dd <write_image+0x4d>
lea    0xe37(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d8 <write_image+0x148>
movl   $0x0,-0x1034(%rbp)
movl   $0x0,-0x1038(%rbp)
mov    -0x28(%rbp),%rdi
call   1070 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    120f <write_image+0x7f>
jmp    1261 <write_image+0xd1>
lea    -0x1030(%rbp),%rdi
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
mov    $0x1000,%edx
call   1040 <fread@plt>
mov    %eax,-0x1034(%rbp)
lea    -0x1030(%rbp),%rdi
mov    -0x1034(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   10b0 <fwrite@plt>
mov    -0x1034(%rbp),%eax
add    -0x1038(%rbp),%eax
mov    %eax,-0x1038(%rbp)
jmp    11f1 <write_image+0x61>
mov    -0x1038(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    12cf <write_image+0x13f>
mov    -0x14(%rbp),%eax
sub    -0x1038(%rbp),%eax
mov    %eax,-0x103c(%rbp)
mov    -0x103c(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,-0x1048(%rbp)
mov    -0x1048(%rbp),%rdi
mov    -0x103c(%rbp),%edx
mov    $0xff,%esi
call   1060 <memset@plt>
mov    -0x1048(%rbp),%rdi
mov    -0x103c(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   10b0 <fwrite@plt>
mov    -0x1048(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x4(%rbp),%eax
add    $0x1050,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_image>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1008,%rsp
mov    %rdx,%r14
mov    %esi,%r15d
lea    0xe52(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1274 <write_image+0xe4>
mov    %rax,%r13
mov    %rax,%rdi
call   1070 <feof@plt>
xor    %ebx,%ebx
test   %eax,%eax
jne    1217 <write_image+0x87>
xor    %ebx,%ebx
mov    %rsp,%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r12,%rdi
mov    $0x1,%esi
mov    $0x1000,%edx
mov    %r13,%rcx
call   1040 <fread@plt>
mov    %eax,%ebp
mov    %r12,%rdi
mov    $0x1,%esi
mov    %eax,%edx
mov    %r14,%rcx
call   10b0 <fwrite@plt>
add    %ebp,%ebx
mov    %r13,%rdi
call   1070 <feof@plt>
test   %eax,%eax
je     11e0 <write_image+0x50>
cmp    %r15d,%ebx
jge    1255 <write_image+0xc5>
sub    %ebx,%r15d
mov    %r15d,%edi
call   1080 <malloc@plt>
mov    %rax,%rbp
mov    %rax,%rdi
mov    $0xff,%esi
mov    %r15d,%edx
call   1060 <memset@plt>
mov    %rbp,%rdi
mov    $0x1,%esi
mov    %r15d,%edx
mov    %r14,%rcx
call   10b0 <fwrite@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %r13,%rdi
call   1050 <fclose@plt>
mov    $0xffffffff,%eax
add    $0x1008,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd88(%rip),%rdi
call   10a0 <perror@plt>
jmp    125d <write_image+0xcd>

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [6 x i8] c"fopen\00"
define i32 @write_image(i8* %0, i32 %1, i32* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca [4096 x i8]
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i8*
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32* %2, i32** %7
%14 = load i8*, i8** %5
%15 = call i32* @fopen(i8* %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %15, i32** %8
%16 = load i32*, i32** %8
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %20
18: ; preds = %3
%19 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %4
br label %60
20: ; preds = %3
store i32 0, i32* %10
store i32 0, i32* %11
br label %21
21: ; preds = %26, %20
%22 = load i32*, i32** %8
%23 = call i32 @feof(i32* %22)
%24 = icmp ne i32 %23, 0
%25 = xor i1 %24, true
br i1 %25, label %26, label %37
26: ; preds = %21
%27 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%28 = load i32*, i32** %8
%29 = call i32 @fread(i8* %27, i32 1, i32 4096, i32* %28)
store i32 %29, i32* %10
%30 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%31 = load i32, i32* %10
%32 = load i32*, i32** %7
%33 = call i32 @fwrite(i8* %30, i32 1, i32 %31, i32* %32)
%34 = load i32, i32* %10
%35 = load i32, i32* %11
%36 = add nsw i32 %35, %34
store i32 %36, i32* %11
br label %21
37: ; preds = %21
%38 = load i32, i32* %11
%39 = load i32, i32* %6
%40 = icmp slt i32 %38, %39
br i1 %40, label %41, label %57
41: ; preds = %37
%42 = load i32, i32* %6
%43 = load i32, i32* %11
%44 = sub nsw i32 %42, %43
store i32 %44, i32* %12
%45 = load i32, i32* %12
%46 = call i64 @malloc(i32 %45)
%47 = inttoptr i64 %46 to i8*
store i8* %47, i8** %13
%48 = load i8*, i8** %13
%49 = load i32, i32* %12
%50 = call i32 @memset(i8* %48, i32 255, i32 %49)
%51 = load i8*, i8** %13
%52 = load i32, i32* %12
%53 = load i32*, i32** %7
%54 = call i32 @fwrite(i8* %51, i32 1, i32 %52, i32* %53)
%55 = load i8*, i8** %13
%56 = call i32 @free(i8* %55)
br label %57
57: ; preds = %41, %37
%58 = load i32*, i32** %8
%59 = call i32 @fclose(i32* %58)
br label %60
60: ; preds = %57, %18
%61 = load i32, i32* %4
ret i32 %61
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [6 x i8] c"fopen\00"
define i32 @write_image(i8* nocapture %0, i32 %1, i32* nocapture %2) {
%4 = alloca [4096 x i8]
%5 = tail call i32* @fopen(i8* %0, 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 = %3
%8 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %32
9: ; preds = %3
%10 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%11 = tail call i32 @feof(i32* %5)
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %20
13: ; preds = %9, %13
%14 = phi i32 [ %17, %13 ], [ 0, %9 ]
%15 = call i32 @fread(i8* %10, i32 1, i32 4096, i32* %5)
%16 = call i32 @fwrite(i8* %10, i32 1, i32 %15, i32* %2)
%17 = add nsw i32 %15, %14
%18 = tail call i32 @feof(i32* %5)
%19 = icmp eq i32 %18, 0
br i1 %19, label %13, label %20
20: ; preds = %13, %9
%21 = phi i32 [ 0, %9 ], [ %17, %13 ]
%22 = icmp slt i32 %21, %1
br i1 %22, label %23, label %30
23: ; preds = %20
%24 = sub nsw i32 %1, %21
%25 = tail call i64 @malloc(i32 %24)
%26 = inttoptr i64 %25 to i8*
%27 = tail call i32 @memset(i8* %26, i32 255, i32 %24)
%28 = tail call i32 @fwrite(i8* %26, i32 1, i32 %24, i32* %2)
%29 = tail call i32 @free(i8* %26)
br label %30
30: ; preds = %23, %20
%31 = tail call i32 @fclose(i32* %5)
br label %32
32: ; preds = %30, %7
ret i32 -1
}