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


x86 assembly (clang, -O0):
<gpio_set_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
lea    -0x60(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    -0x10(%rbp),%r8
mov    $0x40,%esi
lea    0xe87(%rip),%rdx
call   1040 <snprintf@plt>
mov    %eax,-0x1c(%rbp)
lea    -0x60(%rbp),%rdi
mov    0x2e4c(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11b7 <gpio_set_dir+0x67>
lea    0xe7d(%rip),%rdi
call   1070 <perror@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11fd <gpio_set_dir+0xad>
cmpl   $0x0,-0x14(%rbp)
je     11da <gpio_set_dir+0x8a>
mov    -0x18(%rbp),%edi
lea    0xe68(%rip),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
jmp    11ee <gpio_set_dir+0x9e>
mov    -0x18(%rbp),%edi
lea    0xe53(%rip),%rsi
mov    $0x3,%edx
call   1030 <write@plt>
mov    -0x18(%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    %edx,%ebp
mov    %rsi,%r8
mov    %edi,%ecx
lea    0xe9c(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x40,%esi
call   1040 <snprintf@plt>
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11c4 <gpio_set_dir+0x74>
test   %ebp,%ebp
je     119f <gpio_set_dir+0x4f>
lea    0xe9d(%rip),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
jmp    11ad <gpio_set_dir+0x5d>
lea    0xe91(%rip),%rsi
mov    %ebx,%edi
mov    $0x3,%edx
call   1030 <write@plt>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x48,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe59(%rip),%rdi
call   1070 <perror@plt>
jmp    11bb <gpio_set_dir+0x6b>

LLVM IR (-O0):
@.str = [36 x i8] c"/sys/class/gpio/gpio%d_%s/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, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca [64 x i8]
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%11 = getelementptr inbounds [64 x i8], [64 x i8]* %10, i64 0, i64 0
%12 = load i32, i32* %5
%13 = load i8*, i8** %6
%14 = call i32 @snprintf(i8* %11, i32 64, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 %12, i8* %13)
store i32 %14, i32* %9
%15 = getelementptr inbounds [64 x i8], [64 x i8]* %10, i64 0, i64 0
%16 = load i32, i32* @O_WRONLY
%17 = call i32 @open(i8* %15, i32 %16)
store i32 %17, i32* %8
%18 = load i32, i32* %8
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %3
%21 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%22 = load i32, i32* %8
store i32 %22, i32* %4
br label %35
23: ; preds = %3
%24 = load i32, i32* %7
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %23
%27 = load i32, i32* %8
%28 = call i32 @write(i32 %27, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 4)
br label %32
29: ; preds = %23
%30 = load i32, i32* %8
%31 = call i32 @write(i32 %30, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 3)
br label %32
32: ; preds = %29, %26
%33 = load i32, i32* %8
%34 = call i32 @close(i32 %33)
store i32 0, i32* %4
br label %35
35: ; preds = %32, %20
%36 = load i32, i32* %4
ret i32 %36
}

LLVM IR (-O2):
@.str = [36 x i8] c"/sys/class/gpio/gpio%d_%s/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, i8* %1, i32 %2) {
%4 = alloca [64 x i8]
%5 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%6 = call i32 @snprintf(i8* %5, i32 64, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 %0, i8* %1)
%7 = load i32, i32* @O_WRONLY
%8 = call i32 @open(i8* %5, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %3
%11 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %20
12: ; preds = %3
%13 = icmp eq i32 %2, 0
br i1 %13, label %16, label %14
14: ; preds = %12
%15 = call i32 @write(i32 %8, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 4)
br label %18
16: ; preds = %12
%17 = call i32 @write(i32 %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 3)
br label %18
18: ; preds = %16, %14
%19 = call i32 @close(i32 %8)
br label %20
20: ; preds = %18, %10
%21 = phi i32 [ %8, %10 ], [ 0, %18 ]
ret i32 %21
}