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

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fread(int /*<<< orphan*/ *, scalar_t__, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
scalar_t__ lseek(int, scalar_t__, int /*<<< orphan*/);
scalar_t__ malloc(scalar_t__);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, scalar_t__, char const *, scalar_t__);
int write(int, int /*<<< orphan*/ *, scalar_t__);
int file_to_flash(int fd, u_int32_t offset, u_int32_t len,
                  const char *filename) {
  u_int8_t *buf;
  FILE *fp;
  int err;
  if (offset != lseek(fd, offset, SEEK_SET)) {
    perror("lseek()");
    return (1);
  }
  if ((buf = (u_int8_t *)malloc(len * sizeof(u_int8_t))) == NULL) {
    perror("malloc()");
    return (1);
  }
  if ((fp = fopen(filename, "r")) == NULL) {
    perror("fopen()");
    free(buf);
    return (1);
  }
  if (fread(buf, len, 1, fp) != 1 || ferror(fp)) {
    perror("fread()");
    free(buf);
    fclose(fp);
    return (1);
  }
  err = write(fd, buf, len);
  if (err < 0) {
    perror("write()");
    free(buf);
    fclose(fp);
    return (1);
  }
  free(buf);
  fclose(fp);
  printf("Copied %d bytes from %s to address 0x%.8x in flash\n", len, filename,
         offset);
  return (0);
}


x86 assembly (clang, -O0):
<file_to_flash>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2e13(%rip),%rax
mov    (%rax),%edx
call   1090 <lseek@plt>
mov    %rax,%rcx
mov    -0x40(%rbp),%rax
cmp    %rcx,%rax
je     11fc <file_to_flash+0x5c>
lea    0xe15(%rip),%rdi
call   10c0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1342 <file_to_flash+0x1a2>
mov    -0x18(%rbp),%rdi
shl    $0x2,%rdi
call   10a0 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    122f <file_to_flash+0x8f>
lea    0xdea(%rip),%rdi
call   10c0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1342 <file_to_flash+0x1a2>
mov    -0x20(%rbp),%rdi
lea    0xdd7(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    126e <file_to_flash+0xce>
lea    0xdbf(%rip),%rdi
call   10c0 <perror@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0x1,-0x4(%rbp)
jmp    1342 <file_to_flash+0x1a2>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x30(%rbp),%rcx
mov    $0x1,%edx
call   1050 <fread@plt>
cmp    $0x1,%eax
jne    12a0 <file_to_flash+0x100>
mov    -0x30(%rbp),%rdi
call   1040 <ferror@plt>
cmp    $0x0,%rax
je     12ca <file_to_flash+0x12a>
lea    0xd74(%rip),%rdi
call   10c0 <perror@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rdi
call   1070 <fclose@plt>
movl   $0x1,-0x4(%rbp)
jmp    1342 <file_to_flash+0x1a2>
mov    -0x8(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1060 <write@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    1311 <file_to_flash+0x171>
lea    0xd35(%rip),%rdi
call   10c0 <perror@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rdi
call   1070 <fclose@plt>
movl   $0x1,-0x4(%rbp)
jmp    1342 <file_to_flash+0x1a2>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rdi
call   1070 <fclose@plt>
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xcf5(%rip),%rdi
call   1080 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_to_flash>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r12
mov    %rdx,%r13
mov    %rsi,%r15
mov    %edi,%r14d
mov    0x2e22(%rip),%rax
mov    (%rax),%edx
call   1090 <lseek@plt>
cmp    %r15,%rax
jne    1287 <file_to_flash+0xe7>
lea    0x0(,%r13,4),%rdi
call   10a0 <malloc@plt>
test   %rax,%rax
je     1290 <file_to_flash+0xf0>
mov    %rax,%rbp
lea    0xe23(%rip),%rsi
mov    %r12,%rdi
call   10b0 <fopen@plt>
test   %rax,%rax
je     12a4 <file_to_flash+0x104>
mov    %rax,%rbx
mov    %r12,(%rsp)
mov    $0x1,%r12d
mov    %rbp,%rdi
mov    %r13,%rsi
mov    $0x1,%edx
mov    %rax,%rcx
call   1050 <fread@plt>
cmp    $0x1,%eax
jne    127e <file_to_flash+0xde>
mov    %rbx,%rdi
call   1040 <ferror@plt>
test   %rax,%rax
jne    127e <file_to_flash+0xde>
mov    %r14d,%edi
mov    %rbp,%rsi
mov    %r13,%rdx
call   1060 <write@plt>
test   %eax,%eax
js     12c0 <file_to_flash+0x120>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1070 <fclose@plt>
lea    0xdd1(%rip),%rdi
mov    %r13,%rsi
mov    (%rsp),%rdx
mov    %r15,%rcx
call   1080 <printf@plt>
xor    %r12d,%r12d
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd96(%rip),%rdi
jmp    12c7 <file_to_flash+0x127>
lea    0xd72(%rip),%rdi
jmp    1297 <file_to_flash+0xf7>
lea    0xd71(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%r12d
jmp    126c <file_to_flash+0xcc>
lea    0xd68(%rip),%rdi
call   10c0 <perror@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    $0x1,%r12d
jmp    126c <file_to_flash+0xcc>
lea    0xd5c(%rip),%rdi
call   10c0 <perror@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1070 <fclose@plt>
jmp    126c <file_to_flash+0xcc>

LLVM IR (-O0):
@SEEK_SET = global i32 0
@.str = [8 x i8] c"lseek()\00"
@.str.1 = [9 x i8] c"malloc()\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [8 x i8] c"fopen()\00"
@.str.4 = [8 x i8] c"fread()\00"
@.str.5 = [8 x i8] c"write()\00"
@.str.6 = [52 x i8] c"Copied %d bytes from %s to address 0x%.8x in flash\0A\00"
define i32 @file_to_flash(i32 %0, i64 %1, i64 %2, i8* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i32*
%11 = alloca i32*
%12 = alloca i32
store i32 %0, i32* %6
store i64 %1, i64* %7
store i64 %2, i64* %8
store i8* %3, i8** %9
%13 = load i64, i64* %7
%14 = load i32, i32* %6
%15 = load i64, i64* %7
%16 = load i32, i32* @SEEK_SET
%17 = call i64 @lseek(i32 %14, i64 %15, i32 %16)
%18 = icmp ne i64 %13, %17
br i1 %18, label %19, label %21
19: ; preds = %4
%20 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %5
br label %75
21: ; preds = %4
%22 = load i64, i64* %8
%23 = mul i64 %22, 4
%24 = call i64 @malloc(i64 %23)
%25 = inttoptr i64 %24 to i32*
store i32* %25, i32** %10
%26 = icmp eq i32* %25, null
br i1 %26, label %27, label %29
27: ; preds = %21
%28 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %5
br label %75
29: ; preds = %21
%30 = load i8*, i8** %9
%31 = call i32* @fopen(i8* %30, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %31, i32** %11
%32 = icmp eq i32* %31, null
br i1 %32, label %33, label %37
33: ; preds = %29
%34 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%35 = load i32*, i32** %10
%36 = call i32 @free(i32* %35)
store i32 1, i32* %5
br label %75
37: ; preds = %29
%38 = load i32*, i32** %10
%39 = load i64, i64* %8
%40 = load i32*, i32** %11
%41 = call i32 @fread(i32* %38, i64 %39, i32 1, i32* %40)
%42 = icmp ne i32 %41, 1
br i1 %42, label %47, label %43
43: ; preds = %37
%44 = load i32*, i32** %11
%45 = call i64 @ferror(i32* %44)
%46 = icmp ne i64 %45, 0
br i1 %46, label %47, label %53
47: ; preds = %43, %37
%48 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
%49 = load i32*, i32** %10
%50 = call i32 @free(i32* %49)
%51 = load i32*, i32** %11
%52 = call i32 @fclose(i32* %51)
store i32 1, i32* %5
br label %75
53: ; preds = %43
%54 = load i32, i32* %6
%55 = load i32*, i32** %10
%56 = load i64, i64* %8
%57 = call i32 @write(i32 %54, i32* %55, i64 %56)
store i32 %57, i32* %12
%58 = load i32, i32* %12
%59 = icmp slt i32 %58, 0
br i1 %59, label %60, label %66
60: ; preds = %53
%61 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0))
%62 = load i32*, i32** %10
%63 = call i32 @free(i32* %62)
%64 = load i32*, i32** %11
%65 = call i32 @fclose(i32* %64)
store i32 1, i32* %5
br label %75
66: ; preds = %53
%67 = load i32*, i32** %10
%68 = call i32 @free(i32* %67)
%69 = load i32*, i32** %11
%70 = call i32 @fclose(i32* %69)
%71 = load i64, i64* %8
%72 = load i8*, i8** %9
%73 = load i64, i64* %7
%74 = call i32 @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.6, i64 0, i64 0), i64 %71, i8* %72, i64 %73)
store i32 0, i32* %5
br label %75
75: ; preds = %66, %60, %47, %33, %27, %19
%76 = load i32, i32* %5
ret i32 %76
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
@.str = [8 x i8] c"lseek()\00"
@.str.1 = [9 x i8] c"malloc()\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [8 x i8] c"fopen()\00"
@.str.4 = [8 x i8] c"fread()\00"
@.str.5 = [8 x i8] c"write()\00"
@.str.6 = [52 x i8] c"Copied %d bytes from %s to address 0x%.8x in flash\0A\00"
define i32 @file_to_flash(i32 %0, i64 %1, i64 %2, i8* %3) {
%5 = load i32, i32* @SEEK_SET
%6 = tail call i64 @lseek(i32 %0, i64 %1, i32 %5)
%7 = icmp eq i64 %6, %1
br i1 %7, label %10, label %8
8: ; preds = %4
%9 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %44
10: ; preds = %4
%11 = shl i64 %2, 2
%12 = tail call i64 @malloc(i64 %11)
%13 = inttoptr i64 %12 to i32*
%14 = icmp eq i64 %12, 0
br i1 %14, label %15, label %17
15: ; preds = %10
%16 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %44
17: ; preds = %10
%18 = tail call i32* @fopen(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %23
20: ; preds = %17
%21 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%22 = tail call i32 @free(i32* %13)
br label %44
23: ; preds = %17
%24 = tail call i32 @fread(i32* %13, i64 %2, i32 1, i32* %18)
%25 = icmp eq i32 %24, 1
br i1 %25, label %26, label %29
26: ; preds = %23
%27 = tail call i64 @ferror(i32* %18)
%28 = icmp eq i64 %27, 0
br i1 %28, label %33, label %29
29: ; preds = %26, %23
%30 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
%31 = tail call i32 @free(i32* %13)
%32 = tail call i32 @fclose(i32* %18)
br label %44
33: ; preds = %26
%34 = tail call i32 @write(i32 %0, i32* %13, i64 %2)
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %40
36: ; preds = %33
%37 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0))
%38 = tail call i32 @free(i32* %13)
%39 = tail call i32 @fclose(i32* %18)
br label %44
40: ; preds = %33
%41 = tail call i32 @free(i32* %13)
%42 = tail call i32 @fclose(i32* %18)
%43 = tail call i32 @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.6, i64 0, i64 0), i64 %2, i8* %3, i64 %1)
br label %44
44: ; preds = %40, %36, %29, %20, %15, %8
%45 = phi i32 [ 1, %8 ], [ 1, %15 ], [ 1, %20 ], [ 1, %29 ], [ 1, %36 ], [ 0, %40 ]
ret i32 %45
}