#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 open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int write(int, char *, int);
int g_write(int pino, int estado) {
  int arquivo;
  char caminho[33];
  snprintf(caminho, 33, "/sys/class/gpio/gpio%d/value", pino);
  arquivo = open(caminho, O_WRONLY);
  if (arquivo == -1)
    return -1;
  if (write(arquivo, (estado == 0) ? "0" : "1", 1) == -1) {
    close(arquivo);
    return -2;
  }
  close(arquivo);
  return 1;
}


x86 assembly (clang, -O0):
<g_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x40(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x21,%esi
lea    0xe9f(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x40(%rbp),%rdi
mov    0x2e67(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    1191 <g_write+0x51>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e2 <g_write+0xa2>
mov    -0x10(%rbp),%edi
mov    -0xc(%rbp),%ecx
lea    0xe81(%rip),%rsi
lea    0xe78(%rip),%rax
cmp    $0x0,%ecx
cmove  %rax,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    11d3 <g_write+0x93>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    11e2 <g_write+0xa2>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<g_write>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    %esi,%ebx
mov    %edi,%ecx
lea    0xeaf(%rip),%rdx
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0x21,%esi
call   1040 <snprintf@plt>
mov    0x2e70(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11b0 <g_write+0x70>
mov    %eax,%ebp
test   %ebx,%ebx
lea    0xe9b(%rip),%rax
lea    0xe96(%rip),%rsi
cmove  %rax,%rsi
mov    %ebp,%edi
mov    $0x1,%edx
call   1030 <write@plt>
xor    %ebx,%ebx
cmp    $0xffffffff,%eax
setne  %bl
mov    %ebp,%edi
call   1050 <close@plt>
lea    (%rbx,%rbx,2),%eax
add    $0xfffffffe,%eax
jmp    11b5 <g_write+0x75>
mov    $0xffffffff,%eax
add    $0x28,%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 = [2 x i8] c"0\00"
@.str.2 = [2 x i8] c"1\00"
define i32 @g_write(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca [33 x i8]
store i32 %0, i32* %4
store i32 %1, i32* %5
%8 = getelementptr inbounds [33 x i8], [33 x i8]* %7, i64 0, i64 0
%9 = load i32, i32* %4
%10 = call i32 @snprintf(i8* %8, i32 33, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = getelementptr inbounds [33 x i8], [33 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 = load i32, i32* %6
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %17
16: ; preds = %2
store i32 -1, i32* %3
br label %31
17: ; preds = %2
%18 = load i32, i32* %6
%19 = load i32, i32* %5
%20 = icmp eq i32 %19, 0
%21 = zext i1 %20 to i64
%22 = select i1 %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0)
%23 = call i32 @write(i32 %18, i8* %22, i32 1)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %28
25: ; preds = %17
%26 = load i32, i32* %6
%27 = call i32 @close(i32 %26)
store i32 -2, i32* %3
br label %31
28: ; preds = %17
%29 = load i32, i32* %6
%30 = call i32 @close(i32 %29)
store i32 1, i32* %3
br label %31
31: ; preds = %28, %25, %16
%32 = load i32, i32* %3
ret i32 %32
}

LLVM IR (-O2):
@.str = [29 x i8] c"/sys/class/gpio/gpio%d/value\00"
@O_WRONLY = global i32 0
@.str.1 = [2 x i8] c"0\00"
@.str.2 = [2 x i8] c"1\00"
define i32 @g_write(i32 %0, i32 %1) {
%3 = alloca [33 x i8]
%4 = getelementptr inbounds [33 x i8], [33 x i8]* %3, i64 0, i64 0
%5 = call i32 @snprintf(i8* %4, i32 33, 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 %16, label %9
9: ; preds = %2
%10 = icmp eq i32 %1, 0
%11 = select i1 %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0)
%12 = call i32 @write(i32 %7, i8* %11, i32 1)
%13 = icmp eq i32 %12, -1
%14 = call i32 @close(i32 %7)
%15 = select i1 %13, i32 -2, i32 1
br label %16
16: ; preds = %9, %2
%17 = phi i32 [ -1, %2 ], [ %15, %9 ]
ret i32 %17
}