#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_RDWR;
int /*<<< orphan*/ SEEK_SET;
int lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
int snprintf(char *, int, char *, unsigned int);
int GPIO__Open(unsigned int gpio) {
  int fd;
  int len;
  int status;
  char buf[64];
  len = snprintf(buf, sizeof(buf),
                 "/sys/class/gpio"
                 "/gpio%d/value",
                 gpio);
  fd = open(buf, O_RDWR);
  if (fd < 0 || len <= 0) {
    perror("gpio/fd_open");
  } else {
    status = lseek(fd, 0, SEEK_SET);
    if (status < 0) {
      printf("error lseek value\n");
    }
    status = read(fd, buf, 2);
    if (status != 2) {
      printf("error read value\n");
    }
    return fd;
  }
  return -1;
}


x86 assembly (clang, -O0):
<GPIO__Open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x60(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x40,%esi
lea    0xe82(%rip),%rdx
call   1040 <snprintf@plt>
mov    %eax,-0x10(%rbp)
lea    -0x60(%rbp),%rdi
mov    0x2e57(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jl     11af <GPIO__Open+0x4f>
cmpl   $0x0,-0x10(%rbp)
jg     11c0 <GPIO__Open+0x60>
lea    0xe67(%rip),%rdi
call   1080 <perror@plt>
jmp    1221 <GPIO__Open+0xc1>
mov    -0xc(%rbp),%edi
mov    0x2e0e(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11ec <GPIO__Open+0x8c>
lea    0xe43(%rip),%rdi
call   1030 <printf@plt>
mov    -0xc(%rbp),%edi
lea    -0x60(%rbp),%rsi
mov    $0x2,%edx
call   1060 <read@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x2,-0x14(%rbp)
je     1216 <GPIO__Open+0xb6>
lea    0xe2c(%rip),%rdi
call   1030 <printf@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1228 <GPIO__Open+0xc8>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<GPIO__Open>:
push   %rbp
push   %rbx
sub    $0x48,%rsp
mov    %edi,%ecx
lea    0xe91(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x40,%esi
call   1040 <snprintf@plt>
mov    %eax,%ebp
mov    0x2e60(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
test   %eax,%eax
js     11e7 <GPIO__Open+0x87>
test   %ebp,%ebp
jle    11e7 <GPIO__Open+0x87>
mov    %eax,%ebx
mov    0x2e35(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1050 <lseek@plt>
test   %eax,%eax
jns    11be <GPIO__Open+0x5e>
lea    0xe82(%rip),%rdi
call   1030 <puts@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x2,%edx
call   1060 <read@plt>
cmp    $0x2,%eax
je     11de <GPIO__Open+0x7e>
lea    0xe51(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%eax
add    $0x48,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe2f(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%ebx
jmp    11de <GPIO__Open+0x7e>

LLVM IR (-O0):
@.str = [29 x i8] c"/sys/class/gpio/gpio%d/value\00"
@O_RDWR = global i32 0
@.str.1 = [13 x i8] c"gpio/fd_open\00"
@SEEK_SET = global i32 0
@.str.2 = [19 x i8] c"error lseek value\0A\00"
@.str.3 = [18 x i8] c"error read value\0A\00"
define i32 @GPIO__Open(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca [64 x i8]
store i32 %0, i32* %3
%8 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%9 = load i32, i32* %3
%10 = call i32 @snprintf(i8* %8, i32 64, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %9)
store i32 %10, i32* %5
%11 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%12 = load i32, i32* @O_RDWR
%13 = call i32 @open(i8* %11, i32 %12)
store i32 %13, i32* %4
%14 = load i32, i32* %4
%15 = icmp slt i32 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %1
%17 = load i32, i32* %5
%18 = icmp sle i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %16, %1
%20 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %39
21: ; preds = %16
%22 = load i32, i32* %4
%23 = load i32, i32* @SEEK_SET
%24 = call i32 @lseek(i32 %22, i32 0, i32 %23)
store i32 %24, i32* %6
%25 = load i32, i32* %6
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %21
%28 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %29
29: ; preds = %27, %21
%30 = load i32, i32* %4
%31 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%32 = call i32 @read(i32 %30, i8* %31, i32 2)
store i32 %32, i32* %6
%33 = load i32, i32* %6
%34 = icmp ne i32 %33, 2
br i1 %34, label %35, label %37
35: ; preds = %29
%36 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
br label %37
37: ; preds = %35, %29
%38 = load i32, i32* %4
store i32 %38, i32* %2
br label %40
39: ; preds = %19
store i32 -1, i32* %2
br label %40
40: ; preds = %39, %37
%41 = load i32, i32* %2
ret i32 %41
}

LLVM IR (-O2):
@.str = [29 x i8] c"/sys/class/gpio/gpio%d/value\00"
@O_RDWR = global i32 0
@.str.1 = [13 x i8] c"gpio/fd_open\00"
@SEEK_SET = global i32 0
@str = [17 x i8] c"error read value\00"
@str.4 = [18 x i8] c"error lseek value\00"
define i32 @GPIO__Open(i32 %0) {
%2 = alloca [64 x i8]
%3 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 0
%4 = call i32 @snprintf(i8* %3, i32 64, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %0)
%5 = load i32, i32* @O_RDWR
%6 = call i32 @open(i8* %3, i32 %5)
%7 = icmp slt i32 %6, 0
%8 = icmp slt i32 %4, 1
%9 = select i1 %7, i1 true, i1 %8
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %23
12: ; preds = %1
%13 = load i32, i32* @SEEK_SET
%14 = call i32 @lseek(i32 %6, i32 0, i32 %13)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %12
%17 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.4, i64 0, i64 0))
br label %18
18: ; preds = %16, %12
%19 = call i32 @read(i32 %6, i8* %3, i32 2)
%20 = icmp eq i32 %19, 2
br i1 %20, label %23, label %21
21: ; preds = %18
%22 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
br label %23
23: ; preds = %18, %21, %10
%24 = phi i32 [ -1, %10 ], [ %6, %21 ], [ %6, %18 ]
ret i32 %24
}