#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 /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ assert(int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *);
int read(int, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
char *strerror(int /*<<< orphan*/);
int getPin(int pin) {
  int fd;
  char val = 'x';
  char buf[64];
  sprintf(buf, "/sys/class/gpio/gpio%d/value", pin);
  fd = open(buf, O_RDONLY);
  if (fd < 0) {
    printf("ioctl error: %s\n", strerror(errno));
    return fd;
  }
  if (read(fd, &val, 1) != 1) {
    printf("ioctl error: %s\n", strerror(errno));
    return -1;
  };
  if (val == '0')
    return 0;
  if (val == '1')
    return 1;
  assert(0);
  return -1;
}


x86 assembly (clang, -O0):
<getPin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
movb   $0x78,-0xd(%rbp)
lea    -0x50(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xe83(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x50(%rbp),%rdi
mov    0x2e5b(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11c9 <getPin+0x69>
mov    0x2e30(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %rax,%rsi
lea    0xe64(%rip),%rdi
call   1030 <printf@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    124c <getPin+0xec>
mov    -0xc(%rbp),%edi
lea    -0xd(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0x1,%eax
je     120c <getPin+0xac>
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %rax,%rsi
lea    0xe22(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    124c <getPin+0xec>
movsbl -0xd(%rbp),%eax
cmp    $0x30,%eax
jne    1225 <getPin+0xc5>
movl   $0x0,-0x4(%rbp)
jmp    124c <getPin+0xec>
movsbl -0xd(%rbp),%eax
cmp    $0x31,%eax
jne    123e <getPin+0xde>
movl   $0x1,-0x4(%rbp)
jmp    124c <getPin+0xec>
xor    %edi,%edi
call   1050 <assert@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getPin>:
push   %rbx
sub    $0x50,%rsp
mov    %edi,%edx
movb   $0x78,0xf(%rsp)
lea    0xe8d(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   1070 <sprintf@plt>
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11c0 <getPin+0x60>
lea    0xf(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0x1,%eax
jne    11df <getPin+0x7f>
mov    0xf(%rsp),%al
cmp    $0x30,%al
je     11fe <getPin+0x9e>
cmp    $0x31,%al
jne    1202 <getPin+0xa2>
mov    $0x1,%ebx
jmp    120e <getPin+0xae>
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe48(%rip),%rdi
mov    %rax,%rsi
call   1030 <printf@plt>
jmp    120e <getPin+0xae>
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe29(%rip),%rdi
mov    %rax,%rsi
call   1030 <printf@plt>
jmp    1209 <getPin+0xa9>
xor    %ebx,%ebx
jmp    120e <getPin+0xae>
xor    %edi,%edi
call   1050 <assert@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x50,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [29 x i8] c"/sys/class/gpio/gpio%d/value\00"
@O_RDONLY = global i32 0
@.str.1 = [17 x i8] c"ioctl error: %s\0A\00"
@errno = global i32 0
define i32 @getPin(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8
%6 = alloca [64 x i8]
store i32 %0, i32* %3
store i8 120, i8* %5
%7 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%8 = load i32, i32* %3
%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 [64 x i8], [64 x i8]* %6, i64 0, i64 0
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* %10, i32 %11)
store i32 %12, i32* %4
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %1
%16 = load i32, i32* @errno
%17 = call i8* @strerror(i32 %16)
%18 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %17)
%19 = load i32, i32* %4
store i32 %19, i32* %2
br label %40
20: ; preds = %1
%21 = load i32, i32* %4
%22 = call i32 @read(i32 %21, i8* %5, i32 1)
%23 = icmp ne i32 %22, 1
br i1 %23, label %24, label %28
24: ; preds = %20
%25 = load i32, i32* @errno
%26 = call i8* @strerror(i32 %25)
%27 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %26)
store i32 -1, i32* %2
br label %40
28: ; preds = %20
%29 = load i8, i8* %5
%30 = sext i8 %29 to i32
%31 = icmp eq i32 %30, 48
br i1 %31, label %32, label %33
32: ; preds = %28
store i32 0, i32* %2
br label %40
33: ; preds = %28
%34 = load i8, i8* %5
%35 = sext i8 %34 to i32
%36 = icmp eq i32 %35, 49
br i1 %36, label %37, label %38
37: ; preds = %33
store i32 1, i32* %2
br label %40
38: ; preds = %33
%39 = call i32 @assert(i32 0)
store i32 -1, i32* %2
br label %40
40: ; preds = %38, %37, %32, %24, %15
%41 = load i32, i32* %2
ret i32 %41
}

LLVM IR (-O2):
@.str = [29 x i8] c"/sys/class/gpio/gpio%d/value\00"
@O_RDONLY = global i32 0
@.str.1 = [17 x i8] c"ioctl error: %s\0A\00"
@errno = global i32 0
define i32 @getPin(i32 %0) {
%2 = alloca i8
%3 = alloca [64 x i8]
store i8 120, i8* %2
%4 = getelementptr inbounds [64 x i8], [64 x i8]* %3, 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_RDONLY
%7 = call i32 @open(i8* %4, i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %13
9: ; preds = %1
%10 = load i32, i32* @errno
%11 = tail call i8* @strerror(i32 %10)
%12 = tail call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %11)
br label %25
13: ; preds = %1
%14 = call i32 @read(i32 %7, i8* %2, i32 1)
%15 = icmp eq i32 %14, 1
br i1 %15, label %20, label %16
16: ; preds = %13
%17 = load i32, i32* @errno
%18 = tail call i8* @strerror(i32 %17)
%19 = tail call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %18)
br label %25
20: ; preds = %13
%21 = load i8, i8* %2
switch i8 %21, label %23 [
i8 48, label %25
i8 49, label %22
]
22: ; preds = %20
br label %25
23: ; preds = %20
%24 = tail call i32 @assert(i32 0)
br label %25
25: ; preds = %20, %23, %22, %16, %9
%26 = phi i32 [ %7, %9 ], [ -1, %16 ], [ 1, %22 ], [ -1, %23 ], [ 0, %20 ]
ret i32 %26
}