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

/* Variables and functions */
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ ihex_hexout(int /*<<< orphan*/ *, int /*<<< orphan*/, int,
                               int);
int /*<<< orphan*/ printf(char *, ...);
void ihex_save_file(char *filename, uint8_t *memory, uint32_t start_addr,
                    uint32_t len) {
  int begin = start_addr;
  int end = begin + len - 1;
  int addr;
  FILE *fhex;
  fhex = fopen(filename, "w");
  if (fhex == NULL) {
    printf("   Can't open '%s' for writing.\n", filename);
    return;
  }
  for (addr = begin; addr <= end; addr++)
    ihex_hexout(fhex, memory[addr], addr, 0);
  ihex_hexout(fhex, 0, 0, 1);
  printf("Memory %04X to %04X written to '%s'\n", begin, end, filename);
}


x86 assembly (clang, -O0):
<ihex_save_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
add    -0x18(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe9d(%rip),%rsi
call   1040 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    118e <ihex_save_file+0x5e>
mov    -0x8(%rbp),%rsi
lea    0xe80(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11f1 <ihex_save_file+0xc1>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x20(%rbp),%eax
jg     11c7 <ihex_save_file+0x97>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,4),%esi
mov    -0x24(%rbp),%edx
xor    %ecx,%ecx
call   1050 <ihex_hexout@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1194 <ihex_save_file+0x64>
mov    -0x30(%rbp),%rdi
xor    %edx,%edx
mov    $0x1,%ecx
mov    %edx,%esi
call   1050 <ihex_hexout@plt>
mov    -0x1c(%rbp),%esi
mov    -0x20(%rbp),%edx
mov    -0x8(%rbp),%rcx
lea    0xe39(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ihex_save_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%r13d
mov    %edx,%r15d
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0xeaf(%rip),%rsi
call   1040 <fopen@plt>
test   %rax,%rax
je     11d3 <ihex_save_file+0xa3>
mov    %rax,%rbp
mov    %rbx,0x10(%rsp)
lea    (%r15,%r13,1),%eax
add    $0xffffffff,%eax
mov    %eax,0xc(%rsp)
test   %r13d,%r13d
jle    119a <ihex_save_file+0x6a>
add    %r15d,%r13d
movslq %r15d,%r12
movslq %r13d,%rbx
nopl   0x0(%rax)
mov    (%r14,%r12,4),%esi
mov    %rbp,%rdi
mov    %r12d,%edx
xor    %ecx,%ecx
call   1050 <ihex_hexout@plt>
add    $0x1,%r12
cmp    %rbx,%r12
jl     1180 <ihex_save_file+0x50>
mov    %rbp,%rdi
xor    %esi,%esi
xor    %edx,%edx
mov    $0x1,%ecx
call   1050 <ihex_hexout@plt>
lea    0xe71(%rip),%rdi
mov    %r15d,%esi
mov    0xc(%rsp),%edx
mov    0x10(%rsp),%rcx
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <printf@plt>
lea    0xe28(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <printf@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@.str.1 = [33 x i8] c" Can't open '%s' for writing.\0A\00"
@.str.2 = [37 x i8] c"Memory %04X to %04X written to '%s'\0A\00"
define void @ihex_save_file(i8* %0, i32* %1, i32 %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32*
store i8* %0, i8** %5
store i32* %1, i32** %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%13 = load i32, i32* %7
store i32 %13, i32* %9
%14 = load i32, i32* %9
%15 = load i32, i32* %8
%16 = add nsw i32 %14, %15
%17 = sub nsw i32 %16, 1
store i32 %17, i32* %10
%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** %12
%20 = load i32*, i32** %12
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %25
22: ; preds = %4
%23 = load i8*, i8** %5
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %23)
br label %50
25: ; preds = %4
%26 = load i32, i32* %9
store i32 %26, i32* %11
br label %27
27: ; preds = %40, %25
%28 = load i32, i32* %11
%29 = load i32, i32* %10
%30 = icmp sle i32 %28, %29
br i1 %30, label %31, label %43
31: ; preds = %27
%32 = load i32*, i32** %12
%33 = load i32*, i32** %6
%34 = load i32, i32* %11
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i32, i32* %33, i64 %35
%37 = load i32, i32* %36
%38 = load i32, i32* %11
%39 = call i32 @ihex_hexout(i32* %32, i32 %37, i32 %38, i32 0)
br label %40
40: ; preds = %31
%41 = load i32, i32* %11
%42 = add nsw i32 %41, 1
store i32 %42, i32* %11
br label %27
43: ; preds = %27
%44 = load i32*, i32** %12
%45 = call i32 @ihex_hexout(i32* %44, i32 0, i32 0, i32 1)
%46 = load i32, i32* %9
%47 = load i32, i32* %10
%48 = load i8*, i8** %5
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i32 %46, i32 %47, i8* %48)
br label %50
50: ; preds = %43, %22
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@.str.1 = [33 x i8] c" Can't open '%s' for writing.\0A\00"
@.str.2 = [37 x i8] c"Memory %04X to %04X written to '%s'\0A\00"
define void @ihex_save_file(i8* %0, i32* nocapture %1, i32 %2, i32 %3) {
%5 = add nsw i32 %3, %2
%6 = add nsw i32 %5, -1
%7 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %14, label %9
9: ; preds = %4
%10 = icmp sgt i32 %3, 0
br i1 %10, label %11, label %24
11: ; preds = %9
%12 = sext i32 %2 to i64
%13 = sext i32 %5 to i64
br label %16
14: ; preds = %4
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %27
16: ; preds = %11, %16
%17 = phi i64 [ %12, %11 ], [ %22, %16 ]
%18 = getelementptr inbounds i32, i32* %1, i64 %17
%19 = load i32, i32* %18
%20 = trunc i64 %17 to i32
%21 = tail call i32 @ihex_hexout(i32* %7, i32 %19, i32 %20, i32 0)
%22 = add nsw i64 %17, 1
%23 = icmp slt i64 %22, %13
br i1 %23, label %16, label %24
24: ; preds = %16, %9
%25 = tail call i32 @ihex_hexout(i32* %7, i32 0, i32 0, i32 1)
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i32 %2, i32 %6, i8* %0)
br label %27
27: ; preds = %24, %14
ret void
}