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


x86 assembly (clang, -O0):
<gpio_set_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
lea    -0x50(%rbp),%rdi
mov    -0x4(%rbp),%ecx
mov    $0x40,%esi
lea    0xe8f(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x50(%rbp),%rdi
mov    0x2e57(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11a1 <gpio_set_dir+0x51>
lea    0xe85(%rip),%rdi
call   1060 <error_exit@plt>
cmpl   $0x0,-0x8(%rbp)
je     11c4 <gpio_set_dir+0x74>
mov    -0xc(%rbp),%edi
lea    0xe7b(%rip),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
jmp    11d8 <gpio_set_dir+0x88>
mov    -0xc(%rbp),%edi
lea    0xe67(%rip),%rsi
mov    $0x3,%edx
call   1030 <write@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x50,%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,%rbx
mov    %rbx,%rdi
mov    $0x40,%esi
call   1040 <snprintf@plt>
mov    0x2e60(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     119c <gpio_set_dir+0x4c>
test   %ebp,%ebp
je     11ac <gpio_set_dir+0x5c>
lea    0xe9d(%rip),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
jmp    11ba <gpio_set_dir+0x6a>
lea    0xe7e(%rip),%rdi
call   1060 <error_exit@plt>
test   %ebp,%ebp
jne    118c <gpio_set_dir+0x3c>
lea    0xe82(%rip),%rsi
mov    %ebx,%edi
mov    $0x3,%edx
call   1030 <write@plt>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x48,%rsp
pop    %rbx
pop    %rbp
ret

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 = [5 x i8] c"out\0A\00"
@.str.3 = [4 x i8] c"in\0A\00"
define i32 @gpio_set_dir(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [64 x i8]
store i32 %0, i32* %3
store i32 %1, i32* %4
%7 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%8 = load i32, i32* %3
%9 = call i32 @snprintf(i8* %7, i32 64, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%11 = load i32, i32* @O_WRONLY
%12 = call i32 @open(i8* %10, i32 %11)
store i32 %12, i32* %5
%13 = load i32, i32* %5
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = call i32 @error_exit(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %17
17: ; preds = %15, %2
%18 = load i32, i32* %4
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %17
%21 = load i32, i32* %5
%22 = call i32 @write(i32 %21, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 4)
br label %26
23: ; preds = %17
%24 = load i32, i32* %5
%25 = call i32 @write(i32 %24, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 3)
br label %26
26: ; preds = %23, %20
%27 = load i32, i32* %5
%28 = call i32 @close(i32 %27)
ret i32 0
}

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 = [5 x i8] c"out\0A\00"
@.str.3 = [4 x i8] c"in\0A\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 = 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 %11
9: ; preds = %2
%10 = call i32 @error_exit(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %11
11: ; preds = %9, %2
%12 = icmp eq i32 %1, 0
br i1 %12, label %15, label %13
13: ; preds = %11
%14 = call i32 @write(i32 %7, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 4)
br label %17
15: ; preds = %11
%16 = call i32 @write(i32 %7, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 3)
br label %17
17: ; preds = %15, %13
%18 = call i32 @close(i32 %7)
ret i32 0
}