#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 */

/* Variables and functions */
unsigned char get_byte();
unsigned char getchr();
int /*<<< orphan*/ print_string(char *);
int /*<<< orphan*/ putchr(char);
void stub1();
int main(int argc, char *argv[]) {
  unsigned char record_length;
  unsigned int address;
  unsigned char record_type;
  unsigned char check_sum;
  unsigned char i;
  unsigned char c;
  for (;;) {
    print_string("\r\nLOAD > ");
    for (;;) {
      while ((c = getchr()) != ':')
        ;
      check_sum = 0;
      c = get_byte();
      check_sum += c;
      record_length = c;
      c = get_byte();
      check_sum += c;
      address = c << 8;
      c = get_byte();
      check_sum += c;
      address |= c;
      c = get_byte();
      check_sum += c;
      record_type = c;
      for (i = 0; i < record_length; i++) {
        c = get_byte();
        ((char *)address)[i] = c;
        check_sum += c;
      }
      c = get_byte();
      check_sum += c;
      if (check_sum)
        break;
      putchr('.');
      if (record_type == 1) {
        print_string("\r\nDONE.\r\n");
        ((void (*)())address)();
      }
    }
    print_string("\r\nCHECKSUM ERROR.");
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xea3(%rip),%rdi
call   1040 <print_string@plt>
jmp    1167 <main+0x27>
mov    $0x0,%al
call   1030 <getchr@plt>
mov    %al,-0x1c(%rbp)
movzbl %al,%eax
cmp    $0x3a,%eax
je     1182 <main+0x42>
jmp    1167 <main+0x27>
movb   $0x0,-0x1a(%rbp)
mov    $0x0,%al
call   1050 <get_byte@plt>
mov    %al,-0x1c(%rbp)
movzbl -0x1c(%rbp),%ecx
movzbl -0x1a(%rbp),%eax
add    %ecx,%eax
mov    %al,-0x1a(%rbp)
mov    -0x1c(%rbp),%al
mov    %al,-0x11(%rbp)
mov    $0x0,%al
call   1050 <get_byte@plt>
mov    %al,-0x1c(%rbp)
movzbl -0x1c(%rbp),%ecx
movzbl -0x1a(%rbp),%eax
add    %ecx,%eax
mov    %al,-0x1a(%rbp)
movzbl -0x1c(%rbp),%eax
shl    $0x8,%eax
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1050 <get_byte@plt>
mov    %al,-0x1c(%rbp)
movzbl -0x1c(%rbp),%ecx
movzbl -0x1a(%rbp),%eax
add    %ecx,%eax
mov    %al,-0x1a(%rbp)
movzbl -0x1c(%rbp),%eax
or     -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1050 <get_byte@plt>
mov    %al,-0x1c(%rbp)
movzbl -0x1c(%rbp),%ecx
movzbl -0x1a(%rbp),%eax
add    %ecx,%eax
mov    %al,-0x1a(%rbp)
mov    -0x1c(%rbp),%al
mov    %al,-0x19(%rbp)
movb   $0x0,-0x1b(%rbp)
movzbl -0x1b(%rbp),%eax
movzbl -0x11(%rbp),%ecx
cmp    %ecx,%eax
jge    1247 <main+0x107>
mov    $0x0,%al
call   1050 <get_byte@plt>
mov    %al,-0x1c(%rbp)
mov    -0x1c(%rbp),%dl
mov    -0x18(%rbp),%eax
movzbl -0x1b(%rbp),%ecx
mov    %dl,(%rax,%rcx,1)
movzbl -0x1c(%rbp),%ecx
movzbl -0x1a(%rbp),%eax
add    %ecx,%eax
mov    %al,-0x1a(%rbp)
mov    -0x1b(%rbp),%al
add    $0x1,%al
mov    %al,-0x1b(%rbp)
jmp    1206 <main+0xc6>
mov    $0x0,%al
call   1050 <get_byte@plt>
mov    %al,-0x1c(%rbp)
movzbl -0x1c(%rbp),%ecx
movzbl -0x1a(%rbp),%eax
add    %ecx,%eax
mov    %al,-0x1a(%rbp)
cmpb   $0x0,-0x1a(%rbp)
je     126d <main+0x12d>
jmp    129e <main+0x15e>
mov    $0x2e,%edi
call   1060 <putchr@plt>
movzbl -0x19(%rbp),%eax
cmp    $0x1,%eax
jne    1299 <main+0x159>
lea    0xd7f(%rip),%rdi
call   1040 <print_string@plt>
mov    -0x18(%rbp),%eax
mov    %eax,%ecx
mov    $0x0,%al
call   *%rcx
jmp    1162 <main+0x22>
lea    0xd6f(%rip),%rdi
call   1040 <print_string@plt>
jmp    1156 <main+0x16>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xeb8(%rip),%r12
jmp    116c <main+0x2c>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
lea    0xead(%rip),%rdi
call   1040 <print_string@plt>
lea    0xe8d(%rip),%rdi
call   1040 <print_string@plt>
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1030 <getchr@plt>
cmp    $0x3a,%al
jne    1180 <main+0x40>
xor    %eax,%eax
call   1050 <get_byte@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1050 <get_byte@plt>
mov    %eax,%ebp
lea    (%rbx,%rbp,1),%r14d
xor    %eax,%eax
call   1050 <get_byte@plt>
add    %al,%r14b
movzbl %bpl,%ecx
shl    $0x8,%rcx
movzbl %al,%ebp
or     %rcx,%rbp
xor    %eax,%eax
call   1050 <get_byte@plt>
mov    %eax,%r13d
add    %al,%r14b
test   %bl,%bl
je     11e8 <main+0xa8>
movzbl %bl,%ebx
xor    %r15d,%r15d
xor    %eax,%eax
call   1050 <get_byte@plt>
mov    %al,0x0(%rbp,%r15,1)
add    %al,%r14b
add    $0x1,%r15
cmp    %r15,%rbx
jne    11d0 <main+0x90>
xor    %eax,%eax
call   1050 <get_byte@plt>
add    %r14b,%al
jne    1160 <main+0x20>
mov    $0x2e,%edi
call   1060 <putchr@plt>
cmp    $0x1,%r13b
jne    1180 <main+0x40>
mov    %r12,%rdi
call   1040 <print_string@plt>
mov    %ebp,%ecx
xor    %eax,%eax
call   *%rcx
jmp    1180 <main+0x40>

LLVM IR (-O0):
@.str = [10 x i8] c"\0D\0ALOAD > \00"
@.str.1 = [10 x i8] c"\0D\0ADONE.\0D\0A\00"
@.str.2 = [18 x i8] c"\0D\0ACHECKSUM ERROR.\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8
%7 = alloca i32
%8 = alloca i8
%9 = alloca i8
%10 = alloca i8
%11 = alloca i8
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
br label %12
12: ; preds = %104, %2
%13 = call i32 @print_string(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %103, %12
br label %15
15: ; preds = %19, %14
%16 = call zeroext i8 (...) @getchr()
store i8 %16, i8* %11
%17 = zext i8 %16 to i32
%18 = icmp ne i32 %17, 58
br i1 %18, label %19, label %20
19: ; preds = %15
br label %15
20: ; preds = %15
store i8 0, i8* %9
%21 = call zeroext i8 (...) @get_byte()
store i8 %21, i8* %11
%22 = load i8, i8* %11
%23 = zext i8 %22 to i32
%24 = load i8, i8* %9
%25 = zext i8 %24 to i32
%26 = add nsw i32 %25, %23
%27 = trunc i32 %26 to i8
store i8 %27, i8* %9
%28 = load i8, i8* %11
store i8 %28, i8* %6
%29 = call zeroext i8 (...) @get_byte()
store i8 %29, i8* %11
%30 = load i8, i8* %11
%31 = zext i8 %30 to i32
%32 = load i8, i8* %9
%33 = zext i8 %32 to i32
%34 = add nsw i32 %33, %31
%35 = trunc i32 %34 to i8
store i8 %35, i8* %9
%36 = load i8, i8* %11
%37 = zext i8 %36 to i32
%38 = shl i32 %37, 8
store i32 %38, i32* %7
%39 = call zeroext i8 (...) @get_byte()
store i8 %39, i8* %11
%40 = load i8, i8* %11
%41 = zext i8 %40 to i32
%42 = load i8, i8* %9
%43 = zext i8 %42 to i32
%44 = add nsw i32 %43, %41
%45 = trunc i32 %44 to i8
store i8 %45, i8* %9
%46 = load i8, i8* %11
%47 = zext i8 %46 to i32
%48 = load i32, i32* %7
%49 = or i32 %48, %47
store i32 %49, i32* %7
%50 = call zeroext i8 (...) @get_byte()
store i8 %50, i8* %11
%51 = load i8, i8* %11
%52 = zext i8 %51 to i32
%53 = load i8, i8* %9
%54 = zext i8 %53 to i32
%55 = add nsw i32 %54, %52
%56 = trunc i32 %55 to i8
store i8 %56, i8* %9
%57 = load i8, i8* %11
store i8 %57, i8* %8
store i8 0, i8* %10
br label %58
58: ; preds = %79, %20
%59 = load i8, i8* %10
%60 = zext i8 %59 to i32
%61 = load i8, i8* %6
%62 = zext i8 %61 to i32
%63 = icmp slt i32 %60, %62
br i1 %63, label %64, label %82
64: ; preds = %58
%65 = call zeroext i8 (...) @get_byte()
store i8 %65, i8* %11
%66 = load i8, i8* %11
%67 = load i32, i32* %7
%68 = zext i32 %67 to i64
%69 = inttoptr i64 %68 to i8*
%70 = load i8, i8* %10
%71 = zext i8 %70 to i64
%72 = getelementptr inbounds i8, i8* %69, i64 %71
store i8 %66, i8* %72
%73 = load i8, i8* %11
%74 = zext i8 %73 to i32
%75 = load i8, i8* %9
%76 = zext i8 %75 to i32
%77 = add nsw i32 %76, %74
%78 = trunc i32 %77 to i8
store i8 %78, i8* %9
br label %79
79: ; preds = %64
%80 = load i8, i8* %10
%81 = add i8 %80, 1
store i8 %81, i8* %10
br label %58
82: ; preds = %58
%83 = call zeroext i8 (...) @get_byte()
store i8 %83, i8* %11
%84 = load i8, i8* %11
%85 = zext i8 %84 to i32
%86 = load i8, i8* %9
%87 = zext i8 %86 to i32
%88 = add nsw i32 %87, %85
%89 = trunc i32 %88 to i8
store i8 %89, i8* %9
%90 = load i8, i8* %9
%91 = icmp ne i8 %90, 0
br i1 %91, label %92, label %93
92: ; preds = %82
br label %104
93: ; preds = %82
%94 = call i32 @putchr(i8 signext 46)
%95 = load i8, i8* %8
%96 = zext i8 %95 to i32
%97 = icmp eq i32 %96, 1
br i1 %97, label %98, label %103
98: ; preds = %93
%99 = call i32 @print_string(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%100 = load i32, i32* %7
%101 = zext i32 %100 to i64
%102 = inttoptr i64 %101 to void (...)*
br label %103
103: ; preds = %98, %93
br label %14
104: ; preds = %92
%105 = call i32 @print_string(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
br label %12
}

LLVM IR (-O2):
@.str = [10 x i8] c"\0D\0ALOAD > \00"
@.str.1 = [10 x i8] c"\0D\0ADONE.\0D\0A\00"
@.str.2 = [18 x i8] c"\0D\0ACHECKSUM ERROR.\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
br label %3
3: ; preds = %46, %2
%4 = tail call i32 @print_string(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %5
5: ; preds = %8, %3
%6 = tail call zeroext i8 (...) @getchr()
%7 = icmp eq i8 %6, 58
br i1 %7, label %9, label %8
8: ; preds = %5, %42, %39
br label %5
9: ; preds = %5
%10 = tail call zeroext i8 (...) @get_byte()
%11 = tail call zeroext i8 (...) @get_byte()
%12 = zext i8 %11 to i32
%13 = add i8 %11, %10
%14 = shl nuw nsw i32 %12, 8
%15 = tail call zeroext i8 (...) @get_byte()
%16 = zext i8 %15 to i32
%17 = add i8 %13, %15
%18 = or i32 %14, %16
%19 = tail call zeroext i8 (...) @get_byte()
%20 = add i8 %17, %19
%21 = icmp eq i8 %10, 0
br i1 %21, label %34, label %22
22: ; preds = %9
%23 = zext i32 %18 to i64
%24 = inttoptr i64 %23 to i8*
%25 = zext i8 %10 to i64
br label %26
26: ; preds = %22, %26
%27 = phi i64 [ 0, %22 ], [ %32, %26 ]
%28 = phi i8 [ %20, %22 ], [ %31, %26 ]
%29 = tail call zeroext i8 (...) @get_byte()
%30 = getelementptr inbounds i8, i8* %24, i64 %27
store i8 %29, i8* %30
%31 = add i8 %29, %28
%32 = add nuw nsw i64 %27, 1
%33 = icmp eq i64 %32, %25
br i1 %33, label %34, label %26
34: ; preds = %26, %9
%35 = phi i8 [ %20, %9 ], [ %31, %26 ]
%36 = tail call zeroext i8 (...) @get_byte()
%37 = sub i8 0, %36
%38 = icmp eq i8 %35, %37
br i1 %38, label %39, label %46
39: ; preds = %34
%40 = tail call i32 @putchr(i8 signext 46)
%41 = icmp eq i8 %19, 1
br i1 %41, label %42, label %8
42: ; preds = %39
%43 = tail call i32 @print_string(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%44 = zext i32 %18 to i64
%45 = inttoptr i64 %44 to void (...)*
tail call void (...) %45()
br label %8
46: ; preds = %34
%47 = tail call i32 @print_string(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
br label %3
}