#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 /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned int);
scalar_t__ write(int, char *, int);
int gpio_set_pull(unsigned int gpio, unsigned int value) {
  int fd;
  char filename[128];
  snprintf(filename, sizeof(filename), "/sys/class/gpio/gpio%d/pull", gpio);
  if ((fd = open(filename, O_WRONLY)) < 0)
    return -1;
  if (value == 2) {
    if (write(fd, "down", 5) < 0)
      return -1;
  } else if (value == 1) {
    if (write(fd, "up", 3) < 0)
      return -1;
  } else {
    if (write(fd, "disable", 8) < 0)
      return -1;
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<gpio_set_pull>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x90(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x80,%esi
lea    0xe99(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x90(%rbp),%rdi
mov    0x2e5e(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jge    1199 <gpio_set_pull+0x59>
movl   $0xffffffff,-0x4(%rbp)
jmp    124e <gpio_set_pull+0x10e>
cmpl   $0x2,-0xc(%rbp)
jne    11d2 <gpio_set_pull+0x92>
mov    -0x10(%rbp),%edi
lea    0xe6f(%rip),%rsi
mov    $0x5,%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    11cd <gpio_set_pull+0x8d>
movl   $0xffffffff,-0x4(%rbp)
jmp    124e <gpio_set_pull+0x10e>
jmp    123f <gpio_set_pull+0xff>
cmpl   $0x1,-0xc(%rbp)
jne    120b <gpio_set_pull+0xcb>
mov    -0x10(%rbp),%edi
lea    0xe3b(%rip),%rsi
mov    $0x3,%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    1206 <gpio_set_pull+0xc6>
movl   $0xffffffff,-0x4(%rbp)
jmp    124e <gpio_set_pull+0x10e>
jmp    123a <gpio_set_pull+0xfa>
mov    -0x10(%rbp),%edi
lea    0xe0f(%rip),%rsi
mov    $0x8,%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    1235 <gpio_set_pull+0xf5>
movl   $0xffffffff,-0x4(%rbp)
jmp    124e <gpio_set_pull+0x10e>
jmp    123a <gpio_set_pull+0xfa>
jmp    123f <gpio_set_pull+0xff>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gpio_set_pull>:
push   %rbp
push   %r14
push   %rbx
sub    $0x80,%rsp
mov    %esi,%ebx
mov    %edi,%ecx
lea    0xeaa(%rip),%rdx
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0x80,%esi
call   1040 <snprintf@plt>
mov    0x2e6b(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1060 <open@plt>
mov    $0xffffffff,%r14d
test   %eax,%eax
js     11cf <gpio_set_pull+0x8f>
mov    %eax,%ebp
cmp    $0x1,%ebx
je     119d <gpio_set_pull+0x5d>
cmp    $0x2,%ebx
jne    11ad <gpio_set_pull+0x6d>
lea    0xe88(%rip),%rsi
mov    %ebp,%edi
mov    $0x5,%edx
jmp    11bb <gpio_set_pull+0x7b>
lea    0xe7d(%rip),%rsi
mov    %ebp,%edi
mov    $0x3,%edx
jmp    11bb <gpio_set_pull+0x7b>
lea    0xe70(%rip),%rsi
mov    %ebp,%edi
mov    $0x8,%edx
call   1030 <write@plt>
test   %rax,%rax
js     11cf <gpio_set_pull+0x8f>
mov    %ebp,%edi
call   1050 <close@plt>
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x80,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [28 x i8] c"/sys/class/gpio/gpio%d/pull\00"
@O_WRONLY = global i32 0
@.str.1 = [5 x i8] c"down\00"
@.str.2 = [3 x i8] c"up\00"
@.str.3 = [8 x i8] c"disable\00"
define i32 @gpio_set_pull(i32 %0, i32 %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 ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %0)
%6 = load i32, i32* @O_WRONLY
%7 = call i32 @open(i8* %4, i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %21, label %9
9: ; preds = %2
switch i32 %1, label %16 [
i32 2, label %10
i32 1, label %13
]
10: ; preds = %9
%11 = call i64 @write(i32 %7, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 5)
%12 = icmp slt i64 %11, 0
br i1 %12, label %21, label %19
13: ; preds = %9
%14 = call i64 @write(i32 %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 3)
%15 = icmp slt i64 %14, 0
br i1 %15, label %21, label %19
16: ; preds = %9
%17 = call i64 @write(i32 %7, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32 8)
%18 = icmp slt i64 %17, 0
br i1 %18, label %21, label %19
19: ; preds = %13, %16, %10
%20 = call i32 @close(i32 %7)
br label %21
21: ; preds = %16, %13, %10, %2, %19
%22 = phi i32 [ 0, %19 ], [ -1, %2 ], [ -1, %10 ], [ -1, %13 ], [ -1, %16 ]
ret i32 %22
}