#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*/ uint8_t;
typedef int /*<<< orphan*/ uint32_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fwrite(int /*<<< orphan*/ *, int, int /*<<< orphan*/,
                          int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, ...);
int /*<<< orphan*/ system(char *);
void generate_hex_file(char *file_name, uint8_t *data, uint32_t length) {
  char bin_file_name[256];
  sprintf(bin_file_name, "%s.bin", file_name);
  char hex_file_name[256];
  sprintf(hex_file_name, "%s.hex", file_name);
  FILE *p_bin_out = fopen(bin_file_name, "wb");
  if (!p_bin_out) {
    printf("Failed to open %s, exiting.\n", bin_file_name);
    exit(1);
  }
  fwrite(data, 1, length, p_bin_out);
  fclose(p_bin_out);
  char tohex_str[256];
  sprintf(tohex_str, "objcopy --change-address %#X -I binary -O ihex %s %s",
          (0x3FC00), bin_file_name, hex_file_name);
  system(tohex_str);
}


x86 assembly (clang, -O0):
<generate_hex_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x330,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
lea    -0x120(%rbp),%rdi
mov    -0x8(%rbp),%rdx
lea    0xe68(%rip),%rsi
mov    $0x0,%al
call   1070 <sprintf@plt>
lea    -0x220(%rbp),%rdi
mov    -0x8(%rbp),%rdx
lea    0xe56(%rip),%rsi
mov    $0x0,%al
call   1070 <sprintf@plt>
lea    -0x120(%rbp),%rdi
lea    0xe48(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x228(%rbp)
cmpq   $0x0,-0x228(%rbp)
jne    11fd <generate_hex_file+0x8d>
lea    -0x120(%rbp),%rsi
lea    0xe23(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
mov    -0x228(%rbp),%rcx
mov    $0x1,%esi
call   1090 <fwrite@plt>
mov    -0x228(%rbp),%rdi
call   1030 <fclose@plt>
lea    -0x330(%rbp),%rdi
lea    -0x120(%rbp),%rcx
lea    -0x220(%rbp),%r8
lea    0xdf1(%rip),%rsi
mov    $0x3fc00,%edx
mov    $0x0,%al
call   1070 <sprintf@plt>
lea    -0x330(%rbp),%rdi
call   1040 <system@plt>
add    $0x330,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<generate_hex_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x308,%rsp
mov    %edx,%r14d
mov    %rsi,%r15
mov    %rdi,%rbp
lea    0xe73(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %rbp,%rdx
xor    %eax,%eax
call   1070 <sprintf@plt>
lea    0xe63(%rip),%rsi
lea    0x100(%rsp),%rdi
mov    %rbp,%rdx
xor    %eax,%eax
call   1070 <sprintf@plt>
lea    0xe51(%rip),%rsi
mov    %rbx,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     1227 <generate_hex_file+0xb7>
mov    %rax,%rbp
mov    %r15,%rdi
mov    $0x1,%esi
mov    %r14d,%edx
mov    %rax,%rcx
call   1090 <fwrite@plt>
mov    %rbp,%rdi
call   1030 <fclose@plt>
lea    0xe3f(%rip),%rsi
lea    0x200(%rsp),%rbx
mov    %rsp,%rcx
lea    0x100(%rsp),%r8
mov    %rbx,%rdi
mov    $0x3fc00,%edx
xor    %eax,%eax
call   1070 <sprintf@plt>
mov    %rbx,%rdi
call   1040 <system@plt>
add    $0x308,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xde3(%rip),%rdi
mov    %rsp,%rsi
call   1050 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"%s.bin\00"
@.str.1 = [7 x i8] c"%s.hex\00"
@.str.2 = [3 x i8] c"wb\00"
@.str.3 = [29 x i8] c"Failed to open %s, exiting.\0A\00"
@.str.4 = [53 x i8] c"objcopy --change-address %#X -I binary -O ihex %s %s\00"
define void @generate_hex_file(i8* %0, i32* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca [256 x i8]
%8 = alloca [256 x i8]
%9 = alloca i32*
%10 = alloca [256 x i8]
store i8* %0, i8** %4
store i32* %1, i32** %5
store i32 %2, i32* %6
%11 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%12 = load i8*, i8** %4
%13 = call i32 (i8*, i8*, ...) @sprintf(i8* %11, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* %12)
%14 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%15 = load i8*, i8** %4
%16 = call i32 (i8*, i8*, ...) @sprintf(i8* %14, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %15)
%17 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%18 = call i32* @fopen(i8* %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %18, i32** %9
%19 = load i32*, i32** %9
%20 = icmp ne i32* %19, null
br i1 %20, label %25, label %21
21: ; preds = %3
%22 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%23 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %22)
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %3
%26 = load i32*, i32** %5
%27 = load i32, i32* %6
%28 = load i32*, i32** %9
%29 = call i32 @fwrite(i32* %26, i32 1, i32 %27, i32* %28)
%30 = load i32*, i32** %9
%31 = call i32 @fclose(i32* %30)
%32 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%33 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%34 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%35 = call i32 (i8*, i8*, ...) @sprintf(i8* %32, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.4, i64 0, i64 0), i32 261120, i8* %33, i8* %34)
%36 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%37 = call i32 @system(i8* %36)
ret void
}

LLVM IR (-O2):
@.str = [7 x i8] c"%s.bin\00"
@.str.1 = [7 x i8] c"%s.hex\00"
@.str.2 = [3 x i8] c"wb\00"
@.str.3 = [29 x i8] c"Failed to open %s, exiting.\0A\00"
@.str.4 = [53 x i8] c"objcopy --change-address %#X -I binary -O ihex %s %s\00"
define void @generate_hex_file(i8* %0, i32* nocapture %1, i32 %2) {
%4 = alloca [256 x i8]
%5 = alloca [256 x i8]
%6 = alloca [256 x i8]
%7 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%8 = call i32 (i8*, i8*, ...) @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* %0)
%9 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%10 = call i32 (i8*, i8*, ...) @sprintf(i8* %9, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%11 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %16
13: ; preds = %3
%14 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %7)
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %3
%17 = tail call i32 @fwrite(i32* %1, i32 1, i32 %2, i32* %11)
%18 = tail call i32 @fclose(i32* %11)
%19 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%20 = call i32 (i8*, i8*, ...) @sprintf(i8* %19, i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @.str.4, i64 0, i64 0), i32 261120, i8* %7, i8* %9)
%21 = call i32 @system(i8* %19)
ret void
}