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


x86 assembly (clang, -O0):
<gpio_set_edge>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x60(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x40,%esi
lea    0xe7e(%rip),%rdx
call   1050 <snprintf@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    119f <gpio_set_edge+0x3f>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    120e <gpio_set_edge+0xae>
lea    -0x60(%rbp),%rdi
mov    0x2e2e(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11d5 <gpio_set_edge+0x75>
lea    0xe57(%rip),%rdi
call   1080 <perror@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    120e <gpio_set_edge+0xae>
mov    -0x14(%rbp),%eax
mov    %eax,-0x6c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x6c(%rbp),%edi
mov    -0x68(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
call   1030 <write@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gpio_set_edge>:
push   %rbp
push   %rbx
sub    $0x48,%rsp
mov    %rsi,%rbx
mov    %edi,%ecx
lea    0xe8e(%rip),%rdx
mov    %rsp,%rdi
mov    $0x40,%esi
call   1050 <snprintf@plt>
test   %eax,%eax
js     11bb <gpio_set_edge+0x5b>
mov    0x2e4e(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdi
call   1070 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
js     11c6 <gpio_set_edge+0x66>
mov    %rbx,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%rdx
mov    %ebp,%edi
mov    %rbx,%rsi
call   1030 <write@plt>
mov    %ebp,%edi
call   1060 <close@plt>
xor    %ebp,%ebp
jmp    11bd <gpio_set_edge+0x5d>
mov    %eax,%ebp
mov    %ebp,%eax
add    $0x48,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe4f(%rip),%rdi
call   1080 <perror@plt>
jmp    11bd <gpio_set_edge+0x5d>

LLVM IR (-O0):
@.str = [28 x i8] c"/sys/class/gpio/gpio%d/edge\00"
@O_WRONLY = global i32 0
@.str.1 = [14 x i8] c"gpio/set-edge\00"
define i32 @gpio_set_edge(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca [64 x i8]
store i32 %0, i32* %4
store i8* %1, i8** %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 ([28 x i8], [28 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 %34
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 ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%24 = load i32, i32* %6
store i32 %24, i32* %3
br label %34
25: ; preds = %16
%26 = load i32, i32* %6
%27 = load i8*, i8** %5
%28 = load i8*, i8** %5
%29 = call i64 @strlen(i8* %28)
%30 = add nsw i64 %29, 1
%31 = call i32 @write(i32 %26, i8* %27, i64 %30)
%32 = load i32, i32* %6
%33 = call i32 @close(i32 %32)
store i32 0, i32* %3
br label %34
34: ; preds = %25, %22, %14
%35 = load i32, i32* %3
ret i32 %35
}

LLVM IR (-O2):
@.str = [28 x i8] c"/sys/class/gpio/gpio%d/edge\00"
@O_WRONLY = global i32 0
@.str.1 = [14 x i8] c"gpio/set-edge\00"
define i32 @gpio_set_edge(i32 %0, i8* nocapture %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 ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %0)
%6 = icmp slt i32 %5, 0
br i1 %6, label %18, 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 ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %18
13: ; preds = %7
%14 = call i64 @strlen(i8* dereferenceable(1) %1)
%15 = add nsw i64 %14, 1
%16 = call i32 @write(i32 %9, i8* %1, i64 %15)
%17 = call i32 @close(i32 %9)
br label %18
18: ; preds = %2, %13, %11
%19 = phi i32 [ %9, %11 ], [ 0, %13 ], [ %5, %2 ]
ret i32 %19
}