#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*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(unsigned char *);
scalar_t__ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
int fwrite(unsigned char *, int, int, int /*<<< orphan*/ *);
unsigned char *malloc(int);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
void merge_flash(char *binfile, char *flashfile, int flash_pos) {
  FILE *fbin;
  FILE *fflash;
  unsigned char *tmp_data;
  int file_size = 0;
  int flash_size = 0;
  fbin = fopen(binfile, "r");
  if (fbin == NULL) {
    printf("   Can't open '%s' for reading.\n", binfile);
    return;
  }
  if (fseek(fbin, 0, SEEK_END) != 0) {
  }
  file_size = ftell(fbin);
  if (fseek(fbin, 0, SEEK_SET) != 0) {
  }
  fflash = fopen(flashfile, "r+");
  if (fflash == NULL) {
    printf("   Can't open '%s' for writing.\n", flashfile);
    return;
  }
  if (fseek(fflash, 0, SEEK_END) != 0) {
  }
  flash_size = ftell(fflash);
  rewind(fflash);
  fseek(fflash, flash_pos, SEEK_SET);
  tmp_data = malloc((1 + file_size) * sizeof(char));
  if (file_size <= 0) {
    printf("Not able to get file size %s", binfile);
  }
  int len_read = fread(tmp_data, sizeof(char), file_size, fbin);
  int len_write = fwrite(tmp_data, sizeof(char), file_size, fflash);
  if (len_read != len_write) {
    printf("Not able to merge %s", binfile);
  }
  fclose(fbin);
  if (fseek(fflash, 0x3E8000 * 4, SEEK_SET) != 0) {
  }
  fclose(fflash);
  free(tmp_data);
}


x86 assembly (clang, -O0):
<merge_flash>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x34(%rbp)
movl   $0x0,-0x38(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe34(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11f5 <merge_flash+0x55>
mov    -0x8(%rbp),%rsi
lea    0xe17(%rip),%rdi
call   1060 <printf@plt>
jmp    1386 <merge_flash+0x1e6>
mov    -0x20(%rbp),%rdi
mov    0x2de8(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10a0 <fseek@plt>
cmp    $0x0,%rax
je     1218 <merge_flash+0x78>
jmp    1218 <merge_flash+0x78>
mov    -0x20(%rbp),%rdi
call   1080 <ftell@plt>
mov    %eax,-0x34(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2da9(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10a0 <fseek@plt>
cmp    $0x0,%rax
je     1247 <merge_flash+0xa7>
jmp    1247 <merge_flash+0xa7>
mov    -0x10(%rbp),%rdi
lea    0xdd1(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    127b <merge_flash+0xdb>
mov    -0x10(%rbp),%rsi
lea    0xdb5(%rip),%rdi
call   1060 <printf@plt>
jmp    1386 <merge_flash+0x1e6>
mov    -0x28(%rbp),%rdi
mov    0x2d62(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10a0 <fseek@plt>
cmp    $0x0,%rax
je     129e <merge_flash+0xfe>
jmp    129e <merge_flash+0xfe>
mov    -0x28(%rbp),%rdi
call   1080 <ftell@plt>
mov    %eax,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
call   1070 <rewind@plt>
mov    -0x28(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    0x2d17(%rip),%rax
mov    (%rax),%edx
call   10a0 <fseek@plt>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1090 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpl   $0x0,-0x34(%rbp)
jg     12f9 <merge_flash+0x159>
mov    -0x8(%rbp),%rsi
lea    0xd53(%rip),%rdi
call   1060 <printf@plt>
mov    -0x30(%rbp),%rdi
mov    -0x34(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x34(%rbp),%edx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   10c0 <fwrite@plt>
mov    %eax,-0x40(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0x40(%rbp),%eax
je     1345 <merge_flash+0x1a5>
mov    -0x8(%rbp),%rsi
lea    0xd24(%rip),%rdi
call   1060 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x28(%rbp),%rdi
mov    0x2c7f(%rip),%rax
mov    (%rax),%edx
mov    $0xfa0000,%esi
call   10a0 <fseek@plt>
cmp    $0x0,%rax
je     1374 <merge_flash+0x1d4>
jmp    1374 <merge_flash+0x1d4>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<merge_flash>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %rsi,%rbp
mov    %rdi,%r14
lea    0xe46(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     12e8 <merge_flash+0x148>
mov    %rax,%r15
mov    %ebx,0x4(%rsp)
mov    0x2e12(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    %r15,%rdi
call   1080 <ftell@plt>
mov    %eax,%r12d
mov    0x2de4(%rip),%r13
mov    0x0(%r13),%edx
mov    %r15,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
lea    0xe1a(%rip),%rsi
mov    %rbp,%rdi
call   10b0 <fopen@plt>
test   %rax,%rax
je     12f4 <merge_flash+0x154>
mov    %rax,%rbx
mov    0x2dc4(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    %rbx,%rdi
call   1080 <ftell@plt>
mov    %rbx,%rdi
call   1070 <rewind@plt>
mov    0x0(%r13),%edx
mov    %rbx,%rdi
mov    0x4(%rsp),%esi
call   10a0 <fseek@plt>
lea    0x1(%r12),%edi
call   1090 <malloc@plt>
mov    %rax,%r13
test   %r12d,%r12d
jg     1271 <merge_flash+0xd1>
lea    0xdde(%rip),%rdi
mov    %r14,%rsi
call   1060 <printf@plt>
mov    %r13,%rdi
mov    $0x1,%esi
mov    %r12d,%edx
mov    %r15,%rcx
call   1040 <fread@plt>
mov    %eax,%ebp
mov    %r13,%rdi
mov    $0x1,%esi
mov    %r12d,%edx
mov    %rbx,%rcx
call   10c0 <fwrite@plt>
cmp    %eax,%ebp
je     12ac <merge_flash+0x10c>
lea    0xdc0(%rip),%rdi
mov    %r14,%rsi
call   1060 <printf@plt>
mov    %r15,%rdi
call   1050 <fclose@plt>
mov    0x2d1d(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    $0xfa0000,%esi
call   10a0 <fseek@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %r13,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>
lea    0xd13(%rip),%rdi
mov    %r14,%rsi
jmp    12fe <merge_flash+0x15e>
lea    0xd2b(%rip),%rdi
mov    %rbp,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <printf@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [33 x i8] c" Can't open '%s' for reading.\0A\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [3 x i8] c"r+\00"
@.str.3 = [33 x i8] c" Can't open '%s' for writing.\0A\00"
@.str.4 = [29 x i8] c"Not able to get file size %s\00"
@.str.5 = [21 x i8] c"Not able to merge %s\00"
define void @merge_flash(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 0, i32* %10
store i32 0, i32* %11
%14 = load i8*, i8** %4
%15 = call i32* @fopen(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %15, i32** %7
%16 = load i32*, i32** %7
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %21
18: ; preds = %3
%19 = load i8*, i8** %4
%20 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %19)
br label %97
21: ; preds = %3
%22 = load i32*, i32** %7
%23 = load i32, i32* @SEEK_END
%24 = call i64 @fseek(i32* %22, i32 0, i32 %23)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %21
br label %27
27: ; preds = %26, %21
%28 = load i32*, i32** %7
%29 = call i32 @ftell(i32* %28)
store i32 %29, i32* %10
%30 = load i32*, i32** %7
%31 = load i32, i32* @SEEK_SET
%32 = call i64 @fseek(i32* %30, i32 0, i32 %31)
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %27
br label %35
35: ; preds = %34, %27
%36 = load i8*, i8** %5
%37 = call i32* @fopen(i8* %36, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %37, i32** %8
%38 = load i32*, i32** %8
%39 = icmp eq i32* %38, null
br i1 %39, label %40, label %43
40: ; preds = %35
%41 = load i8*, i8** %5
%42 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i8* %41)
br label %97
43: ; preds = %35
%44 = load i32*, i32** %8
%45 = load i32, i32* @SEEK_END
%46 = call i64 @fseek(i32* %44, i32 0, i32 %45)
%47 = icmp ne i64 %46, 0
br i1 %47, label %48, label %49
48: ; preds = %43
br label %49
49: ; preds = %48, %43
%50 = load i32*, i32** %8
%51 = call i32 @ftell(i32* %50)
store i32 %51, i32* %11
%52 = load i32*, i32** %8
%53 = call i32 @rewind(i32* %52)
%54 = load i32*, i32** %8
%55 = load i32, i32* %6
%56 = load i32, i32* @SEEK_SET
%57 = call i64 @fseek(i32* %54, i32 %55, i32 %56)
%58 = load i32, i32* %10
%59 = add nsw i32 1, %58
%60 = sext i32 %59 to i64
%61 = mul i64 %60, 1
%62 = trunc i64 %61 to i32
%63 = call i8* @malloc(i32 %62)
store i8* %63, i8** %9
%64 = load i32, i32* %10
%65 = icmp sle i32 %64, 0
br i1 %65, label %66, label %69
66: ; preds = %49
%67 = load i8*, i8** %4
%68 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i8* %67)
br label %69
69: ; preds = %66, %49
%70 = load i8*, i8** %9
%71 = load i32, i32* %10
%72 = load i32*, i32** %7
%73 = call i32 @fread(i8* %70, i32 1, i32 %71, i32* %72)
store i32 %73, i32* %12
%74 = load i8*, i8** %9
%75 = load i32, i32* %10
%76 = load i32*, i32** %8
%77 = call i32 @fwrite(i8* %74, i32 1, i32 %75, i32* %76)
store i32 %77, i32* %13
%78 = load i32, i32* %12
%79 = load i32, i32* %13
%80 = icmp ne i32 %78, %79
br i1 %80, label %81, label %84
81: ; preds = %69
%82 = load i8*, i8** %4
%83 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i8* %82)
br label %84
84: ; preds = %81, %69
%85 = load i32*, i32** %7
%86 = call i32 @fclose(i32* %85)
%87 = load i32*, i32** %8
%88 = load i32, i32* @SEEK_SET
%89 = call i64 @fseek(i32* %87, i32 16384000, i32 %88)
%90 = icmp ne i64 %89, 0
br i1 %90, label %91, label %92
91: ; preds = %84
br label %92
92: ; preds = %91, %84
%93 = load i32*, i32** %8
%94 = call i32 @fclose(i32* %93)
%95 = load i8*, i8** %9
%96 = call i32 @free(i8* %95)
br label %97
97: ; preds = %92, %40, %18
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [33 x i8] c" Can't open '%s' for reading.\0A\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [3 x i8] c"r+\00"
@.str.3 = [33 x i8] c" Can't open '%s' for writing.\0A\00"
@.str.4 = [29 x i8] c"Not able to get file size %s\00"
@.str.5 = [21 x i8] c"Not able to merge %s\00"
define void @merge_flash(i8* %0, i8* %1, i32 %2) {
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = tail call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %42
8: ; preds = %3
%9 = load i32, i32* @SEEK_END
%10 = tail call i64 @fseek(i32* %4, i32 0, i32 %9)
%11 = tail call i32 @ftell(i32* %4)
%12 = load i32, i32* @SEEK_SET
%13 = tail call i64 @fseek(i32* %4, i32 0, i32 %12)
%14 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %18
16: ; preds = %8
%17 = tail call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i8* %1)
br label %42
18: ; preds = %8
%19 = load i32, i32* @SEEK_END
%20 = tail call i64 @fseek(i32* %14, i32 0, i32 %19)
%21 = tail call i32 @ftell(i32* %14)
%22 = tail call i32 @rewind(i32* %14)
%23 = load i32, i32* @SEEK_SET
%24 = tail call i64 @fseek(i32* %14, i32 %2, i32 %23)
%25 = add nsw i32 %11, 1
%26 = tail call i8* @malloc(i32 %25)
%27 = icmp slt i32 %11, 1
br i1 %27, label %28, label %30
28: ; preds = %18
%29 = tail call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i8* %0)
br label %30
30: ; preds = %28, %18
%31 = tail call i32 @fread(i8* %26, i32 1, i32 %11, i32* %4)
%32 = tail call i32 @fwrite(i8* %26, i32 1, i32 %11, i32* %14)
%33 = icmp eq i32 %31, %32
br i1 %33, label %36, label %34
34: ; preds = %30
%35 = tail call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i8* %0)
br label %36
36: ; preds = %34, %30
%37 = tail call i32 @fclose(i32* %4)
%38 = load i32, i32* @SEEK_SET
%39 = tail call i64 @fseek(i32* %14, i32 16384000, i32 %38)
%40 = tail call i32 @fclose(i32* %14)
%41 = tail call i32 @free(i8* %26)
br label %42
42: ; preds = %36, %16, %6
ret void
}