#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*/ 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);
int /*<<< orphan*/ sscanf(char *, char *, int *, int *);
int /*<<< orphan*/ stderr;
int getServoPosition(int servo) {
  FILE *fp = fopen("/dev/servoblaster", "r");
  if (!fp) {
    fprintf(stderr, "[-] Can't open servoblaster device to read servo %d\n",
            servo);
    return -1;
  }
  int position = -1;
  char buffer[20];
  while (!feof(fp)) {
    fgets(buffer, 19, fp);
    int s, p;
    sscanf(buffer, "%d=%d", &s, &p);
    if (s == servo) {
      position = p;
      break;
    }
  }
  fclose(fp);
  return position;
}


x86 assembly (clang, -O0):
<getServoPosition>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
lea    0xe8e(%rip),%rdi
lea    0xe97(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11b1 <getServoPosition+0x51>
mov    0x2e4c(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%edx
lea    0xe72(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    122b <getServoPosition+0xcb>
movl   $0xffffffff,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11d6 <getServoPosition+0x76>
jmp    121c <getServoPosition+0xbc>
lea    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x13,%esi
call   1040 <fgets@plt>
lea    -0x30(%rbp),%rdi
lea    0xe54(%rip),%rsi
lea    -0x34(%rbp),%rdx
lea    -0x38(%rbp),%rcx
call   1070 <sscanf@plt>
mov    -0x34(%rbp),%eax
cmp    -0x8(%rbp),%eax
jne    1217 <getServoPosition+0xb7>
mov    -0x38(%rbp),%eax
mov    %eax,-0x14(%rbp)
jmp    121c <getServoPosition+0xbc>
jmp    11b8 <getServoPosition+0x58>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getServoPosition>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %edi,%r14d
lea    0xe88(%rip),%rdi
lea    0xe91(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11ea <getServoPosition+0x8a>
mov    %rax,%rbx
lea    0x10(%rsp),%rbp
lea    0xeaf(%rip),%r15
lea    0xc(%rsp),%r12
lea    0x8(%rsp),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1060 <feof@plt>
test   %eax,%eax
jne    1209 <getServoPosition+0xa9>
mov    %rbp,%rdi
mov    $0x13,%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    %r14d,0xc(%rsp)
jne    11b0 <getServoPosition+0x50>
mov    0x8(%rsp),%ebp
jmp    120e <getServoPosition+0xae>
mov    0x2def(%rip),%rax
mov    (%rax),%edi
lea    0xe18(%rip),%rsi
mov    %r14d,%edx
call   1050 <fprintf@plt>
mov    $0xffffffff,%ebp
jmp    1216 <getServoPosition+0xb6>
mov    $0xffffffff,%ebp
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [18 x i8] c"/dev/servoblaster\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [53 x i8] c"[-] Can't open servoblaster device to read servo %d\0A\00"
@.str.3 = [6 x i8] c"%d=%d\00"
define i32 @getServoPosition(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
%6 = alloca [20 x i8]
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %3
%9 = call i32* @fopen(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %9, i32** %4
%10 = load i32*, i32** %4
%11 = icmp ne i32* %10, null
br i1 %11, label %16, label %12
12: ; preds = %1
%13 = load i32, i32* @stderr
%14 = load i32, i32* %3
%15 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.2, i64 0, i64 0), i32 %14)
store i32 -1, i32* %2
br label %38
16: ; preds = %1
store i32 -1, i32* %5
br label %17
17: ; preds = %33, %16
%18 = load i32*, i32** %4
%19 = call i32 @feof(i32* %18)
%20 = icmp ne i32 %19, 0
%21 = xor i1 %20, true
br i1 %21, label %22, label %34
22: ; preds = %17
%23 = getelementptr inbounds [20 x i8], [20 x i8]* %6, i64 0, i64 0
%24 = load i32*, i32** %4
%25 = call i32 @fgets(i8* %23, i32 19, i32* %24)
%26 = getelementptr inbounds [20 x i8], [20 x i8]* %6, i64 0, i64 0
%27 = call i32 @sscanf(i8* %26, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32* %7, i32* %8)
%28 = load i32, i32* %7
%29 = load i32, i32* %3
%30 = icmp eq i32 %28, %29
br i1 %30, label %31, label %33
31: ; preds = %22
%32 = load i32, i32* %8
store i32 %32, i32* %5
br label %34
33: ; preds = %22
br label %17
34: ; preds = %31, %17
%35 = load i32*, i32** %4
%36 = call i32 @fclose(i32* %35)
%37 = load i32, i32* %5
store i32 %37, i32* %2
br label %38
38: ; preds = %34, %12
%39 = load i32, i32* %2
ret i32 %39
}

LLVM IR (-O2):
@.str = [18 x i8] c"/dev/servoblaster\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [53 x i8] c"[-] Can't open servoblaster device to read servo %d\0A\00"
@.str.3 = [6 x i8] c"%d=%d\00"
define i32 @getServoPosition(i32 %0) {
%2 = alloca [20 x i8]
%3 = alloca i32
%4 = alloca i32
%5 = tail call i32* @fopen(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = load i32, i32* @stderr
%9 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.2, i64 0, i64 0), i32 %0)
br label %26
10: ; preds = %1
%11 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 0
%12 = bitcast i32* %3 to i8*
%13 = bitcast i32* %4 to i8*
br label %14
14: ; preds = %17, %10
%15 = call i32 @feof(i32* %5)
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %23
17: ; preds = %14
%18 = call i32 @fgets(i8* %11, i32 19, i32* %5)
%19 = call i32 @sscanf(i8* %11, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32* %3, i32* %4)
%20 = load i32, i32* %3
%21 = icmp eq i32 %20, %0
%22 = load i32, i32* %4
br i1 %21, label %23, label %14
23: ; preds = %17, %14
%24 = phi i32 [ %22, %17 ], [ -1, %14 ]
%25 = call i32 @fclose(i32* %5)
br label %26
26: ; preds = %23, %7
%27 = phi i32 [ %24, %23 ], [ -1, %7 ]
ret i32 %27
}