#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 uint16_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int EXIT_SUCCESS;
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ strlen(char *);
int write(int, char *, int /*<<< orphan*/);
int gpio_ex_unex_port(uint16_t gpio, bool ex) {
  int r;
  int fd;
  const char *sys_class_gpio_export = "/sys/class/gpio/export";
  const char *sys_class_gpio_unexport = "/sys/class/gpio/unexport";
  char buf[16];
  r = open(ex ? sys_class_gpio_export : sys_class_gpio_unexport, O_WRONLY);
  if (-1 == r) {
    goto out;
  }
  fd = r;
  memset(buf, 0, sizeof(buf));
  snprintf(buf, sizeof(buf) - 1, "%u", gpio);
  r = write(fd, buf, strlen(buf));
  if (-1 == r) {
    goto closefd;
  }
  r = EXIT_SUCCESS;
closefd:
  close(fd);
  fd = -1;
out:
  return r;
}


x86 assembly (clang, -O0):
<gpio_ex_unex_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
lea    0xe8b(%rip),%rax
mov    %rax,-0x18(%rbp)
lea    0xe97(%rip),%rax
mov    %rax,-0x20(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     119b <gpio_ex_unex_port+0x3b>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    11a3 <gpio_ex_unex_port+0x43>
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    0x2e22(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0xc(%rbp)
mov    $0xffffffff,%eax
cmp    -0xc(%rbp),%eax
jne    11cb <gpio_ex_unex_port+0x6b>
jmp    124f <gpio_ex_unex_port+0xef>
mov    -0xc(%rbp),%eax
mov    %eax,-0x10(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <memset@plt>
lea    -0x30(%rbp),%rdi
mov    -0x4(%rbp),%ecx
mov    $0xf,%esi
lea    0xe3c(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x10(%rbp),%eax
mov    %eax,-0x44(%rbp)
lea    -0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
lea    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,-0xc(%rbp)
mov    $0xffffffff,%eax
cmp    -0xc(%rbp),%eax
jne    1234 <gpio_ex_unex_port+0xd4>
jmp    1240 <gpio_ex_unex_port+0xe0>
mov    0x2da5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    -0x10(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x10(%rbp)
mov    -0xc(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gpio_ex_unex_port>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%r14d
test   %esi,%esi
lea    0xea3(%rip),%rax
lea    0xe85(%rip),%rdi
cmove  %rax,%rdi
mov    0x2e4a(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     11ea <gpio_ex_unex_port+0x8a>
mov    %eax,%ebx
mov    %rsp,%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <memset@plt>
lea    0xe83(%rip),%rdx
mov    %rbp,%rdi
mov    $0xf,%esi
mov    %r14d,%ecx
call   1050 <snprintf@plt>
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     11e1 <gpio_ex_unex_port+0x81>
mov    0x2e01(%rip),%rax
mov    (%rax),%ebp
mov    %ebx,%edi
call   1070 <close@plt>
jmp    11ef <gpio_ex_unex_port+0x8f>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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