#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*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int snprintf(char *, int, char *, unsigned int);
int write(int, char *, int);
int gpio_set_dir(unsigned int gpio, unsigned int out_flag) {
  int fd, len;
  char buf[64];
  len = snprintf(buf, sizeof(buf),
                 "/sys/class/gpio"
                 "/gpio%d/direction",
                 gpio);
  if (len < 0)
    return len;
  fd = open(buf, O_WRONLY);
  if (fd < 0) {
    perror("gpio/direction");
    return fd;
  }
  if (out_flag)
    len = write(fd, "out", 4);
  else
    len = write(fd, "in", 3);
  if (len < 0)
    return len;
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<gpio_set_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x60(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x40,%esi
lea    0xe8f(%rip),%rdx
call   1040 <snprintf@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    118e <gpio_set_dir+0x3e>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1225 <gpio_set_dir+0xd5>
lea    -0x60(%rbp),%rdi
mov    0x2e3f(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    11c4 <gpio_set_dir+0x74>
lea    0xe6d(%rip),%rdi
call   1070 <perror@plt>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1225 <gpio_set_dir+0xd5>
cmpl   $0x0,-0xc(%rbp)
je     11ea <gpio_set_dir+0x9a>
mov    -0x10(%rbp),%edi
lea    0xe58(%rip),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
mov    %eax,-0x14(%rbp)
jmp    1201 <gpio_set_dir+0xb1>
mov    -0x10(%rbp),%edi
lea    0xe40(%rip),%rsi
mov    $0x3,%edx
call   1030 <write@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1216 <gpio_set_dir+0xc6>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1225 <gpio_set_dir+0xd5>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gpio_set_dir>:
push   %rbp
push   %rbx
sub    $0x48,%rsp
mov    %esi,%ebp
mov    %edi,%ecx
lea    0xe9f(%rip),%rdx
mov    %rsp,%rdi
mov    $0x40,%esi
call   1040 <snprintf@plt>
test   %eax,%eax
js     11bd <gpio_set_dir+0x6d>
mov    0x2e5f(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdi
call   1060 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11c4 <gpio_set_dir+0x74>
test   %ebp,%ebp
je     119d <gpio_set_dir+0x4d>
lea    0xe9c(%rip),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
jmp    11ab <gpio_set_dir+0x5b>
lea    0xe90(%rip),%rsi
mov    %ebx,%edi
mov    $0x3,%edx
call   1030 <write@plt>
test   %eax,%eax
js     11bd <gpio_set_dir+0x6d>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x48,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe56(%rip),%rdi
call   1070 <perror@plt>
mov    %ebx,%eax
jmp    11bd <gpio_set_dir+0x6d>

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

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