#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 /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
void ihex_hexout(FILE *fhex, int byte, int memory_location, int end) {
  static int byte_buffer[32];
  static int last_mem, buffer_pos, buffer_addr;
  static int writing_in_progress = 0;
  register int i, sum;
  if (!writing_in_progress) {
    last_mem = memory_location - 1;
    buffer_pos = 0;
    buffer_addr = memory_location;
    writing_in_progress = 1;
  }
  if ((memory_location != (last_mem + 1)) || (buffer_pos >= 32) ||
      ((end) && (buffer_pos > 0))) {
    fprintf(fhex, ":%02X%04X00", buffer_pos, buffer_addr);
    sum = buffer_pos + ((buffer_addr >> 8) & 255) + (buffer_addr & 255);
    for (i = 0; i < buffer_pos; i++) {
      fprintf(fhex, "%02X", byte_buffer[i] & 255);
      sum += byte_buffer[i] & 255;
    }
    fprintf(fhex, "%02X\n", (-sum) & 255);
    buffer_addr = memory_location;
    buffer_pos = 0;
  }
  if (end) {
    fprintf(fhex, ":00000001FF\n");
    fclose(fhex);
    writing_in_progress = 0;
  }
  last_mem = memory_location;
  byte_buffer[buffer_pos] = byte & 255;
  buffer_pos++;
}


x86 assembly (clang, -O0):
<ihex_hexout>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
cmpl   $0x0,0x2f90(%rip)
jne    116b <ihex_hexout+0x4b>
mov    -0x10(%rbp),%eax
sub    $0x1,%eax
mov    %eax,0x2f72(%rip)
movl   $0x0,0x2f6c(%rip)
mov    -0x10(%rbp),%eax
mov    %eax,0x2f67(%rip)
movl   $0x1,0x2f61(%rip)
mov    -0x10(%rbp),%eax
mov    0x2f4c(%rip),%ecx
add    $0x1,%ecx
cmp    %ecx,%eax
jne    11a3 <ihex_hexout+0x83>
cmpl   $0x20,0x2f3e(%rip)
jge    11a3 <ihex_hexout+0x83>
cmpl   $0x0,-0x14(%rbp)
je     127c <ihex_hexout+0x15c>
cmpl   $0x0,0x2f27(%rip)
jle    127c <ihex_hexout+0x15c>
mov    -0x8(%rbp),%rdi
mov    0x2f17(%rip),%edx
mov    0x2f15(%rip),%ecx
lea    0xe46(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    0x2efd(%rip),%eax
mov    0x2efb(%rip),%ecx
sar    $0x8,%ecx
and    $0xff,%ecx
add    %ecx,%eax
mov    0x2eea(%rip),%ecx
and    $0xff,%ecx
add    %ecx,%eax
mov    %eax,-0x1c(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    0x2ecb(%rip),%eax
jge    124c <ihex_hexout+0x12c>
mov    -0x8(%rbp),%rdi
movslq -0x18(%rbp),%rcx
lea    0x2e32(%rip),%rax
mov    (%rax,%rcx,4),%edx
and    $0xff,%edx
lea    0xdee(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movslq -0x18(%rbp),%rcx
lea    0x2e10(%rip),%rax
mov    (%rax,%rcx,4),%eax
and    $0xff,%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11f0 <ihex_hexout+0xd0>
mov    -0x8(%rbp),%rdi
xor    %edx,%edx
sub    -0x1c(%rbp),%edx
and    $0xff,%edx
lea    0xdaf(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x10(%rbp),%eax
mov    %eax,0x2e56(%rip)
movl   $0x0,0x2e48(%rip)
cmpl   $0x0,-0x14(%rbp)
je     12ab <ihex_hexout+0x18b>
mov    -0x8(%rbp),%rdi
lea    0xd86(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x8(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,0x2e21(%rip)
mov    -0x10(%rbp),%eax
mov    %eax,0x2e0c(%rip)
mov    -0xc(%rbp),%edx
and    $0xff,%edx
movslq 0x2e00(%rip),%rcx
lea    0x2d75(%rip),%rax
mov    %edx,(%rax,%rcx,4)
mov    0x2df0(%rip),%eax
add    $0x1,%eax
mov    %eax,0x2de7(%rip)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ihex_hexout>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r12d
mov    %esi,%ebx
mov    %rdi,%rbp
cmpb   $0x1,0x2f92(%rip)
jne    115a <ihex_hexout+0x2a>
mov    0x2f7e(%rip),%eax
mov    0x2f7c(%rip),%edx
jmp    117f <ihex_hexout+0x4f>
lea    -0x1(%r12),%eax
mov    %eax,0x2f6b(%rip)
movl   $0x0,0x2f65(%rip)
mov    %r12d,0x2f62(%rip)
movb   $0x1,0x2f5f(%rip)
xor    %edx,%edx
add    $0x1,%eax
cmp    %r12d,%eax
jne    119c <ihex_hexout+0x6c>
cmp    $0x1f,%edx
jg     119c <ihex_hexout+0x6c>
test   %ecx,%ecx
je     124e <ihex_hexout+0x11e>
test   %edx,%edx
jle    124e <ihex_hexout+0x11e>
mov    %ecx,(%rsp)
mov    %ebx,0x4(%rsp)
mov    0x2f2f(%rip),%ecx
lea    0xe50(%rip),%rsi
mov    %rbp,%rdi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    0x2f14(%rip),%eax
mov    0x2f12(%rip),%ecx
movzbl %ch,%edx
movzbl %cl,%r14d
add    %eax,%r14d
add    %edx,%r14d
test   %eax,%eax
jle    121c <ihex_hexout+0xec>
lea    0x2e72(%rip),%r15
lea    0xe27(%rip),%rbx
xor    %r13d,%r13d
nopl   0x0(%rax,%rax,1)
movzbl (%r15),%edx
mov    %rbp,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
movzbl (%r15),%eax
add    %eax,%r14d
add    $0x1,%r13
movslq 0x2ec1(%rip),%rax
add    $0x4,%r15
cmp    %rax,%r13
jl     11f0 <ihex_hexout+0xc0>
neg    %r14d
movzbl %r14b,%edx
lea    0xde7(%rip),%rsi
mov    %rbp,%rdi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    %r12d,0x2e9d(%rip)
movl   $0x0,0x2e8f(%rip)
mov    0x4(%rsp),%ebx
mov    (%rsp),%ecx
xor    %edx,%edx
test   %ecx,%ecx
je     1280 <ihex_hexout+0x150>
lea    0xdbe(%rip),%rdi
mov    $0xc,%esi
mov    $0x1,%edx
mov    %rbp,%rcx
call   1050 <fwrite@plt>
mov    %rbp,%rdi
call   1030 <fclose@plt>
movb   $0x0,0x2e62(%rip)
mov    0x2e54(%rip),%edx
mov    %r12d,0x2e49(%rip)
movzbl %bl,%eax
movslq %edx,%rcx
lea    0x2dbc(%rip),%rdx
mov    %eax,(%rdx,%rcx,4)
add    $0x1,%ecx
mov    %ecx,0x2e34(%rip)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@ihex_hexout.byte_buffer = internal global [32 x i32] zeroinitializer
@ihex_hexout.last_mem = internal global i32 0
@ihex_hexout.buffer_pos = internal global i32 0
@ihex_hexout.buffer_addr = internal global i32 0
@ihex_hexout.writing_in_progress = internal global i32 0
@.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_hexout(i32* %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32* %0, i32** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%11 = load i32, i32* @ihex_hexout.writing_in_progress
%12 = icmp ne i32 %11, 0
br i1 %12, label %17, label %13
13: ; preds = %4
%14 = load i32, i32* %7
%15 = sub nsw i32 %14, 1
store i32 %15, i32* @ihex_hexout.last_mem
store i32 0, i32* @ihex_hexout.buffer_pos
%16 = load i32, i32* %7
store i32 %16, i32* @ihex_hexout.buffer_addr
store i32 1, i32* @ihex_hexout.writing_in_progress
br label %17
17: ; preds = %13, %4
%18 = load i32, i32* %7
%19 = load i32, i32* @ihex_hexout.last_mem
%20 = add nsw i32 %19, 1
%21 = icmp ne i32 %18, %20
br i1 %21, label %31, label %22
22: ; preds = %17
%23 = load i32, i32* @ihex_hexout.buffer_pos
%24 = icmp sge i32 %23, 32
br i1 %24, label %31, label %25
25: ; preds = %22
%26 = load i32, i32* %8
%27 = icmp ne i32 %26, 0
br i1 %27, label %28, label %73
28: ; preds = %25
%29 = load i32, i32* @ihex_hexout.buffer_pos
%30 = icmp sgt i32 %29, 0
br i1 %30, label %31, label %73
31: ; preds = %28, %22, %17
%32 = load i32*, i32** %5
%33 = load i32, i32* @ihex_hexout.buffer_pos
%34 = load i32, i32* @ihex_hexout.buffer_addr
%35 = call i32 (i32*, i8*, ...) @fprintf(i32* %32, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %33, i32 %34)
%36 = load i32, i32* @ihex_hexout.buffer_pos
%37 = load i32, i32* @ihex_hexout.buffer_addr
%38 = ashr i32 %37, 8
%39 = and i32 %38, 255
%40 = add nsw i32 %36, %39
%41 = load i32, i32* @ihex_hexout.buffer_addr
%42 = and i32 %41, 255
%43 = add nsw i32 %40, %42
store i32 %43, i32* %10
store i32 0, i32* %9
br label %44
44: ; preds = %63, %31
%45 = load i32, i32* %9
%46 = load i32, i32* @ihex_hexout.buffer_pos
%47 = icmp slt i32 %45, %46
br i1 %47, label %48, label %66
48: ; preds = %44
%49 = load i32*, i32** %5
%50 = load i32, i32* %9
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds [32 x i32], [32 x i32]* @ihex_hexout.byte_buffer, i64 0, i64 %51
%53 = load i32, i32* %52
%54 = and i32 %53, 255
%55 = call i32 (i32*, i8*, ...) @fprintf(i32* %49, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %54)
%56 = load i32, i32* %9
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds [32 x i32], [32 x i32]* @ihex_hexout.byte_buffer, i64 0, i64 %57
%59 = load i32, i32* %58
%60 = and i32 %59, 255
%61 = load i32, i32* %10
%62 = add nsw i32 %61, %60
store i32 %62, i32* %10
br label %63
63: ; preds = %48
%64 = load i32, i32* %9
%65 = add nsw i32 %64, 1
store i32 %65, i32* %9
br label %44
66: ; preds = %44
%67 = load i32*, i32** %5
%68 = load i32, i32* %10
%69 = sub nsw i32 0, %68
%70 = and i32 %69, 255
%71 = call i32 (i32*, i8*, ...) @fprintf(i32* %67, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %70)
%72 = load i32, i32* %7
store i32 %72, i32* @ihex_hexout.buffer_addr
store i32 0, i32* @ihex_hexout.buffer_pos
br label %73
73: ; preds = %66, %28, %25
%74 = load i32, i32* %8
%75 = icmp ne i32 %74, 0
br i1 %75, label %76, label %81
76: ; preds = %73
%77 = load i32*, i32** %5
%78 = call i32 (i32*, i8*, ...) @fprintf(i32* %77, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%79 = load i32*, i32** %5
%80 = call i32 @fclose(i32* %79)
store i32 0, i32* @ihex_hexout.writing_in_progress
br label %81
81: ; preds = %76, %73
%82 = load i32, i32* %7
store i32 %82, i32* @ihex_hexout.last_mem
%83 = load i32, i32* %6
%84 = and i32 %83, 255
%85 = load i32, i32* @ihex_hexout.buffer_pos
%86 = sext i32 %85 to i64
%87 = getelementptr inbounds [32 x i32], [32 x i32]* @ihex_hexout.byte_buffer, i64 0, i64 %86
store i32 %84, i32* %87
%88 = load i32, i32* @ihex_hexout.buffer_pos
%89 = add nsw i32 %88, 1
store i32 %89, i32* @ihex_hexout.buffer_pos
ret void
}

LLVM IR (-O2):
@ihex_hexout.byte_buffer = internal unnamed_addr global [32 x i32] zeroinitializer
@ihex_hexout.last_mem = internal unnamed_addr global i32 0
@ihex_hexout.buffer_pos = internal unnamed_addr global i32 0
@ihex_hexout.buffer_addr = internal unnamed_addr global i32 0
@ihex_hexout.writing_in_progress = internal unnamed_addr global i1 false
@.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_hexout(i32* nocapture %0, i32 %1, i32 %2, i32 %3) {
%5 = load i1, i1* @ihex_hexout.writing_in_progress
br i1 %5, label %6, label %9
6: ; preds = %4
%7 = load i32, i32* @ihex_hexout.last_mem
%8 = load i32, i32* @ihex_hexout.buffer_pos
br label %11
9: ; preds = %4
%10 = add nsw i32 %2, -1
store i32 %10, i32* @ihex_hexout.last_mem
store i32 0, i32* @ihex_hexout.buffer_pos
store i32 %2, i32* @ihex_hexout.buffer_addr
store i1 true, i1* @ihex_hexout.writing_in_progress
br label %11
11: ; preds = %6, %9
%12 = phi i32 [ %8, %6 ], [ 0, %9 ]
%13 = phi i32 [ %7, %6 ], [ %10, %9 ]
%14 = add nsw i32 %13, 1
%15 = icmp ne i32 %14, %2
%16 = icmp sgt i32 %12, 31
%17 = select i1 %15, i1 true, i1 %16
br i1 %17, label %22, label %18
18: ; preds = %11
%19 = icmp ne i32 %3, 0
%20 = icmp sgt i32 %12, 0
%21 = select i1 %19, i1 %20, i1 false
br i1 %21, label %22, label %52
22: ; preds = %18, %11
%23 = load i32, i32* @ihex_hexout.buffer_addr
%24 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %0, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %12, i32 %23)
%25 = load i32, i32* @ihex_hexout.buffer_pos
%26 = load i32, i32* @ihex_hexout.buffer_addr
%27 = lshr i32 %26, 8
%28 = and i32 %27, 255
%29 = and i32 %26, 255
%30 = add i32 %29, %25
%31 = add i32 %30, %28
%32 = icmp sgt i32 %25, 0
br i1 %32, label %33, label %47
33: ; preds = %22, %33
%34 = phi i64 [ %43, %33 ], [ 0, %22 ]
%35 = phi i32 [ %42, %33 ], [ %31, %22 ]
%36 = getelementptr inbounds [32 x i32], [32 x i32]* @ihex_hexout.byte_buffer, i64 0, i64 %34
%37 = load i32, i32* %36
%38 = and i32 %37, 255
%39 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %38)
%40 = load i32, i32* %36
%41 = and i32 %40, 255
%42 = add nsw i32 %41, %35
%43 = add nuw nsw i64 %34, 1
%44 = load i32, i32* @ihex_hexout.buffer_pos
%45 = sext i32 %44 to i64
%46 = icmp slt i64 %43, %45
br i1 %46, label %33, label %47
47: ; preds = %33, %22
%48 = phi i32 [ %31, %22 ], [ %42, %33 ]
%49 = sub nsw i32 0, %48
%50 = and i32 %49, 255
%51 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %0, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %50)
store i32 %2, i32* @ihex_hexout.buffer_addr
store i32 0, i32* @ihex_hexout.buffer_pos
br label %52
52: ; preds = %47, %18
%53 = phi i32 [ 0, %47 ], [ %12, %18 ]
%54 = icmp eq i32 %3, 0
br i1 %54, label %59, label %55
55: ; preds = %52
%56 = 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)
%57 = tail call i32 @fclose(i32* %0)
store i1 false, i1* @ihex_hexout.writing_in_progress
%58 = load i32, i32* @ihex_hexout.buffer_pos
br label %59
59: ; preds = %55, %52
%60 = phi i32 [ %58, %55 ], [ %53, %52 ]
store i32 %2, i32* @ihex_hexout.last_mem
%61 = and i32 %1, 255
%62 = sext i32 %60 to i64
%63 = getelementptr inbounds [32 x i32], [32 x i32]* @ihex_hexout.byte_buffer, i64 0, i64 %62
store i32 %61, i32* %63
%64 = add nsw i32 %60, 1
store i32 %64, i32* @ihex_hexout.buffer_pos
ret void
}