#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*/ exit(int);
scalar_t__ fprintf(int /*<<< orphan*/ *, char *, int const, ...);
int /*<<< orphan*/ perror(char *);
void write_hex_record(FILE *file, uint16_t addr, const uint8_t *data,
                      size_t size) {
  const uint8_t *end = data + size;
  int sum;
  sum = size + (addr >> 8) + addr;
  if (fprintf(file, ":%02X%04X00", (unsigned)size, addr) < 0)
    goto error;
  while (data != end) {
    if (fprintf(file, "%02X", *data) < 0)
      goto error;
    sum += *data;
    data++;
  }
  if (fprintf(file, "%02X\n", (-sum) & 0xff) < 0)
    goto error;
  return;
error:
  perror("fprintf");
  exit(1);
}


x86 assembly (clang, -O0):
<write_hex_record>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    -0xc(%rbp),%ecx
sar    $0x8,%ecx
movslq %ecx,%rcx
add    %rcx,%rax
movslq -0xc(%rbp),%rcx
add    %rcx,%rax
mov    %eax,-0x2c(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    %eax,%edx
mov    -0xc(%rbp),%ecx
lea    0xe78(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
cmp    $0x0,%rax
jge    119e <write_hex_record+0x6e>
jmp    1227 <write_hex_record+0xf7>
jmp    11a3 <write_hex_record+0x73>
mov    -0x18(%rbp),%rax
cmp    -0x28(%rbp),%rax
je     11f5 <write_hex_record+0xc5>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%edx
lea    0xe4a(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
cmp    $0x0,%rax
jge    11d8 <write_hex_record+0xa8>
jmp    1227 <write_hex_record+0xf7>
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
add    -0x2c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x18(%rbp)
jmp    11a3 <write_hex_record+0x73>
mov    -0x8(%rbp),%rdi
xor    %edx,%edx
sub    -0x2c(%rbp),%edx
and    $0xff,%edx
lea    0xe06(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
cmp    $0x0,%rax
jge    1221 <write_hex_record+0xf1>
jmp    1227 <write_hex_record+0xf7>
add    $0x30,%rsp
pop    %rbp
ret
lea    0xde9(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

x86 assembly (clang, -O2):
<write_hex_record>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r15
mov    %rdx,%r13
mov    %esi,%r12d
mov    %rdi,%r14
lea    0xeb2(%rip),%rsi
mov    %r15d,%edx
mov    %r12d,%ecx
xor    %eax,%eax
call   1030 <fprintf@plt>
test   %rax,%rax
js     11d0 <write_hex_record+0xa0>
mov    %r12d,%ebp
sar    $0x8,%ebp
add    %r12d,%ebp
add    %r15d,%ebp
test   %r15,%r15
je     11a5 <write_hex_record+0x75>
shl    $0x2,%r15
lea    0xe90(%rip),%r12
xor    %ebx,%ebx
xchg   %ax,%ax
mov    0x0(%r13,%rbx,1),%edx
mov    %r14,%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1030 <fprintf@plt>
test   %rax,%rax
js     11d0 <write_hex_record+0xa0>
add    0x0(%r13,%rbx,1),%ebp
add    $0x4,%rbx
cmp    %rbx,%r15
jne    1180 <write_hex_record+0x50>
neg    %ebp
movzbl %bpl,%edx
lea    0xe5f(%rip),%rsi
mov    %r14,%rdi
xor    %eax,%eax
call   1030 <fprintf@plt>
test   %rax,%rax
js     11d0 <write_hex_record+0xa0>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe40(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1050 <exit@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 = [8 x i8] c"fprintf\00"
define void @write_hex_record(i32* %0, i32 %1, i32* %2, i64 %3) {
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i64
%9 = alloca i32*
%10 = alloca i32
store i32* %0, i32** %5
store i32 %1, i32* %6
store i32* %2, i32** %7
store i64 %3, i64* %8
%11 = load i32*, i32** %7
%12 = load i64, i64* %8
%13 = getelementptr inbounds i32, i32* %11, i64 %12
store i32* %13, i32** %9
%14 = load i64, i64* %8
%15 = load i32, i32* %6
%16 = ashr i32 %15, 8
%17 = sext i32 %16 to i64
%18 = add i64 %14, %17
%19 = load i32, i32* %6
%20 = sext i32 %19 to i64
%21 = add i64 %18, %20
%22 = trunc i64 %21 to i32
store i32 %22, i32* %10
%23 = load i32*, i32** %5
%24 = load i64, i64* %8
%25 = trunc i64 %24 to i32
%26 = load i32, i32* %6
%27 = call i64 (i32*, i8*, i32, ...) @fprintf(i32* %23, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %25, i32 %26)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %4
br label %58
30: ; preds = %4
br label %31
31: ; preds = %42, %30
%32 = load i32*, i32** %7
%33 = load i32*, i32** %9
%34 = icmp ne i32* %32, %33
br i1 %34, label %35, label %49
35: ; preds = %31
%36 = load i32*, i32** %5
%37 = load i32*, i32** %7
%38 = load i32, i32* %37
%39 = call i64 (i32*, i8*, i32, ...) @fprintf(i32* %36, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %38)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %42
41: ; preds = %35
br label %58
42: ; preds = %35
%43 = load i32*, i32** %7
%44 = load i32, i32* %43
%45 = load i32, i32* %10
%46 = add nsw i32 %45, %44
store i32 %46, i32* %10
%47 = load i32*, i32** %7
%48 = getelementptr inbounds i32, i32* %47, i32 1
store i32* %48, i32** %7
br label %31
49: ; preds = %31
%50 = load i32*, i32** %5
%51 = load i32, i32* %10
%52 = sub nsw i32 0, %51
%53 = and i32 %52, 255
%54 = call i64 (i32*, i8*, i32, ...) @fprintf(i32* %50, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %53)
%55 = icmp slt i64 %54, 0
br i1 %55, label %56, label %57
56: ; preds = %49
br label %58
57: ; preds = %49
ret void
58: ; preds = %56, %41, %29
%59 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%60 = call i32 @exit(i32 1)
unreachable
}

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 = [8 x i8] c"fprintf\00"
define void @write_hex_record(i32* nocapture %0, i32 %1, i32* %2, i64 %3) {
%5 = getelementptr inbounds i32, i32* %2, i64 %3
%6 = ashr i32 %1, 8
%7 = trunc i64 %3 to i32
%8 = add i32 %6, %1
%9 = add i32 %8, %7
%10 = tail call i64 (i32*, i8*, i32, ...) @fprintf(i32* %0, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %7, i32 %1)
%11 = icmp slt i64 %10, 0
br i1 %11, label %32, label %12
12: ; preds = %4
%13 = icmp eq i64 %3, 0
br i1 %13, label %25, label %14
14: ; preds = %12, %20
%15 = phi i32 [ %22, %20 ], [ %9, %12 ]
%16 = phi i32* [ %23, %20 ], [ %2, %12 ]
%17 = load i32, i32* %16
%18 = tail call i64 (i32*, i8*, i32, ...) @fprintf(i32* %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %17)
%19 = icmp slt i64 %18, 0
br i1 %19, label %32, label %20
20: ; preds = %14
%21 = load i32, i32* %16
%22 = add nsw i32 %21, %15
%23 = getelementptr inbounds i32, i32* %16, i64 1
%24 = icmp eq i32* %23, %5
br i1 %24, label %25, label %14
25: ; preds = %20, %12
%26 = phi i32 [ %9, %12 ], [ %22, %20 ]
%27 = sub nsw i32 0, %26
%28 = and i32 %27, 255
%29 = tail call i64 (i32*, i8*, i32, ...) @fprintf(i32* %0, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %28)
%30 = icmp slt i64 %29, 0
br i1 %30, label %32, label %31
31: ; preds = %25
ret void
32: ; preds = %14, %25, %4
%33 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%34 = tail call i32 @exit(i32 1)
unreachable
}