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

/* 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 write(int, char *, int);
int set_gpio_dir(uint32_t gpio, uint8_t in_out) {
  int fd, ret = 0, w_res = 0;
  char f_name[40] = {0};
  snprintf(f_name, sizeof(f_name), "/sys/class/gpio/gpio%d/direction", gpio);
  fd = open(f_name, O_WRONLY);
  if (fd < 0) {
    printf("%d: ", gpio);
    perror("failed to set gpio direction");
    ret = 1;
    goto out;
  }
  if (in_out == 1)
    w_res = write(fd, "out", 3);
  else
    w_res = write(fd, "in", 2);
  if (w_res < 0) {
    printf("%d: ", gpio);
    perror("failed to set gpio direction");
    ret = 1;
  }
  close(fd);
out:
  return ret;
}


x86 assembly (clang, -O0):
<set_gpio_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1060 <memset@plt>
lea    -0x40(%rbp),%rdi
mov    -0x4(%rbp),%ecx
mov    $0x28,%esi
lea    0xe51(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x40(%rbp),%rdi
mov    0x2e19(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11fa <set_gpio_dir+0x8a>
mov    -0x4(%rbp),%esi
lea    0xe44(%rip),%rdi
call   1040 <printf@plt>
lea    0xe3d(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x10(%rbp)
jmp    126b <set_gpio_dir+0xfb>
cmpl   $0x1,-0x8(%rbp)
jne    1220 <set_gpio_dir+0xb0>
mov    -0xc(%rbp),%edi
lea    0xe35(%rip),%rsi
mov    $0x3,%edx
call   1030 <write@plt>
mov    %eax,-0x14(%rbp)
jmp    1237 <set_gpio_dir+0xc7>
mov    -0xc(%rbp),%edi
lea    0xe1d(%rip),%rsi
mov    $0x2,%edx
call   1030 <write@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1263 <set_gpio_dir+0xf3>
mov    -0x4(%rbp),%esi
lea    0xdd6(%rip),%rdi
call   1040 <printf@plt>
lea    0xdcf(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x10(%rbp)
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
mov    -0x10(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_gpio_dir>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    %esi,%ebx
mov    %edi,%r14d
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
movq   $0x0,0x20(%rsp)
lea    0xe77(%rip),%rdx
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0x28,%esi
mov    %r14d,%ecx
call   1050 <snprintf@plt>
mov    0x2e35(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1070 <open@plt>
test   %eax,%eax
js     11f3 <set_gpio_dir+0x93>
mov    %eax,%ebp
cmp    $0x1,%ebx
jne    11c8 <set_gpio_dir+0x68>
lea    0xe84(%rip),%rsi
mov    %ebp,%edi
mov    $0x3,%edx
jmp    11d6 <set_gpio_dir+0x76>
lea    0xe78(%rip),%rsi
mov    %ebp,%edi
mov    $0x2,%edx
call   1030 <write@plt>
xor    %ebx,%ebx
test   %eax,%eax
js     1215 <set_gpio_dir+0xb5>
mov    %ebp,%edi
call   1060 <close@plt>
mov    %ebx,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe27(%rip),%rdi
mov    %r14d,%esi
call   1040 <printf@plt>
lea    0xe1d(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%ebx
jmp    11e8 <set_gpio_dir+0x88>
lea    0xe05(%rip),%rdi
mov    %r14d,%esi
call   1040 <printf@plt>
lea    0xdfb(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%ebx
jmp    11e1 <set_gpio_dir+0x81>

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

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