#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 */
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, unsigned int, ...);
int record(FILE *infile, FILE *outfile) {
  int rectype, nbytes, c;
  unsigned int chksum, loadaddr;
  do {
    rectype = fgetc(infile);
  } while (rectype == 0x00);
  if (rectype != 0x02 && rectype != 0x16)
    return rectype;
  c = fgetc(infile);
  chksum = c;
  loadaddr = c << 8;
  c = fgetc(infile);
  chksum += c;
  loadaddr |= c;
  switch (rectype) {
  case 0x02:
    nbytes = fgetc(infile);
    chksum += nbytes + 3;
    fprintf(outfile, "S1%02X%04X", nbytes + 3, loadaddr);
    while (nbytes--) {
      c = fgetc(infile);
      chksum += c;
      fprintf(outfile, "%02X", c);
    }
    break;
  case 0x16:
    chksum += 3;
    fprintf(outfile, "S903%04X", loadaddr);
    break;
  }
  fprintf(outfile, "%02X\n", ~chksum & 0xFF);
  return rectype;
}


x86 assembly (clang, -O0):
<record>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <fgetc@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1130 <record+0x10>
cmpl   $0x2,-0x1c(%rbp)
je     1165 <record+0x45>
cmpl   $0x16,-0x1c(%rbp)
je     1165 <record+0x45>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1284 <record+0x164>
mov    -0x10(%rbp),%rdi
call   1030 <fgetc@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%eax
shl    $0x8,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <fgetc@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%ecx
mov    -0x28(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%ecx
mov    -0x2c(%rbp),%eax
or     %ecx,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x30(%rbp)
sub    $0x2,%eax
je     11c7 <record+0xa7>
jmp    11b6 <record+0x96>
mov    -0x30(%rbp),%eax
sub    $0x16,%eax
je     1242 <record+0x122>
jmp    1260 <record+0x140>
mov    -0x10(%rbp),%rdi
call   1030 <fgetc@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
add    $0x3,%eax
add    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%edx
add    $0x3,%edx
mov    -0x2c(%rbp),%ecx
lea    0xe0d(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x20(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x20(%rbp)
cmp    $0x0,%eax
je     123d <record+0x11d>
mov    -0x10(%rbp),%rdi
call   1030 <fgetc@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
add    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x24(%rbp),%edx
lea    0xdda(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    11fa <record+0xda>
jmp    1260 <record+0x140>
mov    -0x28(%rbp),%eax
add    $0x3,%eax
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x2c(%rbp),%edx
lea    0xdb7(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%edx
xor    $0xffffffff,%edx
and    $0xff,%edx
lea    0xda2(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<record>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r12,%rdi
call   1030 <fgetc@plt>
test   %eax,%eax
je     1140 <record+0x20>
mov    %eax,%ebp
cmp    $0x16,%eax
je     115c <record+0x3c>
cmp    $0x2,%ebp
jne    1211 <record+0xf1>
mov    %r12,%rdi
call   1030 <fgetc@plt>
mov    %eax,%r15d
mov    %eax,%r13d
shl    $0x8,%r13d
mov    %r12,%rdi
call   1030 <fgetc@plt>
add    %eax,%r15d
or     %eax,%r13d
cmp    $0x16,%ebp
je     11e1 <record+0xc1>
cmp    $0x2,%ebp
jne    11f9 <record+0xd9>
mov    %r12,%rdi
call   1030 <fgetc@plt>
mov    %eax,%ebx
lea    0x3(%rbx),%edx
add    %ebx,%r15d
add    $0x3,%r15d
lea    0xe5f(%rip),%rsi
mov    %r14,%rdi
mov    %r13d,%ecx
xor    %eax,%eax
call   1040 <fprintf@plt>
test   %ebx,%ebx
je     11f9 <record+0xd9>
lea    0xe52(%rip),%r13
nopl   0x0(%rax)
mov    %r12,%rdi
call   1030 <fgetc@plt>
add    %eax,%r15d
mov    %r14,%rdi
mov    %r13,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <fprintf@plt>
add    $0xffffffff,%ebx
jne    11c0 <record+0xa0>
jmp    11f9 <record+0xd9>
add    $0x3,%r15d
lea    0xe24(%rip),%rsi
mov    %r14,%rdi
mov    %r13d,%edx
xor    %eax,%eax
call   1040 <fprintf@plt>
not    %r15d
movzbl %r15b,%edx
lea    0xe12(%rip),%rsi
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [11 x i8] c"S1%02X%04X\00"
@.str.1 = [5 x i8] c"%02X\00"
@.str.2 = [9 x i8] c"S903%04X\00"
@.str.3 = [6 x i8] c"%02X\0A\00"
define i32 @record(i32* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32* %0, i32** %4
store i32* %1, i32** %5
br label %11
11: ; preds = %14, %2
%12 = load i32*, i32** %4
%13 = call i32 @fgetc(i32* %12)
store i32 %13, i32* %6
br label %14
14: ; preds = %11
%15 = load i32, i32* %6
%16 = icmp eq i32 %15, 0
br i1 %16, label %11, label %17
17: ; preds = %14
%18 = load i32, i32* %6
%19 = icmp ne i32 %18, 2
br i1 %19, label %20, label %25
20: ; preds = %17
%21 = load i32, i32* %6
%22 = icmp ne i32 %21, 22
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = load i32, i32* %6
store i32 %24, i32* %3
br label %79
25: ; preds = %20, %17
%26 = load i32*, i32** %4
%27 = call i32 @fgetc(i32* %26)
store i32 %27, i32* %8
%28 = load i32, i32* %8
store i32 %28, i32* %9
%29 = load i32, i32* %8
%30 = shl i32 %29, 8
store i32 %30, i32* %10
%31 = load i32*, i32** %4
%32 = call i32 @fgetc(i32* %31)
store i32 %32, i32* %8
%33 = load i32, i32* %8
%34 = load i32, i32* %9
%35 = add i32 %34, %33
store i32 %35, i32* %9
%36 = load i32, i32* %8
%37 = load i32, i32* %10
%38 = or i32 %37, %36
store i32 %38, i32* %10
%39 = load i32, i32* %6
switch i32 %39, label %72 [
i32 2, label %40
i32 22, label %66
]
40: ; preds = %25
%41 = load i32*, i32** %4
%42 = call i32 @fgetc(i32* %41)
store i32 %42, i32* %7
%43 = load i32, i32* %7
%44 = add nsw i32 %43, 3
%45 = load i32, i32* %9
%46 = add i32 %45, %44
store i32 %46, i32* %9
%47 = load i32*, i32** %5
%48 = load i32, i32* %7
%49 = add nsw i32 %48, 3
%50 = load i32, i32* %10
%51 = call i32 (i32*, i8*, i32, ...) @fprintf(i32* %47, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %49, i32 %50)
br label %52
52: ; preds = %56, %40
%53 = load i32, i32* %7
%54 = add nsw i32 %53, -1
store i32 %54, i32* %7
%55 = icmp ne i32 %53, 0
br i1 %55, label %56, label %65
56: ; preds = %52
%57 = load i32*, i32** %4
%58 = call i32 @fgetc(i32* %57)
store i32 %58, i32* %8
%59 = load i32, i32* %8
%60 = load i32, i32* %9
%61 = add i32 %60, %59
store i32 %61, i32* %9
%62 = load i32*, i32** %5
%63 = load i32, i32* %8
%64 = call i32 (i32*, i8*, i32, ...) @fprintf(i32* %62, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %63)
br label %52
65: ; preds = %52
br label %72
66: ; preds = %25
%67 = load i32, i32* %9
%68 = add i32 %67, 3
store i32 %68, i32* %9
%69 = load i32*, i32** %5
%70 = load i32, i32* %10
%71 = call i32 (i32*, i8*, i32, ...) @fprintf(i32* %69, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %70)
br label %72
72: ; preds = %25, %66, %65
%73 = load i32*, i32** %5
%74 = load i32, i32* %9
%75 = xor i32 %74, -1
%76 = and i32 %75, 255
%77 = call i32 (i32*, i8*, i32, ...) @fprintf(i32* %73, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 %76)
%78 = load i32, i32* %6
store i32 %78, i32* %3
br label %79
79: ; preds = %72, %23
%80 = load i32, i32* %3
ret i32 %80
}

LLVM IR (-O2):
@.str = [11 x i8] c"S1%02X%04X\00"
@.str.1 = [5 x i8] c"%02X\00"
@.str.2 = [9 x i8] c"S903%04X\00"
@.str.3 = [6 x i8] c"%02X\0A\00"
define i32 @record(i32* nocapture %0, i32* nocapture %1) {
br label %3
3: ; preds = %3, %2
%4 = tail call i32 @fgetc(i32* %0)
switch i32 %4, label %33 [
i32 0, label %3
i32 22, label %5
i32 2, label %5
]
5: ; preds = %3, %3
%6 = tail call i32 @fgetc(i32* %0)
%7 = shl i32 %6, 8
%8 = tail call i32 @fgetc(i32* %0)
%9 = add i32 %8, %6
%10 = or i32 %7, %8
switch i32 %4, label %28 [
i32 2, label %11
i32 22, label %25
]
11: ; preds = %5
%12 = tail call i32 @fgetc(i32* %0)
%13 = add nsw i32 %12, 3
%14 = add i32 %13, %9
%15 = tail call i32 (i32*, i8*, i32, ...) @fprintf(i32* %1, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %13, i32 %10)
%16 = icmp eq i32 %12, 0
br i1 %16, label %28, label %17
17: ; preds = %11, %17
%18 = phi i32 [ %22, %17 ], [ %14, %11 ]
%19 = phi i32 [ %20, %17 ], [ %12, %11 ]
%20 = add nsw i32 %19, -1
%21 = tail call i32 @fgetc(i32* %0)
%22 = add i32 %21, %18
%23 = tail call i32 (i32*, i8*, i32, ...) @fprintf(i32* %1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %21)
%24 = icmp eq i32 %20, 0
br i1 %24, label %28, label %17
25: ; preds = %5
%26 = add i32 %9, 3
%27 = tail call i32 (i32*, i8*, i32, ...) @fprintf(i32* %1, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %10)
br label %28
28: ; preds = %17, %11, %5, %25
%29 = phi i32 [ %9, %5 ], [ %26, %25 ], [ %14, %11 ], [ %22, %17 ]
%30 = and i32 %29, 255
%31 = xor i32 %30, 255
%32 = tail call i32 (i32*, i8*, i32, ...) @fprintf(i32* %1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 %31)
br label %33
33: ; preds = %3, %28
ret i32 %4
}