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

/* Variables and functions */
int EINVAL;
int /*<<< orphan*/ O_WRONLY;
scalar_t__ close(int);
int errno;
int open(char *, int /*<<< orphan*/);
int snprintf(char *, int, char *, int);
int write(int, char *, int);
int gpio_set_mode(int gpio, char mode) {
  int ret;
  char filename[128];
  ret = snprintf(filename, sizeof(filename), "/sys/class/gpio/gpio%d/direction",
                 gpio);
  if (ret < 0)
    goto out;
  int fd = open(filename, O_WRONLY);
  if (fd < 0) {
    ret = -errno;
    goto out;
  }
  switch (mode) {
  case 0:
    if (write(fd, "in", 2) != 2) {
      ret = -errno;
      goto out_close;
    }
    break;
  case 1:
    if (write(fd, "out", 3) != 3) {
      ret = -errno;
      goto out_close;
    }
    break;
  default:
    ret = -EINVAL;
  }
out_close:
  if (close(fd) < 0)
    ret = -errno;
out:
  return ret;
}


x86 assembly (clang, -O0):
<gpio_set_mode>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %sil,%al
mov    %edi,-0x4(%rbp)
mov    %al,-0x5(%rbp)
lea    -0x90(%rbp),%rdi
mov    -0x4(%rbp),%ecx
mov    $0x80,%esi
lea    0xe96(%rip),%rdx
call   1040 <snprintf@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1181 <gpio_set_mode+0x41>
jmp    1292 <gpio_set_mode+0x152>
lea    -0x90(%rbp),%rdi
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x94(%rbp)
cmpl   $0x0,-0x94(%rbp)
jge    11bc <gpio_set_mode+0x7c>
xor    %eax,%eax
mov    0x2e2e(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0xc(%rbp)
jmp    1292 <gpio_set_mode+0x152>
movsbl -0x5(%rbp),%eax
mov    %eax,-0x98(%rbp)
test   %eax,%eax
je     11e7 <gpio_set_mode+0xa7>
jmp    11d3 <gpio_set_mode+0x93>
mov    -0x98(%rbp),%eax
sub    $0x1,%eax
je     121f <gpio_set_mode+0xdf>
jmp    1257 <gpio_set_mode+0x117>
mov    -0x94(%rbp),%edi
lea    0xe2d(%rip),%rsi
mov    $0x2,%edx
call   1030 <write@plt>
cmp    $0x2,%eax
je     121a <gpio_set_mode+0xda>
xor    %eax,%eax
mov    0x2dd0(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0xc(%rbp)
jmp    126a <gpio_set_mode+0x12a>
jmp    1265 <gpio_set_mode+0x125>
mov    -0x94(%rbp),%edi
lea    0xdf8(%rip),%rsi
mov    $0x3,%edx
call   1030 <write@plt>
cmp    $0x3,%eax
je     1252 <gpio_set_mode+0x112>
xor    %eax,%eax
mov    0x2d98(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0xc(%rbp)
jmp    126a <gpio_set_mode+0x12a>
jmp    1265 <gpio_set_mode+0x125>
xor    %eax,%eax
mov    0x2d78(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0xc(%rbp)
jmp    126a <gpio_set_mode+0x12a>
mov    -0x94(%rbp),%edi
call   1050 <close@plt>
cmp    $0x0,%rax
jge    128d <gpio_set_mode+0x14d>
xor    %eax,%eax
mov    0x2d58(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0xc(%rbp)
jmp    1292 <gpio_set_mode+0x152>
mov    -0xc(%rbp),%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gpio_set_mode>:
push   %rbp
push   %r14
push   %rbx
sub    $0x80,%rsp
mov    %esi,%ebp
mov    %edi,%ecx
lea    0xeaa(%rip),%rdx
mov    %rsp,%rdi
mov    $0x80,%esi
call   1040 <snprintf@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11f2 <gpio_set_mode+0xb2>
mov    0x2e54(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdi
call   1060 <open@plt>
test   %eax,%eax
js     11e7 <gpio_set_mode+0xa7>
mov    %eax,%r14d
movsbl %bpl,%eax
cmp    $0x1,%eax
je     11ad <gpio_set_mode+0x6d>
test   %eax,%eax
jne    11cf <gpio_set_mode+0x8f>
lea    0xe88(%rip),%rsi
mov    %r14d,%edi
mov    $0x2,%edx
call   1030 <write@plt>
cmp    $0x2,%eax
jne    11c6 <gpio_set_mode+0x86>
jmp    11da <gpio_set_mode+0x9a>
lea    0xe70(%rip),%rsi
mov    %r14d,%edi
mov    $0x3,%edx
call   1030 <write@plt>
cmp    $0x3,%eax
je     11da <gpio_set_mode+0x9a>
mov    0x2e13(%rip),%rax
jmp    11d6 <gpio_set_mode+0x96>
mov    0x2e02(%rip),%rax
xor    %ebx,%ebx
sub    (%rax),%ebx
mov    %r14d,%edi
call   1050 <close@plt>
test   %rax,%rax
jns    11f2 <gpio_set_mode+0xb2>
mov    0x2df2(%rip),%rax
xor    %ebx,%ebx
sub    (%rax),%ebx
mov    %ebx,%eax
add    $0x80,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [33 x i8] c"/sys/class/gpio/gpio%d/direction\00"
@O_WRONLY = global i32 0
@errno = global i32 0
@.str.1 = [3 x i8] c"in\00"
@.str.2 = [4 x i8] c"out\00"
@EINVAL = global i32 0
define i32 @gpio_set_mode(i32 %0, i8 signext %1) {
%3 = alloca i32
%4 = alloca i8
%5 = alloca i32
%6 = alloca [128 x i8]
%7 = alloca i32
store i32 %0, i32* %3
store i8 %1, i8* %4
%8 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%9 = load i32, i32* %3
%10 = call i32 @snprintf(i8* %8, i32 128, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %9)
store i32 %10, i32* %5
%11 = load i32, i32* %5
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %2
br label %54
14: ; preds = %2
%15 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%16 = load i32, i32* @O_WRONLY
%17 = call i32 @open(i8* %15, i32 %16)
store i32 %17, i32* %7
%18 = load i32, i32* %7
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %14
%21 = load i32, i32* @errno
%22 = sub nsw i32 0, %21
store i32 %22, i32* %5
br label %54
23: ; preds = %14
%24 = load i8, i8* %4
%25 = sext i8 %24 to i32
switch i32 %25, label %42 [
i32 0, label %26
i32 1, label %34
]
26: ; preds = %23
%27 = load i32, i32* %7
%28 = call i32 @write(i32 %27, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 2)
%29 = icmp ne i32 %28, 2
br i1 %29, label %30, label %33
30: ; preds = %26
%31 = load i32, i32* @errno
%32 = sub nsw i32 0, %31
store i32 %32, i32* %5
br label %46
33: ; preds = %26
br label %45
34: ; preds = %23
%35 = load i32, i32* %7
%36 = call i32 @write(i32 %35, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 3)
%37 = icmp ne i32 %36, 3
br i1 %37, label %38, label %41
38: ; preds = %34
%39 = load i32, i32* @errno
%40 = sub nsw i32 0, %39
store i32 %40, i32* %5
br label %46
41: ; preds = %34
br label %45
42: ; preds = %23
%43 = load i32, i32* @EINVAL
%44 = sub nsw i32 0, %43
store i32 %44, i32* %5
br label %45
45: ; preds = %42, %41, %33
br label %46
46: ; preds = %45, %38, %30
%47 = load i32, i32* %7
%48 = call i64 @close(i32 %47)
%49 = icmp slt i64 %48, 0
br i1 %49, label %50, label %53
50: ; preds = %46
%51 = load i32, i32* @errno
%52 = sub nsw i32 0, %51
store i32 %52, i32* %5
br label %53
53: ; preds = %50, %46
br label %54
54: ; preds = %53, %20, %13
%55 = load i32, i32* %5
ret i32 %55
}

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