#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 */
int bcd2int(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ free(char *);
char *get_line(char *, char *);
int /*<<< orphan*/ stderr;
scalar_t__ strlen(char *);
int strtol(char *, char **, int /*<<< orphan*/);
int get_line_as_int(char *fn, char *match, int bcd) {
  int ret = -1;
  char *s = get_line(fn, match);
  if (s && strlen(s) > 0) {
    char *endptr;
    ret = strtol(s, &endptr, 0);
    if (*endptr != '\0' && *endptr != '\n') {
      fprintf(stderr, "E: Couldn't parse number for key '%s'\n", match);
      return -1;
    }
    if (bcd)
      ret = bcd2int(ret);
    free(s);
  } else {
    fprintf(stderr, "E: Got invalid line for key '%s'\n", match);
  }
  return ret;
}


x86 assembly (clang, -O0):
<get_line_as_int>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movl   $0xffffffff,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <get_line@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1223 <get_line_as_int+0xc3>
mov    -0x28(%rbp),%rdi
call   1050 <strlen@plt>
cmp    $0x0,%rax
jle    1223 <get_line_as_int+0xc3>
mov    -0x28(%rbp),%rdi
lea    -0x30(%rbp),%rsi
xor    %edx,%edx
call   1070 <strtol@plt>
mov    %eax,-0x20(%rbp)
mov    -0x30(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1200 <get_line_as_int+0xa0>
mov    -0x30(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xa,%eax
je     1200 <get_line_as_int+0xa0>
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xe11(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1242 <get_line_as_int+0xe2>
cmpl   $0x0,-0x1c(%rbp)
je     1215 <get_line_as_int+0xb5>
mov    -0x20(%rbp),%edi
call   1080 <bcd2int@plt>
mov    %eax,-0x20(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
jmp    123c <get_line_as_int+0xdc>
mov    0x2db6(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xdf0(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_line_as_int>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%r14
call   1040 <get_line@plt>
test   %rax,%rax
je     11bc <get_line_as_int+0x5c>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <strlen@plt>
test   %rax,%rax
jle    11bc <get_line_as_int+0x5c>
mov    %rsp,%rsi
mov    %rbx,%rdi
xor    %edx,%edx
call   1070 <strtol@plt>
mov    %eax,%ebp
mov    (%rsp),%rax
mov    (%rax),%al
cmp    $0xa,%al
je     11a4 <get_line_as_int+0x44>
test   %al,%al
jne    11e6 <get_line_as_int+0x86>
test   %r15d,%r15d
je     11b2 <get_line_as_int+0x52>
mov    %ebp,%edi
call   1080 <bcd2int@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <free@plt>
jmp    11d9 <get_line_as_int+0x79>
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
lea    0xe5b(%rip),%rsi
mov    %r14,%rdx
call   1060 <fprintf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
lea    0xe0a(%rip),%rsi
jmp    11cc <get_line_as_int+0x6c>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [39 x i8] c"E: Couldn't parse number for key '%s'\0A\00"
@.str.1 = [34 x i8] c"E: Got invalid line for key '%s'\0A\00"
define i32 @get_line_as_int(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 -1, i32* %8
%11 = load i8*, i8** %5
%12 = load i8*, i8** %6
%13 = call i8* @get_line(i8* %11, i8* %12)
store i8* %13, i8** %9
%14 = load i8*, i8** %9
%15 = icmp ne i8* %14, null
br i1 %15, label %16, label %45
16: ; preds = %3
%17 = load i8*, i8** %9
%18 = call i64 @strlen(i8* %17)
%19 = icmp sgt i64 %18, 0
br i1 %19, label %20, label %45
20: ; preds = %16
%21 = load i8*, i8** %9
%22 = call i32 @strtol(i8* %21, i8** %10, i32 0)
store i32 %22, i32* %8
%23 = load i8*, i8** %10
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %36
27: ; preds = %20
%28 = load i8*, i8** %10
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = icmp ne i32 %30, 10
br i1 %31, label %32, label %36
32: ; preds = %27
%33 = load i32, i32* @stderr
%34 = load i8*, i8** %6
%35 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i8* %34)
store i32 -1, i32* %4
br label %51
36: ; preds = %27, %20
%37 = load i32, i32* %7
%38 = icmp ne i32 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %36
%40 = load i32, i32* %8
%41 = call i32 @bcd2int(i32 %40)
store i32 %41, i32* %8
br label %42
42: ; preds = %39, %36
%43 = load i8*, i8** %9
%44 = call i32 @free(i8* %43)
br label %49
45: ; preds = %16, %3
%46 = load i32, i32* @stderr
%47 = load i8*, i8** %6
%48 = call i32 @fprintf(i32 %46, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* %47)
br label %49
49: ; preds = %45, %42
%50 = load i32, i32* %8
store i32 %50, i32* %4
br label %51
51: ; preds = %49, %32
%52 = load i32, i32* %4
ret i32 %52
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [39 x i8] c"E: Couldn't parse number for key '%s'\0A\00"
@.str.1 = [34 x i8] c"E: Got invalid line for key '%s'\0A\00"
define i32 @get_line_as_int(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = tail call i8* @get_line(i8* %0, i8* %1)
%6 = icmp eq i8* %5, null
br i1 %6, label %25, label %7
7: ; preds = %3
%8 = tail call i64 @strlen(i8* %5)
%9 = icmp sgt i64 %8, 0
br i1 %9, label %10, label %25
10: ; preds = %7
%11 = bitcast i8** %4 to i8*
%12 = call i32 @strtol(i8* %5, i8** %4, i32 0)
%13 = load i8*, i8** %4
%14 = load i8, i8* %13
switch i8 %14, label %22 [
i8 0, label %15
i8 10, label %15
]
15: ; preds = %10, %10
%16 = icmp eq i32 %2, 0
br i1 %16, label %19, label %17
17: ; preds = %15
%18 = tail call i32 @bcd2int(i32 %12)
br label %19
19: ; preds = %15, %17
%20 = phi i32 [ %18, %17 ], [ %12, %15 ]
%21 = tail call i32 @free(i8* %5)
br label %28
22: ; preds = %10
%23 = load i32, i32* @stderr
%24 = tail call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i8* %1)
br label %28
25: ; preds = %7, %3
%26 = load i32, i32* @stderr
%27 = tail call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* %1)
br label %28
28: ; preds = %25, %19, %22
%29 = phi i32 [ -1, %22 ], [ -1, %25 ], [ %20, %19 ]
ret i32 %29
}