#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 O_RDWR;
int O_SYNC;
int atoi(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ ferr(char *, char *);
int open(char *, int);
int read(int, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int gpio_get(int gpioNr) {
  char filename[(128)];
  sprintf(filename, "/sys/class/gpio/gpio%d/value", gpioNr);
  int fd = open(filename, O_RDWR | O_SYNC);
  if (fd == -1) {
    ferr("open", filename);
  }
  char buf[(128) + 1];
  sprintf(buf, "%d", gpioNr);
  int count;
  do {
    count = read(fd, buf, (128));
  } while (count == 0);
  if (count < 0) {
    close(fd);
    ferr("read", filename);
  }
  close(fd);
  buf[count] = '\0';
  return atoi(buf);
}


x86 assembly (clang, -O0):
<gpio_get>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x90(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe81(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x90(%rbp),%rdi
mov    0x2e56(%rip),%rax
mov    (%rax),%esi
mov    0x2e45(%rip),%rax
or     (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x94(%rbp)
cmpl   $0xffffffff,-0x94(%rbp)
jne    11c8 <gpio_get+0x68>
lea    -0x90(%rbp),%rsi
lea    0xe5a(%rip),%rdi
call   1080 <ferr@plt>
lea    -0x120(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe49(%rip),%rsi
call   1070 <sprintf@plt>
mov    -0x94(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    $0x80,%edx
call   1040 <read@plt>
mov    %eax,-0x124(%rbp)
cmpl   $0x0,-0x124(%rbp)
je     11de <gpio_get+0x7e>
cmpl   $0x0,-0x124(%rbp)
jge    1233 <gpio_get+0xd3>
mov    -0x94(%rbp),%edi
call   1030 <close@plt>
lea    -0x90(%rbp),%rsi
lea    0xdf7(%rip),%rdi
call   1080 <ferr@plt>
mov    -0x94(%rbp),%edi
call   1030 <close@plt>
movslq -0x124(%rbp),%rax
movb   $0x0,-0x120(%rbp,%rax,1)
lea    -0x120(%rbp),%rdi
call   1060 <atoi@plt>
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gpio_get>:
push   %rbp
push   %r14
push   %rbx
sub    $0x110,%rsp
mov    %edi,%ebp
lea    0xe8c(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %ebp,%edx
call   1070 <sprintf@plt>
mov    0x2e60(%rip),%rax
mov    0x2e51(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
call   1050 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    11b1 <gpio_get+0x51>
lea    0xe74(%rip),%rdi
mov    %rsp,%rsi
call   1080 <ferr@plt>
lea    0xe6a(%rip),%rsi
lea    0x80(%rsp),%r14
mov    %r14,%rdi
mov    %ebp,%edx
call   1070 <sprintf@plt>
nopw   0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x80,%edx
call   1040 <read@plt>
test   %eax,%eax
je     11d0 <gpio_get+0x70>
mov    %eax,%ebp
jns    11fd <gpio_get+0x9d>
mov    %ebx,%edi
call   1030 <close@plt>
lea    0xe30(%rip),%rdi
mov    %rsp,%rsi
call   1080 <ferr@plt>
mov    %ebx,%edi
call   1030 <close@plt>
movslq %ebp,%rax
movb   $0x0,0x80(%rsp,%rax,1)
lea    0x80(%rsp),%rdi
call   1060 <atoi@plt>
add    $0x110,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [29 x i8] c"/sys/class/gpio/gpio%d/value\00"
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [5 x i8] c"read\00"
define i32 @gpio_get(i32 %0) {
%2 = alloca i32
%3 = alloca [128 x i8]
%4 = alloca i32
%5 = alloca [129 x i8]
%6 = alloca i32
store i32 %0, i32* %2
%7 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%8 = load i32, i32* %2
%9 = call i32 @sprintf(i8* %7, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%11 = load i32, i32* @O_RDWR
%12 = load i32, i32* @O_SYNC
%13 = or i32 %11, %12
%14 = call i32 @open(i8* %10, i32 %13)
store i32 %14, i32* %4
%15 = load i32, i32* %4
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %20
17: ; preds = %1
%18 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%19 = call i32 @ferr(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %18)
br label %20
20: ; preds = %17, %1
%21 = getelementptr inbounds [129 x i8], [129 x i8]* %5, i64 0, i64 0
%22 = load i32, i32* %2
%23 = call i32 @sprintf(i8* %21, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %22)
br label %24
24: ; preds = %28, %20
%25 = load i32, i32* %4
%26 = getelementptr inbounds [129 x i8], [129 x i8]* %5, i64 0, i64 0
%27 = call i32 @read(i32 %25, i8* %26, i32 128)
store i32 %27, i32* %6
br label %28
28: ; preds = %24
%29 = load i32, i32* %6
%30 = icmp eq i32 %29, 0
br i1 %30, label %24, label %31
31: ; preds = %28
%32 = load i32, i32* %6
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %39
34: ; preds = %31
%35 = load i32, i32* %4
%36 = call i32 @close(i32 %35)
%37 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%38 = call i32 @ferr(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* %37)
br label %39
39: ; preds = %34, %31
%40 = load i32, i32* %4
%41 = call i32 @close(i32 %40)
%42 = load i32, i32* %6
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds [129 x i8], [129 x i8]* %5, i64 0, i64 %43
store i8 0, i8* %44
%45 = getelementptr inbounds [129 x i8], [129 x i8]* %5, i64 0, i64 0
%46 = call i32 @atoi(i8* %45)
ret i32 %46
}

LLVM IR (-O2):
@.str = [29 x i8] c"/sys/class/gpio/gpio%d/value\00"
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [5 x i8] c"read\00"
define i32 @gpio_get(i32 %0) {
%2 = alloca [128 x i8]
%3 = alloca [129 x i8]
%4 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%5 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %0)
%6 = load i32, i32* @O_RDWR
%7 = load i32, i32* @O_SYNC
%8 = or i32 %7, %6
%9 = call i32 @open(i8* %4, i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = call i32 @ferr(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %4)
br label %13
13: ; preds = %11, %1
%14 = getelementptr inbounds [129 x i8], [129 x i8]* %3, i64 0, i64 0
%15 = call i32 @sprintf(i8* %14, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %0)
br label %16
16: ; preds = %16, %13
%17 = call i32 @read(i32 %9, i8* %14, i32 128)
%18 = icmp eq i32 %17, 0
br i1 %18, label %16, label %19
19: ; preds = %16
%20 = icmp slt i32 %17, 0
br i1 %20, label %21, label %24
21: ; preds = %19
%22 = call i32 @close(i32 %9)
%23 = call i32 @ferr(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* %4)
br label %24
24: ; preds = %21, %19
%25 = call i32 @close(i32 %9)
%26 = sext i32 %17 to i64
%27 = getelementptr inbounds [129 x i8], [129 x i8]* %3, i64 0, i64 %26
store i8 0, i8* %27
%28 = call i32 @atoi(i8* %14)
ret i32 %28
}