#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 uint32_t;

/* Variables and functions */
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ strlen(char *);
scalar_t__ write(int, char *, int /*<<< orphan*/);
int close_gpio(uint32_t gpio) {
  int fd, ret = 0;
  char buf[4] = {0};
  fd = open("/sys/class/gpio/unexport", O_WRONLY);
  if (fd < 0) {
    printf("%d: ", gpio);
    perror("failed to close gpio");
    ret = 1;
    goto out;
  }
  snprintf(buf, 4, "%d", gpio);
  if (write(fd, buf, strlen(buf)) < 0) {
    printf("%d: ", gpio);
    perror("failed to close gpio");
    ret = 1;
  }
  close(fd);
out:
  return ret;
}


x86 assembly (clang, -O0):
<close_gpio>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0xc(%rbp)
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1070 <memset@plt>
mov    0x2e2f(%rip),%rax
mov    (%rax),%esi
lea    0xe4e(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11eb <close_gpio+0x6b>
mov    -0x4(%rbp),%esi
lea    0xe4b(%rip),%rdi
call   1050 <printf@plt>
lea    0xe44(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0xc(%rbp)
jmp    125c <close_gpio+0xdc>
lea    -0x10(%rbp),%rdi
mov    -0x4(%rbp),%ecx
mov    $0x4,%esi
lea    0xe35(%rip),%rdx
call   1060 <snprintf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x1c(%rbp)
lea    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
lea    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    1254 <close_gpio+0xd4>
mov    -0x4(%rbp),%esi
lea    0xddd(%rip),%rdi
call   1050 <printf@plt>
lea    0xdd6(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0xc(%rbp)
mov    -0x8(%rbp),%edi
call   1080 <close@plt>
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<close_gpio>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%r14d
movl   $0x0,0xc(%rsp)
mov    0x2e4e(%rip),%rax
mov    (%rax),%esi
lea    0xe6d(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
js     11e7 <close_gpio+0x77>
mov    %eax,%ebp
lea    0xe8e(%rip),%rdx
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x4,%esi
mov    %r14d,%ecx
call   1060 <snprintf@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
xor    %ebx,%ebx
test   %rax,%rax
js     1209 <close_gpio+0x99>
mov    %ebp,%edi
call   1070 <close@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe2b(%rip),%rdi
mov    %r14d,%esi
call   1050 <printf@plt>
lea    0xe21(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%ebx
jmp    11dc <close_gpio+0x6c>
lea    0xe09(%rip),%rdi
mov    %r14d,%esi
call   1050 <printf@plt>
lea    0xdff(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%ebx
jmp    11d5 <close_gpio+0x65>

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

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