#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 checksum(unsigned char *, unsigned int, unsigned int, unsigned int,
             int /*<<< orphan*/);
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, chunk_type, i, byte, line_no = 0,
                                                            greatest_addr = 0;
  chunk_start = start;
  while (chunk_start < end) {
    chunk_len = end - chunk_start;
    if (chunk_len > 32) {
      chunk_len = 32;
    }
    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,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x18(%rbp),%eax
jae    121a <ihex_write+0xfa>
mov    -0x18(%rbp),%eax
sub    -0x20(%rbp),%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x20,-0x1c(%rbp)
jbe    1170 <ihex_write+0x50>
movl   $0x20,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x1c(%rbp),%edx
mov    -0x20(%rbp),%ecx
lea    0xe7f(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x20(%rbp),%eax
sub    -0x14(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
mov    -0x20(%rbp),%ecx
add    -0x1c(%rbp),%ecx
sub    -0x14(%rbp),%ecx
cmp    %ecx,%eax
jae    11d0 <ihex_write+0xb0>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%ecx
movzbl (%rax,%rcx,1),%edx
lea    0xe51(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    1191 <ihex_write+0x71>
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    0xe0c(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x1c(%rbp),%eax
add    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    114a <ihex_write+0x2a>
mov    -0x8(%rbp),%rdi
lea    0xdf2(%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    $0x28,%rsp
mov    %rsi,0x10(%rsp)
mov    %rdi,%rbx
mov    %edx,0x4(%rsp)
cmp    %ecx,%edx
jae    1231 <ihex_write+0x101>
mov    0x4(%rsp),%eax
mov    %eax,%ebp
mov    %ecx,0x8(%rsp)
jmp    11a2 <ihex_write+0x72>
xchg   %ax,%ax
mov    0x18(%rsp),%rdi
add    0x10(%rsp),%rdi
mov    %r15d,%esi
mov    %r15d,%edx
mov    0x20(%rsp),%rcx
xor    %r8d,%r8d
call   1030 <checksum@plt>
mov    %rbx,%rdi
lea    0xe8a(%rip),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    0xc(%rsp),%eax
mov    %eax,%ebp
mov    0x8(%rsp),%ecx
cmp    %ecx,%eax
jae    1231 <ihex_write+0x101>
mov    %ecx,%r15d
sub    %ebp,%r15d
cmp    $0x20,%r15d
mov    $0x20,%eax
cmovae %eax,%r15d
mov    %rbx,%rdi
lea    0xe41(%rip),%rsi
mov    %r15d,%edx
mov    %ebp,%ecx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    %ebp,%edx
mov    0x4(%rsp),%ecx
sub    %ecx,%edx
mov    %rbp,0x20(%rsp)
lea    (%r15,%rbp,1),%eax
mov    %eax,0xc(%rsp)
sub    %ecx,%eax
mov    %rdx,%rcx
mov    %rdx,0x18(%rsp)
cmp    %eax,%edx
lea    0xe19(%rip),%r12
jae    1160 <ihex_write+0x30>
mov    0x10(%rsp),%rax
mov    0x18(%rsp),%rcx
lea    (%rax,%rcx,1),%r13
mov    %r15d,%ebp
xor    %r14d,%r14d
nopl   (%rax)
movzbl 0x0(%r13,%r14,1),%edx
mov    %rbx,%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
add    $0x1,%r14
cmp    %r14d,%ebp
jne    1210 <ihex_write+0xe0>
jmp    1160 <ihex_write+0x30>
lea    0xddf(%rip),%rdi
mov    $0xc,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <fwrite@plt>

LLVM IR (-O0):
@.str = [12 x i8] c":%02X%04X00\00"
@.str.1 = [5 x i8] c"%02X\00"
@.str.2 = [6 x i8] c"%02X\0A\00"
@.str.3 = [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 i32
%14 = alloca i32
%15 = alloca i32
store i32* %0, i32** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 %3, i32* %8
store i32 0, i32* %14
store i32 0, i32* %15
%16 = load i32, i32* %7
store i32 %16, i32* %10
br label %17
17: ; preds = %56, %4
%18 = load i32, i32* %10
%19 = load i32, i32* %8
%20 = icmp ult i32 %18, %19
br i1 %20, label %21, label %72
21: ; preds = %17
%22 = load i32, i32* %8
%23 = load i32, i32* %10
%24 = sub i32 %22, %23
store i32 %24, i32* %9
%25 = load i32, i32* %9
%26 = icmp ugt i32 %25, 32
br i1 %26, label %27, label %28
27: ; preds = %21
store i32 32, i32* %9
br label %28
28: ; preds = %27, %21
%29 = load i32*, i32** %5
%30 = load i32, i32* %9
%31 = load i32, i32* %10
%32 = call i32 (i32*, i8*, ...) @fprintf(i32* %29, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %30, i32 %31)
%33 = load i32, i32* %10
%34 = load i32, i32* %7
%35 = sub i32 %33, %34
store i32 %35, i32* %12
br label %36
36: ; preds = %53, %28
%37 = load i32, i32* %12
%38 = load i32, i32* %10
%39 = load i32, i32* %9
%40 = add i32 %38, %39
%41 = load i32, i32* %7
%42 = sub i32 %40, %41
%43 = icmp ult i32 %37, %42
br i1 %43, label %44, label %56
44: ; preds = %36
%45 = load i32*, i32** %5
%46 = load i8*, i8** %6
%47 = load i32, i32* %12
%48 = zext i32 %47 to i64
%49 = getelementptr inbounds i8, i8* %46, i64 %48
%50 = load i8, i8* %49
%51 = zext i8 %50 to i32
%52 = call i32 (i32*, i8*, ...) @fprintf(i32* %45, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %51)
br label %53
53: ; preds = %44
%54 = load i32, i32* %12
%55 = add i32 %54, 1
store i32 %55, i32* %12
br label %36
56: ; preds = %36
%57 = load i32*, i32** %5
%58 = load i8*, i8** %6
%59 = load i32, i32* %10
%60 = load i32, i32* %7
%61 = sub i32 %59, %60
%62 = zext i32 %61 to i64
%63 = getelementptr inbounds i8, i8* %58, i64 %62
%64 = load i32, i32* %9
%65 = load i32, i32* %9
%66 = load i32, i32* %10
%67 = call i32 @checksum(i8* %63, i32 %64, i32 %65, i32 %66, i32 0)
%68 = call i32 (i32*, i8*, ...) @fprintf(i32* %57, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %67)
%69 = load i32, i32* %9
%70 = load i32, i32* %10
%71 = add i32 %70, %69
store i32 %71, i32* %10
br label %17
72: ; preds = %17
%73 = load i32*, i32** %5
%74 = call i32 (i32*, i8*, ...) @fprintf(i32* %73, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@.str = [12 x i8] c":%02X%04X00\00"
@.str.1 = [5 x i8] c"%02X\00"
@.str.2 = [6 x i8] c"%02X\0A\00"
@.str.3 = [13 x i8] c":00000001FF\0A\00"
define void @ihex_write(i32* nocapture %0, i8* %1, i32 %2, i32 %3) {
%5 = icmp ult i32 %2, %3
br i1 %5, label %6, label %31
6: ; preds = %4, %26
%7 = phi i32 [ %13, %26 ], [ %2, %4 ]
%8 = sub i32 %3, %7
%9 = icmp ult i32 %8, 32
%10 = select i1 %9, i32 %8, i32 32
%11 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %0, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %10, i32 %7)
%12 = sub i32 %7, %2
%13 = add i32 %10, %7
%14 = sub i32 %13, %2
%15 = icmp ult i32 %12, %14
%16 = zext i32 %12 to i64
br i1 %15, label %17, label %26
17: ; preds = %6, %17
%18 = phi i64 [ %23, %17 ], [ %16, %6 ]
%19 = getelementptr inbounds i8, i8* %1, i64 %18
%20 = load i8, i8* %19
%21 = zext i8 %20 to i32
%22 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %21)
%23 = add nuw nsw i64 %18, 1
%24 = trunc i64 %23 to i32
%25 = icmp eq i32 %14, %24
br i1 %25, label %26, label %17
26: ; preds = %17, %6
%27 = getelementptr inbounds i8, i8* %1, i64 %16
%28 = tail call i32 @checksum(i8* %27, i32 %10, i32 %10, i32 %7, i32 0)
%29 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %0, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %28)
%30 = icmp ult i32 %13, %3
br i1 %30, label %6, label %31
31: ; preds = %26, %4
%32 = tail call i64 @fwrite(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i64 12, i64 1, i32* %0)
ret void
}