#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 /*<<< orphan*/ write(int, char *, int);
int gpio_dir(unsigned gpio, unsigned dir) {
  int fd, len;
  char buf[60];
  len = snprintf(buf, sizeof(buf), "/sys/class/gpio/gpio%d/direction", gpio);
  if (len > 60)
    perror("\n***Filename too long in gpio_dir function\n");
  fd = open(buf, O_WRONLY);
  if (fd < 0) {
    perror("gpio/direction");
    return fd;
  }
  if (dir == 1)
    write(fd, "out", 4);
  else
    write(fd, "in", 3);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<gpio_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x50(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x3c,%esi
lea    0xe8f(%rip),%rdx
call   1040 <snprintf@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x3c,-0x14(%rbp)
jle    118f <gpio_dir+0x3f>
lea    0xe97(%rip),%rdi
call   1070 <perror@plt>
lea    -0x50(%rbp),%rdi
mov    0x2e3e(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    11c5 <gpio_dir+0x75>
lea    0xe98(%rip),%rdi
call   1070 <perror@plt>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    120b <gpio_dir+0xbb>
cmpl   $0x1,-0xc(%rbp)
jne    11e8 <gpio_dir+0x98>
mov    -0x10(%rbp),%edi
lea    0xe83(%rip),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
jmp    11fc <gpio_dir+0xac>
mov    -0x10(%rbp),%edi
lea    0xe6e(%rip),%rsi
mov    $0x3,%edx
call   1030 <write@plt>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gpio_dir>:
push   %rbp
push   %rbx
sub    $0x48,%rsp
mov    %esi,%ebp
mov    %edi,%ecx
lea    0xe9f(%rip),%rdx
mov    %rsp,%rdi
mov    $0x3c,%esi
call   1040 <snprintf@plt>
cmp    $0x3d,%eax
jge    11c4 <gpio_dir+0x74>
mov    0x2e5e(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdi
call   1060 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11d2 <gpio_dir+0x82>
cmp    $0x1,%ebp
jne    119f <gpio_dir+0x4f>
lea    0xec6(%rip),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
jmp    11ad <gpio_dir+0x5d>
lea    0xeba(%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    0xe56(%rip),%rdi
call   1070 <perror@plt>
jmp    1173 <gpio_dir+0x23>
lea    0xe74(%rip),%rdi
call   1070 <perror@plt>
jmp    11bb <gpio_dir+0x6b>

LLVM IR (-O0):
@.str = [33 x i8] c"/sys/class/gpio/gpio%d/direction\00"
@.str.1 = [44 x i8] c"\0A***Filename too long in gpio_dir function\0A\00"
@O_WRONLY = global i32 0
@.str.2 = [15 x i8] c"gpio/direction\00"
@.str.3 = [4 x i8] c"out\00"
@.str.4 = [3 x i8] c"in\00"
define i32 @gpio_dir(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca [60 x i8]
store i32 %0, i32* %4
store i32 %1, i32* %5
%9 = getelementptr inbounds [60 x i8], [60 x i8]* %8, i64 0, i64 0
%10 = load i32, i32* %4
%11 = call i32 @snprintf(i8* %9, i32 60, 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 sgt i32 %12, 60
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 @perror(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0))
br label %16
16: ; preds = %14, %2
%17 = getelementptr inbounds [60 x i8], [60 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.2, i64 0, i64 0))
%24 = load i32, i32* %6
store i32 %24, i32* %3
br label %37
25: ; preds = %16
%26 = load i32, i32* %5
%27 = icmp eq i32 %26, 1
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.3, i64 0, i64 0), i32 4)
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.4, i64 0, i64 0), i32 3)
br label %34
34: ; preds = %31, %28
%35 = load i32, i32* %6
%36 = call i32 @close(i32 %35)
store i32 0, i32* %3
br label %37
37: ; preds = %34, %22
%38 = load i32, i32* %3
ret i32 %38
}

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