#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 */

/* Variables and functions */
int /*<<< orphan*/ O_WRONLY;
scalar_t__ close(int);
int /*<<< orphan*/ memset(char *, int, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ warnx(char *);
scalar_t__ write(int, char *, int /*<<< orphan*/);
int set_gpio_direction(const char *gpio_pin) {
  int fd;
  char fn[48];
  if (gpio_pin == NULL || *gpio_pin == '\0') {
    return -1;
  }
  memset(fn, 0x00, 48);
  snprintf(fn, 48 - 1,
           "/tmp"
           "/gpio/gpio%s/direction",
           gpio_pin);
  if ((fd = open(fn, O_WRONLY)) < 0) {
    warnx("open(SYS_GPIO_DIRECTION) failed");
    return -1;
  }
  if (write(fd, "in", strlen("in")) < 0) {
    warnx("write(SYS_GPIO_DIRECTION) failed");
    return -1;
  }
  if (close(fd) < 0) {
    warnx("close(SYS_GPIO_DIRECTION) failed");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<set_gpio_direction>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1197 <set_gpio_direction+0x27>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    11a3 <set_gpio_direction+0x33>
movl   $0xffffffff,-0x4(%rbp)
jmp    1278 <set_gpio_direction+0x108>
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x30,%edx
call   1070 <memset@plt>
lea    -0x50(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x2f,%esi
lea    0xe39(%rip),%rdx
call   1060 <snprintf@plt>
lea    -0x50(%rbp),%rdi
mov    0x2e01(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1202 <set_gpio_direction+0x92>
lea    0xe2a(%rip),%rdi
call   1050 <warnx@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1278 <set_gpio_direction+0x108>
mov    -0x14(%rbp),%eax
mov    %eax,-0x54(%rbp)
lea    0xe2c(%rip),%rdi
call   1040 <strlen@plt>
mov    -0x54(%rbp),%edi
mov    %eax,%edx
lea    0xe1b(%rip),%rsi
call   1030 <write@plt>
cmp    $0x0,%rax
jge    1247 <set_gpio_direction+0xd7>
lea    0xe08(%rip),%rdi
call   1050 <warnx@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1278 <set_gpio_direction+0x108>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
cmp    $0x0,%rax
jge    1271 <set_gpio_direction+0x101>
lea    0xdff(%rip),%rdi
call   1050 <warnx@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1278 <set_gpio_direction+0x108>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_gpio_direction>:
push   %r14
push   %rbx
sub    $0x38,%rsp
mov    $0xffffffff,%eax
test   %rdi,%rdi
je     1225 <set_gpio_direction+0xb5>
mov    %rdi,%rbx
cmpb   $0x0,(%rdi)
je     1225 <set_gpio_direction+0xb5>
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x30,%edx
call   1070 <memset@plt>
lea    0xe56(%rip),%rdx
mov    %r14,%rdi
mov    $0x2f,%esi
mov    %rbx,%rcx
call   1060 <snprintf@plt>
mov    0x2e17(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1090 <open@plt>
test   %eax,%eax
js     120b <set_gpio_direction+0x9b>
mov    %eax,%ebx
lea    0xe63(%rip),%r14
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
test   %rax,%rax
js     1214 <set_gpio_direction+0xa4>
mov    %ebx,%edi
call   1080 <close@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jns    1225 <set_gpio_direction+0xb5>
lea    0xe56(%rip),%rdi
jmp    121b <set_gpio_direction+0xab>
lea    0xe09(%rip),%rdi
jmp    121b <set_gpio_direction+0xab>
lea    0xe23(%rip),%rdi
call   1050 <warnx@plt>
mov    $0xffffffff,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [27 x i8] c"/tmp/gpio/gpio%s/direction\00"
@O_WRONLY = global i32 0
@.str.1 = [32 x i8] c"open(SYS_GPIO_DIRECTION) failed\00"
@.str.2 = [3 x i8] c"in\00"
@.str.3 = [33 x i8] c"write(SYS_GPIO_DIRECTION) failed\00"
@.str.4 = [33 x i8] c"close(SYS_GPIO_DIRECTION) failed\00"
define i32 @set_gpio_direction(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca [48 x i8]
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = icmp eq i8* %6, null
br i1 %7, label %13, label %8
8: ; preds = %1
%9 = load i8*, i8** %3
%10 = load i8, i8* %9
%11 = sext i8 %10 to i32
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %8, %1
store i32 -1, i32* %2
br label %40
14: ; preds = %8
%15 = getelementptr inbounds [48 x i8], [48 x i8]* %5, i64 0, i64 0
%16 = call i32 @memset(i8* %15, i32 0, i32 48)
%17 = getelementptr inbounds [48 x i8], [48 x i8]* %5, i64 0, i64 0
%18 = load i8*, i8** %3
%19 = call i32 @snprintf(i8* %17, i32 47, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %18)
%20 = getelementptr inbounds [48 x i8], [48 x i8]* %5, i64 0, i64 0
%21 = load i32, i32* @O_WRONLY
%22 = call i32 @open(i8* %20, i32 %21)
store i32 %22, i32* %4
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %14
%25 = call i32 @warnx(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %40
26: ; preds = %14
%27 = load i32, i32* %4
%28 = call i32 @strlen(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%29 = call i64 @write(i32 %27, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %28)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %26
%32 = call i32 @warnx(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %2
br label %40
33: ; preds = %26
%34 = load i32, i32* %4
%35 = call i64 @close(i32 %34)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %33
%38 = call i32 @warnx(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %2
br label %40
39: ; preds = %33
store i32 0, i32* %2
br label %40
40: ; preds = %39, %37, %31, %24, %13
%41 = load i32, i32* %2
ret i32 %41
}

LLVM IR (-O2):
@.str = [27 x i8] c"/tmp/gpio/gpio%s/direction\00"
@O_WRONLY = global i32 0
@.str.1 = [32 x i8] c"open(SYS_GPIO_DIRECTION) failed\00"
@.str.2 = [3 x i8] c"in\00"
@.str.3 = [33 x i8] c"write(SYS_GPIO_DIRECTION) failed\00"
@.str.4 = [33 x i8] c"close(SYS_GPIO_DIRECTION) failed\00"
define i32 @set_gpio_direction(i8* %0) {
%2 = alloca [48 x i8]
%3 = getelementptr inbounds [48 x i8], [48 x i8]* %2, i64 0, i64 0
%4 = icmp eq i8* %0, null
br i1 %4, label %24, label %5
5: ; preds = %1
%6 = load i8, i8* %0
%7 = icmp eq i8 %6, 0
br i1 %7, label %24, label %8
8: ; preds = %5
%9 = call i32 @memset(i8* %3, i32 0, i32 48)
%10 = call i32 @snprintf(i8* %3, i32 47, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %0)
%11 = load i32, i32* @O_WRONLY
%12 = call i32 @open(i8* %3, i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %21, label %14
14: ; preds = %8
%15 = call i32 @strlen(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%16 = call i64 @write(i32 %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %15)
%17 = icmp slt i64 %16, 0
br i1 %17, label %21, label %18
18: ; preds = %14
%19 = call i64 @close(i32 %12)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %18, %14, %8
%22 = phi i8* [ getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), %8 ], [ getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), %14 ], [ getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), %18 ]
%23 = call i32 @warnx(i8* %22)
br label %24
24: ; preds = %21, %18, %1, %5
%25 = phi i32 [ -1, %5 ], [ -1, %1 ], [ 0, %18 ], [ -1, %21 ]
ret i32 %25
}