#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 unsigned int uint8_t;

/* Variables and functions */
int /*<<< orphan*/ sprintf(char *, char *, unsigned int, ...);
int /*<<< orphan*/ strcat(char *, char *);
int strlen(char *);
int hexfile_out(char *line, unsigned int type, unsigned int address,
                unsigned char *data, unsigned int bytes) {
  uint8_t cksum = 0;
  uint8_t i = 0;
  char tmp[8];
  sprintf(line, ":%2.2X%4.4X%2.2X", bytes, address, type);
  cksum -= bytes;
  cksum -= address >> 8;
  cksum -= address & 0xFF;
  cksum -= type;
  for (i = 0; i < bytes; i++) {
    sprintf(tmp, "%2.2X", data[i]);
    strcat(line, tmp);
    cksum -= data[i];
  }
  sprintf(tmp, "%2.2X\r\n", cksum);
  strcat(line, tmp);
  return strlen(line);
}


x86 assembly (clang, -O0):
<hexfile_out>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x1c(%rbp),%edx
mov    -0x10(%rbp),%ecx
mov    -0xc(%rbp),%r8d
lea    0xe93(%rip),%rsi
mov    $0x0,%al
call   1050 <sprintf@plt>
mov    -0x1c(%rbp),%ecx
mov    -0x20(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%ecx
shr    $0x8,%ecx
mov    -0x20(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%ecx
and    $0xff,%ecx
mov    -0x20(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x20(%rbp)
mov    -0xc(%rbp),%ecx
mov    -0x20(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jae    1207 <hexfile_out+0xd7>
lea    -0x2c(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    -0x24(%rbp),%ecx
movzbl (%rax,%rcx,1),%edx
lea    0xe39(%rip),%rsi
mov    $0x0,%al
call   1050 <sprintf@plt>
mov    -0x8(%rbp),%rdi
lea    -0x2c(%rbp),%rsi
call   1040 <strcat@plt>
mov    -0x18(%rbp),%rax
mov    -0x24(%rbp),%ecx
movzbl (%rax,%rcx,1),%ecx
mov    -0x20(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x20(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11b0 <hexfile_out+0x80>
lea    -0x2c(%rbp),%rdi
mov    -0x20(%rbp),%edx
lea    0xdfc(%rip),%rsi
mov    $0x0,%al
call   1050 <sprintf@plt>
mov    -0x8(%rbp),%rdi
lea    -0x2c(%rbp),%rsi
call   1040 <strcat@plt>
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hexfile_out>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8d,%r15d
mov    %rcx,%rbx
mov    %edx,%ebp
mov    %esi,%r12d
mov    %rdi,%r14
lea    0xeb0(%rip),%rsi
mov    %r8d,%edx
mov    %ebp,%ecx
mov    %r12d,%r8d
xor    %eax,%eax
call   1050 <sprintf@plt>
mov    %ebp,%eax
shr    $0x8,%eax
add    %r15d,%eax
movzbl %bpl,%ebp
add    %eax,%ebp
add    %r12d,%ebp
neg    %ebp
test   %r15d,%r15d
je     11b1 <hexfile_out+0x81>
mov    %r15d,%r13d
mov    %rsp,%r12
xor    %r15d,%r15d
movzbl (%rbx,%r15,1),%edx
mov    %r12,%rdi
lea    0xe7c(%rip),%rsi
xor    %eax,%eax
call   1050 <sprintf@plt>
mov    %r14,%rdi
mov    %r12,%rsi
call   1040 <strcat@plt>
movzbl (%rbx,%r15,1),%eax
sub    %eax,%ebp
add    $0x1,%r15
cmp    %r15,%r13
jne    1180 <hexfile_out+0x50>
lea    0xe59(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %ebp,%edx
xor    %eax,%eax
call   1050 <sprintf@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <strcat@plt>
mov    %r14,%rdi
call   1030 <strlen@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [17 x i8] c":%2.2X%4.4X%2.2X\00"
@.str.1 = [6 x i8] c"%2.2X\00"
@.str.2 = [8 x i8] c"%2.2X\0D\0A\00"
define i32 @hexfile_out(i8* %0, i32 %1, i32 %2, i8* %3, i32 %4) {
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca [8 x i8]
store i8* %0, i8** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i8* %3, i8** %9
store i32 %4, i32* %10
store i32 0, i32* %11
store i32 0, i32* %12
%14 = load i8*, i8** %6
%15 = load i32, i32* %10
%16 = load i32, i32* %8
%17 = load i32, i32* %7
%18 = call i32 (i8*, i8*, i32, ...) @sprintf(i8* %14, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %15, i32 %16, i32 %17)
%19 = load i32, i32* %10
%20 = load i32, i32* %11
%21 = sub i32 %20, %19
store i32 %21, i32* %11
%22 = load i32, i32* %8
%23 = lshr i32 %22, 8
%24 = load i32, i32* %11
%25 = sub i32 %24, %23
store i32 %25, i32* %11
%26 = load i32, i32* %8
%27 = and i32 %26, 255
%28 = load i32, i32* %11
%29 = sub i32 %28, %27
store i32 %29, i32* %11
%30 = load i32, i32* %7
%31 = load i32, i32* %11
%32 = sub i32 %31, %30
store i32 %32, i32* %11
store i32 0, i32* %12
br label %33
33: ; preds = %57, %5
%34 = load i32, i32* %12
%35 = load i32, i32* %10
%36 = icmp ult i32 %34, %35
br i1 %36, label %37, label %60
37: ; preds = %33
%38 = getelementptr inbounds [8 x i8], [8 x i8]* %13, i64 0, i64 0
%39 = load i8*, i8** %9
%40 = load i32, i32* %12
%41 = zext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %39, i64 %41
%43 = load i8, i8* %42
%44 = zext i8 %43 to i32
%45 = call i32 (i8*, i8*, i32, ...) @sprintf(i8* %38, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %44)
%46 = load i8*, i8** %6
%47 = getelementptr inbounds [8 x i8], [8 x i8]* %13, i64 0, i64 0
%48 = call i32 @strcat(i8* %46, i8* %47)
%49 = load i8*, i8** %9
%50 = load i32, i32* %12
%51 = zext i32 %50 to i64
%52 = getelementptr inbounds i8, i8* %49, i64 %51
%53 = load i8, i8* %52
%54 = zext i8 %53 to i32
%55 = load i32, i32* %11
%56 = sub i32 %55, %54
store i32 %56, i32* %11
br label %57
57: ; preds = %37
%58 = load i32, i32* %12
%59 = add i32 %58, 1
store i32 %59, i32* %12
br label %33
60: ; preds = %33
%61 = getelementptr inbounds [8 x i8], [8 x i8]* %13, i64 0, i64 0
%62 = load i32, i32* %11
%63 = call i32 (i8*, i8*, i32, ...) @sprintf(i8* %61, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32 %62)
%64 = load i8*, i8** %6
%65 = getelementptr inbounds [8 x i8], [8 x i8]* %13, i64 0, i64 0
%66 = call i32 @strcat(i8* %64, i8* %65)
%67 = load i8*, i8** %6
%68 = call i32 @strlen(i8* %67)
ret i32 %68
}

LLVM IR (-O2):
@.str = [17 x i8] c":%2.2X%4.4X%2.2X\00"
@.str.1 = [6 x i8] c"%2.2X\00"
@.str.2 = [8 x i8] c"%2.2X\0D\0A\00"
define i32 @hexfile_out(i8* %0, i32 %1, i32 %2, i8* nocapture %3, i32 %4) {
%6 = alloca [8 x i8]
%7 = getelementptr inbounds [8 x i8], [8 x i8]* %6, i64 0, i64 0
%8 = tail call i32 (i8*, i8*, i32, ...) @sprintf(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %4, i32 %2, i32 %1)
%9 = lshr i32 %2, 8
%10 = add i32 %9, %4
%11 = and i32 %2, 255
%12 = add i32 %11, %10
%13 = add i32 %12, %1
%14 = sub i32 0, %13
%15 = icmp eq i32 %4, 0
br i1 %15, label %31, label %16
16: ; preds = %5
%17 = zext i32 %4 to i64
br label %18
18: ; preds = %16, %18
%19 = phi i64 [ 0, %16 ], [ %29, %18 ]
%20 = phi i32 [ %14, %16 ], [ %28, %18 ]
%21 = getelementptr inbounds i8, i8* %3, i64 %19
%22 = load i8, i8* %21
%23 = zext i8 %22 to i32
%24 = call i32 (i8*, i8*, i32, ...) @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %23)
%25 = call i32 @strcat(i8* %0, i8* %7)
%26 = load i8, i8* %21
%27 = zext i8 %26 to i32
%28 = sub i32 %20, %27
%29 = add nuw nsw i64 %19, 1
%30 = icmp eq i64 %29, %17
br i1 %30, label %31, label %18
31: ; preds = %18, %5
%32 = phi i32 [ %14, %5 ], [ %28, %18 ]
%33 = call i32 (i8*, i8*, i32, ...) @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32 %32)
%34 = call i32 @strcat(i8* %0, i8* %7)
%35 = call i32 @strlen(i8* %0)
ret i32 %35
}