#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;
  unsigned char *flash_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);
  if (patch_hash == 1) {
    int j = 0;
    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, 0, SEEK_END) != 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,-0x3c(%rbp)
movl   $0x0,-0x40(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe31(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11f8 <merge_flash+0x58>
mov    -0x8(%rbp),%rsi
lea    0xe14(%rip),%rdi
call   1060 <printf@plt>
jmp    13cc <merge_flash+0x22c>
mov    -0x20(%rbp),%rdi
mov    0x2de5(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10a0 <fseek@plt>
cmp    $0x0,%rax
je     121b <merge_flash+0x7b>
jmp    121b <merge_flash+0x7b>
mov    -0x20(%rbp),%rdi
call   1080 <ftell@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2da6(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10a0 <fseek@plt>
cmp    $0x0,%rax
je     124a <merge_flash+0xaa>
jmp    124a <merge_flash+0xaa>
mov    -0x10(%rbp),%rdi
lea    0xdce(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    127e <merge_flash+0xde>
mov    -0x10(%rbp),%rsi
lea    0xdb2(%rip),%rdi
call   1060 <printf@plt>
jmp    13cc <merge_flash+0x22c>
mov    -0x28(%rbp),%rdi
mov    0x2d5f(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10a0 <fseek@plt>
cmp    $0x0,%rax
je     12a1 <merge_flash+0x101>
jmp    12a1 <merge_flash+0x101>
mov    -0x28(%rbp),%rdi
call   1080 <ftell@plt>
mov    %eax,-0x40(%rbp)
mov    -0x28(%rbp),%rdi
call   1070 <rewind@plt>
mov    -0x28(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    0x2d14(%rip),%rax
mov    (%rax),%edx
call   10a0 <fseek@plt>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1090 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jg     12fc <merge_flash+0x15c>
mov    -0x8(%rbp),%rsi
lea    0xd50(%rip),%rdi
call   1060 <printf@plt>
mov    -0x30(%rbp),%rdi
mov    -0x3c(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x1,-0x18(%rbp)
jne    135a <merge_flash+0x1ba>
movl   $0x0,-0x48(%rbp)
movl   $0x0,-0x48(%rbp)
cmpl   $0x21,-0x48(%rbp)
jge    1355 <merge_flash+0x1b5>
mov    -0x30(%rbp),%rax
mov    -0x3c(%rbp),%ecx
sub    -0x48(%rbp),%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x48(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x48(%rbp)
jmp    132c <merge_flash+0x18c>
jmp    135a <merge_flash+0x1ba>
mov    -0x30(%rbp),%rdi
mov    -0x3c(%rbp),%edx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   10c0 <fwrite@plt>
mov    %eax,-0x4c(%rbp)
mov    -0x44(%rbp),%eax
cmp    -0x4c(%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    0x2c46(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10a0 <fseek@plt>
cmp    $0x0,%rax
je     13ba <merge_flash+0x21a>
jmp    13ba <merge_flash+0x21a>
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     1312 <merge_flash+0x172>
mov    %rax,%r14
mov    %rbp,0x10(%rsp)
mov    %ebx,0x8(%rsp)
mov    %r12d,0xc(%rsp)
mov    0x2e02(%rip),%r13
mov    0x0(%r13),%edx
mov    %rax,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    %r14,%rdi
call   1080 <ftell@plt>
mov    %eax,%r12d
mov    0x2dd2(%rip),%rbx
mov    (%rbx),%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     131e <merge_flash+0x17e>
mov    %rax,%rbp
mov    0x0(%r13),%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    (%rbx),%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    0x2d00(%rip),%rax
mov    (%rax),%edx
mov    %rbp,%rdi
xor    %esi,%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    0xce9(%rip),%rdi
mov    %rbp,%rsi
jmp    1328 <merge_flash+0x188>
lea    0xd01(%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 i8*
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = 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* %13
store i32 0, i32* %14
%18 = load i8*, i8** %5
%19 = call i32* @fopen(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %19, i32** %9
%20 = load i32*, i32** %9
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %25
22: ; preds = %4
%23 = load i8*, i8** %5
%24 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %23)
br label %119
25: ; preds = %4
%26 = load i32*, i32** %9
%27 = load i32, i32* @SEEK_END
%28 = call i64 @fseek(i32* %26, i32 0, i32 %27)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %25
br label %31
31: ; preds = %30, %25
%32 = load i32*, i32** %9
%33 = call i32 @ftell(i32* %32)
store i32 %33, i32* %13
%34 = load i32*, i32** %9
%35 = load i32, i32* @SEEK_SET
%36 = call i64 @fseek(i32* %34, i32 0, i32 %35)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %31
br label %39
39: ; preds = %38, %31
%40 = load i8*, i8** %6
%41 = call i32* @fopen(i8* %40, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %41, i32** %10
%42 = load i32*, i32** %10
%43 = icmp eq i32* %42, null
br i1 %43, label %44, label %47
44: ; preds = %39
%45 = load i8*, i8** %6
%46 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i8* %45)
br label %119
47: ; preds = %39
%48 = load i32*, i32** %10
%49 = load i32, i32* @SEEK_END
%50 = call i64 @fseek(i32* %48, i32 0, i32 %49)
%51 = icmp ne i64 %50, 0
br i1 %51, label %52, label %53
52: ; preds = %47
br label %53
53: ; preds = %52, %47
%54 = load i32*, i32** %10
%55 = call i32 @ftell(i32* %54)
store i32 %55, i32* %14
%56 = load i32*, i32** %10
%57 = call i32 @rewind(i32* %56)
%58 = load i32*, i32** %10
%59 = load i32, i32* %7
%60 = load i32, i32* @SEEK_SET
%61 = call i64 @fseek(i32* %58, i32 %59, i32 %60)
%62 = load i32, i32* %13
%63 = add nsw i32 1, %62
%64 = sext i32 %63 to i64
%65 = mul i64 %64, 1
%66 = trunc i64 %65 to i32
%67 = call i8* @malloc(i32 %66)
store i8* %67, i8** %11
%68 = load i32, i32* %13
%69 = icmp sle i32 %68, 0
br i1 %69, label %70, label %73
70: ; preds = %53
%71 = load i8*, i8** %5
%72 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i8* %71)
br label %73
73: ; preds = %70, %53
%74 = load i8*, i8** %11
%75 = load i32, i32* %13
%76 = load i32*, i32** %9
%77 = call i32 @fread(i8* %74, i32 1, i32 %75, i32* %76)
store i32 %77, i32* %15
%78 = load i32, i32* %8
%79 = icmp eq i32 %78, 1
br i1 %79, label %80, label %95
80: ; preds = %73
store i32 0, i32* %16
store i32 0, i32* %16
br label %81
81: ; preds = %91, %80
%82 = load i32, i32* %16
%83 = icmp slt i32 %82, 33
br i1 %83, label %84, label %94
84: ; preds = %81
%85 = load i8*, i8** %11
%86 = load i32, i32* %13
%87 = load i32, i32* %16
%88 = sub nsw i32 %86, %87
%89 = sext i32 %88 to i64
%90 = getelementptr inbounds i8, i8* %85, i64 %89
store i8 0, i8* %90
br label %91
91: ; preds = %84
%92 = load i32, i32* %16
%93 = add nsw i32 %92, 1
store i32 %93, i32* %16
br label %81
94: ; preds = %81
br label %95
95: ; preds = %94, %73
%96 = load i8*, i8** %11
%97 = load i32, i32* %13
%98 = load i32*, i32** %10
%99 = call i32 @fwrite(i8* %96, i32 1, i32 %97, i32* %98)
store i32 %99, i32* %17
%100 = load i32, i32* %15
%101 = load i32, i32* %17
%102 = icmp ne i32 %100, %101
br i1 %102, label %103, label %106
103: ; preds = %95
%104 = load i8*, i8** %5
%105 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i8* %104)
br label %106
106: ; preds = %103, %95
%107 = load i32*, i32** %9
%108 = call i32 @fclose(i32* %107)
%109 = load i32*, i32** %10
%110 = load i32, i32* @SEEK_END
%111 = call i64 @fseek(i32* %109, i32 0, i32 %110)
%112 = icmp ne i64 %111, 0
br i1 %112, label %113, label %114
113: ; preds = %106
br label %114
114: ; preds = %113, %106
%115 = load i32*, i32** %10
%116 = call i32 @fclose(i32* %115)
%117 = load i8*, i8** %11
%118 = call i32 @free(i8* %117)
br label %119
119: ; preds = %114, %44, %22
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_END
%46 = tail call i64 @fseek(i32* %15, i32 0, 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
}