#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, char *);
scalar_t__ strlen(char *);
int /*<<< orphan*/ write(int, char *, scalar_t__);
int gpio_set_edge(unsigned int gpio, char *ident, char *edge) {
  int fd, len;
  char buf[64];
  len = snprintf(buf, sizeof(buf),
                 "/sys/class/gpio"
                 "/gpio%d_%s/edge",
                 gpio, ident);
  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)
mov    %rdx,-0x18(%rbp)
lea    -0x60(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    -0x10(%rbp),%r8
mov    $0x40,%esi
lea    0xe76(%rip),%rdx
call   1050 <snprintf@plt>
mov    %eax,-0x20(%rbp)
lea    -0x60(%rbp),%rdi
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11c8 <gpio_set_edge+0x68>
lea    0xe67(%rip),%rdi
call   1080 <perror@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1201 <gpio_set_edge+0xa1>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x6c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x18(%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    -0x1c(%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    %rdx,%rbx
mov    %rsi,%r8
mov    %edi,%ecx
lea    0xe8b(%rip),%rdx
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0x40,%esi
call   1050 <snprintf@plt>
mov    0x2e4c(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1070 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
js     11c4 <gpio_set_edge+0x64>
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
mov    %ebp,%eax
add    $0x48,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe54(%rip),%rdi
call   1080 <perror@plt>
jmp    11bb <gpio_set_edge+0x5b>

LLVM IR (-O0):
@.str = [31 x i8] c"/sys/class/gpio/gpio%d_%s/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, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca [64 x i8]
store i32 %0, i32* %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%11 = getelementptr inbounds [64 x i8], [64 x i8]* %10, i64 0, i64 0
%12 = load i32, i32* %5
%13 = load i8*, i8** %6
%14 = call i32 @snprintf(i8* %11, i32 64, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i32 %12, i8* %13)
store i32 %14, i32* %9
%15 = getelementptr inbounds [64 x i8], [64 x i8]* %10, i64 0, i64 0
%16 = load i32, i32* @O_WRONLY
%17 = call i32 @open(i8* %15, i32 %16)
store i32 %17, i32* %8
%18 = load i32, i32* %8
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %3
%21 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%22 = load i32, i32* %8
store i32 %22, i32* %4
br label %32
23: ; preds = %3
%24 = load i32, i32* %8
%25 = load i8*, i8** %7
%26 = load i8*, i8** %7
%27 = call i64 @strlen(i8* %26)
%28 = add nsw i64 %27, 1
%29 = call i32 @write(i32 %24, i8* %25, i64 %28)
%30 = load i32, i32* %8
%31 = call i32 @close(i32 %30)
store i32 0, i32* %4
br label %32
32: ; preds = %23, %20
%33 = load i32, i32* %4
ret i32 %33
}

LLVM IR (-O2):
@.str = [31 x i8] c"/sys/class/gpio/gpio%d_%s/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, i8* nocapture %2) {
%4 = alloca [64 x i8]
%5 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%6 = call i32 @snprintf(i8* %5, i32 64, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i32 %0, i8* %1)
%7 = load i32, i32* @O_WRONLY
%8 = call i32 @open(i8* %5, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %3
%11 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %17
12: ; preds = %3
%13 = call i64 @strlen(i8* dereferenceable(1) %2)
%14 = add nsw i64 %13, 1
%15 = call i32 @write(i32 %8, i8* %2, i64 %14)
%16 = call i32 @close(i32 %8)
br label %17
17: ; preds = %12, %10
%18 = phi i32 [ %8, %10 ], [ 0, %12 ]
ret i32 %18
}