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

/* Variables and functions */
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ stderr;
int write(int, char const *, int);
int gpio_write_pin(int pin, int setting) {
  static const char s_values_str[] = "01";
  char path[30];
  int fd;
  int values_index;
  snprintf(path, 30, "/sys/class/gpio/gpio%d/value", pin);
  fd = open(path, O_WRONLY);
  if (fd == -1) {
    fputs("Failed to open gpio value for writing!\n", stderr);
    return -1;
  }
  if (setting == 0) {
    values_index = 0;
  } else if (setting == 1) {
    values_index = 1;
  } else {
    fputs("error: invalid pin setting value\n", stderr);
    return -1;
  }
  if (write(fd, &s_values_str[values_index], 1) != 1) {
    fprintf(stderr, "Failed to write value!\n");
    return -1;
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<gpio_write_pin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x1e,%esi
lea    0xe82(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x30(%rbp),%rdi
mov    0x2e3f(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    11c6 <gpio_write_pin+0x66>
mov    0x2e34(%rip),%rax
mov    (%rax),%esi
lea    0xe6b(%rip),%rdi
call   1050 <fputs@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    126c <gpio_write_pin+0x10c>
cmpl   $0x0,-0xc(%rbp)
jne    11dc <gpio_write_pin+0x7c>
movl   $0x0,-0x38(%rbp)
jmp    1218 <gpio_write_pin+0xb8>
cmpl   $0x1,-0xc(%rbp)
jne    11f2 <gpio_write_pin+0x92>
movl   $0x1,-0x38(%rbp)
jmp    1213 <gpio_write_pin+0xb3>
mov    0x2de7(%rip),%rax
mov    (%rax),%esi
lea    0xe46(%rip),%rdi
call   1050 <fputs@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    126c <gpio_write_pin+0x10c>
jmp    1218 <gpio_write_pin+0xb8>
mov    -0x34(%rbp),%edi
movslq -0x38(%rbp),%rax
lea    0xdda(%rip),%rsi
add    %rax,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
je     125d <gpio_write_pin+0xfd>
mov    0x2d9d(%rip),%rax
mov    (%rax),%edi
lea    0xe1e(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    126c <gpio_write_pin+0x10c>
mov    -0x34(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gpio_write_pin>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    %esi,%ebp
mov    %edi,%ecx
lea    0xe92(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x1e,%esi
call   1040 <snprintf@plt>
mov    0x2e48(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     11a9 <gpio_write_pin+0x49>
mov    %eax,%ebx
test   %ebp,%ebp
je     11bb <gpio_write_pin+0x5b>
cmp    $0x1,%ebp
jne    11e3 <gpio_write_pin+0x83>
mov    $0x1,%eax
jmp    11bd <gpio_write_pin+0x5d>
mov    0x2e30(%rip),%rax
mov    (%rax),%esi
lea    0xe67(%rip),%rdi
jmp    11f3 <gpio_write_pin+0x93>
xor    %eax,%eax
lea    0xe3c(%rip),%rsi
add    %rax,%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
jne    11fa <gpio_write_pin+0x9a>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
jmp    1214 <gpio_write_pin+0xb4>
mov    0x2df6(%rip),%rax
mov    (%rax),%esi
lea    0xe55(%rip),%rdi
call   1050 <fputs@plt>
jmp    120f <gpio_write_pin+0xaf>
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
lea    0xe60(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@gpio_write_pin.s_values_str = internal constant [3 x i8] c"01\00"
@.str = [29 x i8] c"/sys/class/gpio/gpio%d/value\00"
@O_WRONLY = global i32 0
@.str.1 = [40 x i8] c"Failed to open gpio value for writing!\0A\00"
@stderr = global i32 0
@.str.2 = [34 x i8] c"error: invalid pin setting value\0A\00"
@.str.3 = [24 x i8] c"Failed to write value!\0A\00"
define i32 @gpio_write_pin(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [30 x i8]
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%9 = getelementptr inbounds [30 x i8], [30 x i8]* %6, i64 0, i64 0
%10 = load i32, i32* %4
%11 = call i32 @snprintf(i8* %9, i32 30, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = getelementptr inbounds [30 x i8], [30 x i8]* %6, i64 0, i64 0
%13 = load i32, i32* @O_WRONLY
%14 = call i32 @open(i8* %12, i32 %13)
store i32 %14, i32* %7
%15 = load i32, i32* %7
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %20
17: ; preds = %2
%18 = load i32, i32* @stderr
%19 = call i32 @fputs(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i32 %18)
store i32 -1, i32* %3
br label %45
20: ; preds = %2
%21 = load i32, i32* %5
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %20
store i32 0, i32* %8
br label %32
24: ; preds = %20
%25 = load i32, i32* %5
%26 = icmp eq i32 %25, 1
br i1 %26, label %27, label %28
27: ; preds = %24
store i32 1, i32* %8
br label %31
28: ; preds = %24
%29 = load i32, i32* @stderr
%30 = call i32 @fputs(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i32 %29)
store i32 -1, i32* %3
br label %45
31: ; preds = %27
br label %32
32: ; preds = %31, %23
%33 = load i32, i32* %7
%34 = load i32, i32* %8
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds [3 x i8], [3 x i8]* @gpio_write_pin.s_values_str, i64 0, i64 %35
%37 = call i32 @write(i32 %33, i8* %36, i32 1)
%38 = icmp ne i32 %37, 1
br i1 %38, label %39, label %42
39: ; preds = %32
%40 = load i32, i32* @stderr
%41 = call i32 @fprintf(i32 %40, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %3
br label %45
42: ; preds = %32
%43 = load i32, i32* %7
%44 = call i32 @close(i32 %43)
store i32 0, i32* %3
br label %45
45: ; preds = %42, %39, %28, %17
%46 = load i32, i32* %3
ret i32 %46
}

LLVM IR (-O2):
@gpio_write_pin.s_values_str = internal constant [3 x i8] c"01\00"
@.str = [29 x i8] c"/sys/class/gpio/gpio%d/value\00"
@O_WRONLY = global i32 0
@.str.1 = [40 x i8] c"Failed to open gpio value for writing!\0A\00"
@stderr = global i32 0
@.str.2 = [34 x i8] c"error: invalid pin setting value\0A\00"
@.str.3 = [24 x i8] c"Failed to write value!\0A\00"
define i32 @gpio_write_pin(i32 %0, i32 %1) {
%3 = alloca [30 x i8]
%4 = getelementptr inbounds [30 x i8], [30 x i8]* %3, i64 0, i64 0
%5 = call i32 @snprintf(i8* %4, i32 30, i8* getelementptr inbounds ([29 x i8], [29 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 eq i32 %7, -1
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = call i32 @fputs(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i32 %10)
br label %27
12: ; preds = %2
switch i32 %1, label %14 [
i32 0, label %17
i32 1, label %13
]
13: ; preds = %12
br label %17
14: ; preds = %12
%15 = load i32, i32* @stderr
%16 = call i32 @fputs(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i32 %15)
br label %27
17: ; preds = %12, %13
%18 = phi i64 [ 1, %13 ], [ 0, %12 ]
%19 = getelementptr inbounds [3 x i8], [3 x i8]* @gpio_write_pin.s_values_str, i64 0, i64 %18
%20 = call i32 @write(i32 %7, i8* %19, i32 1)
%21 = icmp eq i32 %20, 1
br i1 %21, label %25, label %22
22: ; preds = %17
%23 = load i32, i32* @stderr
%24 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
br label %27
25: ; preds = %17
%26 = call i32 @close(i32 %7)
br label %27
27: ; preds = %25, %22, %14, %9
%28 = phi i32 [ -1, %9 ], [ -1, %22 ], [ 0, %25 ], [ -1, %14 ]
ret i32 %28
}