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

/* Variables and functions */
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ W_OK;
int /*<<< orphan*/ access(char *, int /*<<< orphan*/);
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 init_gpio(uint32_t gpio) {
  int fd, ret = 0;
  char buf[4] = {0};
  char f_name[40] = {0};
  snprintf(f_name, sizeof(f_name), "/sys/class/gpio/gpio%d/direction", gpio);
  if (!access(f_name, W_OK))
    goto out;
  fd = open("/sys/class/gpio/export", O_WRONLY);
  if (fd < 0) {
    printf("%d: ", gpio);
    perror("failed to initialize gpio");
    ret = 1;
    goto out;
  }
  snprintf(buf, sizeof(buf), "%d", gpio);
  if (write(fd, buf, strlen(buf)) < 0) {
    printf("%d: ", gpio);
    perror("failed to initialize gpio");
    ret = 1;
  }
  close(fd);
out:
  return ret;
}


x86 assembly (clang, -O0):
<init_gpio>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0xc(%rbp)
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1070 <memset@plt>
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1070 <memset@plt>
lea    -0x40(%rbp),%rdi
mov    -0x4(%rbp),%ecx
mov    $0x28,%esi
lea    0xe2b(%rip),%rdx
call   1060 <snprintf@plt>
lea    -0x40(%rbp),%rdi
mov    0x2e03(%rip),%rax
mov    (%rax),%esi
call   10a0 <access@plt>
cmp    $0x0,%eax
jne    11fa <init_gpio+0x6a>
jmp    12b4 <init_gpio+0x124>
mov    0x2dcf(%rip),%rax
mov    (%rax),%esi
lea    0xe17(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1243 <init_gpio+0xb3>
mov    -0x4(%rbp),%esi
lea    0xe12(%rip),%rdi
call   1050 <printf@plt>
lea    0xe0b(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0xc(%rbp)
jmp    12b4 <init_gpio+0x124>
lea    -0x10(%rbp),%rdi
mov    -0x4(%rbp),%ecx
mov    $0x4,%esi
lea    0xe01(%rip),%rdx
call   1060 <snprintf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4c(%rbp)
lea    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
lea    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    12ac <init_gpio+0x11c>
mov    -0x4(%rbp),%esi
lea    0xda4(%rip),%rdi
call   1050 <printf@plt>
lea    0xd9d(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0xc(%rbp)
mov    -0x8(%rbp),%edi
call   1080 <close@plt>
mov    -0xc(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_gpio>:
push   %rbp
push   %r14
push   %rbx
sub    $0x40,%rsp
mov    %edi,%r14d
movl   $0x0,0xc(%rsp)
xorps  %xmm0,%xmm0
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movq   $0x0,0x30(%rsp)
lea    0xe50(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x28,%esi
mov    %r14d,%ecx
call   1060 <snprintf@plt>
mov    0x2e1c(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1090 <access@plt>
test   %eax,%eax
je     1235 <init_gpio+0xb5>
mov    0x2def(%rip),%rax
mov    (%rax),%esi
lea    0xe37(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
js     1242 <init_gpio+0xc2>
mov    %eax,%ebp
lea    0xe5b(%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     1264 <init_gpio+0xe4>
mov    %ebp,%edi
call   1070 <close@plt>
jmp    1237 <init_gpio+0xb7>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdef(%rip),%rdi
mov    %r14d,%esi
call   1050 <printf@plt>
lea    0xde5(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%ebx
jmp    1237 <init_gpio+0xb7>
lea    0xdcd(%rip),%rdi
mov    %r14d,%esi
call   1050 <printf@plt>
lea    0xdc3(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%ebx
jmp    122c <init_gpio+0xac>

LLVM IR (-O0):
@.str = [33 x i8] c"/sys/class/gpio/gpio%d/direction\00"
@W_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"%d: \00"
@.str.3 = [26 x i8] c"failed to initialize gpio\00"
@.str.4 = [3 x i8] c"%d\00"
define i32 @init_gpio(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [4 x i8]
%6 = alloca [40 x i8]
store i32 %0, i32* %2
store i32 0, i32* %4
%7 = bitcast [4 x i8]* %5 to i8*
%8 = bitcast [40 x i8]* %6 to i8*
%9 = getelementptr inbounds [40 x i8], [40 x i8]* %6, i64 0, i64 0
%10 = load i32, i32* %2
%11 = call i32 @snprintf(i8* %9, i32 40, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = getelementptr inbounds [40 x i8], [40 x i8]* %6, i64 0, i64 0
%13 = load i32, i32* @W_OK
%14 = call i32 @access(i8* %12, i32 %13)
%15 = icmp ne i32 %14, 0
br i1 %15, label %17, label %16
16: ; preds = %1
br label %43
17: ; preds = %1
%18 = load i32, i32* @O_WRONLY
%19 = call i32 @open(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %18)
store i32 %19, i32* %3
%20 = load i32, i32* %3
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %17
%23 = load i32, i32* %2
%24 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 %23)
%25 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %4
br label %43
26: ; preds = %17
%27 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 0
%28 = load i32, i32* %2
%29 = call i32 @snprintf(i8* %27, i32 4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 %28)
%30 = load i32, i32* %3
%31 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 0
%32 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 0
%33 = call i32 @strlen(i8* %32)
%34 = call i64 @write(i32 %30, i8* %31, i32 %33)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %40
36: ; preds = %26
%37 = load i32, i32* %2
%38 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 %37)
%39 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %4
br label %40
40: ; preds = %36, %26
%41 = load i32, i32* %3
%42 = call i32 @close(i32 %41)
br label %43
43: ; preds = %40, %22, %16
%44 = load i32, i32* %4
ret i32 %44
}

LLVM IR (-O2):
@.str = [33 x i8] c"/sys/class/gpio/gpio%d/direction\00"
@W_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"%d: \00"
@.str.3 = [26 x i8] c"failed to initialize gpio\00"
@.str.4 = [3 x i8] c"%d\00"
define i32 @init_gpio(i32 %0) {
%2 = alloca i32
%3 = alloca [40 x i8]
%4 = bitcast i32* %2 to i8*
store i32 0, i32* %2
%5 = getelementptr inbounds [40 x i8], [40 x i8]* %3, i64 0, i64 0
%6 = call i32 @snprintf(i8* %5, i32 40, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %0)
%7 = load i32, i32* @W_OK
%8 = call i32 @access(i8* %5, i32 %7)
%9 = icmp eq i32 %8, 0
br i1 %9, label %28, label %10
10: ; preds = %1
%11 = load i32, i32* @O_WRONLY
%12 = call i32 @open(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %10
%15 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 %0)
%16 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
br label %28
17: ; preds = %10
%18 = call i32 @snprintf(i8* %4, i32 4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 %0)
%19 = call i32 @strlen(i8* %4)
%20 = call i64 @write(i32 %12, i8* %4, i32 %19)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %17
%23 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 %0)
%24 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
br label %25
25: ; preds = %22, %17
%26 = phi i32 [ 1, %22 ], [ 0, %17 ]
%27 = call i32 @close(i32 %12)
br label %28
28: ; preds = %1, %25, %14
%29 = phi i32 [ 1, %14 ], [ %26, %25 ], [ 0, %1 ]
ret i32 %29
}