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


x86 assembly (clang, -O0):
<gpio_set_pull>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x1d,%esi
lea    0xe9f(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x30(%rbp),%rdi
mov    0x2e67(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jge    1190 <gpio_set_pull+0x50>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fe <gpio_set_pull+0xbe>
cmpl   $0x2,-0xc(%rbp)
jne    11b3 <gpio_set_pull+0x73>
mov    -0x10(%rbp),%edi
lea    0xe78(%rip),%rsi
mov    $0x5,%edx
call   1030 <write@plt>
jmp    11ef <gpio_set_pull+0xaf>
cmpl   $0x1,-0xc(%rbp)
jne    11d6 <gpio_set_pull+0x96>
mov    -0x10(%rbp),%edi
lea    0xe5a(%rip),%rsi
mov    $0x3,%edx
call   1030 <write@plt>
jmp    11ea <gpio_set_pull+0xaa>
mov    -0x10(%rbp),%edi
lea    0xe44(%rip),%rsi
mov    $0x8,%edx
call   1030 <write@plt>
jmp    11ef <gpio_set_pull+0xaf>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gpio_set_pull>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    %esi,%ebp
mov    %edi,%ecx
lea    0xeaf(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x1d,%esi
call   1040 <snprintf@plt>
mov    0x2e70(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <open@plt>
test   %eax,%eax
js     1192 <gpio_set_pull+0x52>
mov    %eax,%ebx
cmp    $0x1,%ebp
je     1199 <gpio_set_pull+0x59>
cmp    $0x2,%ebp
jne    11a9 <gpio_set_pull+0x69>
lea    0xe93(%rip),%rsi
mov    %ebx,%edi
mov    $0x5,%edx
jmp    11b7 <gpio_set_pull+0x77>
mov    $0xffffffff,%eax
jmp    11c5 <gpio_set_pull+0x85>
lea    0xe81(%rip),%rsi
mov    %ebx,%edi
mov    $0x3,%edx
jmp    11b7 <gpio_set_pull+0x77>
lea    0xe74(%rip),%rsi
mov    %ebx,%edi
mov    $0x8,%edx
call   1030 <write@plt>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
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 [29 x i8]
store i32 %0, i32* %4
store i32 %1, i32* %5
%8 = getelementptr inbounds [29 x i8], [29 x i8]* %7, i64 0, i64 0
%9 = load i32, i32* %4
%10 = call i32 @snprintf(i8* %8, i32 29, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = getelementptr inbounds [29 x i8], [29 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 %35
16: ; preds = %2
%17 = load i32, i32* %5
%18 = icmp eq i32 %17, 2
br i1 %18, label %19, label %22
19: ; preds = %16
%20 = load i32, i32* %6
%21 = call i32 @write(i32 %20, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 5)
br label %32
22: ; preds = %16
%23 = load i32, i32* %5
%24 = icmp eq i32 %23, 1
br i1 %24, label %25, label %28
25: ; preds = %22
%26 = load i32, i32* %6
%27 = call i32 @write(i32 %26, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 3)
br label %31
28: ; preds = %22
%29 = load i32, i32* %6
%30 = call i32 @write(i32 %29, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32 8)
br label %31
31: ; preds = %28, %25
br label %32
32: ; preds = %31, %19
%33 = load i32, i32* %6
%34 = call i32 @close(i32 %33)
store i32 0, i32* %3
br label %35
35: ; preds = %32, %15
%36 = load i32, i32* %3
ret i32 %36
}

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 [29 x i8]
%4 = getelementptr inbounds [29 x i8], [29 x i8]* %3, i64 0, i64 0
%5 = call i32 @snprintf(i8* %4, i32 29, 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 %18, label %9
9: ; preds = %2
switch i32 %1, label %14 [
i32 2, label %10
i32 1, label %12
]
10: ; preds = %9
%11 = call i32 @write(i32 %7, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 5)
br label %16
12: ; preds = %9
%13 = call i32 @write(i32 %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 3)
br label %16
14: ; preds = %9
%15 = call i32 @write(i32 %7, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32 8)
br label %16
16: ; preds = %12, %14, %10
%17 = call i32 @close(i32 %7)
br label %18
18: ; preds = %2, %16
%19 = phi i32 [ 0, %16 ], [ -1, %2 ]
ret i32 %19
}