#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,
                 int patch_hash) {
  FILE *fbin;
  FILE *fflash;
  unsigned char *tmp_data;
  int j = 0;
  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);
  if (patch_hash == 1) {
    for (j = 0; j < 33; j++) {
      tmp_data[file_size - j] = 0;
    }
  }
  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    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
movl   $0x0,-0x34(%rbp)
movl   $0x0,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe2a(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11ff <merge_flash+0x5f>
mov    -0x8(%rbp),%rsi
lea    0xe0d(%rip),%rdi
call   1060 <printf@plt>
jmp    13cf <merge_flash+0x22f>
mov    -0x20(%rbp),%rdi
mov    0x2dde(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10a0 <fseek@plt>
cmp    $0x0,%rax
je     1222 <merge_flash+0x82>
jmp    1222 <merge_flash+0x82>
mov    -0x20(%rbp),%rdi
call   1080 <ftell@plt>
mov    %eax,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2d9f(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10a0 <fseek@plt>
cmp    $0x0,%rax
je     1251 <merge_flash+0xb1>
jmp    1251 <merge_flash+0xb1>
mov    -0x10(%rbp),%rdi
lea    0xdc7(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1285 <merge_flash+0xe5>
mov    -0x10(%rbp),%rsi
lea    0xdab(%rip),%rdi
call   1060 <printf@plt>
jmp    13cf <merge_flash+0x22f>
mov    -0x28(%rbp),%rdi
mov    0x2d58(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10a0 <fseek@plt>
cmp    $0x0,%rax
je     12a8 <merge_flash+0x108>
jmp    12a8 <merge_flash+0x108>
mov    -0x28(%rbp),%rdi
call   1080 <ftell@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x28(%rbp),%rdi
call   1070 <rewind@plt>
mov    -0x28(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    0x2d0d(%rip),%rax
mov    (%rax),%edx
call   10a0 <fseek@plt>
mov    -0x38(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1090 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpl   $0x0,-0x38(%rbp)
jg     1303 <merge_flash+0x163>
mov    -0x8(%rbp),%rsi
lea    0xd49(%rip),%rdi
call   1060 <printf@plt>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0x1,-0x18(%rbp)
jne    135a <merge_flash+0x1ba>
movl   $0x0,-0x34(%rbp)
cmpl   $0x21,-0x34(%rbp)
jge    1355 <merge_flash+0x1b5>
mov    -0x30(%rbp),%rax
mov    -0x38(%rbp),%ecx
sub    -0x34(%rbp),%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    132c <merge_flash+0x18c>
jmp    135a <merge_flash+0x1ba>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%edx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   10c0 <fwrite@plt>
mov    %eax,-0x44(%rbp)
mov    -0x40(%rbp),%eax
cmp    -0x44(%rbp),%eax
je     138e <merge_flash+0x1ee>
mov    -0x8(%rbp),%rsi
lea    0xcdb(%rip),%rdi
call   1060 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x28(%rbp),%rdi
mov    0x2c36(%rip),%rax
mov    (%rax),%edx
mov    $0xfa0000,%esi
call   10a0 <fseek@plt>
cmp    $0x0,%rax
je     13bd <merge_flash+0x21d>
jmp    13bd <merge_flash+0x21d>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<merge_flash>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%r12d
mov    %edx,%ebx
mov    %rsi,%r15
mov    %rdi,%rbp
lea    0xe40(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     1315 <merge_flash+0x175>
mov    %rax,%r14
mov    %rbp,0x10(%rsp)
mov    %ebx,0x8(%rsp)
mov    %r12d,0xc(%rsp)
mov    0x2e02(%rip),%rbx
mov    (%rbx),%edx
mov    %rax,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    %r14,%rdi
call   1080 <ftell@plt>
mov    %eax,%r12d
mov    0x2dd4(%rip),%r13
mov    0x0(%r13),%edx
mov    %r14,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
lea    0xe0a(%rip),%rsi
mov    %r15,%rdi
call   10b0 <fopen@plt>
test   %rax,%rax
je     1321 <merge_flash+0x181>
mov    %rax,%rbp
mov    (%rbx),%edx
mov    %rax,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    %rbp,%rdi
call   1080 <ftell@plt>
mov    %rbp,%rdi
call   1070 <rewind@plt>
mov    0x0(%r13),%edx
mov    %rbp,%rdi
mov    0x8(%rsp),%esi
call   10a0 <fseek@plt>
lea    0x1(%r12),%edi
call   1090 <malloc@plt>
mov    %rax,%r13
test   %r12d,%r12d
jg     127c <merge_flash+0xdc>
lea    0xdd5(%rip),%rdi
mov    0x10(%rsp),%rsi
call   1060 <printf@plt>
mov    %r13,%rdi
mov    $0x1,%esi
mov    %r12d,%edx
mov    %r14,%rcx
call   1040 <fread@plt>
mov    %eax,%r15d
cmpl   $0x1,0xc(%rsp)
jne    12b0 <merge_flash+0x110>
movslq %r12d,%rax
xorps  %xmm0,%xmm0
movups %xmm0,-0x10(%rax,%r13,1)
movups %xmm0,-0x20(%rax,%r13,1)
movb   $0x0,(%rax,%r13,1)
mov    %r13,%rdi
mov    $0x1,%esi
mov    %r12d,%edx
mov    %rbp,%rcx
call   10c0 <fwrite@plt>
cmp    %eax,%r15d
je     12d9 <merge_flash+0x139>
lea    0xd95(%rip),%rdi
mov    0x10(%rsp),%rsi
call   1060 <printf@plt>
mov    %r14,%rdi
call   1050 <fclose@plt>
mov    0x2cf0(%rip),%rax
mov    (%rax),%edx
mov    %rbp,%rdi
mov    $0xfa0000,%esi
call   10a0 <fseek@plt>
mov    %rbp,%rdi
call   1050 <fclose@plt>
mov    %r13,%rdi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>
lea    0xce6(%rip),%rdi
mov    %rbp,%rsi
jmp    132b <merge_flash+0x18b>
lea    0xcfe(%rip),%rdi
mov    %r15,%rsi
add    $0x18,%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, i32 %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca i8*
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 %3, i32* %8
store i32 0, i32* %12
store i32 0, i32* %13
store i32 0, i32* %14
%17 = load i8*, i8** %5
%18 = call i32* @fopen(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %18, i32** %9
%19 = load i32*, i32** %9
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %24
21: ; preds = %4
%22 = load i8*, i8** %5
%23 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %22)
br label %118
24: ; preds = %4
%25 = load i32*, i32** %9
%26 = load i32, i32* @SEEK_END
%27 = call i64 @fseek(i32* %25, i32 0, i32 %26)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %24
br label %30
30: ; preds = %29, %24
%31 = load i32*, i32** %9
%32 = call i32 @ftell(i32* %31)
store i32 %32, i32* %13
%33 = load i32*, i32** %9
%34 = load i32, i32* @SEEK_SET
%35 = call i64 @fseek(i32* %33, i32 0, i32 %34)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %30
br label %38
38: ; preds = %37, %30
%39 = load i8*, i8** %6
%40 = call i32* @fopen(i8* %39, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %40, i32** %10
%41 = load i32*, i32** %10
%42 = icmp eq i32* %41, null
br i1 %42, label %43, label %46
43: ; preds = %38
%44 = load i8*, i8** %6
%45 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i8* %44)
br label %118
46: ; preds = %38
%47 = load i32*, i32** %10
%48 = load i32, i32* @SEEK_END
%49 = call i64 @fseek(i32* %47, i32 0, i32 %48)
%50 = icmp ne i64 %49, 0
br i1 %50, label %51, label %52
51: ; preds = %46
br label %52
52: ; preds = %51, %46
%53 = load i32*, i32** %10
%54 = call i32 @ftell(i32* %53)
store i32 %54, i32* %14
%55 = load i32*, i32** %10
%56 = call i32 @rewind(i32* %55)
%57 = load i32*, i32** %10
%58 = load i32, i32* %7
%59 = load i32, i32* @SEEK_SET
%60 = call i64 @fseek(i32* %57, i32 %58, i32 %59)
%61 = load i32, i32* %13
%62 = add nsw i32 1, %61
%63 = sext i32 %62 to i64
%64 = mul i64 %63, 1
%65 = trunc i64 %64 to i32
%66 = call i8* @malloc(i32 %65)
store i8* %66, i8** %11
%67 = load i32, i32* %13
%68 = icmp sle i32 %67, 0
br i1 %68, label %69, label %72
69: ; preds = %52
%70 = load i8*, i8** %5
%71 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i8* %70)
br label %72
72: ; preds = %69, %52
%73 = load i8*, i8** %11
%74 = load i32, i32* %13
%75 = load i32*, i32** %9
%76 = call i32 @fread(i8* %73, i32 1, i32 %74, i32* %75)
store i32 %76, i32* %15
%77 = load i32, i32* %8
%78 = icmp eq i32 %77, 1
br i1 %78, label %79, label %94
79: ; preds = %72
store i32 0, i32* %12
br label %80
80: ; preds = %90, %79
%81 = load i32, i32* %12
%82 = icmp slt i32 %81, 33
br i1 %82, label %83, label %93
83: ; preds = %80
%84 = load i8*, i8** %11
%85 = load i32, i32* %13
%86 = load i32, i32* %12
%87 = sub nsw i32 %85, %86
%88 = sext i32 %87 to i64
%89 = getelementptr inbounds i8, i8* %84, i64 %88
store i8 0, i8* %89
br label %90
90: ; preds = %83
%91 = load i32, i32* %12
%92 = add nsw i32 %91, 1
store i32 %92, i32* %12
br label %80
93: ; preds = %80
br label %94
94: ; preds = %93, %72
%95 = load i8*, i8** %11
%96 = load i32, i32* %13
%97 = load i32*, i32** %10
%98 = call i32 @fwrite(i8* %95, i32 1, i32 %96, i32* %97)
store i32 %98, i32* %16
%99 = load i32, i32* %15
%100 = load i32, i32* %16
%101 = icmp ne i32 %99, %100
br i1 %101, label %102, label %105
102: ; preds = %94
%103 = load i8*, i8** %5
%104 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i8* %103)
br label %105
105: ; preds = %102, %94
%106 = load i32*, i32** %9
%107 = call i32 @fclose(i32* %106)
%108 = load i32*, i32** %10
%109 = load i32, i32* @SEEK_SET
%110 = call i64 @fseek(i32* %108, i32 16384000, i32 %109)
%111 = icmp ne i64 %110, 0
br i1 %111, label %112, label %113
112: ; preds = %105
br label %113
113: ; preds = %112, %105
%114 = load i32*, i32** %10
%115 = call i32 @fclose(i32* %114)
%116 = load i8*, i8** %11
%117 = call i32 @free(i8* %116)
br label %118
118: ; preds = %113, %43, %21
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, i32 %3) {
%5 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %9
7: ; preds = %4
%8 = tail call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %49
9: ; preds = %4
%10 = load i32, i32* @SEEK_END
%11 = tail call i64 @fseek(i32* %5, i32 0, i32 %10)
%12 = tail call i32 @ftell(i32* %5)
%13 = load i32, i32* @SEEK_SET
%14 = tail call i64 @fseek(i32* %5, i32 0, i32 %13)
%15 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %19
17: ; preds = %9
%18 = tail call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i8* %1)
br label %49
19: ; preds = %9
%20 = load i32, i32* @SEEK_END
%21 = tail call i64 @fseek(i32* %15, i32 0, i32 %20)
%22 = tail call i32 @ftell(i32* %15)
%23 = tail call i32 @rewind(i32* %15)
%24 = load i32, i32* @SEEK_SET
%25 = tail call i64 @fseek(i32* %15, i32 %2, i32 %24)
%26 = add nsw i32 %12, 1
%27 = tail call i8* @malloc(i32 %26)
%28 = icmp slt i32 %12, 1
br i1 %28, label %29, label %31
29: ; preds = %19
%30 = tail call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i8* %0)
br label %31
31: ; preds = %29, %19
%32 = tail call i32 @fread(i8* %27, i32 1, i32 %12, i32* %5)
%33 = icmp eq i32 %3, 1
br i1 %33, label %34, label %38
34: ; preds = %31
%35 = sext i32 %12 to i64
%36 = add nsw i64 %35, -32
%37 = getelementptr i8, i8* %27, i64 %36
br label %38
38: ; preds = %34, %31
%39 = tail call i32 @fwrite(i8* %27, i32 1, i32 %12, i32* %15)
%40 = icmp eq i32 %32, %39
br i1 %40, label %43, label %41
41: ; preds = %38
%42 = tail call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i8* %0)
br label %43
43: ; preds = %41, %38
%44 = tail call i32 @fclose(i32* %5)
%45 = load i32, i32* @SEEK_SET
%46 = tail call i64 @fseek(i32* %15, i32 16384000, i32 %45)
%47 = tail call i32 @fclose(i32* %15)
%48 = tail call i32 @free(i8* %27)
br label %49
49: ; preds = %43, %17, %7
ret void
}