#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) {
        fclose(file);
        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    127d <get_calibration_value+0x10d>
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    126d <get_calibration_value+0xfd>
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    1268 <get_calibration_value+0xf8>
mov    -0x1024(%rbp),%eax
cmp    -0x8(%rbp),%eax
jne    1263 <get_calibration_value+0xf3>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x1028(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    127d <get_calibration_value+0x10d>
jmp    1268 <get_calibration_value+0xf8>
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     11bb <get_calibration_value+0x4b>
mov    %edi,%r14d
lea    0xe70(%rip),%rax
mov    %rsi,%rdi
mov    %rax,%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1237 <get_calibration_value+0xc7>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <feof@plt>
test   %eax,%eax
je     11d2 <get_calibration_value+0x62>
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
lea    0x10(%rsp),%rbp
lea    0xe41(%rip),%r15
lea    0xc(%rsp),%r12
lea    0x8(%rsp),%r13
jmp    11fc <get_calibration_value+0x8c>
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1060 <feof@plt>
test   %eax,%eax
jne    11b3 <get_calibration_value+0x43>
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    11f0 <get_calibration_value+0x80>
cmp    %r14d,0xc(%rsp)
jne    11f0 <get_calibration_value+0x80>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    0x8(%rsp),%eax
jmp    11c0 <get_calibration_value+0x50>
mov    0x2da2(%rip),%rax
mov    (%rax),%edi
lea    0xdbb(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2d85(%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 %49
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 = %45, %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 %46
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 %45
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 %44
40: ; preds = %36
%41 = load i32*, i32** %6
%42 = call i32 @fclose(i32* %41)
%43 = load i32, i32* %9
store i32 %43, i32* %3
br label %49
44: ; preds = %36
br label %45
45: ; preds = %44, %29
br label %24
46: ; preds = %24
%47 = load i32*, i32** %6
%48 = call i32 @fclose(i32* %47)
store i32 2200, i32* %3
br label %49
49: ; preds = %46, %40, %12
%50 = load i32, i32* %3
ret i32 %50
}

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 %39, 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 = call i32 @feof(i32* %8)
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %35
19: ; preds = %15
%20 = bitcast i32* %4 to i8*
%21 = bitcast i32* %5 to i8*
br label %22
22: ; preds = %19, %32
%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
br i1 %28, label %29, label %32
29: ; preds = %22
%30 = call i32 @fclose(i32* %8)
%31 = load i32, i32* %5
br label %37
32: ; preds = %22
%33 = call i32 @feof(i32* %8)
%34 = icmp eq i32 %33, 0
br i1 %34, label %22, label %35
35: ; preds = %32, %15
%36 = call i32 @fclose(i32* %8)
br label %37
37: ; preds = %29, %35
%38 = phi i32 [ 2200, %35 ], [ %31, %29 ]
br label %39
39: ; preds = %2, %37
%40 = phi i32 [ %38, %37 ], [ 2200, %2 ]
ret i32 %40
}