#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 /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
scalar_t__ isdigit(char);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ put_all_segs(char *);
int /*<<< orphan*/ put_seg(int, char *);
int /*<<< orphan*/ save_patch_rule(int /*<<< orphan*/ *, char *);
int /*<<< orphan*/ sscanf(char *, char *, int *);
int /*<<< orphan*/ stderr;
void split_bihx(char *name) {
  int n;
  char buf[256];
  FILE *fp = fopen(name, "r");
  FILE *fr;
  if (fp == NULL) {
    perror(name);
    exit(1);
  }
  fr = fopen("relocs.dat", "w");
  if (fr == NULL) {
    perror("relocs.dat");
    exit(1);
  }
  while (fgets(buf, 256, fp)) {
    if (isdigit(*buf)) {
      sscanf(buf, "%x", &n);
      put_seg(n, buf + 2);
    } else if (*buf == ':')
      put_all_segs(buf);
    else if (*buf == ';')
      continue;
    else if (*buf == 'B')
      save_patch_rule(fr, buf);
    else
      fprintf(stderr, "%s: invalid bihx line: %s.\n", name, buf);
  }
  fclose(fp);
  fclose(fr);
}


x86 assembly (clang, -O0):
<split_bihx>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe36(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x118(%rbp)
cmpq   $0x0,-0x118(%rbp)
jne    11f7 <split_bihx+0x47>
mov    -0x8(%rbp),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xe04(%rip),%rdi
lea    0xe08(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x120(%rbp)
cmpq   $0x0,-0x120(%rbp)
jne    1235 <split_bihx+0x85>
lea    0xddc(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
jmp    123a <split_bihx+0x8a>
lea    -0x110(%rbp),%rdi
mov    -0x118(%rbp),%rdx
mov    $0x100,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     1333 <split_bihx+0x183>
movsbl -0x110(%rbp),%edi
call   1030 <isdigit@plt>
cmp    $0x0,%rax
je     12a1 <split_bihx+0xf1>
lea    -0x110(%rbp),%rdi
lea    0xd8f(%rip),%rsi
lea    -0xc(%rbp),%rdx
call   1070 <sscanf@plt>
mov    -0xc(%rbp),%edi
lea    -0x110(%rbp),%rsi
add    $0x2,%rsi
call   10a0 <put_seg@plt>
jmp    132e <split_bihx+0x17e>
movsbl -0x110(%rbp),%eax
cmp    $0x3a,%eax
jne    12c2 <split_bihx+0x112>
lea    -0x110(%rbp),%rdi
call   10c0 <put_all_segs@plt>
jmp    1329 <split_bihx+0x179>
movsbl -0x110(%rbp),%eax
cmp    $0x3b,%eax
jne    12d7 <split_bihx+0x127>
jmp    123a <split_bihx+0x8a>
movsbl -0x110(%rbp),%eax
cmp    $0x42,%eax
jne    12ff <split_bihx+0x14f>
mov    -0x120(%rbp),%rdi
lea    -0x110(%rbp),%rsi
call   10d0 <save_patch_rule@plt>
jmp    131f <split_bihx+0x16f>
mov    0x2cda(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    -0x110(%rbp),%rcx
lea    0xcf8(%rip),%rsi
call   1060 <fprintf@plt>
jmp    1324 <split_bihx+0x174>
jmp    1329 <split_bihx+0x179>
jmp    132e <split_bihx+0x17e>
jmp    123a <split_bihx+0x8a>
mov    -0x118(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x120(%rbp),%rdi
call   1040 <fclose@plt>
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<split_bihx>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
lea    0xe38(%rip),%rsi
mov    %rdi,0x8(%rsp)
call   1080 <fopen@plt>
test   %rax,%rax
je     12d7 <split_bihx+0x127>
mov    %rax,%r13
lea    0xe1d(%rip),%rdi
lea    0xe21(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     12de <split_bihx+0x12e>
mov    %rax,%r15
lea    0x10(%rsp),%rdi
mov    $0x100,%esi
mov    %r13,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     12b5 <split_bihx+0x105>
lea    0x12(%rsp),%r12
lea    0x10(%rsp),%rbx
lea    0xde6(%rip),%rbp
lea    0x4(%rsp),%r14
jmp    125f <split_bihx+0xaf>
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %r14,%rdx
call   1070 <sscanf@plt>
mov    0x4(%rsp),%edi
mov    %r12,%rsi
call   10a0 <put_seg@plt>
mov    %rbx,%rdi
mov    $0x100,%esi
mov    %r13,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     12b5 <split_bihx+0x105>
movsbl 0x10(%rsp),%edi
call   1030 <isdigit@plt>
test   %rax,%rax
jne    1230 <split_bihx+0x80>
movzbl 0x10(%rsp),%eax
cmp    $0x42,%al
je     1289 <split_bihx+0xd9>
cmp    $0x3b,%al
je     124a <split_bihx+0x9a>
cmp    $0x3a,%al
jne    1296 <split_bihx+0xe6>
mov    %rbx,%rdi
call   10c0 <put_all_segs@plt>
jmp    124a <split_bihx+0x9a>
mov    %r15,%rdi
mov    %rbx,%rsi
call   10d0 <save_patch_rule@plt>
jmp    124a <split_bihx+0x9a>
mov    0x2d43(%rip),%rax
mov    (%rax),%edi
lea    0xd6c(%rip),%rsi
mov    0x8(%rsp),%rdx
mov    %rbx,%rcx
call   1060 <fprintf@plt>
jmp    124a <split_bihx+0x9a>
mov    %r13,%rdi
call   1040 <fclose@plt>
mov    %r15,%rdi
call   1040 <fclose@plt>
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x8(%rsp),%rdi
jmp    12e5 <split_bihx+0x135>
lea    0xd1d(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [11 x i8] c"relocs.dat\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [3 x i8] c"%x\00"
@stderr = global i32 0
@.str.4 = [28 x i8] c"%s: invalid bihx line: %s.\0A\00"
define void @split_bihx(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca [256 x i8]
%5 = alloca i32*
%6 = alloca i32*
store i8* %0, i8** %2
%7 = load i8*, i8** %2
%8 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %8, i32** %5
%9 = load i32*, i32** %5
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %15
11: ; preds = %1
%12 = load i8*, i8** %2
%13 = call i32 @perror(i8* %12)
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %1
%16 = call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %16, i32** %6
%17 = load i32*, i32** %6
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %15
br label %23
23: ; preds = %71, %53, %22
%24 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%25 = load i32*, i32** %5
%26 = call i64 @fgets(i8* %24, i32 256, i32* %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %72
28: ; preds = %23
%29 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%30 = load i8, i8* %29
%31 = call i64 @isdigit(i8 signext %30)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %40
33: ; preds = %28
%34 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%35 = call i32 @sscanf(i8* %34, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %3)
%36 = load i32, i32* %3
%37 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%38 = getelementptr inbounds i8, i8* %37, i64 2
%39 = call i32 @put_seg(i32 %36, i8* %38)
br label %71
40: ; preds = %28
%41 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%42 = load i8, i8* %41
%43 = sext i8 %42 to i32
%44 = icmp eq i32 %43, 58
br i1 %44, label %45, label %48
45: ; preds = %40
%46 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%47 = call i32 @put_all_segs(i8* %46)
br label %70
48: ; preds = %40
%49 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%50 = load i8, i8* %49
%51 = sext i8 %50 to i32
%52 = icmp eq i32 %51, 59
br i1 %52, label %53, label %54
53: ; preds = %48
br label %23
54: ; preds = %48
%55 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%56 = load i8, i8* %55
%57 = sext i8 %56 to i32
%58 = icmp eq i32 %57, 66
br i1 %58, label %59, label %63
59: ; preds = %54
%60 = load i32*, i32** %6
%61 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%62 = call i32 @save_patch_rule(i32* %60, i8* %61)
br label %68
63: ; preds = %54
%64 = load i32, i32* @stderr
%65 = load i8*, i8** %2
%66 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%67 = call i32 @fprintf(i32 %64, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i8* %65, i8* %66)
br label %68
68: ; preds = %63, %59
br label %69
69: ; preds = %68
br label %70
70: ; preds = %69, %45
br label %71
71: ; preds = %70, %33
br label %23
72: ; preds = %23
%73 = load i32*, i32** %5
%74 = call i32 @fclose(i32* %73)
%75 = load i32*, i32** %6
%76 = call i32 @fclose(i32* %75)
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [11 x i8] c"relocs.dat\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [3 x i8] c"%x\00"
@stderr = global i32 0
@.str.4 = [28 x i8] c"%s: invalid bihx line: %s.\0A\00"
define void @split_bihx(i8* %0) {
%2 = alloca i32
%3 = alloca [256 x i8]
%4 = bitcast i32* %2 to i8*
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%6 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %11
8: ; preds = %1
%9 = tail call i32 @perror(i8* %0)
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %1
%12 = tail call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %19, label %14
14: ; preds = %11
%15 = call i64 @fgets(i8* %5, i32 256, i32* %6)
%16 = icmp eq i64 %15, 0
br i1 %16, label %42, label %17
17: ; preds = %14
%18 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 2
br label %22
19: ; preds = %11
%20 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%21 = tail call i32 @exit(i32 1)
unreachable
22: ; preds = %17, %39
%23 = load i8, i8* %5
%24 = call i64 @isdigit(i8 signext %23)
%25 = icmp eq i64 %24, 0
br i1 %25, label %30, label %26
26: ; preds = %22
%27 = call i32 @sscanf(i8* %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %2)
%28 = load i32, i32* %2
%29 = call i32 @put_seg(i32 %28, i8* %18)
br label %39
30: ; preds = %22
%31 = load i8, i8* %5
switch i8 %31, label %36 [
i8 58, label %32
i8 59, label %39
i8 66, label %34
]
32: ; preds = %30
%33 = call i32 @put_all_segs(i8* %5)
br label %39
34: ; preds = %30
%35 = call i32 @save_patch_rule(i32* %12, i8* %5)
br label %39
36: ; preds = %30
%37 = load i32, i32* @stderr
%38 = call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i8* %0, i8* %5)
br label %39
39: ; preds = %26, %36, %34, %32, %30
%40 = call i64 @fgets(i8* %5, i32 256, i32* %6)
%41 = icmp eq i64 %40, 0
br i1 %41, label %42, label %22
42: ; preds = %39, %14
%43 = call i32 @fclose(i32* %6)
%44 = call i32 @fclose(i32* %12)
ret void
}