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

/* Variables and functions */
int fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ stderr;
ssize_t ihex_write(const char *fname, void *image_ptr, size_t bytes) {
  unsigned char *image = image_ptr;
  size_t addr = 0;
  FILE *f;
  if (!(f = fopen(fname, "w"))) {
    fprintf(stderr, "%s: could not open file\n", fname);
    return -1;
  }
  while (addr < bytes) {
    unsigned int sum, i = bytes - addr;
    if (addr >> 16 && !(addr & 0xFFFF)) {
      unsigned segment = addr >> 16 & 0xFFFF;
      fprintf(f, ":02000002%04X%02X\n", segment,
              -(segment & 0xFF) - (segment >> 8 & 0xFF) - 4 & 0xFF);
    }
    if (i > 16)
      i = 16;
    sum = i + (addr & 0xFF) + (addr >> 8 & 0xFF);
    fprintf(f, ":%02X%04X00", i, (unsigned int)addr & 0xFFFF);
    while (i--) {
      unsigned char c = image[addr++];
      sum += c;
      fprintf(f, "%02X", c);
    }
    fprintf(f, "%02X\n", (-sum) & 0xFF);
  }
  fprintf(f, ":00000001FF\n");
  return fclose(f);
}


x86 assembly (clang, -O0):
<ihex_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xea1(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    119a <ihex_write+0x6a>
mov    0x2e67(%rip),%rax
movslq (%rax),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe7b(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1310 <ihex_write+0x1e0>
jmp    119f <ihex_write+0x6f>
mov    -0x30(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    12f2 <ihex_write+0x1c2>
mov    -0x20(%rbp),%rax
sub    -0x30(%rbp),%rax
mov    %eax,-0x40(%rbp)
mov    -0x30(%rbp),%rax
shr    $0x10,%rax
cmp    $0x0,%rax
je     1226 <ihex_write+0xf6>
mov    -0x30(%rbp),%rax
and    $0xffff,%rax
cmp    $0x0,%rax
jne    1226 <ihex_write+0xf6>
mov    -0x30(%rbp),%rax
shr    $0x10,%rax
and    $0xffff,%rax
mov    %eax,-0x44(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x44(%rbp),%edx
mov    -0x44(%rbp),%eax
and    $0xff,%eax
xor    %ecx,%ecx
sub    %eax,%ecx
mov    -0x44(%rbp),%eax
shr    $0x8,%eax
and    $0xff,%eax
sub    %eax,%ecx
sub    $0x4,%ecx
and    $0xff,%ecx
lea    0xdfc(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
cmpl   $0x10,-0x40(%rbp)
jbe    1237 <ihex_write+0x107>
movl   $0x10,-0x40(%rbp)
mov    -0x40(%rbp),%eax
mov    -0x30(%rbp),%rcx
and    $0xff,%rcx
add    %rcx,%rax
mov    -0x30(%rbp),%rcx
shr    $0x8,%rcx
and    $0xff,%rcx
add    %rcx,%rax
mov    %eax,-0x3c(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%edx
mov    -0x30(%rbp),%rax
mov    %eax,%ecx
and    $0xffff,%ecx
lea    0xdb7(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x40(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x40(%rbp)
cmp    $0x0,%eax
je     12d0 <ihex_write+0x1a0>
mov    -0x28(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x30(%rbp)
mov    (%rax,%rcx,1),%al
mov    %al,-0x45(%rbp)
movzbl -0x45(%rbp),%eax
add    -0x3c(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x38(%rbp),%rdi
movzbl -0x45(%rbp),%edx
lea    0xd76(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    127e <ihex_write+0x14e>
mov    -0x38(%rbp),%rdi
xor    %edx,%edx
sub    -0x3c(%rbp),%edx
and    $0xff,%edx
lea    0xd42(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    119f <ihex_write+0x6f>
mov    -0x38(%rbp),%rdi
lea    0xd42(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x38(%rbp),%rdi
call   1030 <fclose@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ihex_write>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%rbp
mov    %rsi,%r15
mov    %rdi,%rbx
lea    0xea5(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     1282 <ihex_write+0x142>
mov    %rax,%r14
test   %rbp,%rbp
je     1253 <ihex_write+0x113>
lea    0xebe(%rip),%r12
xor    %ebx,%ebx
mov    %rbp,(%rsp)
jmp    11b4 <ihex_write+0x74>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
neg    %ebp
movzbl %bpl,%edx
mov    %r14,%rdi
lea    0xe88(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    (%rsp),%rbp
cmp    %rbp,%rbx
jae    1253 <ihex_write+0x113>
mov    %ebp,%r13d
sub    %ebx,%r13d
cmp    $0x10000,%rbx
jb     11f2 <ihex_write+0xb2>
movzwl %bx,%eax
test   %rax,%rax
jne    11f2 <ihex_write+0xb2>
mov    %ebx,%edx
shr    $0x10,%edx
mov    %ebx,%eax
shr    $0x18,%eax
add    %edx,%eax
mov    $0xfc,%ecx
sub    %eax,%ecx
movzbl %cl,%ecx
mov    %r14,%rdi
lea    0xe30(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
cmp    $0x10,%r13d
mov    $0x10,%eax
cmovae %eax,%r13d
movzbl %bl,%eax
movzbl %bh,%ebp
add    %eax,%ebp
add    %r13d,%ebp
movzwl %bx,%ecx
mov    %r14,%rdi
lea    0xe17(%rip),%rsi
mov    %r13d,%edx
xor    %eax,%eax
call   1040 <fprintf@plt>
test   %r13d,%r13d
je     1190 <ihex_write+0x50>
nopw   0x0(%rax,%rax,1)
movzbl (%r15,%rbx,1),%edx
add    $0x1,%rbx
add    %edx,%ebp
mov    %r14,%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
add    $0xffffffff,%r13d
jne    1230 <ihex_write+0xf0>
jmp    1190 <ihex_write+0x50>
lea    0xde5(%rip),%rdi
mov    $0xc,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1060 <fwrite@plt>
mov    %r14,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <fclose@plt>
mov    0x2d57(%rip),%rax
movslq (%rax),%rdi
lea    0xd6f(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.1 = [25 x i8] c"%s: could not open file\0A\00"
@.str.2 = [19 x i8] c":02000002%04X%02X\0A\00"
@.str.3 = [12 x i8] c":%02X%04X00\00"
@.str.4 = [5 x i8] c"%02X\00"
@.str.5 = [6 x i8] c"%02X\0A\00"
@.str.6 = [13 x i8] c":00000001FF\0A\00"
define i32 @ihex_write(i8* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i8
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%15 = load i8*, i8** %6
store i8* %15, i8** %8
store i64 0, i64* %9
%16 = load i8*, i8** %5
%17 = call i32* @fopen(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %17, i32** %10
%18 = icmp ne i32* %17, null
br i1 %18, label %25, label %19
19: ; preds = %3
%20 = load i32, i32* @stderr
%21 = sext i32 %20 to i64
%22 = inttoptr i64 %21 to i32*
%23 = load i8*, i8** %5
%24 = call i32 (i32*, i8*, ...) @fprintf(i32* %22, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %23)
store i32 -1, i32* %4
br label %109
25: ; preds = %3
br label %26
26: ; preds = %98, %25
%27 = load i64, i64* %9
%28 = load i64, i64* %7
%29 = icmp ult i64 %27, %28
br i1 %29, label %30, label %104
30: ; preds = %26
%31 = load i64, i64* %7
%32 = load i64, i64* %9
%33 = sub i64 %31, %32
%34 = trunc i64 %33 to i32
store i32 %34, i32* %12
%35 = load i64, i64* %9
%36 = lshr i64 %35, 16
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %59
38: ; preds = %30
%39 = load i64, i64* %9
%40 = and i64 %39, 65535
%41 = icmp ne i64 %40, 0
br i1 %41, label %59, label %42
42: ; preds = %38
%43 = load i64, i64* %9
%44 = lshr i64 %43, 16
%45 = and i64 %44, 65535
%46 = trunc i64 %45 to i32
store i32 %46, i32* %13
%47 = load i32*, i32** %10
%48 = load i32, i32* %13
%49 = load i32, i32* %13
%50 = and i32 %49, 255
%51 = sub i32 0, %50
%52 = load i32, i32* %13
%53 = lshr i32 %52, 8
%54 = and i32 %53, 255
%55 = sub i32 %51, %54
%56 = sub i32 %55, 4
%57 = and i32 %56, 255
%58 = call i32 (i32*, i8*, ...) @fprintf(i32* %47, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %48, i32 %57)
br label %59
59: ; preds = %42, %38, %30
%60 = load i32, i32* %12
%61 = icmp ugt i32 %60, 16
br i1 %61, label %62, label %63
62: ; preds = %59
store i32 16, i32* %12
br label %63
63: ; preds = %62, %59
%64 = load i32, i32* %12
%65 = zext i32 %64 to i64
%66 = load i64, i64* %9
%67 = and i64 %66, 255
%68 = add i64 %65, %67
%69 = load i64, i64* %9
%70 = lshr i64 %69, 8
%71 = and i64 %70, 255
%72 = add i64 %68, %71
%73 = trunc i64 %72 to i32
store i32 %73, i32* %11
%74 = load i32*, i32** %10
%75 = load i32, i32* %12
%76 = load i64, i64* %9
%77 = trunc i64 %76 to i32
%78 = and i32 %77, 65535
%79 = call i32 (i32*, i8*, ...) @fprintf(i32* %74, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %75, i32 %78)
br label %80
80: ; preds = %84, %63
%81 = load i32, i32* %12
%82 = add i32 %81, -1
store i32 %82, i32* %12
%83 = icmp ne i32 %81, 0
br i1 %83, label %84, label %98
84: ; preds = %80
%85 = load i8*, i8** %8
%86 = load i64, i64* %9
%87 = add i64 %86, 1
store i64 %87, i64* %9
%88 = getelementptr inbounds i8, i8* %85, i64 %86
%89 = load i8, i8* %88
store i8 %89, i8* %14
%90 = load i8, i8* %14
%91 = zext i8 %90 to i32
%92 = load i32, i32* %11
%93 = add i32 %92, %91
store i32 %93, i32* %11
%94 = load i32*, i32** %10
%95 = load i8, i8* %14
%96 = zext i8 %95 to i32
%97 = call i32 (i32*, i8*, ...) @fprintf(i32* %94, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32 %96)
br label %80
98: ; preds = %80
%99 = load i32*, i32** %10
%100 = load i32, i32* %11
%101 = sub i32 0, %100
%102 = and i32 %101, 255
%103 = call i32 (i32*, i8*, ...) @fprintf(i32* %99, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i32 %102)
br label %26
104: ; preds = %26
%105 = load i32*, i32** %10
%106 = call i32 (i32*, i8*, ...) @fprintf(i32* %105, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0))
%107 = load i32*, i32** %10
%108 = call i32 @fclose(i32* %107)
store i32 %108, i32* %4
br label %109
109: ; preds = %104, %19
%110 = load i32, i32* %4
ret i32 %110
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.1 = [25 x i8] c"%s: could not open file\0A\00"
@.str.2 = [19 x i8] c":02000002%04X%02X\0A\00"
@.str.3 = [12 x i8] c":%02X%04X00\00"
@.str.4 = [5 x i8] c"%02X\00"
@.str.5 = [6 x i8] c"%02X\0A\00"
@.str.6 = [13 x i8] c":00000001FF\0A\00"
define i32 @ihex_write(i8* %0, i8* nocapture %1, i64 %2) {
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %8, label %6
6: ; preds = %3
%7 = icmp eq i64 %2, 0
br i1 %7, label %60, label %13
8: ; preds = %3
%9 = load i32, i32* @stderr
%10 = sext i32 %9 to i64
%11 = inttoptr i64 %10 to i32*
%12 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %11, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %63
13: ; preds = %6, %53
%14 = phi i64 [ %55, %53 ], [ 0, %6 ]
%15 = sub i64 %2, %14
%16 = trunc i64 %15 to i32
%17 = icmp ugt i64 %14, 65535
%18 = and i64 %14, 65535
%19 = icmp eq i64 %18, 0
%20 = and i1 %17, %19
%21 = trunc i64 %14 to i32
br i1 %20, label %22, label %29
22: ; preds = %13
%23 = lshr i32 %21, 16
%24 = lshr i32 %21, 24
%25 = add nuw nsw i32 %24, %23
%26 = sub nsw i32 252, %25
%27 = and i32 %26, 255
%28 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %4, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %23, i32 %27)
br label %29
29: ; preds = %13, %22
%30 = icmp ult i32 %16, 16
%31 = select i1 %30, i32 %16, i32 16
%32 = and i64 %14, 255
%33 = lshr i64 %14, 8
%34 = and i64 %33, 255
%35 = add nuw nsw i64 %34, %32
%36 = trunc i64 %35 to i32
%37 = add nuw nsw i32 %31, %36
%38 = and i32 %21, 65535
%39 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %4, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %31, i32 %38)
%40 = icmp eq i32 %31, 0
br i1 %40, label %53, label %41
41: ; preds = %29, %41
%42 = phi i64 [ %46, %41 ], [ %14, %29 ]
%43 = phi i32 [ %45, %41 ], [ %31, %29 ]
%44 = phi i32 [ %50, %41 ], [ %37, %29 ]
%45 = add nsw i32 %43, -1
%46 = add i64 %42, 1
%47 = getelementptr inbounds i8, i8* %1, i64 %42
%48 = load i8, i8* %47
%49 = zext i8 %48 to i32
%50 = add i32 %44, %49
%51 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %4, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32 %49)
%52 = icmp eq i32 %45, 0
br i1 %52, label %53, label %41
53: ; preds = %41, %29
%54 = phi i32 [ %37, %29 ], [ %50, %41 ]
%55 = phi i64 [ %14, %29 ], [ %46, %41 ]
%56 = sub i32 0, %54
%57 = and i32 %56, 255
%58 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %4, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i32 %57)
%59 = icmp ult i64 %55, %2
br i1 %59, label %13, label %60
60: ; preds = %53, %6
%61 = tail call i64 @fwrite(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0), i64 12, i64 1, i32* %4)
%62 = tail call i32 @fclose(i32* %4)
br label %63
63: ; preds = %60, %8
%64 = phi i32 [ %62, %60 ], [ -1, %8 ]
ret i32 %64
}