#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_WRONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int snprintf(char *, int, char *, unsigned int);
int write(int, char *, int);
int gpio_export(unsigned int gpio) {
  int fd, len;
  char buf[64];
  fd = open("/sys/class/gpio"
            "/export",
            O_WRONLY);
  if (fd < 0) {
    perror("gpio/export");
    return fd;
  }
  len = snprintf(buf, sizeof(buf), "%d", gpio);
  if (len < 0)
    return len;
  len = write(fd, buf, len);
  if (len < 0)
    return len;
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<gpio_export>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e76(%rip),%rax
mov    (%rax),%esi
lea    0xe95(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1194 <gpio_export+0x44>
lea    0xe87(%rip),%rdi
call   1070 <perror@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11fa <gpio_export+0xaa>
lea    -0x50(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x40,%esi
lea    0xe70(%rip),%rdx
call   1040 <snprintf@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    11c4 <gpio_export+0x74>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11fa <gpio_export+0xaa>
mov    -0xc(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    -0x10(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    11eb <gpio_export+0x9b>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11fa <gpio_export+0xaa>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gpio_export>:
push   %rbp
push   %rbx
sub    $0x48,%rsp
mov    %edi,%ebp
mov    0x2e79(%rip),%rax
mov    (%rax),%esi
lea    0xe98(%rip),%rdi
call   1060 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11b3 <gpio_export+0x63>
lea    0xe9d(%rip),%rdx
mov    %rsp,%rdi
mov    $0x40,%esi
mov    %ebp,%ecx
call   1040 <snprintf@plt>
test   %eax,%eax
js     11a8 <gpio_export+0x58>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    %eax,%edx
call   1030 <write@plt>
test   %eax,%eax
js     11a8 <gpio_export+0x58>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %ebx,%ebx
jmp    11aa <gpio_export+0x5a>
mov    %eax,%ebx
mov    %ebx,%eax
add    $0x48,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe51(%rip),%rdi
call   1070 <perror@plt>
jmp    11aa <gpio_export+0x5a>

LLVM IR (-O0):
@.str = [23 x i8] c"/sys/class/gpio/export\00"
@O_WRONLY = global i32 0
@.str.1 = [12 x i8] c"gpio/export\00"
@.str.2 = [3 x i8] c"%d\00"
define i32 @gpio_export(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [64 x i8]
store i32 %0, i32* %3
%7 = load i32, i32* @O_WRONLY
%8 = call i32 @open(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 %14
11: ; preds = %1
%12 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%13 = load i32, i32* %4
store i32 %13, i32* %2
br label %34
14: ; preds = %1
%15 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%16 = load i32, i32* %3
%17 = call i32 @snprintf(i8* %15, i32 64, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %16)
store i32 %17, i32* %5
%18 = load i32, i32* %5
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %14
%21 = load i32, i32* %5
store i32 %21, i32* %2
br label %34
22: ; preds = %14
%23 = load i32, i32* %4
%24 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%25 = load i32, i32* %5
%26 = call i32 @write(i32 %23, i8* %24, i32 %25)
store i32 %26, i32* %5
%27 = load i32, i32* %5
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %22
%30 = load i32, i32* %5
store i32 %30, i32* %2
br label %34
31: ; preds = %22
%32 = load i32, i32* %4
%33 = call i32 @close(i32 %32)
store i32 0, i32* %2
br label %34
34: ; preds = %31, %29, %20, %11
%35 = load i32, i32* %2
ret i32 %35
}

LLVM IR (-O2):
@.str = [23 x i8] c"/sys/class/gpio/export\00"
@O_WRONLY = global i32 0
@.str.1 = [12 x i8] c"gpio/export\00"
@.str.2 = [3 x i8] c"%d\00"
define i32 @gpio_export(i32 %0) {
%2 = alloca [64 x i8]
%3 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 0
%4 = load i32, i32* @O_WRONLY
%5 = tail call i32 @open(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %17
9: ; preds = %1
%10 = call i32 @snprintf(i8* %3, i32 64, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %0)
%11 = icmp slt i32 %10, 0
br i1 %11, label %17, label %12
12: ; preds = %9
%13 = call i32 @write(i32 %5, i8* %3, i32 %10)
%14 = icmp slt i32 %13, 0
br i1 %14, label %17, label %15
15: ; preds = %12
%16 = call i32 @close(i32 %5)
br label %17
17: ; preds = %12, %9, %15, %7
%18 = phi i32 [ %5, %7 ], [ 0, %15 ], [ %10, %9 ], [ %13, %12 ]
ret i32 %18
}