#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*/ F_OK;
int /*<<< orphan*/ O_WRONLY;
int access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
scalar_t__ gpio_set_value(int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int snprintf(char *, int, char *, unsigned int);
int /*<<< orphan*/ stderr;
int write(int, char *, int);
int gpio_init_out(unsigned int gpio) {
  int fd;
  int len;
  int wr_err = 0;
  char buf[100];
  snprintf(buf, sizeof(buf),
           "/sys/class/gpio/"
           "gpio%d",
           gpio);
  if (access(buf, F_OK) == -1) {
    fd = open("/sys/class/gpio/"
              "export",
              O_WRONLY);
    if (fd < 0)
      return -1;
    len = snprintf(buf, sizeof(buf), "%d", gpio);
    wr_err += write(fd, buf, len) != len;
    close(fd);
  }
  snprintf(buf, sizeof(buf),
           "/sys/class/gpio/"
           "gpio%d/direction",
           gpio);
  fd = open(buf, O_WRONLY);
  if (fd < 0)
    return -1;
  wr_err += write(fd, "out", 3) != 3;
  close(fd);
  if (gpio_set_value(20, 0) < 0)
    return -1;
  if (wr_err) {
    fprintf(stderr, "Write errors during GPIO init out: %d\n", wr_err);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<gpio_init_out>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x0,-0x14(%rbp)
lea    -0x80(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x64,%esi
lea    0xe68(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x80(%rbp),%rdi
mov    0x2e20(%rip),%rax
mov    (%rax),%esi
call   1090 <access@plt>
cmp    $0xffffffff,%eax
jne    1229 <gpio_init_out+0xb9>
mov    0x2e11(%rip),%rax
mov    (%rax),%esi
lea    0xe4f(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11e6 <gpio_init_out+0x76>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f0 <gpio_init_out+0x180>
lea    -0x80(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x64,%esi
lea    0xe1b(%rip),%rdx
call   1040 <snprintf@plt>
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x80(%rbp),%rsi
mov    -0x10(%rbp),%edx
call   1030 <write@plt>
cmp    -0x10(%rbp),%eax
setne  %al
and    $0x1,%al
movzbl %al,%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
lea    -0x80(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x64,%esi
lea    0xdf2(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x80(%rbp),%rdi
mov    0x2d84(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    126c <gpio_init_out+0xfc>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f0 <gpio_init_out+0x180>
mov    -0xc(%rbp),%edi
lea    0xdd9(%rip),%rsi
mov    $0x3,%edx
call   1030 <write@plt>
cmp    $0x3,%eax
setne  %al
and    $0x1,%al
movzbl %al,%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    $0x14,%edi
xor    %esi,%esi
call   1070 <gpio_set_value@plt>
cmp    $0x0,%rax
jge    12bb <gpio_init_out+0x14b>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f0 <gpio_init_out+0x180>
cmpl   $0x0,-0x14(%rbp)
je     12e9 <gpio_init_out+0x179>
mov    0x2d14(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
lea    0xd7b(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f0 <gpio_init_out+0x180>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gpio_init_out>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x70,%rsp
mov    %edi,%ebp
lea    0xe7b(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x64,%esi
mov    %ebp,%ecx
call   1040 <snprintf@plt>
mov    0x2e2a(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1090 <access@plt>
xor    %r12d,%r12d
cmp    $0xffffffff,%eax
jne    120b <gpio_init_out+0x9b>
mov    0x2e19(%rip),%rax
mov    (%rax),%esi
lea    0xe57(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
js     1291 <gpio_init_out+0x121>
mov    %eax,%r14d
lea    0xe3d(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x64,%esi
mov    %ebp,%ecx
call   1040 <snprintf@plt>
mov    %eax,%r15d
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
xor    %r12d,%r12d
cmp    %r15d,%eax
setne  %r12b
mov    %r14d,%edi
call   1050 <close@plt>
lea    0xe1c(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x64,%esi
mov    %ebp,%ecx
call   1040 <snprintf@plt>
mov    0x2da5(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1080 <open@plt>
test   %eax,%eax
js     1291 <gpio_init_out+0x121>
mov    %eax,%ebp
lea    0xe0d(%rip),%rsi
mov    %eax,%edi
mov    $0x3,%edx
call   1030 <write@plt>
xor    %ebx,%ebx
cmp    $0x3,%eax
setne  %r14b
mov    %ebp,%edi
call   1050 <close@plt>
xor    %ebp,%ebp
mov    $0x14,%edi
xor    %esi,%esi
call   1070 <gpio_set_value@plt>
test   %rax,%rax
js     1291 <gpio_init_out+0x121>
mov    %r14b,%bl
add    %ebx,%r12d
je     1296 <gpio_init_out+0x126>
mov    0x2d60(%rip),%rax
mov    (%rax),%edi
lea    0xdca(%rip),%rsi
mov    %r12d,%edx
call   1060 <fprintf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x70,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [23 x i8] c"/sys/class/gpio/gpio%d\00"
@F_OK = global i32 0
@.str.1 = [23 x i8] c"/sys/class/gpio/export\00"
@O_WRONLY = global i32 0
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [33 x i8] c"/sys/class/gpio/gpio%d/direction\00"
@.str.4 = [4 x i8] c"out\00"
@stderr = global i32 0
@.str.5 = [39 x i8] c"Write errors during GPIO init out: %d\0A\00"
define i32 @gpio_init_out(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca [100 x i8]
store i32 %0, i32* %3
store i32 0, i32* %6
%8 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%9 = load i32, i32* %3
%10 = call i32 @snprintf(i8* %8, i32 100, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%12 = load i32, i32* @F_OK
%13 = call i32 @access(i8* %11, i32 %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %36
15: ; preds = %1
%16 = load i32, i32* @O_WRONLY
%17 = call i32 @open(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %16)
store i32 %17, i32* %4
%18 = load i32, i32* %4
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %15
store i32 -1, i32* %2
br label %66
21: ; preds = %15
%22 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%23 = load i32, i32* %3
%24 = call i32 @snprintf(i8* %22, i32 100, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %23)
store i32 %24, i32* %5
%25 = load i32, i32* %4
%26 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%27 = load i32, i32* %5
%28 = call i32 @write(i32 %25, i8* %26, i32 %27)
%29 = load i32, i32* %5
%30 = icmp ne i32 %28, %29
%31 = zext i1 %30 to i32
%32 = load i32, i32* %6
%33 = add nsw i32 %32, %31
store i32 %33, i32* %6
%34 = load i32, i32* %4
%35 = call i32 @close(i32 %34)
br label %36
36: ; preds = %21, %1
%37 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%38 = load i32, i32* %3
%39 = call i32 @snprintf(i8* %37, i32 100, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i32 %38)
%40 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%41 = load i32, i32* @O_WRONLY
%42 = call i32 @open(i8* %40, i32 %41)
store i32 %42, i32* %4
%43 = load i32, i32* %4
%44 = icmp slt i32 %43, 0
br i1 %44, label %45, label %46
45: ; preds = %36
store i32 -1, i32* %2
br label %66
46: ; preds = %36
%47 = load i32, i32* %4
%48 = call i32 @write(i32 %47, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 3)
%49 = icmp ne i32 %48, 3
%50 = zext i1 %49 to i32
%51 = load i32, i32* %6
%52 = add nsw i32 %51, %50
store i32 %52, i32* %6
%53 = load i32, i32* %4
%54 = call i32 @close(i32 %53)
%55 = call i64 @gpio_set_value(i32 20, i32 0)
%56 = icmp slt i64 %55, 0
br i1 %56, label %57, label %58
57: ; preds = %46
store i32 -1, i32* %2
br label %66
58: ; preds = %46
%59 = load i32, i32* %6
%60 = icmp ne i32 %59, 0
br i1 %60, label %61, label %65
61: ; preds = %58
%62 = load i32, i32* @stderr
%63 = load i32, i32* %6
%64 = call i32 @fprintf(i32 %62, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.5, i64 0, i64 0), i32 %63)
store i32 -1, i32* %2
br label %66
65: ; preds = %58
store i32 0, i32* %2
br label %66
66: ; preds = %65, %61, %57, %45, %20
%67 = load i32, i32* %2
ret i32 %67
}

LLVM IR (-O2):
@.str = [23 x i8] c"/sys/class/gpio/gpio%d\00"
@F_OK = global i32 0
@.str.1 = [23 x i8] c"/sys/class/gpio/export\00"
@O_WRONLY = global i32 0
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [33 x i8] c"/sys/class/gpio/gpio%d/direction\00"
@.str.4 = [4 x i8] c"out\00"
@stderr = global i32 0
@.str.5 = [39 x i8] c"Write errors during GPIO init out: %d\0A\00"
define i32 @gpio_init_out(i32 %0) {
%2 = alloca [100 x i8]
%3 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%4 = call i32 @snprintf(i8* %3, i32 100, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %0)
%5 = load i32, i32* @F_OK
%6 = call i32 @access(i8* %3, i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %18
8: ; preds = %1
%9 = load i32, i32* @O_WRONLY
%10 = call i32 @open(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %37, label %12
12: ; preds = %8
%13 = call i32 @snprintf(i8* %3, i32 100, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %0)
%14 = call i32 @write(i32 %10, i8* %3, i32 %13)
%15 = icmp ne i32 %14, %13
%16 = zext i1 %15 to i32
%17 = call i32 @close(i32 %10)
br label %18
18: ; preds = %12, %1
%19 = phi i32 [ %16, %12 ], [ 0, %1 ]
%20 = call i32 @snprintf(i8* %3, i32 100, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i32 %0)
%21 = load i32, i32* @O_WRONLY
%22 = call i32 @open(i8* %3, i32 %21)
%23 = icmp slt i32 %22, 0
br i1 %23, label %37, label %24
24: ; preds = %18
%25 = call i32 @write(i32 %22, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 3)
%26 = icmp ne i32 %25, 3
%27 = zext i1 %26 to i32
%28 = add nuw nsw i32 %19, %27
%29 = call i32 @close(i32 %22)
%30 = call i64 @gpio_set_value(i32 20, i32 0)
%31 = icmp slt i64 %30, 0
br i1 %31, label %37, label %32
32: ; preds = %24
%33 = icmp eq i32 %28, 0
br i1 %33, label %37, label %34
34: ; preds = %32
%35 = load i32, i32* @stderr
%36 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.5, i64 0, i64 0), i32 %28)
br label %37
37: ; preds = %32, %24, %18, %8, %34
%38 = phi i32 [ -1, %34 ], [ -1, %8 ], [ -1, %18 ], [ -1, %24 ], [ 0, %32 ]
ret i32 %38
}