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

/* Variables and functions */
int checksum(unsigned char *, unsigned int, unsigned int, unsigned int, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
void ihex_write(FILE *pFile, unsigned char *buf, unsigned int start,
                unsigned int end) {
  unsigned int chunk_len, chunk_start, i;
  int cur_ela = 0;
  if (end > 65535) {
    cur_ela = -1;
  }
  chunk_start = start;
  while (chunk_start < end) {
    chunk_len = end - chunk_start;
    if (chunk_len > 32) {
      chunk_len = 32;
    }
    if (((chunk_start & 0xffff) + chunk_len) > 0xffff) {
      chunk_len = 0x10000 - (chunk_start & 0xffff);
    }
    if (cur_ela != (chunk_start >> 16)) {
      unsigned char ela_bytes[2];
      cur_ela = chunk_start >> 16;
      ela_bytes[0] = (((cur_ela)&0xff00) >> 8);
      ela_bytes[1] = ((cur_ela)&0xff);
      fprintf(pFile, ":02000004%04X%02X\n", cur_ela,
              checksum(ela_bytes, 2, 2, 0, 4));
    }
    fprintf(pFile, ":%02X%04X00", chunk_len, chunk_start);
    for (i = chunk_start - start; i < (chunk_start + chunk_len - start); i++) {
      fprintf(pFile, "%02X", buf[i]);
    }
    fprintf(pFile, "%02X\n",
            checksum(&buf[chunk_start - start], chunk_len, chunk_len,
                     chunk_start, 0));
    chunk_start += chunk_len;
  }
  fprintf(pFile, ":00000001FF\n");
}


x86 assembly (clang, -O0):
<ihex_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
movl   $0x0,-0x28(%rbp)
cmpl   $0xffff,-0x18(%rbp)
jbe    1151 <ihex_write+0x31>
movl   $0xffffffff,-0x28(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x18(%rbp),%eax
jae    12c0 <ihex_write+0x1a0>
mov    -0x18(%rbp),%eax
sub    -0x20(%rbp),%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x20,-0x1c(%rbp)
jbe    117d <ihex_write+0x5d>
movl   $0x20,-0x1c(%rbp)
mov    -0x20(%rbp),%eax
and    $0xffff,%eax
add    -0x1c(%rbp),%eax
cmp    $0xffff,%eax
jbe    11a6 <ihex_write+0x86>
mov    -0x20(%rbp),%ecx
and    $0xffff,%ecx
mov    $0x10000,%eax
sub    %ecx,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x28(%rbp),%eax
mov    -0x20(%rbp),%ecx
shr    $0x10,%ecx
cmp    %ecx,%eax
je     1216 <ihex_write+0xf6>
mov    -0x20(%rbp),%eax
shr    $0x10,%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
and    $0xff00,%eax
sar    $0x8,%eax
mov    %al,-0x2a(%rbp)
mov    -0x28(%rbp),%eax
and    $0xff,%eax
mov    %al,-0x29(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%eax
mov    %eax,-0x30(%rbp)
lea    -0x2a(%rbp),%rdi
mov    $0x2,%edx
xor    %ecx,%ecx
mov    $0x4,%r8d
mov    %edx,%esi
call   1030 <checksum@plt>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%edx
mov    %eax,%ecx
lea    0xdf1(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x8(%rbp),%rdi
mov    -0x1c(%rbp),%edx
mov    -0x20(%rbp),%ecx
lea    0xdec(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x20(%rbp),%eax
sub    -0x14(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    -0x20(%rbp),%ecx
add    -0x1c(%rbp),%ecx
sub    -0x14(%rbp),%ecx
cmp    %ecx,%eax
jae    1276 <ihex_write+0x156>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    -0x24(%rbp),%ecx
movzbl (%rax,%rcx,1),%edx
lea    0xdbe(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1237 <ihex_write+0x117>
mov    -0x8(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%eax
sub    -0x14(%rbp),%eax
mov    %eax,%eax
add    %rax,%rdi
mov    -0x1c(%rbp),%esi
mov    -0x1c(%rbp),%edx
mov    -0x20(%rbp),%ecx
xor    %r8d,%r8d
call   1030 <checksum@plt>
mov    -0x40(%rbp),%rdi
mov    %eax,%edx
lea    0xd62(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x1c(%rbp),%eax
add    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    1157 <ihex_write+0x37>
mov    -0x8(%rbp),%rdi
lea    0xd59(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ihex_write>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rsi,0x20(%rsp)
mov    %rdi,%rbx
mov    %edx,0x10(%rsp)
cmp    %ecx,%edx
jae    12b1 <ihex_write+0x181>
cmp    $0x10000,%ecx
mov    $0x0,%eax
adc    $0xffffffff,%eax
mov    %eax,0x14(%rsp)
mov    0x10(%rsp),%eax
mov    %eax,%ebp
mov    %ecx,0x18(%rsp)
jmp    11b1 <ihex_write+0x81>
add    0x20(%rsp),%r12
mov    %r12,%rdi
mov    0x28(%rsp),%rdx
mov    %edx,%esi
mov    0x30(%rsp),%rcx
xor    %r8d,%r8d
call   1030 <checksum@plt>
mov    %rbx,%rdi
lea    0xe77(%rip),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    0x1c(%rsp),%eax
mov    %eax,%ebp
mov    0x18(%rsp),%ecx
cmp    %ecx,%eax
jae    12b1 <ihex_write+0x181>
mov    %ecx,%eax
sub    %ebp,%eax
cmp    $0x20,%eax
mov    $0x20,%ecx
cmovae %ecx,%eax
movzwl %bp,%ecx
lea    (%rax,%rcx,1),%edx
mov    $0x10000,%r15d
sub    %ecx,%r15d
cmp    $0x10000,%edx
cmovb  %eax,%r15d
mov    %ebp,%r14d
shr    $0x10,%r14d
cmp    %r14d,0x14(%rsp)
je     122c <ihex_write+0xfc>
mov    %ebp,%eax
shr    $0x18,%eax
mov    %al,0xe(%rsp)
mov    %r14b,0xf(%rsp)
lea    0xe(%rsp),%rdi
mov    $0x2,%esi
mov    $0x2,%edx
xor    %ecx,%ecx
mov    $0x4,%r8d
call   1030 <checksum@plt>
mov    %rbx,%rdi
lea    0xde5(%rip),%rsi
mov    %r14d,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    %r14d,0x14(%rsp)
mov    %rbx,%rdi
lea    0xddd(%rip),%rsi
mov    %r15d,%edx
mov    %ebp,%ecx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    %ebp,%r12d
mov    0x10(%rsp),%ecx
sub    %ecx,%r12d
mov    %rbp,0x30(%rsp)
mov    %r15,0x28(%rsp)
lea    (%r15,%rbp,1),%eax
mov    %eax,0x1c(%rsp)
sub    %ecx,%eax
cmp    %eax,%r12d
lea    0xdb5(%rip),%r15
jae    1170 <ihex_write+0x40>
mov    0x20(%rsp),%rax
lea    (%rax,%r12,1),%rbp
mov    0x28(%rsp),%r13d
xor    %r14d,%r14d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movzbl 0x0(%rbp,%r14,1),%edx
mov    %rbx,%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
add    $0x1,%r14
cmp    %r14d,%r13d
jne    1290 <ihex_write+0x160>
jmp    1170 <ihex_write+0x40>
lea    0xd6c(%rip),%rdi
mov    $0xc,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1050 <fwrite@plt>
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [19 x i8] c":02000004%04X%02X\0A\00"
@.str.1 = [12 x i8] c":%02X%04X00\00"
@.str.2 = [5 x i8] c"%02X\00"
@.str.3 = [6 x i8] c"%02X\0A\00"
@.str.4 = [13 x i8] c":00000001FF\0A\00"
define void @ihex_write(i32* nocapture %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca [2 x i8]
%6 = icmp ult i32 %2, %3
br i1 %6, label %7, label %53
7: ; preds = %4
%8 = icmp ugt i32 %3, 65535
%9 = sext i1 %8 to i32
%10 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 0
%11 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 1
br label %12
12: ; preds = %7, %48
%13 = phi i32 [ %9, %7 ], [ %32, %48 ]
%14 = phi i32 [ %2, %7 ], [ %35, %48 ]
%15 = sub i32 %3, %14
%16 = icmp ult i32 %15, 32
%17 = select i1 %16, i32 %15, i32 32
%18 = and i32 %14, 65535
%19 = add nuw nsw i32 %17, %18
%20 = icmp ugt i32 %19, 65535
%21 = sub nuw nsw i32 65536, %18
%22 = select i1 %20, i32 %21, i32 %17
%23 = lshr i32 %14, 16
%24 = icmp eq i32 %13, %23
br i1 %24, label %31, label %25
25: ; preds = %12
%26 = lshr i32 %14, 24
%27 = trunc i32 %26 to i8
store i8 %27, i8* %10
%28 = trunc i32 %23 to i8
store i8 %28, i8* %11
%29 = call i32 @checksum(i8* %10, i32 2, i32 2, i32 0, i32 4)
%30 = call i32 (i32*, i8*, ...) @fprintf(i32* %0, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %23, i32 %29)
br label %31
31: ; preds = %25, %12
%32 = phi i32 [ %23, %25 ], [ %13, %12 ]
%33 = call i32 (i32*, i8*, ...) @fprintf(i32* %0, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %22, i32 %14)
%34 = sub i32 %14, %2
%35 = add i32 %22, %14
%36 = sub i32 %35, %2
%37 = icmp ult i32 %34, %36
%38 = zext i32 %34 to i64
br i1 %37, label %39, label %48
39: ; preds = %31, %39
%40 = phi i64 [ %45, %39 ], [ %38, %31 ]
%41 = getelementptr inbounds i8, i8* %1, i64 %40
%42 = load i8, i8* %41
%43 = zext i8 %42 to i32
%44 = call i32 (i32*, i8*, ...) @fprintf(i32* %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 %43)
%45 = add nuw nsw i64 %40, 1
%46 = trunc i64 %45 to i32
%47 = icmp eq i32 %36, %46
br i1 %47, label %48, label %39
48: ; preds = %39, %31
%49 = getelementptr inbounds i8, i8* %1, i64 %38
%50 = call i32 @checksum(i8* %49, i32 %22, i32 %22, i32 %14, i32 0)
%51 = call i32 (i32*, i8*, ...) @fprintf(i32* %0, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 %50)
%52 = icmp ult i32 %35, %3
br i1 %52, label %12, label %53
53: ; preds = %48, %4
%54 = call i64 @fwrite(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i64 12, i64 1, i32* %0)
ret void
}