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

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int, ...);
void write_hex_line(FILE *out, uint32_t address, uint8_t *data, int len,
                    uint32_t *segment) {
  int checksum;
  int n;
  if ((address & 0xffff0000) != *segment) {
    *segment = address & 0xffff0000;
    checksum =
        4 + (((*segment) >> 24) & 0xff) + (((*segment) >> 16) & 0xff) + 2;
    fprintf(out, ":02000004%04X%02X\n", ((*segment) >> 16) & 0xffff,
            (((checksum & 0xff) ^ 0xff) + 1) & 0xff);
  }
  address = address & 0xffff;
  fprintf(out, ":%02X%04X00", len, address);
  checksum = len + (address >> 8) + (address & 255);
  for (n = 0; n < len; n++) {
    fprintf(out, "%02X", data[n]);
    checksum = checksum + data[n];
  }
  fprintf(out, "%02X\n", (((checksum & 0xff) ^ 0xff) + 1) & 0xff);
}


x86 assembly (clang, -O0):
<write_hex_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0xc(%rbp),%eax
and    $0xffff0000,%eax
mov    -0x28(%rbp),%rcx
cmp    (%rcx),%eax
je     11ae <write_hex_line+0x9e>
mov    -0xc(%rbp),%ecx
and    $0xffff0000,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rax
mov    (%rax),%eax
sar    $0x18,%eax
and    $0xff,%eax
add    $0x4,%eax
mov    -0x28(%rbp),%rcx
mov    (%rcx),%ecx
sar    $0x10,%ecx
and    $0xff,%ecx
add    %ecx,%eax
add    $0x2,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    (%rax),%edx
sar    $0x10,%edx
and    $0xffff,%edx
mov    -0x2c(%rbp),%ecx
and    $0xff,%ecx
xor    $0xff,%ecx
add    $0x1,%ecx
and    $0xff,%ecx
lea    0xe59(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    -0xc(%rbp),%eax
and    $0xffff,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x1c(%rbp),%edx
mov    -0xc(%rbp),%ecx
lea    0xe49(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    -0x1c(%rbp),%eax
mov    -0xc(%rbp),%ecx
sar    $0x8,%ecx
add    %ecx,%eax
mov    -0xc(%rbp),%ecx
and    $0xff,%ecx
add    %ecx,%eax
mov    %eax,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    1239 <write_hex_line+0x129>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rax
movslq -0x30(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
lea    0xe0c(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    -0x2c(%rbp),%eax
mov    -0x18(%rbp),%rcx
movslq -0x30(%rbp),%rdx
add    (%rcx,%rdx,4),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    11f1 <write_hex_line+0xe1>
mov    -0x8(%rbp),%rdi
mov    -0x2c(%rbp),%edx
and    $0xff,%edx
xor    $0xff,%edx
add    $0x1,%edx
and    $0xff,%edx
lea    0xdb1(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_hex_line>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%r15d
mov    %rdx,%r12
mov    %esi,%ebx
mov    %rdi,%r14
mov    %esi,%eax
and    $0xffff0000,%eax
cmp    (%r8),%eax
je     115f <write_hex_line+0x4f>
mov    %eax,(%r8)
mov    %ebx,%eax
shr    $0x18,%eax
mov    %ebx,%ecx
sar    $0x10,%ecx
movzwl %cx,%edx
add    %eax,%ecx
mov    $0xfa,%eax
sub    %ecx,%eax
movzbl %al,%ecx
lea    0xeab(%rip),%rsi
mov    %r14,%rdi
xor    %eax,%eax
call   1030 <fprintf@plt>
movzwl %bx,%r13d
lea    0xea9(%rip),%rsi
mov    %r14,%rdi
mov    %r15d,%edx
mov    %r13d,%ecx
xor    %eax,%eax
call   1030 <fprintf@plt>
shr    $0x8,%r13d
movzbl %bl,%ebp
add    %r15d,%ebp
add    %r13d,%ebp
test   %r15d,%r15d
jle    11be <write_hex_line+0xae>
mov    %r15d,%r13d
lea    0xe89(%rip),%r15
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
mov    (%r12,%rbx,4),%edx
mov    %r14,%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1030 <fprintf@plt>
add    (%r12,%rbx,4),%ebp
add    $0x1,%rbx
cmp    %rbx,%r13
jne    11a0 <write_hex_line+0x90>
neg    %ebp
movzbl %bpl,%edx
lea    0xe42(%rip),%rsi
mov    %r14,%rdi
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <fprintf@plt>

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