#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 EXIT_SUCCESS;
char fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned int);
int get_pin_value(unsigned int pin, unsigned int *val) {
  FILE *pin_file;
  char pin_value_path[128];
  snprintf(pin_value_path, 128,
           "/sys/class/gpio/"
           "gpio%d/value",
           pin);
  pin_file = fopen(pin_value_path, "r");
  if (pin_file == NULL) {
    return 300;
  }
  char c = fgetc(pin_file);
  if (c != '0') {
    *val = 1;
  } else {
    *val = 0;
  }
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<get_pin_value>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0xa0(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x80,%esi
lea    0xea8(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0xa0(%rbp),%rdi
lea    0xeb2(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    118b <get_pin_value+0x5b>
movl   $0x12c,-0x4(%rbp)
jmp    11cf <get_pin_value+0x9f>
mov    -0x18(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %al,-0xa1(%rbp)
movsbl -0xa1(%rbp),%eax
cmp    $0x30,%eax
je     11b9 <get_pin_value+0x89>
mov    -0x10(%rbp),%rax
movl   $0x1,(%rax)
jmp    11c3 <get_pin_value+0x93>
mov    -0x10(%rbp),%rax
movl   $0x0,(%rax)
mov    0x2e16(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_pin_value>:
push   %r14
push   %rbx
sub    $0x88,%rsp
mov    %rsi,%r14
mov    %edi,%ecx
lea    0xeba(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x80,%esi
call   1030 <snprintf@plt>
lea    0xec0(%rip),%rsi
mov    %rbx,%rdi
call   1050 <fopen@plt>
test   %rax,%rax
je     1187 <get_pin_value+0x57>
mov    %rax,%rdi
call   1040 <fgetc@plt>
xor    %ecx,%ecx
cmp    $0x30,%al
setne  %cl
mov    %ecx,(%r14)
mov    0x2e5d(%rip),%rax
mov    (%rax),%eax
jmp    118c <get_pin_value+0x5c>
mov    $0x12c,%eax
add    $0x88,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [29 x i8] c"/sys/class/gpio/gpio%d/value\00"
@.str.1 = [2 x i8] c"r\00"
@EXIT_SUCCESS = global i32 0
define i32 @get_pin_value(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca [128 x i8]
%8 = alloca i8
store i32 %0, i32* %4
store i32* %1, i32** %5
%9 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%10 = load i32, i32* %4
%11 = call i32 @snprintf(i8* %9, i32 128, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %13, i32** %6
%14 = load i32*, i32** %6
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %17
16: ; preds = %2
store i32 300, i32* %3
br label %29
17: ; preds = %2
%18 = load i32*, i32** %6
%19 = call signext i8 @fgetc(i32* %18)
store i8 %19, i8* %8
%20 = load i8, i8* %8
%21 = sext i8 %20 to i32
%22 = icmp ne i32 %21, 48
br i1 %22, label %23, label %25
23: ; preds = %17
%24 = load i32*, i32** %5
store i32 1, i32* %24
br label %27
25: ; preds = %17
%26 = load i32*, i32** %5
store i32 0, i32* %26
br label %27
27: ; preds = %25, %23
%28 = load i32, i32* @EXIT_SUCCESS
store i32 %28, i32* %3
br label %29
29: ; preds = %27, %16
%30 = load i32, i32* %3
ret i32 %30
}

LLVM IR (-O2):
@.str = [29 x i8] c"/sys/class/gpio/gpio%d/value\00"
@.str.1 = [2 x i8] c"r\00"
@EXIT_SUCCESS = global i32 0
define i32 @get_pin_value(i32 %0, i32* nocapture writeonly %1) {
%3 = alloca [128 x i8]
%4 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%5 = call i32 @snprintf(i8* %4, i32 128, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %0)
%6 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %13, label %8
8: ; preds = %2
%9 = call signext i8 @fgetc(i32* %6)
%10 = icmp ne i8 %9, 48
%11 = zext i1 %10 to i32
store i32 %11, i32* %1
%12 = load i32, i32* @EXIT_SUCCESS
br label %13
13: ; preds = %2, %8
%14 = phi i32 [ %12, %8 ], [ 300, %2 ]
ret i32 %14
}