#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 scalar_t__ uint8_t;
typedef int uint32_t;
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 /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ write(int, char *, int);
int gpio_configure_dir(uint32_t gpio_num, uint8_t dir_value) {
  int fd;
  char buf[100];
  snprintf(buf, sizeof(buf),
           "/sys/class/gpio"
           "/gpio%d/direction",
           gpio_num);
  fd = open(buf, O_WRONLY);
  if (fd < 0) {
    perror("gpio direction configure\n");
    return fd;
  }
  if (dir_value)
    write(fd, "out", 4);
  else
    write(fd, "in", 3);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<gpio_configure_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x80(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x64,%esi
lea    0xe8b(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x80(%rbp),%rdi
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11b0 <gpio_configure_dir+0x60>
lea    0xe81(%rip),%rdi
call   1070 <perror@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11f7 <gpio_configure_dir+0xa7>
cmpq   $0x0,-0x10(%rbp)
je     11d4 <gpio_configure_dir+0x84>
mov    -0x14(%rbp),%edi
lea    0xe76(%rip),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
jmp    11e8 <gpio_configure_dir+0x98>
mov    -0x14(%rbp),%edi
lea    0xe61(%rip),%rsi
mov    $0x3,%edx
call   1030 <write@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gpio_configure_dir>:
push   %r14
push   %rbx
sub    $0x68,%rsp
mov    %rsi,%r14
mov    %edi,%ecx
lea    0xe9d(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x64,%esi
call   1040 <snprintf@plt>
mov    0x2e5e(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11c5 <gpio_configure_dir+0x75>
test   %r14,%r14
je     119f <gpio_configure_dir+0x4f>
lea    0xea5(%rip),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
jmp    11ad <gpio_configure_dir+0x5d>
lea    0xe99(%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    $0x68,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe55(%rip),%rdi
call   1070 <perror@plt>
jmp    11bb <gpio_configure_dir+0x6b>

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

LLVM IR (-O2):
@.str = [33 x i8] c"/sys/class/gpio/gpio%d/direction\00"
@O_WRONLY = global i32 0
@.str.1 = [26 x i8] c"gpio direction configure\0A\00"
@.str.2 = [4 x i8] c"out\00"
@.str.3 = [3 x i8] c"in\00"
define i32 @gpio_configure_dir(i32 %0, i64 %1) {
%3 = alloca [100 x i8]
%4 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%5 = call i32 @snprintf(i8* %4, i32 100, 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 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
br label %19
11: ; preds = %2
%12 = icmp eq i64 %1, 0
br i1 %12, label %15, label %13
13: ; preds = %11
%14 = call i32 @write(i32 %7, i8* getelementptr inbounds ([4 x i8], [4 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 ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 3)
br label %17
17: ; preds = %15, %13
%18 = call i32 @close(i32 %7)
br label %19
19: ; preds = %17, %9
%20 = phi i32 [ %7, %9 ], [ 0, %17 ]
ret i32 %20
}