#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*/ buf;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned int);
char gpio_get_value(unsigned int gpio) {
  int fd;
  char buf[64];
  char ch;
  snprintf(buf, sizeof(buf),
           "/sys/class/gpio"
           "/gpio%d/value",
           gpio);
  fd = open(buf, O_RDONLY);
  if (fd < 0) {
    perror("gpio/get-value");
    return fd;
  }
  read(fd, &ch, 1);
  close(fd);
  return ch;
}


x86 assembly (clang, -O0):
<gpio_get_value>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x50(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x40,%esi
lea    0xe92(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x50(%rbp),%rdi
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11a9 <gpio_get_value+0x59>
lea    0xe84(%rip),%rdi
call   1070 <perror@plt>
mov    -0xc(%rbp),%eax
mov    %al,-0x1(%rbp)
jmp    11c8 <gpio_get_value+0x78>
mov    -0xc(%rbp),%edi
lea    -0x51(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
mov    -0x51(%rbp),%al
mov    %al,-0x1(%rbp)
movsbl -0x1(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gpio_get_value>:
push   %rbx
sub    $0x50,%rsp
mov    %edi,%ecx
lea    0xea2(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x40,%esi
call   1030 <snprintf@plt>
mov    0x2e71(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11ab <gpio_get_value+0x5b>
lea    0xf(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1050 <read@plt>
mov    %ebx,%edi
call   1040 <close@plt>
mov    0xf(%rsp),%bl
mov    %ebx,%eax
add    $0x50,%rsp
pop    %rbx
ret
lea    0xe6b(%rip),%rdi
call   1070 <perror@plt>
jmp    11a3 <gpio_get_value+0x53>

LLVM IR (-O0):
@.str = [29 x i8] c"/sys/class/gpio/gpio%d/value\00"
@O_RDONLY = global i32 0
@.str.1 = [15 x i8] c"gpio/get-value\00"
define signext i8 @gpio_get_value(i32 %0) {
%2 = alloca i8
%3 = alloca i32
%4 = alloca i32
%5 = alloca [64 x i8]
%6 = alloca i8
store i32 %0, i32* %3
%7 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%8 = load i32, i32* %3
%9 = call i32 @snprintf(i8* %7, i32 64, 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]* %5, 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 %19
15: ; preds = %1
%16 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%17 = load i32, i32* %4
%18 = trunc i32 %17 to i8
store i8 %18, i8* %2
br label %25
19: ; preds = %1
%20 = load i32, i32* %4
%21 = call i32 @read(i32 %20, i8* %6, i32 1)
%22 = load i32, i32* %4
%23 = call i32 @close(i32 %22)
%24 = load i8, i8* %6
store i8 %24, i8* %2
br label %25
25: ; preds = %19, %15
%26 = load i8, i8* %2
ret i8 %26
}

LLVM IR (-O2):
@.str = [29 x i8] c"/sys/class/gpio/gpio%d/value\00"
@O_RDONLY = global i32 0
@.str.1 = [15 x i8] c"gpio/get-value\00"
define signext i8 @gpio_get_value(i32 %0) {
%2 = alloca [64 x i8]
%3 = alloca i8
%4 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 0
%5 = call i32 @snprintf(i8* %4, i32 64, i8* 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 %12
9: ; preds = %1
%10 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%11 = trunc i32 %7 to i8
br label %16
12: ; preds = %1
%13 = call i32 @read(i32 %7, i8* %3, i32 1)
%14 = call i32 @close(i32 %7)
%15 = load i8, i8* %3
br label %16
16: ; preds = %12, %9
%17 = phi i8 [ %11, %9 ], [ %15, %12 ]
ret i8 %17
}