#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*/ uint8_t;

/* Variables and functions */
int O_CREAT;
int O_RDWR;
int O_TRUNC;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *malloc(int);
int /*<<< orphan*/ memset(int /*<<< orphan*/ *, int /*<<< orphan*/, int);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ read_whole_flash(int, int /*<<< orphan*/,
                                    int /*<<< orphan*/ *, int);
int /*<<< orphan*/ write(int, int /*<<< orphan*/ *, int);
int flash_read_to_file(int fd, char *name) {
  uint8_t *buf = NULL;
  int size = 512 * 1024;
  int binfd;
  printf("read whole flash to %s\n", name);
  buf = malloc(size);
  if (buf == NULL) {
    perror("malloc failed");
    return -1;
  }
  memset(buf, 0, size);
  binfd = open(name, O_CREAT | O_TRUNC | O_RDWR);
  if (binfd < 0) {
    perror("open failed.");
    free(buf);
    return -1;
  }
  read_whole_flash(fd, 0, buf, size);
  write(binfd, buf, size);
  close(binfd);
  free(buf);
  return 0;
}


x86 assembly (clang, -O0):
<flash_read_to_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movl   $0x80000,-0x1c(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xe47(%rip),%rdi
call   1050 <printf@plt>
mov    -0x1c(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11ed <flash_read_to_file+0x5d>
lea    0xe3c(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1285 <flash_read_to_file+0xf5>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%edx
xor    %esi,%esi
call   1060 <memset@plt>
mov    -0x10(%rbp),%rdi
mov    0x2df2(%rip),%rax
mov    (%rax),%esi
mov    0x2de1(%rip),%rax
or     (%rax),%esi
mov    0x2dc0(%rip),%rax
or     (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    124d <flash_read_to_file+0xbd>
lea    0xdf3(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1285 <flash_read_to_file+0xf5>
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
xor    %esi,%esi
call   1090 <read_whole_flash@plt>
mov    -0x20(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1040 <write@plt>
mov    -0x20(%rbp),%edi
call   1070 <close@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<flash_read_to_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbp
mov    %edi,%r15d
lea    0xe5c(%rip),%rdi
call   1050 <printf@plt>
mov    $0x80000,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     1234 <flash_read_to_file+0xa4>
mov    %rax,%rbx
xor    %r14d,%r14d
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x80000,%edx
call   1060 <memset@plt>
mov    0x2e24(%rip),%rax
mov    0x2e15(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2df2(%rip),%rax
or     (%rax),%esi
mov    %rbp,%rdi
call   10a0 <open@plt>
test   %eax,%eax
js     1248 <flash_read_to_file+0xb8>
mov    %eax,%ebp
mov    %r15d,%edi
xor    %esi,%esi
mov    %rbx,%rdx
mov    $0x80000,%ecx
call   1090 <read_whole_flash@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x80000,%edx
call   1040 <write@plt>
mov    %ebp,%edi
call   1070 <close@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xddd(%rip),%rdi
call   10b0 <perror@plt>
mov    $0xffffffff,%r14d
jmp    1226 <flash_read_to_file+0x96>
lea    0xdd7(%rip),%rdi
call   10b0 <perror@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    $0xffffffff,%r14d
jmp    1226 <flash_read_to_file+0x96>

LLVM IR (-O0):
@.str = [24 x i8] c"read whole flash to %s\0A\00"
@.str.1 = [14 x i8] c"malloc failed\00"
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@O_RDWR = global i32 0
@.str.2 = [13 x i8] c"open failed.\00"
define i32 @flash_read_to_file(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32* null, i32** %6
store i32 524288, i32* %7
%9 = load i8*, i8** %5
%10 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %9)
%11 = load i32, i32* %7
%12 = call i32* @malloc(i32 %11)
store i32* %12, i32** %6
%13 = load i32*, i32** %6
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %47
17: ; preds = %2
%18 = load i32*, i32** %6
%19 = load i32, i32* %7
%20 = call i32 @memset(i32* %18, i32 0, i32 %19)
%21 = load i8*, i8** %5
%22 = load i32, i32* @O_CREAT
%23 = load i32, i32* @O_TRUNC
%24 = or i32 %22, %23
%25 = load i32, i32* @O_RDWR
%26 = or i32 %24, %25
%27 = call i32 @open(i8* %21, i32 %26)
store i32 %27, i32* %8
%28 = load i32, i32* %8
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %17
%31 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%32 = load i32*, i32** %6
%33 = call i32 @free(i32* %32)
store i32 -1, i32* %3
br label %47
34: ; preds = %17
%35 = load i32, i32* %4
%36 = load i32*, i32** %6
%37 = load i32, i32* %7
%38 = call i32 @read_whole_flash(i32 %35, i32 0, i32* %36, i32 %37)
%39 = load i32, i32* %8
%40 = load i32*, i32** %6
%41 = load i32, i32* %7
%42 = call i32 @write(i32 %39, i32* %40, i32 %41)
%43 = load i32, i32* %8
%44 = call i32 @close(i32 %43)
%45 = load i32*, i32** %6
%46 = call i32 @free(i32* %45)
store i32 0, i32* %3
br label %47
47: ; preds = %34, %30, %15
%48 = load i32, i32* %3
ret i32 %48
}

LLVM IR (-O2):
@.str = [24 x i8] c"read whole flash to %s\0A\00"
@.str.1 = [14 x i8] c"malloc failed\00"
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@O_RDWR = global i32 0
@.str.2 = [13 x i8] c"open failed.\00"
define i32 @flash_read_to_file(i32 %0, i8* %1) {
%3 = tail call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %1)
%4 = tail call i32* @malloc(i32 524288)
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %25
8: ; preds = %2
%9 = tail call i32 @memset(i32* %4, i32 0, i32 524288)
%10 = load i32, i32* @O_CREAT
%11 = load i32, i32* @O_TRUNC
%12 = or i32 %11, %10
%13 = load i32, i32* @O_RDWR
%14 = or i32 %12, %13
%15 = tail call i32 @open(i8* %1, i32 %14)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %8
%18 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%19 = tail call i32 @free(i32* %4)
br label %25
20: ; preds = %8
%21 = tail call i32 @read_whole_flash(i32 %0, i32 0, i32* %4, i32 524288)
%22 = tail call i32 @write(i32 %15, i32* %4, i32 524288)
%23 = tail call i32 @close(i32 %15)
%24 = tail call i32 @free(i32* %4)
br label %25
25: ; preds = %20, %17, %6
%26 = phi i32 [ -1, %6 ], [ -1, %17 ], [ 0, %20 ]
ret i32 %26
}