#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_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int sscanf(char *, char *, int *, int *);
int /*<<< orphan*/ stderr;
int get_calibration_value(int sensor_index, char *calibration_file) {
  if (!calibration_file)
    return 2200;
  FILE *file = fopen(calibration_file, "r");
  if (!file) {
    fprintf(stderr, "Can't open calibration file\n");
    exit(EXIT_FAILURE);
  }
  char buf[4096];
  while (!feof(file)) {
    int sensor, value;
    fgets(buf, 4095, file);
    if (sscanf(buf, "%d: %d", &sensor, &value) == 2) {
      if (sensor == sensor_index)
        return value;
    }
  }
  fclose(file);
  return 2200;
}


x86 assembly (clang, -O0):
<get_calibration_value>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1199 <get_calibration_value+0x29>
movl   $0x898,-0x4(%rbp)
jmp    1274 <get_calibration_value+0x104>
mov    -0x10(%rbp),%rdi
lea    0xe5c(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11db <get_calibration_value+0x6b>
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
lea    0xe3a(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
jmp    11e0 <get_calibration_value+0x70>
mov    -0x18(%rbp),%rdi
call   1060 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11fe <get_calibration_value+0x8e>
jmp    1264 <get_calibration_value+0xf4>
lea    -0x1020(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0xfff,%esi
call   1040 <fgets@plt>
lea    -0x1020(%rbp),%rdi
lea    0xdfe(%rip),%rsi
lea    -0x1024(%rbp),%rdx
lea    -0x1028(%rbp),%rcx
call   1070 <sscanf@plt>
cmp    $0x2,%eax
jne    125f <get_calibration_value+0xef>
mov    -0x1024(%rbp),%eax
cmp    -0x8(%rbp),%eax
jne    125a <get_calibration_value+0xea>
mov    -0x1028(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1274 <get_calibration_value+0x104>
jmp    125f <get_calibration_value+0xef>
jmp    11e0 <get_calibration_value+0x70>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x898,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_calibration_value>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1018,%rsp
test   %rsi,%rsi
je     1207 <get_calibration_value+0x97>
mov    %edi,%r14d
lea    0xe6c(%rip),%rax
mov    %rsi,%rdi
mov    %rax,%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     121e <get_calibration_value+0xae>
mov    %rax,%rbx
lea    0x10(%rsp),%rbp
lea    0xe6c(%rip),%r15
lea    0xc(%rsp),%r12
lea    0x8(%rsp),%r13
nopl   (%rax)
mov    %rbx,%rdi
call   1060 <feof@plt>
test   %eax,%eax
jne    11ff <get_calibration_value+0x8f>
mov    %rbp,%rdi
mov    $0xfff,%esi
mov    %rbx,%rdx
call   1040 <fgets@plt>
mov    %rbp,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
mov    %r13,%rcx
call   1070 <sscanf@plt>
cmp    $0x2,%eax
jne    11c0 <get_calibration_value+0x50>
cmp    %r14d,0xc(%rsp)
jne    11c0 <get_calibration_value+0x50>
mov    0x8(%rsp),%eax
jmp    120c <get_calibration_value+0x9c>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    $0x898,%eax
add    $0x1018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dbb(%rip),%rax
mov    (%rax),%edi
lea    0xdd4(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2d9e(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [29 x i8] c"Can't open calibration file\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [7 x i8] c"%d: %d\00"
define i32 @get_calibration_value(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca [4096 x i8]
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
%10 = load i8*, i8** %5
%11 = icmp ne i8* %10, null
br i1 %11, label %13, label %12
12: ; preds = %2
store i32 2200, i32* %3
br label %47
13: ; preds = %2
%14 = load i8*, i8** %5
%15 = call i32* @fopen(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %15, i32** %6
%16 = load i32*, i32** %6
%17 = icmp ne i32* %16, null
br i1 %17, label %23, label %18
18: ; preds = %13
%19 = load i32, i32* @stderr
%20 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%21 = load i32, i32* @EXIT_FAILURE
%22 = call i32 @exit(i32 %21)
unreachable
23: ; preds = %13
br label %24
24: ; preds = %43, %23
%25 = load i32*, i32** %6
%26 = call i32 @feof(i32* %25)
%27 = icmp ne i32 %26, 0
%28 = xor i1 %27, true
br i1 %28, label %29, label %44
29: ; preds = %24
%30 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%31 = load i32*, i32** %6
%32 = call i32 @fgets(i8* %30, i32 4095, i32* %31)
%33 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%34 = call i32 @sscanf(i8* %33, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32* %8, i32* %9)
%35 = icmp eq i32 %34, 2
br i1 %35, label %36, label %43
36: ; preds = %29
%37 = load i32, i32* %8
%38 = load i32, i32* %4
%39 = icmp eq i32 %37, %38
br i1 %39, label %40, label %42
40: ; preds = %36
%41 = load i32, i32* %9
store i32 %41, i32* %3
br label %47
42: ; preds = %36
br label %43
43: ; preds = %42, %29
br label %24
44: ; preds = %24
%45 = load i32*, i32** %6
%46 = call i32 @fclose(i32* %45)
store i32 2200, i32* %3
br label %47
47: ; preds = %44, %40, %12
%48 = load i32, i32* %3
ret i32 %48
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [29 x i8] c"Can't open calibration file\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [7 x i8] c"%d: %d\00"
define i32 @get_calibration_value(i32 %0, i8* %1) {
%3 = alloca [4096 x i8]
%4 = alloca i32
%5 = alloca i32
%6 = icmp eq i8* %1, null
br i1 %6, label %34, label %7
7: ; preds = %2
%8 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %15
10: ; preds = %7
%11 = load i32, i32* @stderr
%12 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%13 = load i32, i32* @EXIT_FAILURE
%14 = tail call i32 @exit(i32 %13)
unreachable
15: ; preds = %7
%16 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%17 = bitcast i32* %4 to i8*
%18 = bitcast i32* %5 to i8*
br label %19
19: ; preds = %22, %15
%20 = call i32 @feof(i32* %8)
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %30
22: ; preds = %19
%23 = call i32 @fgets(i8* %16, i32 4095, i32* %8)
%24 = call i32 @sscanf(i8* %16, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32* %4, i32* %5)
%25 = icmp eq i32 %24, 2
%26 = load i32, i32* %4
%27 = icmp eq i32 %26, %0
%28 = select i1 %25, i1 %27, i1 false
%29 = load i32, i32* %5
br i1 %28, label %32, label %19
30: ; preds = %19
%31 = call i32 @fclose(i32* %8)
br label %32
32: ; preds = %22, %30
%33 = phi i32 [ 2200, %30 ], [ %29, %22 ]
br label %34
34: ; preds = %2, %32
%35 = phi i32 [ %33, %32 ], [ 2200, %2 ]
ret i32 %35
}