#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__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fwrite(unsigned int *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sscanf(char *, char *, unsigned int *, unsigned int *,
                          unsigned int *, unsigned int *, unsigned int *,
                          unsigned int *, unsigned int *, unsigned int *,
                          unsigned int *);
void save_rom(char *infile, char *outfile) {
  FILE *fdump;
  FILE *fbin;
  char ptr[100];
  unsigned int data[10];
  unsigned int sum;
  unsigned int calc_sum = 0;
  int lineno = 0;
  fdump = fopen(infile, "r");
  if (fdump == NULL) {
    printf("   Can't open '%s' for reading.\n", infile);
    return;
  }
  fbin = fopen(outfile, "w");
  if (fbin == NULL) {
    printf("   Can't open '%s' for writing.\n", outfile);
    return;
  }
  while (fgets(ptr, 99, fdump)) {
    if (!sscanf(ptr, "%x,%x,%x,%x,%x,%x,%x,%x,:%x:", &data[0], &data[1],
                &data[2], &data[3], &data[4], &data[5], &data[6], &data[7],
                &sum)) {
      printf("Failed read line %d\n", lineno);
    }
    lineno++;
    for (int j = 0; j < 8; j++) {
      calc_sum = calc_sum ^ data[j];
      fwrite(&data[j], sizeof(unsigned int), 1, fbin);
    }
    if (calc_sum != sum) {
      printf("Check error line %d,%08X:%08X\n", lineno, calc_sum, sum);
    }
    calc_sum = 0;
  }
}


x86 assembly (clang, -O0):
<save_rom>:
push   %rbp
mov    %rsp,%rbp
push   %r14
push   %rbx
sub    $0x100,%rsp
mov    %rdi,-0x18(%rbp)
mov    %rsi,-0x20(%rbp)
movl   $0x0,-0xd8(%rbp)
movl   $0x0,-0xdc(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe7b(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11b0 <save_rom+0x60>
mov    -0x18(%rbp),%rsi
lea    0xe5e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    136a <save_rom+0x21a>
mov    -0x20(%rbp),%rdi
lea    0xe68(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11e6 <save_rom+0x96>
mov    -0x20(%rbp),%rsi
lea    0xe4b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    136a <save_rom+0x21a>
jmp    11eb <save_rom+0x9b>
lea    -0xa0(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    $0x63,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     136a <save_rom+0x21a>
lea    -0xa0(%rbp),%rdi
lea    -0xd0(%rbp),%rdx
lea    -0xd0(%rbp),%rcx
add    $0x4,%rcx
lea    -0xd0(%rbp),%r8
add    $0x8,%r8
lea    -0xd0(%rbp),%r9
add    $0xc,%r9
lea    -0xd0(%rbp),%r14
add    $0x10,%r14
lea    -0xd0(%rbp),%rbx
add    $0x14,%rbx
lea    -0xd0(%rbp),%r11
add    $0x18,%r11
lea    -0xd0(%rbp),%r10
add    $0x1c,%r10
lea    0xdda(%rip),%rsi
lea    -0xd4(%rbp),%rax
mov    %r14,(%rsp)
mov    %rbx,0x8(%rsp)
mov    %r11,0x10(%rsp)
mov    %r10,0x18(%rsp)
mov    %rax,0x20(%rsp)
call   1050 <sscanf@plt>
cmp    $0x0,%eax
jne    12ad <save_rom+0x15d>
mov    -0xdc(%rbp),%esi
lea    0xdbd(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0xdc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xdc(%rbp)
movl   $0x0,-0xe0(%rbp)
cmpl   $0x8,-0xe0(%rbp)
jge    1329 <save_rom+0x1d9>
mov    -0xd8(%rbp),%eax
movslq -0xe0(%rbp),%rcx
xor    -0xd0(%rbp,%rcx,4),%eax
mov    %eax,-0xd8(%rbp)
movslq -0xe0(%rbp),%rax
lea    -0xd0(%rbp),%rdi
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x30(%rbp),%rcx
mov    $0x4,%esi
mov    $0x1,%edx
call   1070 <fwrite@plt>
mov    -0xe0(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xe0(%rbp)
jmp    12c6 <save_rom+0x176>
mov    -0xd8(%rbp),%eax
cmp    -0xd4(%rbp),%eax
je     135b <save_rom+0x20b>
mov    -0xdc(%rbp),%esi
mov    -0xd8(%rbp),%edx
mov    -0xd4(%rbp),%ecx
lea    0xd24(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0xd8(%rbp)
jmp    11eb <save_rom+0x9b>
add    $0x100,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

x86 assembly (clang, -O2):
<save_rom>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xe8,%rsp
mov    %rsi,%rbp
mov    %rdi,%rbx
lea    0xe92(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,0x60(%rsp)
test   %rax,%rax
je     1419 <save_rom+0x2c9>
lea    0xe9b(%rip),%rsi
mov    %rbp,%rdi
call   1060 <fopen@plt>
mov    %rax,0x68(%rsp)
test   %rax,%rax
je     1425 <save_rom+0x2d5>
lea    0x80(%rsp),%rdi
mov    $0x63,%esi
mov    0x60(%rsp),%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     1407 <save_rom+0x2b7>
lea    0x4(%rsp),%r13
lea    0x8(%rsp),%r14
lea    0xc(%rsp),%r15
lea    0x10(%rsp),%r10
lea    0x14(%rsp),%r11
lea    0x18(%rsp),%rbx
mov    $0x1,%ecx
mov    %rcx,0x30(%rsp)
lea    0x1c(%rsp),%rax
lea    0x80(%rsp),%rbp
mov    %rsp,%r12
jmp    1251 <save_rom+0x101>
nopl   0x0(%rax,%rax,1)
lea    0x80(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x63,%esi
mov    0x60(%rsp),%rdx
call   1040 <fgets@plt>
add    $0x1,%ebx
mov    %rbx,0x30(%rsp)
test   %rax,%rax
lea    0x4(%rsp),%r13
lea    0x8(%rsp),%r14
lea    0xc(%rsp),%r15
lea    0x10(%rsp),%r10
lea    0x14(%rsp),%r11
lea    0x18(%rsp),%rbx
lea    0x1c(%rsp),%rax
mov    %rsp,%r12
je     1407 <save_rom+0x2b7>
sub    $0x8,%rsp
mov    %rbp,%rdi
lea    0xde7(%rip),%rsi
mov    %r12,%rdx
mov    %r13,%rcx
mov    %r14,%r8
mov    %r15,%r9
lea    0x44(%rsp),%rbp
push   %rbp
push   %rax
push   %rbx
push   %r11
push   %r10
mov    %rbx,%rbp
call   1050 <sscanf@plt>
add    $0x30,%rsp
test   %eax,%eax
jne    129d <save_rom+0x14d>
mov    0x30(%rsp),%rax
lea    -0x1(%rax),%esi
lea    0xdcd(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
movss  (%rsp),%xmm0
movaps %xmm0,0x50(%rsp)
mov    %r12,%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    0x68(%rsp),%rbx
mov    %rbx,%rcx
call   1070 <fwrite@plt>
movss  0x4(%rsp),%xmm0
unpcklps 0x50(%rsp),%xmm0
movaps %xmm0,0x50(%rsp)
mov    %r13,%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1070 <fwrite@plt>
movss  0x8(%rsp),%xmm0
movaps %xmm0,0x40(%rsp)
mov    %r14,%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1070 <fwrite@plt>
movss  0xc(%rsp),%xmm0
unpcklps 0x40(%rsp),%xmm0
unpcklpd 0x50(%rsp),%xmm0
movaps %xmm0,0x50(%rsp)
mov    %r15,%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1070 <fwrite@plt>
movss  0x10(%rsp),%xmm0
movaps %xmm0,0x40(%rsp)
lea    0x10(%rsp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1070 <fwrite@plt>
movss  0x14(%rsp),%xmm0
unpcklps 0x40(%rsp),%xmm0
movaps %xmm0,0x40(%rsp)
lea    0x14(%rsp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1070 <fwrite@plt>
movss  0x18(%rsp),%xmm0
movaps %xmm0,0x70(%rsp)
mov    %rbp,%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1070 <fwrite@plt>
movd   0x1c(%rsp),%xmm0
punpckldq 0x70(%rsp),%xmm0
punpcklqdq 0x40(%rsp),%xmm0
pxor   0x50(%rsp),%xmm0
pshufd $0xee,%xmm0,%xmm1
pxor   %xmm0,%xmm1
pshufd $0x55,%xmm1,%xmm0
pxor   %xmm1,%xmm0
movd   %xmm0,%ebp
lea    0x1c(%rsp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1070 <fwrite@plt>
mov    0x3c(%rsp),%ecx
cmp    %ecx,%ebp
mov    0x30(%rsp),%rbx
je     1200 <save_rom+0xb0>
lea    0xc81(%rip),%rdi
mov    %ebx,%esi
mov    %ebp,%edx
xor    %eax,%eax
call   1030 <printf@plt>
jmp    1200 <save_rom+0xb0>
add    $0xe8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xbe2(%rip),%rdi
mov    %rbx,%rsi
jmp    142f <save_rom+0x2df>
lea    0xbf9(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
add    $0xe8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <printf@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [33 x i8] c" Can't open '%s' for reading.\0A\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [33 x i8] c" Can't open '%s' for writing.\0A\00"
@.str.4 = [29 x i8] c"%x,%x,%x,%x,%x,%x,%x,%x,:%x:\00"
@.str.5 = [21 x i8] c"Failed read line %d\0A\00"
@.str.6 = [31 x i8] c"Check error line %d,%08X:%08X\0A\00"
define void @save_rom(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca [100 x i8]
%8 = alloca [10 x i32]
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
store i32 0, i32* %10
store i32 0, i32* %11
%13 = load i8*, i8** %3
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %14, i32** %5
%15 = load i32*, i32** %5
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %20
17: ; preds = %2
%18 = load i8*, i8** %3
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %18)
br label %80
20: ; preds = %2
%21 = load i8*, i8** %4
%22 = call i32* @fopen(i8* %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %22, i32** %6
%23 = load i32*, i32** %6
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %28
25: ; preds = %20
%26 = load i8*, i8** %4
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i8* %26)
br label %80
28: ; preds = %20
br label %29
29: ; preds = %79, %28
%30 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%31 = load i32*, i32** %5
%32 = call i64 @fgets(i8* %30, i32 99, i32* %31)
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %80
34: ; preds = %29
%35 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%36 = getelementptr inbounds [10 x i32], [10 x i32]* %8, i64 0, i64 0
%37 = getelementptr inbounds [10 x i32], [10 x i32]* %8, i64 0, i64 1
%38 = getelementptr inbounds [10 x i32], [10 x i32]* %8, i64 0, i64 2
%39 = getelementptr inbounds [10 x i32], [10 x i32]* %8, i64 0, i64 3
%40 = getelementptr inbounds [10 x i32], [10 x i32]* %8, i64 0, i64 4
%41 = getelementptr inbounds [10 x i32], [10 x i32]* %8, i64 0, i64 5
%42 = getelementptr inbounds [10 x i32], [10 x i32]* %8, i64 0, i64 6
%43 = getelementptr inbounds [10 x i32], [10 x i32]* %8, i64 0, i64 7
%44 = call i32 @sscanf(i8* %35, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i32* %36, i32* %37, i32* %38, i32* %39, i32* %40, i32* %41, i32* %42, i32* %43, i32* %9)
%45 = icmp ne i32 %44, 0
br i1 %45, label %49, label %46
46: ; preds = %34
%47 = load i32, i32* %11
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i32 %47)
br label %49
49: ; preds = %46, %34
%50 = load i32, i32* %11
%51 = add nsw i32 %50, 1
store i32 %51, i32* %11
store i32 0, i32* %12
br label %52
52: ; preds = %67, %49
%53 = load i32, i32* %12
%54 = icmp slt i32 %53, 8
br i1 %54, label %55, label %70
55: ; preds = %52
%56 = load i32, i32* %10
%57 = load i32, i32* %12
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds [10 x i32], [10 x i32]* %8, i64 0, i64 %58
%60 = load i32, i32* %59
%61 = xor i32 %56, %60
store i32 %61, i32* %10
%62 = load i32, i32* %12
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds [10 x i32], [10 x i32]* %8, i64 0, i64 %63
%65 = load i32*, i32** %6
%66 = call i32 @fwrite(i32* %64, i32 4, i32 1, i32* %65)
br label %67
67: ; preds = %55
%68 = load i32, i32* %12
%69 = add nsw i32 %68, 1
store i32 %69, i32* %12
br label %52
70: ; preds = %52
%71 = load i32, i32* %10
%72 = load i32, i32* %9
%73 = icmp ne i32 %71, %72
br i1 %73, label %74, label %79
74: ; preds = %70
%75 = load i32, i32* %11
%76 = load i32, i32* %10
%77 = load i32, i32* %9
%78 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.6, i64 0, i64 0), i32 %75, i32 %76, i32 %77)
br label %79
79: ; preds = %74, %70
store i32 0, i32* %10
br label %29
80: ; preds = %17, %25, %29
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [33 x i8] c" Can't open '%s' for reading.\0A\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [33 x i8] c" Can't open '%s' for writing.\0A\00"
@.str.4 = [29 x i8] c"%x,%x,%x,%x,%x,%x,%x,%x,:%x:\00"
@.str.5 = [21 x i8] c"Failed read line %d\0A\00"
@.str.6 = [31 x i8] c"Check error line %d,%08X:%08X\0A\00"
define void @save_rom(i8* %0, i8* %1) {
%3 = alloca [100 x i8]
%4 = alloca [10 x i32]
%5 = alloca i32
%6 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%7 = bitcast [10 x i32]* %4 to i8*
%8 = bitcast i32* %5 to i8*
%9 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %70
13: ; preds = %2
%14 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%15 = icmp eq i32* %14, null
br i1 %15, label %28, label %16
16: ; preds = %13
%17 = call i64 @fgets(i8* %6, i32 99, i32* %9)
%18 = icmp eq i64 %17, 0
br i1 %18, label %70, label %19
19: ; preds = %16
%20 = getelementptr inbounds [10 x i32], [10 x i32]* %4, i64 0, i64 0
%21 = getelementptr inbounds [10 x i32], [10 x i32]* %4, i64 0, i64 1
%22 = getelementptr inbounds [10 x i32], [10 x i32]* %4, i64 0, i64 2
%23 = getelementptr inbounds [10 x i32], [10 x i32]* %4, i64 0, i64 3
%24 = getelementptr inbounds [10 x i32], [10 x i32]* %4, i64 0, i64 4
%25 = getelementptr inbounds [10 x i32], [10 x i32]* %4, i64 0, i64 5
%26 = getelementptr inbounds [10 x i32], [10 x i32]* %4, i64 0, i64 6
%27 = getelementptr inbounds [10 x i32], [10 x i32]* %4, i64 0, i64 7
br label %30
28: ; preds = %13
%29 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i8* %1)
br label %70
30: ; preds = %19, %67
%31 = phi i32 [ 0, %19 ], [ %62, %67 ]
%32 = call i32 @sscanf(i8* %6, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i32* %20, i32* %21, i32* %22, i32* %23, i32* %24, i32* %25, i32* %26, i32* %27, i32* %5)
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %30
%35 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i32 %31)
br label %36
36: ; preds = %34, %30
%37 = load i32, i32* %20
%38 = call i32 @fwrite(i32* %20, i32 4, i32 1, i32* %14)
%39 = load i32, i32* %21
%40 = call i32 @fwrite(i32* %21, i32 4, i32 1, i32* %14)
%41 = load i32, i32* %22
%42 = call i32 @fwrite(i32* %22, i32 4, i32 1, i32* %14)
%43 = load i32, i32* %23
%44 = call i32 @fwrite(i32* %23, i32 4, i32 1, i32* %14)
%45 = load i32, i32* %24
%46 = call i32 @fwrite(i32* %24, i32 4, i32 1, i32* %14)
%47 = load i32, i32* %25
%48 = call i32 @fwrite(i32* %25, i32 4, i32 1, i32* %14)
%49 = load i32, i32* %26
%50 = call i32 @fwrite(i32* %26, i32 4, i32 1, i32* %14)
%51 = load i32, i32* %27
%52 = insertelement <8 x i32> poison, i32 %51, i64 0
%53 = insertelement <8 x i32> %52, i32 %49, i64 1
%54 = insertelement <8 x i32> %53, i32 %47, i64 2
%55 = insertelement <8 x i32> %54, i32 %45, i64 3
%56 = insertelement <8 x i32> %55, i32 %43, i64 4
%57 = insertelement <8 x i32> %56, i32 %41, i64 5
%58 = insertelement <8 x i32> %57, i32 %39, i64 6
%59 = insertelement <8 x i32> %58, i32 %37, i64 7
%60 = call i32 @llvm.vector.reduce.xor.v8i32(<8 x i32> %59)
%61 = call i32 @fwrite(i32* %27, i32 4, i32 1, i32* %14)
%62 = add nuw nsw i32 %31, 1
%63 = load i32, i32* %5
%64 = icmp eq i32 %60, %63
br i1 %64, label %67, label %65
65: ; preds = %36
%66 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.6, i64 0, i64 0), i32 %62, i32 %60, i32 %63)
br label %67
67: ; preds = %65, %36
%68 = call i64 @fgets(i8* %6, i32 99, i32* %9)
%69 = icmp eq i64 %68, 0
br i1 %69, label %70, label %30
70: ; preds = %67, %16, %28, %11
ret void
}