#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 *);
scalar_t__ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
int fwrite(unsigned char const *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
void merge_rom(const unsigned char *data, char *flashfile, int flash_pos,
               int size) {
  FILE *fflash;
  int j = 0;
  int flash_size = 0;
  fflash = fopen(flashfile, "rb+");
  if (fflash == NULL) {
    printf("   Can't open '%s' for writing.\n", flashfile);
    return;
  }
  if (fseek(fflash, 0, SEEK_END) != 0) {
    printf("   Can't seek end of '%s'.\n", flashfile);
  }
  flash_size = ftell(fflash);
  rewind(fflash);
  fseek(fflash, flash_pos, SEEK_SET);
  int len_write = fwrite(data, sizeof(char), size, fflash);
  fclose(fflash);
}


x86 assembly (clang, -O0):
<merge_rom>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe61(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11c8 <merge_rom+0x58>
mov    -0x10(%rbp),%rsi
lea    0xe46(%rip),%rdi
call   1040 <printf@plt>
jmp    1241 <merge_rom+0xd1>
mov    -0x20(%rbp),%rdi
mov    0x2e15(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
cmp    $0x0,%rax
je     11f6 <merge_rom+0x86>
mov    -0x10(%rbp),%rsi
lea    0xe34(%rip),%rdi
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1060 <ftell@plt>
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <rewind@plt>
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    0x2dbf(%rip),%rax
mov    (%rax),%edx
call   1070 <fseek@plt>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1090 <fwrite@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<merge_rom>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %ecx,%r14d
mov    %edx,%r12d
mov    %rsi,%rbx
mov    %rdi,%r15
lea    0xe75(%rip),%rsi
mov    %rbx,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     1209 <merge_rom+0x99>
mov    %rax,%rbp
mov    0x2e46(%rip),%rax
mov    (%rax),%edx
mov    %rbp,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
test   %rax,%rax
je     11c2 <merge_rom+0x52>
lea    0xe6b(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
mov    %rbp,%rdi
call   1060 <ftell@plt>
mov    %rbp,%rdi
call   1050 <rewind@plt>
mov    0x2dff(%rip),%rax
mov    (%rax),%edx
mov    %rbp,%rdi
mov    %r12d,%esi
call   1070 <fseek@plt>
mov    %r15,%rdi
mov    $0x1,%esi
mov    %r14d,%edx
mov    %rbp,%rcx
call   1090 <fwrite@plt>
mov    %rbp,%rdi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <fclose@plt>
lea    0xdf4(%rip),%rdi
mov    %rbx,%rsi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <printf@plt>

LLVM IR (-O0):
@.str = [4 x i8] c"rb+\00"
@.str.1 = [33 x i8] c" Can't open '%s' for writing.\0A\00"
@SEEK_END = global i32 0
@.str.2 = [28 x i8] c" Can't seek end of '%s'.\0A\00"
@SEEK_SET = global i32 0
define void @merge_rom(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 i32
%12 = 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* %10
store i32 0, i32* %11
%13 = load i8*, i8** %6
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i32* %14, i32** %9
%15 = load i32*, i32** %9
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %20
17: ; preds = %4
%18 = load i8*, i8** %6
%19 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %18)
br label %43
20: ; preds = %4
%21 = load i32*, i32** %9
%22 = load i32, i32* @SEEK_END
%23 = call i64 @fseek(i32* %21, i32 0, i32 %22)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %20
%26 = load i8*, i8** %6
%27 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %26)
br label %28
28: ; preds = %25, %20
%29 = load i32*, i32** %9
%30 = call i32 @ftell(i32* %29)
store i32 %30, i32* %11
%31 = load i32*, i32** %9
%32 = call i32 @rewind(i32* %31)
%33 = load i32*, i32** %9
%34 = load i32, i32* %7
%35 = load i32, i32* @SEEK_SET
%36 = call i64 @fseek(i32* %33, i32 %34, i32 %35)
%37 = load i8*, i8** %5
%38 = load i32, i32* %8
%39 = load i32*, i32** %9
%40 = call i32 @fwrite(i8* %37, i32 1, i32 %38, i32* %39)
store i32 %40, i32* %12
%41 = load i32*, i32** %9
%42 = call i32 @fclose(i32* %41)
br label %43
43: ; preds = %28, %17
ret void
}

LLVM IR (-O2):
@.str = [4 x i8] c"rb+\00"
@.str.1 = [33 x i8] c" Can't open '%s' for writing.\0A\00"
@SEEK_END = global i32 0
@.str.2 = [28 x i8] c" Can't seek end of '%s'.\0A\00"
@SEEK_SET = global i32 0
define void @merge_rom(i8* nocapture %0, i8* %1, i32 %2, i32 %3) {
%5 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([4 x i8], [4 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* %1)
br label %22
9: ; preds = %4
%10 = load i32, i32* @SEEK_END
%11 = tail call i64 @fseek(i32* %5, i32 0, i32 %10)
%12 = icmp eq i64 %11, 0
br i1 %12, label %15, label %13
13: ; preds = %9
%14 = tail call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %1)
br label %15
15: ; preds = %13, %9
%16 = tail call i32 @ftell(i32* %5)
%17 = tail call i32 @rewind(i32* %5)
%18 = load i32, i32* @SEEK_SET
%19 = tail call i64 @fseek(i32* %5, i32 %2, i32 %18)
%20 = tail call i32 @fwrite(i8* %0, i32 1, i32 %3, i32* %5)
%21 = tail call i32 @fclose(i32* %5)
br label %22
22: ; preds = %15, %7
ret void
}