#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;

/* Variables and functions */
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int creat(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, int,
                           size_t);
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
scalar_t__ lseek(int, scalar_t__, int /*<<< orphan*/);
scalar_t__ malloc(size_t);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, size_t, scalar_t__, char const *);
int read(int, int /*<<< orphan*/ *, size_t);
int /*<<< orphan*/ stderr;
int write(int, int /*<<< orphan*/ *, size_t);
int flash_to_file(int fd, u_int32_t offset, size_t len, const char *filename) {
  u_int8_t *buf;
  int outfd, err;
  if (offset != lseek(fd, offset, SEEK_SET)) {
    perror("lseek()");
    goto err0;
  }
  if ((buf = (u_int8_t *)malloc(len * sizeof(u_int8_t))) == NULL) {
    perror("malloc()");
    goto err0;
  }
  outfd = creat(filename, O_WRONLY);
  if (outfd < 0) {
    perror("creat()");
    goto err1;
  }
  err = read(fd, buf, len);
  if (err < 0) {
    perror("read()");
    goto err2;
  }
  err = write(outfd, buf, len);
  if (err < 0) {
    perror("write()");
    goto err2;
  }
  if (err != len) {
    fprintf(stderr, "Couldn't copy entire buffer to %s. (%d/%d bytes copied)\n",
            filename, err, len);
    goto err2;
  }
  free(buf);
  close(outfd);
  printf("Copied %d bytes from address 0x%.8x in flash to %s\n", len, offset,
         filename);
  return (0);
err2:
  close(outfd);
err1:
  free(buf);
err0:
  return (1);
}


x86 assembly (clang, -O0):
<flash_to_file>:
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,-0x38(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2e0b(%rip),%rax
mov    (%rax),%edx
call   1060 <lseek@plt>
mov    %rax,%rcx
mov    -0x38(%rbp),%rax
cmp    %rcx,%rax
je     11f5 <flash_to_file+0x55>
lea    0xe15(%rip),%rdi
call   10b0 <perror@plt>
jmp    1326 <flash_to_file+0x186>
mov    -0x18(%rbp),%rdi
shl    $0x2,%rdi
call   10a0 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    1221 <flash_to_file+0x81>
lea    0xdf1(%rip),%rdi
call   10b0 <perror@plt>
jmp    1326 <flash_to_file+0x186>
mov    -0x20(%rbp),%rdi
mov    0x2d9c(%rip),%rax
mov    (%rax),%esi
call   10c0 <creat@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    1251 <flash_to_file+0xb1>
lea    0xdca(%rip),%rdi
call   10b0 <perror@plt>
jmp    131d <flash_to_file+0x17d>
mov    -0x8(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1080 <read@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jge    127f <flash_to_file+0xdf>
lea    0xda4(%rip),%rdi
call   10b0 <perror@plt>
jmp    1315 <flash_to_file+0x175>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <write@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jge    12ad <flash_to_file+0x10d>
lea    0xd7d(%rip),%rdi
call   10b0 <perror@plt>
jmp    1315 <flash_to_file+0x175>
movslq -0x30(%rbp),%rax
cmp    -0x18(%rbp),%rax
je     12e0 <flash_to_file+0x140>
mov    0x2d1e(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
mov    -0x30(%rbp),%ecx
mov    -0x18(%rbp),%r8
lea    0xd52(%rip),%rsi
call   1090 <fprintf@plt>
jmp    1315 <flash_to_file+0x175>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x2c(%rbp),%edi
call   1070 <close@plt>
mov    -0x18(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    -0x20(%rbp),%rcx
lea    0xd5d(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    132d <flash_to_file+0x18d>
mov    -0x2c(%rbp),%edi
call   1070 <close@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<flash_to_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%rbx
mov    %rsi,%r12
mov    %edi,%ebp
mov    0x2e1b(%rip),%rax
mov    (%rax),%edx
call   1060 <lseek@plt>
cmp    %r12,%rax
jne    129e <flash_to_file+0xfe>
lea    0x0(,%rbx,4),%rdi
call   10a0 <malloc@plt>
test   %rax,%rax
je     12ac <flash_to_file+0x10c>
mov    %rax,%r15
mov    0x2ddb(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   10c0 <creat@plt>
test   %eax,%eax
js     12ba <flash_to_file+0x11a>
mov    %eax,%r13d
mov    %ebp,%edi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1080 <read@plt>
test   %eax,%eax
js     12c8 <flash_to_file+0x128>
mov    %r13d,%edi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1040 <write@plt>
test   %eax,%eax
js     12d6 <flash_to_file+0x136>
mov    %eax,%ecx
cmp    %rbx,%rcx
jne    125d <flash_to_file+0xbd>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %r13d,%edi
call   1070 <close@plt>
lea    0xe16(%rip),%rdi
mov    %rbx,%rsi
mov    %r12,%rdx
mov    %r14,%rcx
call   1050 <printf@plt>
xor    %eax,%eax
jmp    128f <flash_to_file+0xef>
mov    0x2d7c(%rip),%rcx
mov    (%rcx),%edi
lea    0xdbb(%rip),%rsi
mov    %r14,%rdx
mov    %eax,%ecx
mov    %rbx,%r8
call   1090 <fprintf@plt>
mov    %r13d,%edi
call   1070 <close@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd5b(%rip),%rdi
call   10b0 <perror@plt>
jmp    128a <flash_to_file+0xea>
lea    0xd55(%rip),%rdi
call   10b0 <perror@plt>
jmp    128a <flash_to_file+0xea>
lea    0xd50(%rip),%rdi
call   10b0 <perror@plt>
jmp    1282 <flash_to_file+0xe2>
lea    0xd4a(%rip),%rdi
call   10b0 <perror@plt>
jmp    127a <flash_to_file+0xda>
lea    0xd43(%rip),%rdi
call   10b0 <perror@plt>
jmp    127a <flash_to_file+0xda>

LLVM IR (-O0):
@SEEK_SET = global i32 0
@.str = [8 x i8] c"lseek()\00"
@.str.1 = [9 x i8] c"malloc()\00"
@O_WRONLY = global i32 0
@.str.2 = [8 x i8] c"creat()\00"
@.str.3 = [7 x i8] c"read()\00"
@.str.4 = [8 x i8] c"write()\00"
@stderr = global i32 0
@.str.5 = [57 x i8] c"Couldn't copy entire buffer to %s. (%d/%d bytes copied)\0A\00"
@.str.6 = [52 x i8] c"Copied %d bytes from address 0x%.8x in flash to %s\0A\00"
define i32 @flash_to_file(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))
br label %81
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))
br label %81
29: ; preds = %21
%30 = load i8*, i8** %9
%31 = load i32, i32* @O_WRONLY
%32 = call i32 @creat(i8* %30, i32 %31)
store i32 %32, i32* %11
%33 = load i32, i32* %11
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %29
%36 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %78
37: ; preds = %29
%38 = load i32, i32* %6
%39 = load i32*, i32** %10
%40 = load i64, i64* %8
%41 = call i32 @read(i32 %38, i32* %39, i64 %40)
store i32 %41, i32* %12
%42 = load i32, i32* %12
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %37
%45 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %75
46: ; preds = %37
%47 = load i32, i32* %11
%48 = load i32*, i32** %10
%49 = load i64, i64* %8
%50 = call i32 @write(i32 %47, i32* %48, i64 %49)
store i32 %50, i32* %12
%51 = load i32, i32* %12
%52 = icmp slt i32 %51, 0
br i1 %52, label %53, label %55
53: ; preds = %46
%54 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
br label %75
55: ; preds = %46
%56 = load i32, i32* %12
%57 = sext i32 %56 to i64
%58 = load i64, i64* %8
%59 = icmp ne i64 %57, %58
br i1 %59, label %60, label %66
60: ; preds = %55
%61 = load i32, i32* @stderr
%62 = load i8*, i8** %9
%63 = load i32, i32* %12
%64 = load i64, i64* %8
%65 = call i32 @fprintf(i32 %61, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.5, i64 0, i64 0), i8* %62, i32 %63, i64 %64)
br label %75
66: ; preds = %55
%67 = load i32*, i32** %10
%68 = call i32 @free(i32* %67)
%69 = load i32, i32* %11
%70 = call i32 @close(i32 %69)
%71 = load i64, i64* %8
%72 = load i64, i64* %7
%73 = load i8*, i8** %9
%74 = call i32 @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.6, i64 0, i64 0), i64 %71, i64 %72, i8* %73)
store i32 0, i32* %5
br label %82
75: ; preds = %60, %53, %44
%76 = load i32, i32* %11
%77 = call i32 @close(i32 %76)
br label %78
78: ; preds = %75, %35
%79 = load i32*, i32** %10
%80 = call i32 @free(i32* %79)
br label %81
81: ; preds = %78, %27, %19
store i32 1, i32* %5
br label %82
82: ; preds = %81, %66
%83 = load i32, i32* %5
ret i32 %83
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
@.str = [8 x i8] c"lseek()\00"
@.str.1 = [9 x i8] c"malloc()\00"
@O_WRONLY = global i32 0
@.str.2 = [8 x i8] c"creat()\00"
@.str.3 = [7 x i8] c"read()\00"
@.str.4 = [8 x i8] c"write()\00"
@stderr = global i32 0
@.str.5 = [57 x i8] c"Couldn't copy entire buffer to %s. (%d/%d bytes copied)\0A\00"
@.str.6 = [52 x i8] c"Copied %d bytes from address 0x%.8x in flash to %s\0A\00"
define i32 @flash_to_file(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 %47
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 %47
17: ; preds = %10
%18 = load i32, i32* @O_WRONLY
%19 = tail call i32 @creat(i8* %3, i32 %18)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %45
23: ; preds = %17
%24 = tail call i32 @read(i32 %0, i32* %13, i64 %2)
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %23
%27 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %43
28: ; preds = %23
%29 = tail call i32 @write(i32 %19, i32* %13, i64 %2)
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %28
%32 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
br label %43
33: ; preds = %28
%34 = zext i32 %29 to i64
%35 = icmp eq i64 %34, %2
br i1 %35, label %39, label %36
36: ; preds = %33
%37 = load i32, i32* @stderr
%38 = tail call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.5, i64 0, i64 0), i8* %3, i32 %29, i64 %2)
br label %43
39: ; preds = %33
%40 = tail call i32 @free(i32* %13)
%41 = tail call i32 @close(i32 %19)
%42 = tail call i32 @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.6, i64 0, i64 0), i64 %2, i64 %1, i8* %3)
br label %47
43: ; preds = %36, %31, %26
%44 = tail call i32 @close(i32 %19)
br label %45
45: ; preds = %43, %21
%46 = tail call i32 @free(i32* %13)
br label %47
47: ; preds = %8, %15, %45, %39
%48 = phi i32 [ 0, %39 ], [ 1, %45 ], [ 1, %15 ], [ 1, %8 ]
ret i32 %48
}