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

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int, ...);
void write_ihex(FILE *file, uint8_t *data, int len, uint16_t relative_address) {
  int i;
  for (i = 0; i < len; i += 32) {
    uint16_t address = i + relative_address;
    if (relative_address == 0xFFFF) {
      address = 0;
    }
    uint8_t checksum = 0;
    uint8_t record_type = 0;
    uint8_t bytes = 32;
    if (len - i < 32) {
      bytes = len - i;
    }
    checksum += bytes;
    checksum += address & 0xFF;
    checksum += (address >> 8) & 0xFF;
    fprintf(file, ":%02X%04X%02X", bytes, address, record_type);
    int j;
    for (j = 0; j < bytes; j++) {
      checksum += data[j];
      fprintf(file, "%02X", data[j]);
    }
    checksum = 0x100 - checksum;
    fprintf(file, "%02X\r\n", checksum);
    data += bytes;
  }
}


x86 assembly (clang, -O0):
<write_ihex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1259 <write_ihex+0x149>
mov    -0x1c(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x20(%rbp)
cmpl   $0xffff,-0x18(%rbp)
jne    1156 <write_ihex+0x46>
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x20,-0x2c(%rbp)
mov    -0x14(%rbp),%eax
sub    -0x1c(%rbp),%eax
cmp    $0x20,%eax
jge    1183 <write_ihex+0x73>
mov    -0x14(%rbp),%eax
sub    -0x1c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%eax
and    $0xff,%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%eax
sar    $0x8,%eax
and    $0xff,%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x2c(%rbp),%edx
mov    -0x20(%rbp),%ecx
mov    -0x28(%rbp),%r8d
lea    0xe40(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jge    1216 <write_ihex+0x106>
mov    -0x10(%rbp),%rax
movslq -0x30(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rax
movslq -0x30(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
lea    0xe08(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    11ce <write_ihex+0xbe>
mov    $0x100,%eax
sub    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x24(%rbp),%edx
lea    0xddf(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    -0x2c(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x20,%eax
mov    %eax,-0x1c(%rbp)
jmp    112d <write_ihex+0x1d>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_ihex>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,0x8(%rsp)
test   %edx,%edx
jle    11f1 <write_ihex+0xe1>
mov    %rsi,%r14
mov    %rdi,%r15
lea    0xed1(%rip),%r13
xor    %esi,%esi
mov    %edx,0x4(%rsp)
jmp    1170 <write_ihex+0x60>
mov    $0x100,%edx
sub    %ebp,%edx
mov    %r15,%rdi
lea    0xebd(%rip),%rsi
xor    %eax,%eax
call   1030 <fprintf@plt>
lea    (%r14,%r12,4),%r14
mov    0x10(%rsp),%rsi
add    $0x20,%esi
mov    0x4(%rsp),%edx
cmp    %edx,%esi
jge    11f1 <write_ihex+0xe1>
mov    0x8(%rsp),%rax
lea    (%rsi,%rax,1),%ecx
cmp    $0xffff,%eax
mov    $0x0,%eax
cmove  %eax,%ecx
mov    %edx,%ebx
mov    %rsi,0x10(%rsp)
sub    %esi,%ebx
cmp    $0x20,%ebx
mov    $0x20,%r12d
cmovl  %ebx,%r12d
movzbl %cl,%eax
add    %r12d,%eax
movzbl %ch,%ebp
add    %eax,%ebp
mov    %r15,%rdi
lea    0xe50(%rip),%rsi
mov    %r12d,%edx
xor    %r8d,%r8d
xor    %eax,%eax
call   1030 <fprintf@plt>
movslq %r12d,%r12
test   %ebx,%ebx
jle    1140 <write_ihex+0x30>
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
mov    (%r14,%rbx,4),%edx
add    %edx,%ebp
mov    %r15,%rdi
mov    %r13,%rsi
xor    %eax,%eax
call   1030 <fprintf@plt>
add    $0x1,%rbx
cmp    %r12,%rbx
jl     11d0 <write_ihex+0xc0>
jmp    1140 <write_ihex+0x30>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [14 x i8] c":%02X%04X%02X\00"
@.str.1 = [5 x i8] c"%02X\00"
@.str.2 = [7 x i8] c"%02X\0D\0A\00"
define void @write_ihex(i32* nocapture %0, i32* nocapture %1, i32 %2, i32 %3) {
%5 = icmp sgt i32 %2, 0
br i1 %5, label %6, label %40
6: ; preds = %4
%7 = icmp eq i32 %3, 65535
br label %8
8: ; preds = %6, %33
%9 = phi i32* [ %1, %6 ], [ %37, %33 ]
%10 = phi i32 [ 0, %6 ], [ %38, %33 ]
%11 = add nsw i32 %10, %3
%12 = select i1 %7, i32 0, i32 %11
%13 = sub nsw i32 %2, %10
%14 = icmp slt i32 %13, 32
%15 = select i1 %14, i32 %13, i32 32
%16 = and i32 %12, 255
%17 = add nsw i32 %15, %16
%18 = lshr i32 %12, 8
%19 = and i32 %18, 255
%20 = add nsw i32 %17, %19
%21 = tail call i32 (i32*, i8*, i32, ...) @fprintf(i32* %0, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %15, i32 %12, i32 0)
%22 = icmp sgt i32 %13, 0
%23 = sext i32 %15 to i64
br i1 %22, label %24, label %33
24: ; preds = %8, %24
%25 = phi i64 [ %31, %24 ], [ 0, %8 ]
%26 = phi i32 [ %29, %24 ], [ %20, %8 ]
%27 = getelementptr inbounds i32, i32* %9, i64 %25
%28 = load i32, i32* %27
%29 = add nsw i32 %28, %26
%30 = tail call i32 (i32*, i8*, i32, ...) @fprintf(i32* %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %28)
%31 = add nuw nsw i64 %25, 1
%32 = icmp slt i64 %31, %23
br i1 %32, label %24, label %33
33: ; preds = %24, %8
%34 = phi i32 [ %20, %8 ], [ %29, %24 ]
%35 = sub nsw i32 256, %34
%36 = tail call i32 (i32*, i8*, i32, ...) @fprintf(i32* %0, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %35)
%37 = getelementptr inbounds i32, i32* %9, i64 %23
%38 = add nuw nsw i32 %10, 32
%39 = icmp slt i32 %38, %2
br i1 %39, label %8, label %40
40: ; preds = %33, %4
ret void
}