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

/* Variables and functions */
scalar_t__ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int hex_to_int(char);
int /*<<< orphan*/ printf(char *, long);
int /*<<< orphan*/ strncmp(char *, char *, int);
int load_srec(FILE *in, char *buffer) {
  char buf[200];
  int num_bytes, i, p, done = 0;
  long start_addr;
  char value;
  fgets(buf, 200, in);
  while (!done) {
    num_bytes = (16 * hex_to_int(buf[2]) + hex_to_int(buf[3])) - 3;
    start_addr = 0;
    for (p = 4, i = 0; i < 4; i++)
      start_addr = (start_addr << 4) + hex_to_int(buf[p++]);
    printf("%04x\n", start_addr);
    for (i = 0; i < num_bytes; i++) {
      value = 16 * hex_to_int(buf[p]) + hex_to_int(buf[p + 1]);
      p += 2;
      buffer[start_addr + i - 0x8000] = value;
    }
    if (!strncmp("S9", buf, 2)) {
      break;
    }
    fgets(buf, 200, in);
    if (feof(in))
      done = 1;
  }
  return (1);
}


x86 assembly (clang, -O0):
<load_srec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x110,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0xf0(%rbp)
lea    -0xe0(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    $0xc8,%esi
call   1050 <fgets@plt>
cmpl   $0x0,-0xf0(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    119b <load_srec+0x4b>
jmp    1370 <load_srec+0x220>
movsbl -0xde(%rbp),%edi
call   1070 <hex_to_int@plt>
shl    $0x4,%eax
mov    %eax,-0x100(%rbp)
movsbl -0xdd(%rbp),%edi
call   1070 <hex_to_int@plt>
mov    %eax,%ecx
mov    -0x100(%rbp),%eax
add    %ecx,%eax
sub    $0x3,%eax
mov    %eax,-0xe4(%rbp)
movq   $0x0,-0xf8(%rbp)
movl   $0x4,-0xec(%rbp)
movl   $0x0,-0xe8(%rbp)
cmpl   $0x4,-0xe8(%rbp)
jge    1257 <load_srec+0x107>
mov    -0xf8(%rbp),%rax
shl    $0x4,%rax
mov    %rax,-0x108(%rbp)
mov    -0xec(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0xec(%rbp)
cltq
movsbl -0xe0(%rbp,%rax,1),%edi
ff
call   1070 <hex_to_int@plt>
mov    %eax,%ecx
mov    -0x108(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0xf8(%rbp)
mov    -0xe8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xe8(%rbp)
jmp    11ee <load_srec+0x9e>
mov    -0xf8(%rbp),%rsi
lea    0xd9b(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0xe8(%rbp)
mov    -0xe8(%rbp),%eax
cmp    -0xe4(%rbp),%eax
jge    1313 <load_srec+0x1c3>
movslq -0xec(%rbp),%rax
movsbl -0xe0(%rbp,%rax,1),%edi
ff
call   1070 <hex_to_int@plt>
shl    $0x4,%eax
mov    %eax,-0x10c(%rbp)
mov    -0xec(%rbp),%eax
add    $0x1,%eax
cltq
movsbl -0xe0(%rbp,%rax,1),%edi
ff
call   1070 <hex_to_int@plt>
mov    %eax,%ecx
mov    -0x10c(%rbp),%eax
add    %ecx,%eax
mov    %al,-0xf9(%rbp)
mov    -0xec(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0xec(%rbp)
mov    -0xf9(%rbp),%dl
mov    -0x10(%rbp),%rax
mov    -0xf8(%rbp),%rcx
movslq -0xe8(%rbp),%rsi
add    %rsi,%rcx
sub    $0x8000,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0xe8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xe8(%rbp)
jmp    1274 <load_srec+0x124>
lea    -0xe0(%rbp),%rsi
lea    0xce5(%rip),%rdi
mov    $0x2,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    1339 <load_srec+0x1e9>
jmp    1370 <load_srec+0x220>
lea    -0xe0(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    $0xc8,%esi
call   1050 <fgets@plt>
mov    -0x8(%rbp),%rdi
call   1060 <feof@plt>
cmp    $0x0,%rax
je     136b <load_srec+0x21b>
movl   $0x1,-0xf0(%rbp)
jmp    1182 <load_srec+0x32>
mov    $0x1,%eax
add    $0x110,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_srec>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xe8,%rsp
mov    %rsi,%rbx
mov    %rdi,%rdx
lea    0x20(%rsp),%rdi
mov    $0xc8,%esi
mov    %rdx,0x10(%rsp)
call   1050 <fgets@plt>
add    $0xffffffffffff8000,%rbx
mov    %rbx,0x8(%rsp)
nopw   0x0(%rax,%rax,1)
movsbl 0x22(%rsp),%edi
call   1070 <hex_to_int@plt>
mov    %eax,%ebp
shl    $0x4,%ebp
movsbl 0x23(%rsp),%edi
call   1070 <hex_to_int@plt>
mov    %eax,%ebx
movsbl 0x24(%rsp),%edi
call   1070 <hex_to_int@plt>
movslq %eax,%r15
mov    %r15,%r13
shl    $0x4,%r13
movsbl 0x25(%rsp),%edi
call   1070 <hex_to_int@plt>
movslq %eax,%r12
add    %r12,%r13
shl    $0x4,%r13
movsbl 0x26(%rsp),%edi
call   1070 <hex_to_int@plt>
movslq %eax,%r14
add    %r14,%r13
shl    $0x4,%r13
movsbl 0x27(%rsp),%edi
call   1070 <hex_to_int@plt>
cltq
mov    %rax,0x18(%rsp)
add    %rax,%r13
add    %ebp,%ebx
add    $0xfffffffd,%ebx
lea    0xdf9(%rip),%rdi
mov    %r13,%rsi
call   1040 <printf@plt>
test   %ebx,%ebx
jle    1268 <load_srec+0x118>
mov    %ebx,%r13d
shl    $0xc,%r15
shl    $0x8,%r12
add    %r15,%r12
shl    $0x4,%r14
add    %r12,%r14
add    0x18(%rsp),%r14
add    0x8(%rsp),%r14
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movsbl 0x28(%rsp,%rbx,2),%edi
call   1070 <hex_to_int@plt>
mov    %eax,%ebp
shl    $0x4,%ebp
movsbl 0x29(%rsp,%rbx,2),%edi
call   1070 <hex_to_int@plt>
add    %ebp,%eax
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%r13
jne    1240 <load_srec+0xf0>
lea    0xd97(%rip),%rdi
lea    0x20(%rsp),%rbx
mov    %rbx,%rsi
mov    $0x2,%edx
call   1030 <strncmp@plt>
test   %eax,%eax
je     12ab <load_srec+0x15b>
mov    %rbx,%rdi
mov    $0xc8,%esi
mov    0x10(%rsp),%rbx
mov    %rbx,%rdx
call   1050 <fgets@plt>
mov    %rbx,%rdi
call   1060 <feof@plt>
test   %rax,%rax
je     1190 <load_srec+0x40>
mov    $0x1,%eax
add    $0xe8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"%04x\0A\00"
@.str.1 = [3 x i8] c"S9\00"
define i32 @load_srec(i32* %0, i8* %1) {
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca [200 x i8]
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i64
%11 = alloca i8
store i32* %0, i32** %3
store i8* %1, i8** %4
store i32 0, i32* %9
%12 = getelementptr inbounds [200 x i8], [200 x i8]* %5, i64 0, i64 0
%13 = load i32*, i32** %3
%14 = call i32 @fgets(i8* %12, i32 200, i32* %13)
br label %15
15: ; preds = %94, %2
%16 = load i32, i32* %9
%17 = icmp ne i32 %16, 0
%18 = xor i1 %17, true
br i1 %18, label %19, label %95
19: ; preds = %15
%20 = getelementptr inbounds [200 x i8], [200 x i8]* %5, i64 0, i64 2
%21 = load i8, i8* %20
%22 = call i32 @hex_to_int(i8 signext %21)
%23 = mul nsw i32 16, %22
%24 = getelementptr inbounds [200 x i8], [200 x i8]* %5, i64 0, i64 3
%25 = load i8, i8* %24
%26 = call i32 @hex_to_int(i8 signext %25)
%27 = add nsw i32 %23, %26
%28 = sub nsw i32 %27, 3
store i32 %28, i32* %6
store i64 0, i64* %10
store i32 4, i32* %8
store i32 0, i32* %7
br label %29
29: ; preds = %43, %19
%30 = load i32, i32* %7
%31 = icmp slt i32 %30, 4
br i1 %31, label %32, label %46
32: ; preds = %29
%33 = load i64, i64* %10
%34 = shl i64 %33, 4
%35 = load i32, i32* %8
%36 = add nsw i32 %35, 1
store i32 %36, i32* %8
%37 = sext i32 %35 to i64
%38 = getelementptr inbounds [200 x i8], [200 x i8]* %5, i64 0, i64 %37
%39 = load i8, i8* %38
%40 = call i32 @hex_to_int(i8 signext %39)
%41 = sext i32 %40 to i64
%42 = add nsw i64 %34, %41
store i64 %42, i64* %10
br label %43
43: ; preds = %32
%44 = load i32, i32* %7
%45 = add nsw i32 %44, 1
store i32 %45, i32* %7
br label %29
46: ; preds = %29
%47 = load i64, i64* %10
%48 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i64 %47)
store i32 0, i32* %7
br label %49
49: ; preds = %78, %46
%50 = load i32, i32* %7
%51 = load i32, i32* %6
%52 = icmp slt i32 %50, %51
br i1 %52, label %53, label %81
53: ; preds = %49
%54 = load i32, i32* %8
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds [200 x i8], [200 x i8]* %5, i64 0, i64 %55
%57 = load i8, i8* %56
%58 = call i32 @hex_to_int(i8 signext %57)
%59 = mul nsw i32 16, %58
%60 = load i32, i32* %8
%61 = add nsw i32 %60, 1
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds [200 x i8], [200 x i8]* %5, i64 0, i64 %62
%64 = load i8, i8* %63
%65 = call i32 @hex_to_int(i8 signext %64)
%66 = add nsw i32 %59, %65
%67 = trunc i32 %66 to i8
store i8 %67, i8* %11
%68 = load i32, i32* %8
%69 = add nsw i32 %68, 2
store i32 %69, i32* %8
%70 = load i8, i8* %11
%71 = load i8*, i8** %4
%72 = load i64, i64* %10
%73 = load i32, i32* %7
%74 = sext i32 %73 to i64
%75 = add nsw i64 %72, %74
%76 = sub nsw i64 %75, 32768
%77 = getelementptr inbounds i8, i8* %71, i64 %76
store i8 %70, i8* %77
br label %78
78: ; preds = %53
%79 = load i32, i32* %7
%80 = add nsw i32 %79, 1
store i32 %80, i32* %7
br label %49
81: ; preds = %49
%82 = getelementptr inbounds [200 x i8], [200 x i8]* %5, i64 0, i64 0
%83 = call i32 @strncmp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %82, i32 2)
%84 = icmp ne i32 %83, 0
br i1 %84, label %86, label %85
85: ; preds = %81
br label %95
86: ; preds = %81
%87 = getelementptr inbounds [200 x i8], [200 x i8]* %5, i64 0, i64 0
%88 = load i32*, i32** %3
%89 = call i32 @fgets(i8* %87, i32 200, i32* %88)
%90 = load i32*, i32** %3
%91 = call i64 @feof(i32* %90)
%92 = icmp ne i64 %91, 0
br i1 %92, label %93, label %94
93: ; preds = %86
store i32 1, i32* %9
br label %94
94: ; preds = %93, %86
br label %15
95: ; preds = %85, %15
ret i32 1
}

LLVM IR (-O2):
@.str = [6 x i8] c"%04x\0A\00"
@.str.1 = [3 x i8] c"S9\00"
define i32 @load_srec(i32* nocapture %0, i8* nocapture writeonly %1) {
%3 = alloca [200 x i8]
%4 = getelementptr inbounds [200 x i8], [200 x i8]* %3, i64 0, i64 0
%5 = call i32 @fgets(i8* %4, i32 200, i32* %0)
%6 = getelementptr inbounds [200 x i8], [200 x i8]* %3, i64 0, i64 2
%7 = getelementptr inbounds [200 x i8], [200 x i8]* %3, i64 0, i64 3
%8 = getelementptr inbounds [200 x i8], [200 x i8]* %3, i64 0, i64 4
%9 = getelementptr inbounds [200 x i8], [200 x i8]* %3, i64 0, i64 5
%10 = getelementptr inbounds [200 x i8], [200 x i8]* %3, i64 0, i64 6
%11 = getelementptr inbounds [200 x i8], [200 x i8]* %3, i64 0, i64 7
br label %12
12: ; preds = %2, %64
%13 = load i8, i8* %6
%14 = call i32 @hex_to_int(i8 signext %13)
%15 = shl nsw i32 %14, 4
%16 = load i8, i8* %7
%17 = call i32 @hex_to_int(i8 signext %16)
%18 = add i32 %15, -3
%19 = load i8, i8* %8
%20 = call i32 @hex_to_int(i8 signext %19)
%21 = sext i32 %20 to i64
%22 = shl nsw i64 %21, 4
%23 = load i8, i8* %9
%24 = call i32 @hex_to_int(i8 signext %23)
%25 = sext i32 %24 to i64
%26 = add nsw i64 %22, %25
%27 = shl nsw i64 %26, 4
%28 = load i8, i8* %10
%29 = call i32 @hex_to_int(i8 signext %28)
%30 = sext i32 %29 to i64
%31 = add nsw i64 %27, %30
%32 = shl nsw i64 %31, 4
%33 = load i8, i8* %11
%34 = call i32 @hex_to_int(i8 signext %33)
%35 = sext i32 %34 to i64
%36 = add nsw i64 %32, %35
%37 = add i32 %18, %17
%38 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i64 %36)
%39 = icmp sgt i32 %37, 0
br i1 %39, label %40, label %61
40: ; preds = %12
%41 = add i64 %36, -32768
%42 = zext i32 %37 to i64
br label %43
43: ; preds = %40, %43
%44 = phi i64 [ 8, %40 ], [ %56, %43 ]
%45 = phi i64 [ 0, %40 ], [ %59, %43 ]
%46 = getelementptr inbounds [200 x i8], [200 x i8]* %3, i64 0, i64 %44
%47 = load i8, i8* %46
%48 = call i32 @hex_to_int(i8 signext %47)
%49 = shl nsw i32 %48, 4
%50 = or i64 %44, 1
%51 = getelementptr inbounds [200 x i8], [200 x i8]* %3, i64 0, i64 %50
%52 = load i8, i8* %51
%53 = call i32 @hex_to_int(i8 signext %52)
%54 = add nsw i32 %53, %49
%55 = trunc i32 %54 to i8
%56 = add nuw nsw i64 %44, 2
%57 = add nsw i64 %41, %45
%58 = getelementptr inbounds i8, i8* %1, i64 %57
store i8 %55, i8* %58
%59 = add nuw nsw i64 %45, 1
%60 = icmp eq i64 %59, %42
br i1 %60, label %61, label %43
61: ; preds = %43, %12
%62 = call i32 @strncmp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %4, i32 2)
%63 = icmp eq i32 %62, 0
br i1 %63, label %68, label %64
64: ; preds = %61
%65 = call i32 @fgets(i8* %4, i32 200, i32* %0)
%66 = call i64 @feof(i32* %0)
%67 = icmp eq i64 %66, 0
br i1 %67, label %12, label %68
68: ; preds = %61, %64
ret i32 1
}