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


x86 assembly (clang, -O0):
<gpio_set_value>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
lea    -0x50(%rbp),%rdi
mov    -0x4(%rbp),%ecx
mov    $0x40,%esi
lea    0xe8f(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x50(%rbp),%rdi
mov    0x2e57(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11a1 <gpio_set_value+0x51>
lea    0xe81(%rip),%rdi
call   1060 <error_exit@plt>
cmpl   $0x0,-0x8(%rbp)
je     11c4 <gpio_set_value+0x74>
mov    -0xc(%rbp),%edi
lea    0xe77(%rip),%rsi
mov    $0x2,%edx
call   1030 <write@plt>
jmp    11d8 <gpio_set_value+0x88>
mov    -0xc(%rbp),%edi
lea    0xe61(%rip),%rsi
mov    $0x2,%edx
call   1030 <write@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gpio_set_value>:
push   %rbp
push   %rbx
sub    $0x48,%rsp
mov    %esi,%ebx
mov    %edi,%ecx
lea    0xe9f(%rip),%rdx
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0x40,%esi
call   1040 <snprintf@plt>
mov    0x2e60(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1070 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1194 <gpio_set_value+0x44>
lea    0xe8e(%rip),%rdi
call   1060 <error_exit@plt>
test   %ebx,%ebx
lea    0xe92(%rip),%rax
lea    0xe88(%rip),%rsi
cmove  %rax,%rsi
mov    %ebp,%edi
mov    $0x2,%edx
call   1030 <write@plt>
mov    %ebp,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x48,%rsp
pop    %rbx
pop    %rbp
ret

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 = [3 x i8] c"1\0A\00"
@.str.3 = [3 x i8] c"0\0A\00"
define i32 @gpio_set_value(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [64 x i8]
store i32 %0, i32* %3
store i32 %1, i32* %4
%7 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%8 = load i32, i32* %3
%9 = call i32 @snprintf(i8* %7, i32 64, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%11 = load i32, i32* @O_WRONLY
%12 = call i32 @open(i8* %10, i32 %11)
store i32 %12, i32* %5
%13 = load i32, i32* %5
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = call i32 @error_exit(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %17
17: ; preds = %15, %2
%18 = load i32, i32* %4
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %17
%21 = load i32, i32* %5
%22 = call i32 @write(i32 %21, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 2)
br label %26
23: ; preds = %17
%24 = load i32, i32* %5
%25 = call i32 @write(i32 %24, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 2)
br label %26
26: ; preds = %23, %20
%27 = load i32, i32* %5
%28 = call i32 @close(i32 %27)
ret i32 0
}

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 = [3 x i8] c"1\0A\00"
@.str.3 = [3 x i8] c"0\0A\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 = load i32, i32* @O_WRONLY
%7 = call i32 @open(i8* %4, i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = call i32 @error_exit(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %11
11: ; preds = %9, %2
%12 = icmp eq i32 %1, 0
%13 = select i1 %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0)
%14 = call i32 @write(i32 %7, i8* %13, i32 2)
%15 = call i32 @close(i32 %7)
ret i32 0
}