#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 */

/* Variables and functions */
int EINVAL;
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int errno;
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sprintf(char *, char *, ...);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strlen(char *);
int write(int, char *, int /*<<< orphan*/);
int goio_direction(unsigned offset, int d) {
  int ret;
  int fd;
  char buf[8] = "";
  char file[128];
  sprintf(file,
          "/sys/class/gpio/"
          "gpio%d/direction",
          offset);
  fd = open(file, O_WRONLY);
  if (fd < 0) {
    printf("open file [%s] failed [%d]\n", file, errno);
    return -EINVAL;
  }
  if (!d) {
    if (strcmp(buf, "in"))
      sprintf(buf, "in");
  } else {
    if (strcmp(buf, "out"))
      sprintf(buf, "out");
  }
  ret = write(fd, buf, strlen(buf));
  if (ret < 0) {
    printf("set [%d] gpio direction error %d\n", offset, errno);
  } else {
    ret = 0;
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<goio_direction>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x1c(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
lea    -0xa0(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xe4e(%rip),%rsi
mov    $0x0,%al
call   10a0 <sprintf@plt>
lea    -0xa0(%rbp),%rdi
mov    0x2e01(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    120c <goio_direction+0x8c>
lea    -0xa0(%rbp),%rsi
mov    0x2df7(%rip),%rax
mov    (%rax),%edx
lea    0xe2f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %eax,%eax
mov    0x2dd6(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    12eb <goio_direction+0x16b>
cmpl   $0x0,-0xc(%rbp)
jne    1247 <goio_direction+0xc7>
lea    -0x1c(%rbp),%rdi
lea    0xe1c(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
je     1242 <goio_direction+0xc2>
lea    -0x1c(%rbp),%rdi
lea    0xe02(%rip),%rsi
mov    $0x0,%al
call   10a0 <sprintf@plt>
jmp    1278 <goio_direction+0xf8>
lea    -0x1c(%rbp),%rdi
lea    0xdee(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
je     1273 <goio_direction+0xf3>
lea    -0x1c(%rbp),%rdi
lea    0xdd4(%rip),%rsi
mov    $0x0,%al
call   10a0 <sprintf@plt>
jmp    1278 <goio_direction+0xf8>
mov    -0x14(%rbp),%eax
mov    %eax,-0xac(%rbp)
lea    -0x1c(%rbp),%rax
mov    %rax,-0xa8(%rbp)
lea    -0x1c(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0xac(%rbp),%edi
mov    -0xa8(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    12d5 <goio_direction+0x155>
mov    -0x8(%rbp),%esi
mov    0x2d20(%rip),%rax
mov    (%rax),%edx
lea    0xd7b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12dc <goio_direction+0x15c>
movl   $0x0,-0x10(%rbp)
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<goio_direction>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x98,%rsp
mov    %esi,%r15d
mov    %edi,%r14d
movq   $0x0,0x8(%rsp)
lea    0xe6d(%rip),%rsi
xor    %ebx,%ebx
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    %r14d,%edx
xor    %eax,%eax
call   1090 <sprintf@plt>
mov    0x2e1a(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1080 <open@plt>
test   %eax,%eax
js     11e3 <goio_direction+0x73>
mov    %eax,%ebp
test   %r15d,%r15d
je     120a <goio_direction+0x9a>
lea    0xe76(%rip),%rsi
lea    0x8(%rsp),%rdi
call   1070 <strcmp@plt>
test   %rax,%rax
je     122c <goio_direction+0xbc>
movl   $0x74756f,0x8(%rsp)
jmp    122c <goio_direction+0xbc>
mov    0x2df6(%rip),%rax
mov    (%rax),%edx
lea    0xe2e(%rip),%rdi
lea    0x10(%rsp),%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2dd2(%rip),%rax
sub    (%rax),%ebx
jmp    126c <goio_direction+0xfc>
lea    0xe2c(%rip),%rsi
lea    0x8(%rsp),%rdi
call   1070 <strcmp@plt>
test   %rax,%rax
je     122c <goio_direction+0xbc>
movb   $0x0,0xa(%rsp)
movw   $0x6e69,0x8(%rsp)
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
test   %eax,%eax
jns    1263 <goio_direction+0xf3>
mov    0x2d90(%rip),%rax
mov    (%rax),%edx
lea    0xdeb(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebp,%edi
call   1060 <close@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x98,%rsp
pop    %rbx
pop    %r14
pop    %r15
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 = [28 x i8] c"open file [%s] failed [%d]\0A\00"
@errno = global i32 0
@EINVAL = global i32 0
@.str.2 = [3 x i8] c"in\00"
@.str.3 = [4 x i8] c"out\00"
@.str.4 = [34 x i8] c"set [%d] gpio direction error %d\0A\00"
define i32 @goio_direction(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca [8 x i8]
%9 = alloca [128 x i8]
store i32 %0, i32* %4
store i32 %1, i32* %5
%10 = bitcast [8 x i8]* %8 to i8*
%11 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%12 = load i32, i32* %4
%13 = call i32 (i8*, i8*, ...) @sprintf(i8* %11, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %12)
%14 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%15 = load i32, i32* @O_WRONLY
%16 = call i32 @open(i8* %14, i32 %15)
store i32 %16, i32* %7
%17 = load i32, i32* %7
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %25
19: ; preds = %2
%20 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%21 = load i32, i32* @errno
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %20, i32 %21)
%23 = load i32, i32* @EINVAL
%24 = sub nsw i32 0, %23
store i32 %24, i32* %3
br label %60
25: ; preds = %2
%26 = load i32, i32* %5
%27 = icmp ne i32 %26, 0
br i1 %27, label %36, label %28
28: ; preds = %25
%29 = getelementptr inbounds [8 x i8], [8 x i8]* %8, i64 0, i64 0
%30 = call i64 @strcmp(i8* %29, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %28
%33 = getelementptr inbounds [8 x i8], [8 x i8]* %8, i64 0, i64 0
%34 = call i32 (i8*, i8*, ...) @sprintf(i8* %33, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
br label %35
35: ; preds = %32, %28
br label %44
36: ; preds = %25
%37 = getelementptr inbounds [8 x i8], [8 x i8]* %8, i64 0, i64 0
%38 = call i64 @strcmp(i8* %37, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %36
%41 = getelementptr inbounds [8 x i8], [8 x i8]* %8, i64 0, i64 0
%42 = call i32 (i8*, i8*, ...) @sprintf(i8* %41, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
br label %43
43: ; preds = %40, %36
br label %44
44: ; preds = %43, %35
%45 = load i32, i32* %7
%46 = getelementptr inbounds [8 x i8], [8 x i8]* %8, i64 0, i64 0
%47 = getelementptr inbounds [8 x i8], [8 x i8]* %8, i64 0, i64 0
%48 = call i32 @strlen(i8* %47)
%49 = call i32 @write(i32 %45, i8* %46, i32 %48)
store i32 %49, i32* %6
%50 = load i32, i32* %6
%51 = icmp slt i32 %50, 0
br i1 %51, label %52, label %56
52: ; preds = %44
%53 = load i32, i32* %4
%54 = load i32, i32* @errno
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0), i32 %53, i32 %54)
br label %57
56: ; preds = %44
store i32 0, i32* %6
br label %57
57: ; preds = %56, %52
%58 = load i32, i32* %7
%59 = call i32 @close(i32 %58)
store i32 0, i32* %3
br label %60
60: ; preds = %57, %19
%61 = load i32, i32* %3
ret i32 %61
}

LLVM IR (-O2):
@.str = [33 x i8] c"/sys/class/gpio/gpio%d/direction\00"
@O_WRONLY = global i32 0
@.str.1 = [28 x i8] c"open file [%s] failed [%d]\0A\00"
@errno = global i32 0
@EINVAL = global i32 0
@.str.2 = [3 x i8] c"in\00"
@.str.3 = [4 x i8] c"out\00"
@.str.4 = [34 x i8] c"set [%d] gpio direction error %d\0A\00"
define i32 @goio_direction(i32 %0, i32 %1) {
%3 = alloca i64
%4 = alloca [128 x i8]
%5 = bitcast i64* %3 to i8*
store i64 0, i64* %3
%6 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%7 = call i32 (i8*, i8*, ...) @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %0)
%8 = load i32, i32* @O_WRONLY
%9 = call i32 @open(i8* %6, i32 %8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %16
11: ; preds = %2
%12 = load i32, i32* @errno
%13 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %6, i32 %12)
%14 = load i32, i32* @EINVAL
%15 = sub nsw i32 0, %14
br label %36
16: ; preds = %2
%17 = icmp eq i32 %1, 0
br i1 %17, label %18, label %22
18: ; preds = %16
%19 = call i64 @strcmp(i8* %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%20 = icmp eq i64 %19, 0
br i1 %20, label %27, label %21
21: ; preds = %18
br label %27
22: ; preds = %16
%23 = call i64 @strcmp(i8* %5, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%24 = icmp eq i64 %23, 0
br i1 %24, label %27, label %25
25: ; preds = %22
%26 = bitcast i64* %3 to i32*
store i32 7632239, i32* %26
br label %27
27: ; preds = %22, %25, %18, %21
%28 = call i32 @strlen(i8* %5)
%29 = call i32 @write(i32 %9, i8* %5, i32 %28)
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %27
%32 = load i32, i32* @errno
%33 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0), i32 %0, i32 %32)
br label %34
34: ; preds = %27, %31
%35 = call i32 @close(i32 %9)
br label %36
36: ; preds = %34, %11
%37 = phi i32 [ %15, %11 ], [ 0, %34 ]
ret i32 %37
}