#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 snprintf(char *, int, char *, unsigned int);
int write(int, char *, int);
int gpio_set_value(unsigned int gpio, unsigned int value) {
  int fd, len;
  char buf[64];
  len = snprintf(buf, sizeof(buf),
                 "/sys/class/gpio"
                 "/gpio%d/value",
                 gpio);
  if (len < 0)
    return len;
  fd = open(buf, O_WRONLY);
  if (fd < 0) {
    perror("gpio/set-value");
    return fd;
  }
  if (value)
    len = write(fd, "1", 2);
  else
    len = write(fd, "0", 2);
  if (len < 0)
    return len;
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<gpio_set_value>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x60(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x40,%esi
lea    0xe8f(%rip),%rdx
call   1040 <snprintf@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    118e <gpio_set_value+0x3e>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1225 <gpio_set_value+0xd5>
lea    -0x60(%rbp),%rdi
mov    0x2e3f(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    11c4 <gpio_set_value+0x74>
lea    0xe69(%rip),%rdi
call   1070 <perror@plt>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1225 <gpio_set_value+0xd5>
cmpl   $0x0,-0xc(%rbp)
je     11ea <gpio_set_value+0x9a>
mov    -0x10(%rbp),%edi
lea    0xe54(%rip),%rsi
mov    $0x2,%edx
call   1030 <write@plt>
mov    %eax,-0x14(%rbp)
jmp    1201 <gpio_set_value+0xb1>
mov    -0x10(%rbp),%edi
lea    0xe3a(%rip),%rsi
mov    $0x2,%edx
call   1030 <write@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1216 <gpio_set_value+0xc6>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1225 <gpio_set_value+0xd5>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gpio_set_value>:
push   %rbp
push   %rbx
sub    $0x48,%rsp
mov    %esi,%ebp
mov    %edi,%ecx
lea    0xe9f(%rip),%rdx
mov    %rsp,%rdi
mov    $0x40,%esi
call   1040 <snprintf@plt>
test   %eax,%eax
js     11b8 <gpio_set_value+0x68>
mov    0x2e5f(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdi
call   1060 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11c3 <gpio_set_value+0x73>
test   %ebp,%ebp
lea    0xe9c(%rip),%rax
lea    0xe93(%rip),%rsi
cmove  %rax,%rsi
mov    %ebx,%edi
mov    $0x2,%edx
call   1030 <write@plt>
test   %eax,%eax
js     11b8 <gpio_set_value+0x68>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %ebx,%ebx
jmp    11ba <gpio_set_value+0x6a>
mov    %eax,%ebx
mov    %ebx,%eax
add    $0x48,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe53(%rip),%rdi
call   1070 <perror@plt>
jmp    11ba <gpio_set_value+0x6a>

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

LLVM IR (-O2):
@.str = [29 x i8] c"/sys/class/gpio/gpio%d/value\00"
@O_WRONLY = global i32 0
@.str.1 = [15 x i8] c"gpio/set-value\00"
@.str.2 = [2 x i8] c"1\00"
@.str.3 = [2 x i8] c"0\00"
define i32 @gpio_set_value(i32 %0, i32 %1) {
%3 = alloca [64 x i8]
%4 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%5 = call i32 @snprintf(i8* %4, i32 64, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %0)
%6 = icmp slt i32 %5, 0
br i1 %6, label %20, label %7
7: ; preds = %2
%8 = load i32, i32* @O_WRONLY
%9 = call i32 @open(i8* %4, i32 %8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %7
%12 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %20
13: ; preds = %7
%14 = icmp eq i32 %1, 0
%15 = select i1 %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0)
%16 = call i32 @write(i32 %9, i8* %15, i32 2)
%17 = icmp slt i32 %16, 0
br i1 %17, label %20, label %18
18: ; preds = %13
%19 = call i32 @close(i32 %9)
br label %20
20: ; preds = %13, %2, %18, %11
%21 = phi i32 [ %9, %11 ], [ 0, %18 ], [ %5, %2 ], [ %16, %13 ]
ret i32 %21
}