#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 /*<<< orphan*/ snprintf(char *, int, char *, unsigned char);
int strlen(char *);
int /*<<< orphan*/ write(int, char *, int);
int setup_gpio() {
  char buf[16];
  int fd;
  unsigned char conf = 32 + 16 + 0 + 4 + 2 + 1;
  snprintf(buf, sizeof(buf), "%x", conf);
  fd = open("/sys/kernel/debug/omap_mux/gpmc_a1", O_WRONLY);
  if (fd == -1) {
    perror("open");
    return -1;
  }
  write(fd, buf, strlen(buf));
  close(fd);
  fd = open("/sys/class/gpio/export", O_WRONLY);
  if (fd == -1) {
    perror("open");
    return -1;
  }
  write(fd, "49", 2);
  close(fd);
  fd = open("/sys/class/gpio/gpio49/direction", O_WRONLY);
  if (fd == -1) {
    perror("open");
    return -1;
  }
  write(fd, "in", 2);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<setup_gpio>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movb   $0x37,-0x25(%rbp)
lea    -0x20(%rbp),%rdi
mov    $0x10,%esi
lea    0xe84(%rip),%rdx
movzbl -0x25(%rbp),%ecx
call   1050 <snprintf@plt>
mov    0x2e4c(%rip),%rax
mov    (%rax),%esi
lea    0xe6e(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11bf <setup_gpio+0x5f>
lea    0xe78(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    129f <setup_gpio+0x13f>
mov    -0x24(%rbp),%eax
mov    %eax,-0x34(%rbp)
lea    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
lea    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
mov    0x2de5(%rip),%rax
mov    (%rax),%esi
lea    0xe2f(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    1226 <setup_gpio+0xc6>
lea    0xe11(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    129f <setup_gpio+0x13f>
mov    -0x24(%rbp),%edi
lea    0xe12(%rip),%rsi
mov    $0x2,%edx
call   1030 <write@plt>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
mov    0x2d8f(%rip),%rax
mov    (%rax),%esi
lea    0xdf3(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    127c <setup_gpio+0x11c>
lea    0xdbb(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    129f <setup_gpio+0x13f>
mov    -0x24(%rbp),%edi
lea    0xde0(%rip),%rsi
mov    $0x2,%edx
call   1030 <write@plt>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setup_gpio>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
lea    0xe90(%rip),%rdx
mov    %rsp,%rdi
mov    $0x10,%esi
mov    $0x37,%ecx
call   1050 <snprintf@plt>
mov    0x2e4f(%rip),%r15
mov    (%r15),%esi
lea    0xe70(%rip),%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     122d <setup_gpio+0xcd>
mov    %eax,%ebx
mov    %rsp,%r14
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    (%r15),%esi
lea    0xe60(%rip),%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     122d <setup_gpio+0xcd>
mov    %eax,%ebx
lea    0xe64(%rip),%rsi
mov    %eax,%edi
mov    $0x2,%edx
call   1030 <write@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    (%r15),%esi
lea    0xe4a(%rip),%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     122d <setup_gpio+0xcd>
mov    %eax,%ebx
lea    0xe58(%rip),%rsi
mov    %eax,%edi
mov    $0x2,%edx
call   1030 <write@plt>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xdf2(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%eax
jmp    1223 <setup_gpio+0xc3>

LLVM IR (-O0):
@.str = [3 x i8] c"%x\00"
@.str.1 = [35 x i8] c"/sys/kernel/debug/omap_mux/gpmc_a1\00"
@O_WRONLY = global i32 0
@.str.2 = [5 x i8] c"open\00"
@.str.3 = [23 x i8] c"/sys/class/gpio/export\00"
@.str.4 = [3 x i8] c"49\00"
@.str.5 = [33 x i8] c"/sys/class/gpio/gpio49/direction\00"
@.str.6 = [3 x i8] c"in\00"
define i32 @setup_gpio() {
%1 = alloca i32
%2 = alloca [16 x i8]
%3 = alloca i32
%4 = alloca i8
store i8 55, i8* %4
%5 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 0
%6 = load i8, i8* %4
%7 = call i32 @snprintf(i8* %5, i32 16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8 zeroext %6)
%8 = load i32, i32* @O_WRONLY
%9 = call i32 @open(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i32 %8)
store i32 %9, i32* %3
%10 = load i32, i32* %3
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %0
%13 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %1
br label %44
14: ; preds = %0
%15 = load i32, i32* %3
%16 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 0
%17 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 0
%18 = call i32 @strlen(i8* %17)
%19 = call i32 @write(i32 %15, i8* %16, i32 %18)
%20 = load i32, i32* %3
%21 = call i32 @close(i32 %20)
%22 = load i32, i32* @O_WRONLY
%23 = call i32 @open(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i32 %22)
store i32 %23, i32* %3
%24 = load i32, i32* %3
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %28
26: ; preds = %14
%27 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %1
br label %44
28: ; preds = %14
%29 = load i32, i32* %3
%30 = call i32 @write(i32 %29, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 2)
%31 = load i32, i32* %3
%32 = call i32 @close(i32 %31)
%33 = load i32, i32* @O_WRONLY
%34 = call i32 @open(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), i32 %33)
store i32 %34, i32* %3
%35 = load i32, i32* %3
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %39
37: ; preds = %28
%38 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %1
br label %44
39: ; preds = %28
%40 = load i32, i32* %3
%41 = call i32 @write(i32 %40, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i32 2)
%42 = load i32, i32* %3
%43 = call i32 @close(i32 %42)
store i32 0, i32* %1
br label %44
44: ; preds = %39, %37, %26, %12
%45 = load i32, i32* %1
ret i32 %45
}

LLVM IR (-O2):
@.str = [3 x i8] c"%x\00"
@.str.1 = [35 x i8] c"/sys/kernel/debug/omap_mux/gpmc_a1\00"
@O_WRONLY = global i32 0
@.str.2 = [5 x i8] c"open\00"
@.str.3 = [23 x i8] c"/sys/class/gpio/export\00"
@.str.4 = [3 x i8] c"49\00"
@.str.5 = [33 x i8] c"/sys/class/gpio/gpio49/direction\00"
@.str.6 = [3 x i8] c"in\00"
define i32 @setup_gpio() {
%1 = alloca [16 x i8]
%2 = getelementptr inbounds [16 x i8], [16 x i8]* %1, i64 0, i64 0
%3 = call i32 @snprintf(i8* %2, i32 16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8 zeroext 55)
%4 = load i32, i32* @O_WRONLY
%5 = call i32 @open(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %29
9: ; preds = %0
%10 = call i32 @strlen(i8* %2)
%11 = call i32 @write(i32 %5, i8* %2, i32 %10)
%12 = call i32 @close(i32 %5)
%13 = load i32, i32* @O_WRONLY
%14 = call i32 @open(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %9
%17 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %29
18: ; preds = %9
%19 = call i32 @write(i32 %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 2)
%20 = call i32 @close(i32 %14)
%21 = load i32, i32* @O_WRONLY
%22 = call i32 @open(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), i32 %21)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %26
24: ; preds = %18
%25 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %29
26: ; preds = %18
%27 = call i32 @write(i32 %22, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i32 2)
%28 = call i32 @close(i32 %22)
br label %29
29: ; preds = %26, %24, %16, %7
%30 = phi i32 [ -1, %7 ], [ -1, %16 ], [ -1, %24 ], [ 0, %26 ]
ret i32 %30
}