#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*/ F_OK;
int /*<<< orphan*/ O_WRONLY;
int access(char *, int /*<<< orphan*/);
int asprintf(char **, char *, unsigned int);
int /*<<< orphan*/ close(int);
int dprintf(int, char *, unsigned int);
int /*<<< orphan*/ free(char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int gpio_export(unsigned int gpio) {
  int ret;
  char *directory;
  ret = asprintf(&directory, "/sys/class/gpio/gpio%u", gpio);
  if (ret < 0)
    return ret;
  if (access(directory, F_OK) != -1) {
    return 0;
  }
  free(directory);
  int f = open("/sys/class/gpio/export", O_WRONLY);
  if (f < 0) {
    perror("open");
    return f;
  }
  ret = dprintf(f, "%u", gpio);
  close(f);
  return ret < 0 ? ret : 0;
}


x86 assembly (clang, -O0):
<gpio_export>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edx
lea    -0x18(%rbp),%rdi
lea    0xe77(%rip),%rsi
call   1060 <asprintf@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11a6 <gpio_export+0x36>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1251 <gpio_export+0xe1>
mov    -0x18(%rbp),%rdi
mov    0x2e1f(%rip),%rax
mov    (%rax),%esi
call   1080 <access@plt>
cmp    $0xffffffff,%eax
je     11cd <gpio_export+0x5d>
movl   $0x0,-0x4(%rbp)
jmp    1251 <gpio_export+0xe1>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    0x2dfb(%rip),%rax
mov    (%rax),%esi
lea    0xe31(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    120f <gpio_export+0x9f>
lea    0xe2f(%rip),%rdi
call   1090 <perror@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1251 <gpio_export+0xe1>
mov    -0x1c(%rbp),%edi
mov    -0x8(%rbp),%edx
lea    0xdf8(%rip),%rsi
call   1050 <dprintf@plt>
mov    %eax,-0xc(%rbp)
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
cmpl   $0x0,-0xc(%rbp)
jge    1241 <gpio_export+0xd1>
mov    -0xc(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    124b <gpio_export+0xdb>
xor    %eax,%eax
mov    %eax,-0x20(%rbp)
jmp    124b <gpio_export+0xdb>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gpio_export>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
lea    0xe84(%rip),%rsi
mov    %rsp,%rdi
mov    %ebx,%edx
call   1060 <asprintf@plt>
test   %eax,%eax
js     11e9 <gpio_export+0x79>
mov    (%rsp),%rdi
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
call   1080 <access@plt>
xor    %ebp,%ebp
cmp    $0xffffffff,%eax
jne    11eb <gpio_export+0x7b>
mov    (%rsp),%rdi
call   1030 <free@plt>
mov    0x2e25(%rip),%rax
mov    (%rax),%esi
lea    0xe5b(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
js     11f4 <gpio_export+0x84>
lea    0xe46(%rip),%rsi
mov    %ebp,%edi
mov    %ebx,%edx
call   1050 <dprintf@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1040 <close@plt>
mov    %ebx,%ebp
sar    $0x1f,%ebp
and    %ebx,%ebp
jmp    11eb <gpio_export+0x7b>
mov    %eax,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe33(%rip),%rdi
call   1090 <perror@plt>
jmp    11eb <gpio_export+0x7b>

LLVM IR (-O0):
@.str = [23 x i8] c"/sys/class/gpio/gpio%u\00"
@F_OK = global i32 0
@.str.1 = [23 x i8] c"/sys/class/gpio/export\00"
@O_WRONLY = global i32 0
@.str.2 = [5 x i8] c"open\00"
@.str.3 = [3 x i8] c"%u\00"
define i32 @gpio_export(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
store i32 %0, i32* %3
%7 = load i32, i32* %3
%8 = call i32 @asprintf(i8** %5, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %7)
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = load i32, i32* %4
store i32 %12, i32* %2
br label %42
13: ; preds = %1
%14 = load i8*, i8** %5
%15 = load i32, i32* @F_OK
%16 = call i32 @access(i8* %14, i32 %15)
%17 = icmp ne i32 %16, -1
br i1 %17, label %18, label %19
18: ; preds = %13
store i32 0, i32* %2
br label %42
19: ; preds = %13
%20 = load i8*, i8** %5
%21 = call i32 @free(i8* %20)
%22 = load i32, i32* @O_WRONLY
%23 = call i32 @open(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %22)
store i32 %23, i32* %6
%24 = load i32, i32* %6
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %19
%27 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%28 = load i32, i32* %6
store i32 %28, i32* %2
br label %42
29: ; preds = %19
%30 = load i32, i32* %6
%31 = load i32, i32* %3
%32 = call i32 @dprintf(i32 %30, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %31)
store i32 %32, i32* %4
%33 = load i32, i32* %6
%34 = call i32 @close(i32 %33)
%35 = load i32, i32* %4
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %29
%38 = load i32, i32* %4
br label %40
39: ; preds = %29
br label %40
40: ; preds = %39, %37
%41 = phi i32 [ %38, %37 ], [ 0, %39 ]
store i32 %41, i32* %2
br label %42
42: ; preds = %40, %26, %18, %11
%43 = load i32, i32* %2
ret i32 %43
}

LLVM IR (-O2):
@.str = [23 x i8] c"/sys/class/gpio/gpio%u\00"
@F_OK = global i32 0
@.str.1 = [23 x i8] c"/sys/class/gpio/export\00"
@O_WRONLY = global i32 0
@.str.2 = [5 x i8] c"open\00"
@.str.3 = [3 x i8] c"%u\00"
define i32 @gpio_export(i32 %0) {
%2 = alloca i8*
%3 = bitcast i8** %2 to i8*
%4 = call i32 @asprintf(i8** %2, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %0)
%5 = icmp slt i32 %4, 0
br i1 %5, label %24, label %6
6: ; preds = %1
%7 = load i8*, i8** %2
%8 = load i32, i32* @F_OK
%9 = call i32 @access(i8* %7, i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %24
11: ; preds = %6
%12 = load i8*, i8** %2
%13 = call i32 @free(i8* %12)
%14 = load i32, i32* @O_WRONLY
%15 = call i32 @open(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %14)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %11
%18 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %24
19: ; preds = %11
%20 = call i32 @dprintf(i32 %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %0)
%21 = call i32 @close(i32 %15)
%22 = icmp slt i32 %20, 0
%23 = select i1 %22, i32 %20, i32 0
br label %24
24: ; preds = %17, %19, %6, %1
%25 = phi i32 [ %4, %1 ], [ 0, %6 ], [ %15, %17 ], [ %23, %19 ]
ret i32 %25
}