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

/* Variables and functions */
int ENOMEM;
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *, char *);
int read_flash(int, char *, int, int);
int write(int, char *, int);
int flash_to_file(int fd, int ip, char *fn) {
  char *data;
  int fout, ret = 0, rlen = ip ? (256) : (32 * (512));
  data = (char *)malloc(sizeof(char) * (rlen + 3));
  if (!data)
    return -ENOMEM;
  if ((ret = read_flash(fd, data, rlen, ip)) < 0) {
    ret = -errno;
    goto outm;
  }
  if ((ret = fout = open(fn, O_RDWR | O_CREAT, 0666)) < 0) {
    ret = -errno;
    goto outm;
  }
  ret = write(fout, data + 3, rlen);
  printf("Written file %s \n", fn);
  if (ret < 0)
    ret = -errno;
  else
    ret = 0;
  close(fout);
outm:
  free(data);
  return ret;
}


x86 assembly (clang, -O0):
<flash_to_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0xc(%rbp),%edx
mov    $0x4000,%eax
mov    $0x100,%ecx
cmp    $0x0,%edx
cmovne %ecx,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x3,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11d4 <flash_to_file+0x64>
xor    %eax,%eax
mov    0x2e06(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    12a8 <flash_to_file+0x138>
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x2c(%rbp),%edx
mov    -0xc(%rbp),%ecx
call   1080 <read_flash@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jge    1205 <flash_to_file+0x95>
xor    %eax,%eax
mov    0x2dcd(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x28(%rbp)
jmp    1299 <flash_to_file+0x129>
mov    -0x18(%rbp),%rdi
mov    0x2dd0(%rip),%rax
mov    (%rax),%esi
mov    0x2ddf(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
call   1090 <open@plt>
mov    %eax,-0x24(%rbp)
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jge    1247 <flash_to_file+0xd7>
xor    %eax,%eax
mov    0x2d8b(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x28(%rbp)
jmp    1299 <flash_to_file+0x129>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rsi
add    $0x3,%rsi
mov    -0x2c(%rbp),%edx
call   1040 <write@plt>
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%rsi
lea    0xd98(%rip),%rdi
call   1050 <printf@plt>
cmpl   $0x0,-0x28(%rbp)
jge    128a <flash_to_file+0x11a>
xor    %eax,%eax
mov    0x2d48(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x28(%rbp)
jmp    1291 <flash_to_file+0x121>
movl   $0x0,-0x28(%rbp)
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<flash_to_file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %esi,%ebp
mov    %edi,%r12d
test   %esi,%esi
mov    $0x4000,%eax
mov    $0x100,%ebx
cmove  %eax,%ebx
lea    0x3(%rbx),%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     1211 <flash_to_file+0xa1>
mov    %rax,%r15
mov    %r12d,%edi
mov    %rax,%rsi
mov    %ebx,%edx
mov    %ebp,%ecx
call   1080 <read_flash@plt>
test   %eax,%eax
js     121e <flash_to_file+0xae>
mov    0x2e27(%rip),%rax
mov    0x2e38(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %r14,%rdi
mov    $0x1b6,%edx
call   1090 <open@plt>
test   %eax,%eax
js     121e <flash_to_file+0xae>
mov    %eax,%ebp
mov    %r15,%rsi
add    $0x3,%rsi
mov    %eax,%edi
mov    %ebx,%edx
call   1040 <write@plt>
mov    %eax,%ebx
lea    0xe10(%rip),%rdi
mov    %r14,%rsi
call   1050 <printf@plt>
mov    0x2dc9(%rip),%rax
xor    %ecx,%ecx
sub    (%rax),%ecx
sar    $0x1f,%ebx
and    %ecx,%ebx
mov    %ebp,%edi
call   1060 <close@plt>
jmp    1229 <flash_to_file+0xb9>
mov    0x2db8(%rip),%rax
xor    %ebx,%ebx
sub    (%rax),%ebx
jmp    1231 <flash_to_file+0xc1>
mov    0x2da3(%rip),%rax
xor    %ebx,%ebx
sub    (%rax),%ebx
mov    %r15,%rdi
call   1030 <free@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@ENOMEM = global i32 0
@errno = global i32 0
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str = [18 x i8] c"Written file %s \0A\00"
define i32 @flash_to_file(i32 %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i32 0, i32* %10
%12 = load i32, i32* %6
%13 = icmp ne i32 %12, 0
%14 = zext i1 %13 to i64
%15 = select i1 %13, i32 256, i32 16384
store i32 %15, i32* %11
%16 = load i32, i32* %11
%17 = add nsw i32 %16, 3
%18 = sext i32 %17 to i64
%19 = mul i64 1, %18
%20 = trunc i64 %19 to i32
%21 = call i64 @malloc(i32 %20)
%22 = inttoptr i64 %21 to i8*
store i8* %22, i8** %8
%23 = load i8*, i8** %8
%24 = icmp ne i8* %23, null
br i1 %24, label %28, label %25
25: ; preds = %3
%26 = load i32, i32* @ENOMEM
%27 = sub nsw i32 0, %26
store i32 %27, i32* %4
br label %69
28: ; preds = %3
%29 = load i32, i32* %5
%30 = load i8*, i8** %8
%31 = load i32, i32* %11
%32 = load i32, i32* %6
%33 = call i32 @read_flash(i32 %29, i8* %30, i32 %31, i32 %32)
store i32 %33, i32* %10
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %28
%36 = load i32, i32* @errno
%37 = sub nsw i32 0, %36
store i32 %37, i32* %10
br label %65
38: ; preds = %28
%39 = load i8*, i8** %7
%40 = load i32, i32* @O_RDWR
%41 = load i32, i32* @O_CREAT
%42 = or i32 %40, %41
%43 = call i32 @open(i8* %39, i32 %42, i32 438)
store i32 %43, i32* %9
store i32 %43, i32* %10
%44 = icmp slt i32 %43, 0
br i1 %44, label %45, label %48
45: ; preds = %38
%46 = load i32, i32* @errno
%47 = sub nsw i32 0, %46
store i32 %47, i32* %10
br label %65
48: ; preds = %38
%49 = load i32, i32* %9
%50 = load i8*, i8** %8
%51 = getelementptr inbounds i8, i8* %50, i64 3
%52 = load i32, i32* %11
%53 = call i32 @write(i32 %49, i8* %51, i32 %52)
store i32 %53, i32* %10
%54 = load i8*, i8** %7
%55 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %54)
%56 = load i32, i32* %10
%57 = icmp slt i32 %56, 0
br i1 %57, label %58, label %61
58: ; preds = %48
%59 = load i32, i32* @errno
%60 = sub nsw i32 0, %59
store i32 %60, i32* %10
br label %62
61: ; preds = %48
store i32 0, i32* %10
br label %62
62: ; preds = %61, %58
%63 = load i32, i32* %9
%64 = call i32 @close(i32 %63)
br label %65
65: ; preds = %62, %45, %35
%66 = load i8*, i8** %8
%67 = call i32 @free(i8* %66)
%68 = load i32, i32* %10
store i32 %68, i32* %4
br label %69
69: ; preds = %65, %25
%70 = load i32, i32* %4
ret i32 %70
}

LLVM IR (-O2):
@ENOMEM = global i32 0
@errno = global i32 0
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str = [18 x i8] c"Written file %s \0A\00"
define i32 @flash_to_file(i32 %0, i32 %1, i8* %2) {
%4 = icmp eq i32 %1, 0
%5 = select i1 %4, i32 16384, i32 256
%6 = or i32 %5, 3
%7 = tail call i64 @malloc(i32 %6)
%8 = inttoptr i64 %7 to i8*
%9 = icmp eq i64 %7, 0
br i1 %9, label %10, label %13
10: ; preds = %3
%11 = load i32, i32* @ENOMEM
%12 = sub nsw i32 0, %11
br label %40
13: ; preds = %3
%14 = tail call i32 @read_flash(i32 %0, i8* %8, i32 %5, i32 %1)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = load i32, i32* @errno
%18 = sub nsw i32 0, %17
br label %37
19: ; preds = %13
%20 = load i32, i32* @O_RDWR
%21 = load i32, i32* @O_CREAT
%22 = or i32 %21, %20
%23 = tail call i32 @open(i8* %2, i32 %22, i32 438)
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %19
%26 = load i32, i32* @errno
%27 = sub nsw i32 0, %26
br label %37
28: ; preds = %19
%29 = getelementptr inbounds i8, i8* %8, i64 3
%30 = tail call i32 @write(i32 %23, i8* %29, i32 %5)
%31 = tail call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %2)
%32 = icmp slt i32 %30, 0
%33 = load i32, i32* @errno
%34 = sub nsw i32 0, %33
%35 = select i1 %32, i32 %34, i32 0
%36 = tail call i32 @close(i32 %23)
br label %37
37: ; preds = %28, %25, %16
%38 = phi i32 [ %18, %16 ], [ %27, %25 ], [ %35, %28 ]
%39 = tail call i32 @free(i8* %8)
br label %40
40: ; preds = %37, %10
%41 = phi i32 [ %38, %37 ], [ %12, %10 ]
ret i32 %41
}