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

/* Variables and functions */
int EINVAL;
int ENODEV;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ gpio_set(int, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ strcat(char *, char *);
int gpio_open(int gpio, int val) {
  FILE *fd;
  char num[8];
  char path[40] = "/sys/class/gpio/gpio";
  if (gpio < 0)
    return -EINVAL;
  fd = fopen("/sys/class/gpio/export", "w");
  if (fd < 0)
    return -ENODEV;
  sprintf(num, "%d", gpio);
  fputs(num, fd);
  fclose(fd);
  strcat(path, num);
  strcat(path, "/direction");
  fd = fopen(path, "r");
  if (fd < 0)
    return -ENODEV;
  fclose(fd);
  fd = fopen(path, "w");
  if (fd < 0)
    return -ENODEV;
  if (val < 0)
    fputs("in", fd);
  else
    fputs("out", fd);
  fclose(fd);
  if (val >= 0)
    gpio_set(gpio, val);
  return 0;
}


x86 assembly (clang, -O0):
<gpio_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x50(%rbp),%rdi
lea    0xe77(%rip),%rsi
mov    $0x28,%edx
call   1050 <memcpy@plt>
cmpl   $0x0,-0x8(%rbp)
jge    11b0 <gpio_open+0x40>
xor    %eax,%eax
mov    0x2e3a(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    12ec <gpio_open+0x17c>
lea    0xe71(%rip),%rdi
lea    0xe81(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jae    11e5 <gpio_open+0x75>
xor    %eax,%eax
mov    0x2dfd(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    12ec <gpio_open+0x17c>
lea    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xe4e(%rip),%rsi
call   1090 <sprintf@plt>
lea    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <fputs@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
lea    -0x50(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1080 <strcat@plt>
lea    -0x50(%rbp),%rdi
lea    0xe1e(%rip),%rsi
call   1080 <strcat@plt>
lea    -0x50(%rbp),%rdi
lea    0xe19(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jae    125d <gpio_open+0xed>
xor    %eax,%eax
mov    0x2d85(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    12ec <gpio_open+0x17c>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
lea    -0x50(%rbp),%rdi
lea    0xdce(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jae    1298 <gpio_open+0x128>
xor    %eax,%eax
mov    0x2d4a(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    12ec <gpio_open+0x17c>
cmpl   $0x0,-0xc(%rbp)
jge    12b7 <gpio_open+0x147>
mov    -0x18(%rbp),%rsi
lea    0xda4(%rip),%rdi
call   1040 <fputs@plt>
jmp    12c7 <gpio_open+0x157>
mov    -0x18(%rbp),%rsi
lea    0xd92(%rip),%rdi
call   1040 <fputs@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
cmpl   $0x0,-0xc(%rbp)
jl     12e5 <gpio_open+0x175>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1060 <gpio_set@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gpio_open>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x40,%rsp
movaps 0xe8d(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
movaps 0xe71(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movq   $0x0,0x30(%rsp)
test   %edi,%edi
js     1265 <gpio_open+0xf5>
mov    %esi,%r15d
mov    %edi,%r12d
lea    0xe76(%rip),%rdi
lea    0xe86(%rip),%r14
mov    %r14,%rsi
call   1060 <fopen@plt>
mov    %rax,%rbx
lea    0xe76(%rip),%rsi
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
mov    %r12d,%edx
call   1080 <sprintf@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1040 <fputs@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1070 <strcat@plt>
lea    0xe3f(%rip),%rsi
mov    %rbx,%rdi
call   1070 <strcat@plt>
lea    0xe3b(%rip),%rsi
mov    %rbx,%rdi
call   1060 <fopen@plt>
mov    %rax,%rdi
call   1030 <fclose@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1060 <fopen@plt>
mov    %rax,%rbx
test   %r15d,%r15d
js     1272 <gpio_open+0x102>
lea    0xe16(%rip),%rdi
mov    $0x3,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1090 <fwrite@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %r12d,%edi
mov    %r15d,%esi
call   1050 <gpio_set@plt>
jmp    1293 <gpio_open+0x123>
mov    0x2d74(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
jmp    1295 <gpio_open+0x125>
lea    0xdd8(%rip),%rdi
mov    $0x2,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1090 <fwrite@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.gpio_open.path = [40 x i8] c"/sys/class/gpio/gpio\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@EINVAL = global i32 0
@.str = [23 x i8] c"/sys/class/gpio/export\00"
@.str.1 = [2 x i8] c"w\00"
@ENODEV = global i32 0
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [11 x i8] c"/direction\00"
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [3 x i8] c"in\00"
@.str.6 = [4 x i8] c"out\00"
define i32 @gpio_open(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
%7 = alloca [8 x i8]
%8 = alloca [40 x i8]
store i32 %0, i32* %4
store i32 %1, i32* %5
%9 = bitcast [40 x i8]* %8 to i8*
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = load i32, i32* @EINVAL
%14 = sub nsw i32 0, %13
store i32 %14, i32* %3
br label %72
15: ; preds = %2
%16 = call i32* @fopen(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %16, i32** %6
%17 = load i32*, i32** %6
%18 = icmp ult i32* %17, null
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = load i32, i32* @ENODEV
%21 = sub nsw i32 0, %20
store i32 %21, i32* %3
br label %72
22: ; preds = %15
%23 = getelementptr inbounds [8 x i8], [8 x i8]* %7, i64 0, i64 0
%24 = load i32, i32* %4
%25 = call i32 @sprintf(i8* %23, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %24)
%26 = getelementptr inbounds [8 x i8], [8 x i8]* %7, i64 0, i64 0
%27 = load i32*, i32** %6
%28 = call i32 @fputs(i8* %26, i32* %27)
%29 = load i32*, i32** %6
%30 = call i32 @fclose(i32* %29)
%31 = getelementptr inbounds [40 x i8], [40 x i8]* %8, i64 0, i64 0
%32 = getelementptr inbounds [8 x i8], [8 x i8]* %7, i64 0, i64 0
%33 = call i32 @strcat(i8* %31, i8* %32)
%34 = getelementptr inbounds [40 x i8], [40 x i8]* %8, i64 0, i64 0
%35 = call i32 @strcat(i8* %34, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%36 = getelementptr inbounds [40 x i8], [40 x i8]* %8, i64 0, i64 0
%37 = call i32* @fopen(i8* %36, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %37, i32** %6
%38 = load i32*, i32** %6
%39 = icmp ult i32* %38, null
br i1 %39, label %40, label %43
40: ; preds = %22
%41 = load i32, i32* @ENODEV
%42 = sub nsw i32 0, %41
store i32 %42, i32* %3
br label %72
43: ; preds = %22
%44 = load i32*, i32** %6
%45 = call i32 @fclose(i32* %44)
%46 = getelementptr inbounds [40 x i8], [40 x i8]* %8, i64 0, i64 0
%47 = call i32* @fopen(i8* %46, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %47, i32** %6
%48 = load i32*, i32** %6
%49 = icmp ult i32* %48, null
br i1 %49, label %50, label %53
50: ; preds = %43
%51 = load i32, i32* @ENODEV
%52 = sub nsw i32 0, %51
store i32 %52, i32* %3
br label %72
53: ; preds = %43
%54 = load i32, i32* %5
%55 = icmp slt i32 %54, 0
br i1 %55, label %56, label %59
56: ; preds = %53
%57 = load i32*, i32** %6
%58 = call i32 @fputs(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32* %57)
br label %62
59: ; preds = %53
%60 = load i32*, i32** %6
%61 = call i32 @fputs(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i32* %60)
br label %62
62: ; preds = %59, %56
%63 = load i32*, i32** %6
%64 = call i32 @fclose(i32* %63)
%65 = load i32, i32* %5
%66 = icmp sge i32 %65, 0
br i1 %66, label %67, label %71
67: ; preds = %62
%68 = load i32, i32* %4
%69 = load i32, i32* %5
%70 = call i32 @gpio_set(i32 %68, i32 %69)
br label %71
71: ; preds = %67, %62
store i32 0, i32* %3
br label %72
72: ; preds = %71, %50, %40, %19, %12
%73 = load i32, i32* %3
ret i32 %73
}

LLVM IR (-O2):
@__const.gpio_open.path = [40 x i8] c"/sys/class/gpio/gpio\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@EINVAL = global i32 0
@.str = [23 x i8] c"/sys/class/gpio/export\00"
@.str.1 = [2 x i8] c"w\00"
@ENODEV = global i32 0
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [11 x i8] c"/direction\00"
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [3 x i8] c"in\00"
@.str.6 = [4 x i8] c"out\00"
define i32 @gpio_open(i32 %0, i32 %1) {
%3 = alloca [8 x i8]
%4 = alloca [40 x i8]
%5 = getelementptr inbounds [8 x i8], [8 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [40 x i8], [40 x i8]* %4, i64 0, i64 0
%7 = icmp slt i32 %0, 0
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = load i32, i32* @EINVAL
%10 = sub nsw i32 0, %9
br label %29
11: ; preds = %2
%12 = tail call i32* @fopen(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%13 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %0)
%14 = call i32 @fputs(i8* %5, i32* %12)
%15 = tail call i32 @fclose(i32* %12)
%16 = call i32 @strcat(i8* %6, i8* %5)
%17 = call i32 @strcat(i8* %6, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%18 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%19 = call i32 @fclose(i32* %18)
%20 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%21 = icmp slt i32 %1, 0
br i1 %21, label %22, label %25
22: ; preds = %11
%23 = call i64 @fwrite(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i64 2, i64 1, i32* %20)
%24 = call i32 @fclose(i32* %20)
br label %29
25: ; preds = %11
%26 = call i64 @fwrite(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i64 3, i64 1, i32* %20)
%27 = call i32 @fclose(i32* %20)
%28 = call i32 @gpio_set(i32 %0, i32 %1)
br label %29
29: ; preds = %22, %25, %8
%30 = phi i32 [ %10, %8 ], [ 0, %25 ], [ 0, %22 ]
ret i32 %30
}