#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 asprintf(char **, char *, unsigned int);
int /*<<< orphan*/ close(int);
int dprintf(int, char *, unsigned int);
int /*<<< orphan*/ free(char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int gpio_out(unsigned int gpio, unsigned int value) {
  int ret;
  char *filename;
  int f;
  ret = asprintf(&filename, "/sys/class/gpio/gpio%u/value", gpio);
  if (ret < 0)
    return ret;
  f = open(filename, O_WRONLY);
  if (f < 0) {
    perror("open");
    return f;
  }
  ret = dprintf(f, "%u", value);
  close(f);
  free(filename);
  return ret < 0 ? ret : 0;
}


x86 assembly (clang, -O0):
<gpio_out>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%edx
lea    -0x18(%rbp),%rdi
lea    0xe84(%rip),%rsi
call   1060 <asprintf@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    1199 <gpio_out+0x39>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    121a <gpio_out+0xba>
mov    -0x18(%rbp),%rdi
mov    0x2e34(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11cf <gpio_out+0x6f>
lea    0xe5e(%rip),%rdi
call   1080 <perror@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    121a <gpio_out+0xba>
mov    -0x1c(%rbp),%edi
mov    -0xc(%rbp),%edx
lea    0xe46(%rip),%rsi
call   1050 <dprintf@plt>
mov    %eax,-0x10(%rbp)
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
cmpl   $0x0,-0x10(%rbp)
jge    120a <gpio_out+0xaa>
mov    -0x10(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    1214 <gpio_out+0xb4>
xor    %eax,%eax
mov    %eax,-0x20(%rbp)
jmp    1214 <gpio_out+0xb4>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gpio_out>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %edi,%edx
lea    0xe92(%rip),%rsi
mov    %rsp,%rdi
call   1060 <asprintf@plt>
test   %eax,%eax
js     11bd <gpio_out+0x5d>
mov    (%rsp),%rdi
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11c8 <gpio_out+0x68>
lea    0xe89(%rip),%rsi
mov    %ebx,%edi
mov    %ebp,%edx
call   1050 <dprintf@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1040 <close@plt>
mov    (%rsp),%rdi
call   1030 <free@plt>
mov    %ebp,%ebx
sar    $0x1f,%ebx
and    %ebp,%ebx
jmp    11bf <gpio_out+0x5f>
mov    %eax,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe4e(%rip),%rdi
call   1080 <perror@plt>
jmp    11bf <gpio_out+0x5f>

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

LLVM IR (-O2):
@.str = [29 x i8] c"/sys/class/gpio/gpio%u/value\00"
@O_WRONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [3 x i8] c"%u\00"
define i32 @gpio_out(i32 %0, i32 %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
%5 = call i32 @asprintf(i8** %3, 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 %21, label %7
7: ; preds = %2
%8 = load i8*, i8** %3
%9 = load i32, i32* @O_WRONLY
%10 = call i32 @open(i8* %8, i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %7
%13 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %21
14: ; preds = %7
%15 = call i32 @dprintf(i32 %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %1)
%16 = call i32 @close(i32 %10)
%17 = load i8*, i8** %3
%18 = call i32 @free(i8* %17)
%19 = icmp slt i32 %15, 0
%20 = select i1 %19, i32 %15, i32 0
br label %21
21: ; preds = %2, %14, %12
%22 = phi i32 [ %10, %12 ], [ %20, %14 ], [ %5, %2 ]
ret i32 %22
}