#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*/ *fopen(char *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int gpio_read(int pin) {
  char path[40];
  char val[2];
  FILE *fp = NULL;
  snprintf(path, 40, "/sys/class/gpio/gpio%d/value", pin);
  fp = fopen(path, "r");
  if (!fp) {
    printf("Failed to open gpio value %d!\n", pin);
    return 1;
  }
  if (fread(val, 1, 1, fp) != 1) {
    printf("Failed to read value %d!\n", pin);
    fclose(fp);
    return 1;
  }
  val[1] = '\0';
  fclose(fp);
  return val[0] - '0';
}


x86 assembly (clang, -O0):
<gpio_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
movq   $0x0,-0x40(%rbp)
lea    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x28,%esi
lea    0xe8a(%rip),%rdx
call   1060 <snprintf@plt>
lea    -0x30(%rbp),%rdi
lea    0xe97(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    11b5 <gpio_read+0x65>
mov    -0x8(%rbp),%esi
lea    0xe7b(%rip),%rdi
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    120d <gpio_read+0xbd>
lea    -0x32(%rbp),%rdi
mov    -0x40(%rbp),%rcx
mov    $0x1,%edx
mov    %edx,%esi
call   1030 <fread@plt>
cmp    $0x1,%eax
je     11f6 <gpio_read+0xa6>
mov    -0x8(%rbp),%esi
lea    0xe62(%rip),%rdi
call   1050 <printf@plt>
mov    -0x40(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x1,-0x4(%rbp)
jmp    120d <gpio_read+0xbd>
movb   $0x0,-0x31(%rbp)
mov    -0x40(%rbp),%rdi
call   1040 <fclose@plt>
movsbl -0x32(%rbp),%eax
sub    $0x30,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gpio_read>:
push   %rbp
push   %r14
push   %rbx
sub    $0x40,%rsp
mov    %edi,%r14d
lea    0xe9e(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x28,%esi
mov    %r14d,%ecx
call   1060 <snprintf@plt>
lea    0xe9f(%rip),%rsi
mov    %rbx,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     11c1 <gpio_read+0x71>
mov    %rax,%rbx
lea    0xe(%rsp),%rdi
mov    $0x1,%ebp
mov    $0x1,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
cmp    $0x1,%eax
jne    11d7 <gpio_read+0x87>
mov    %rbx,%rdi
call   1040 <fclose@plt>
movsbl 0xe(%rsp),%ebp
add    $0xffffffd0,%ebp
jmp    11ee <gpio_read+0x9e>
lea    0xe57(%rip),%rdi
mov    %r14d,%esi
call   1050 <printf@plt>
mov    $0x1,%ebp
jmp    11ee <gpio_read+0x9e>
lea    0xe60(%rip),%rdi
mov    %r14d,%esi
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %ebp,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [29 x i8] c"/sys/class/gpio/gpio%d/value\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [31 x i8] c"Failed to open gpio value %d!\0A\00"
@.str.3 = [26 x i8] c"Failed to read value %d!\0A\00"
define i32 @gpio_read(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [40 x i8]
%5 = alloca [2 x i8]
%6 = alloca i32*
store i32 %0, i32* %3
store i32* null, i32** %6
%7 = getelementptr inbounds [40 x i8], [40 x i8]* %4, i64 0, i64 0
%8 = load i32, i32* %3
%9 = call i32 @snprintf(i8* %7, i32 40, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = getelementptr inbounds [40 x i8], [40 x i8]* %4, i64 0, i64 0
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %11, i32** %6
%12 = load i32*, i32** %6
%13 = icmp ne i32* %12, null
br i1 %13, label %17, label %14
14: ; preds = %1
%15 = load i32, i32* %3
%16 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %15)
store i32 1, i32* %2
br label %35
17: ; preds = %1
%18 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 0
%19 = load i32*, i32** %6
%20 = call i32 @fread(i8* %18, i32 1, i32 1, i32* %19)
%21 = icmp ne i32 %20, 1
br i1 %21, label %22, label %27
22: ; preds = %17
%23 = load i32, i32* %3
%24 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i32 %23)
%25 = load i32*, i32** %6
%26 = call i32 @fclose(i32* %25)
store i32 1, i32* %2
br label %35
27: ; preds = %17
%28 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 1
store i8 0, i8* %28
%29 = load i32*, i32** %6
%30 = call i32 @fclose(i32* %29)
%31 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 0
%32 = load i8, i8* %31
%33 = sext i8 %32 to i32
%34 = sub nsw i32 %33, 48
store i32 %34, i32* %2
br label %35
35: ; preds = %27, %22, %14
%36 = load i32, i32* %2
ret i32 %36
}

LLVM IR (-O2):
@.str = [29 x i8] c"/sys/class/gpio/gpio%d/value\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [31 x i8] c"Failed to open gpio value %d!\0A\00"
@.str.3 = [26 x i8] c"Failed to read value %d!\0A\00"
define i32 @gpio_read(i32 %0) {
%2 = alloca [40 x i8]
%3 = alloca [2 x i8]
%4 = getelementptr inbounds [40 x i8], [40 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [2 x i8], [2 x i8]* %3, i64 0, i64 0
%6 = call i32 @snprintf(i8* %4, i32 40, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %0)
%7 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %0)
br label %22
11: ; preds = %1
%12 = call i32 @fread(i8* %5, i32 1, i32 1, i32* %7)
%13 = icmp eq i32 %12, 1
br i1 %13, label %17, label %14
14: ; preds = %11
%15 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i32 %0)
%16 = call i32 @fclose(i32* %7)
br label %22
17: ; preds = %11
%18 = call i32 @fclose(i32* %7)
%19 = load i8, i8* %5
%20 = sext i8 %19 to i32
%21 = add nsw i32 %20, -48
br label %22
22: ; preds = %17, %14, %9
%23 = phi i32 [ 1, %14 ], [ %21, %17 ], [ 1, %9 ]
ret i32 %23
}