#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;
scalar_t__ close(int);
int /*<<< orphan*/ memset(char *, int, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *);
int /*<<< orphan*/ strlen(char const *);
int /*<<< orphan*/ warnx(char *);
scalar_t__ write(int, char const *, int /*<<< orphan*/);
int set_gpio_trigger_mode(const char *gpio_pin, const char *gpio_trigger_mode) {
  int fd;
  char fn[48];
  if (gpio_pin == NULL || *gpio_pin == '\0') {
    return -1;
  }
  if (gpio_trigger_mode == NULL || *gpio_trigger_mode == '\0') {
    return -1;
  }
  memset(fn, 0x00, 48);
  snprintf(fn, 48 - 1,
           "/tmp"
           "/gpio/gpio%s/edge",
           gpio_pin);
  if ((fd = open(fn, O_WRONLY)) < 0) {
    warnx("open(SYS_GPIO_EDGE) failed");
    return -1;
  }
  if (write(fd, gpio_trigger_mode, strlen(gpio_trigger_mode)) < 0) {
    warnx("write(SYS_GPIO_DIRECTION) failed");
    return -1;
  }
  if (close(fd) < 0) {
    warnx("close(SYS_GPIO_DIRECTION) failed");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<set_gpio_trigger_mode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     119b <set_gpio_trigger_mode+0x2b>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    11a7 <set_gpio_trigger_mode+0x37>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a5 <set_gpio_trigger_mode+0x135>
cmpq   $0x0,-0x18(%rbp)
je     11c2 <set_gpio_trigger_mode+0x52>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    11ce <set_gpio_trigger_mode+0x5e>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a5 <set_gpio_trigger_mode+0x135>
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x30,%edx
call   1070 <memset@plt>
lea    -0x50(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x2f,%esi
lea    0xe0e(%rip),%rdx
call   1060 <snprintf@plt>
lea    -0x50(%rbp),%rdi
mov    0x2dd6(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    122d <set_gpio_trigger_mode+0xbd>
lea    0xdfa(%rip),%rdi
call   1050 <warnx@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a5 <set_gpio_trigger_mode+0x135>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x5c(%rbp),%edi
mov    -0x58(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    1274 <set_gpio_trigger_mode+0x104>
lea    0xdce(%rip),%rdi
call   1050 <warnx@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a5 <set_gpio_trigger_mode+0x135>
mov    -0x1c(%rbp),%edi
call   1080 <close@plt>
cmp    $0x0,%rax
jge    129e <set_gpio_trigger_mode+0x12e>
lea    0xdc5(%rip),%rdi
call   1050 <warnx@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a5 <set_gpio_trigger_mode+0x135>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_gpio_trigger_mode>:
push   %r15
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    $0xffffffff,%eax
test   %rdi,%rdi
je     1236 <set_gpio_trigger_mode+0xc6>
mov    %rsi,%r14
test   %rsi,%rsi
je     1236 <set_gpio_trigger_mode+0xc6>
mov    %rdi,%rbx
cmpb   $0x0,(%rdi)
je     1236 <set_gpio_trigger_mode+0xc6>
cmpb   $0x0,(%r14)
je     1236 <set_gpio_trigger_mode+0xc6>
mov    %rsp,%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x30,%edx
call   1070 <memset@plt>
lea    0xe3e(%rip),%rdx
mov    %r15,%rdi
mov    $0x2f,%esi
mov    %rbx,%rcx
call   1060 <snprintf@plt>
mov    0x2dff(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdi
call   1090 <open@plt>
test   %eax,%eax
js     121c <set_gpio_trigger_mode+0xac>
mov    %eax,%ebx
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
test   %rax,%rax
js     1225 <set_gpio_trigger_mode+0xb5>
mov    %ebx,%edi
call   1080 <close@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jns    1236 <set_gpio_trigger_mode+0xc6>
lea    0xe38(%rip),%rdi
jmp    122c <set_gpio_trigger_mode+0xbc>
lea    0xdf3(%rip),%rdi
jmp    122c <set_gpio_trigger_mode+0xbc>
lea    0xe05(%rip),%rdi
call   1050 <warnx@plt>
mov    $0xffffffff,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [22 x i8] c"/tmp/gpio/gpio%s/edge\00"
@O_WRONLY = global i32 0
@.str.1 = [27 x i8] c"open(SYS_GPIO_EDGE) failed\00"
@.str.2 = [33 x i8] c"write(SYS_GPIO_DIRECTION) failed\00"
@.str.3 = [33 x i8] c"close(SYS_GPIO_DIRECTION) failed\00"
define i32 @set_gpio_trigger_mode(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca [48 x i8]
store i8* %0, i8** %4
store i8* %1, i8** %5
%8 = load i8*, i8** %4
%9 = icmp eq i8* %8, null
br i1 %9, label %15, label %10
10: ; preds = %2
%11 = load i8*, i8** %4
%12 = load i8, i8* %11
%13 = sext i8 %12 to i32
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %10, %2
store i32 -1, i32* %3
br label %53
16: ; preds = %10
%17 = load i8*, i8** %5
%18 = icmp eq i8* %17, null
br i1 %18, label %24, label %19
19: ; preds = %16
%20 = load i8*, i8** %5
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %19, %16
store i32 -1, i32* %3
br label %53
25: ; preds = %19
%26 = getelementptr inbounds [48 x i8], [48 x i8]* %7, i64 0, i64 0
%27 = call i32 @memset(i8* %26, i32 0, i32 48)
%28 = getelementptr inbounds [48 x i8], [48 x i8]* %7, i64 0, i64 0
%29 = load i8*, i8** %4
%30 = call i32 @snprintf(i8* %28, i32 47, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %29)
%31 = getelementptr inbounds [48 x i8], [48 x i8]* %7, i64 0, i64 0
%32 = load i32, i32* @O_WRONLY
%33 = call i32 @open(i8* %31, i32 %32)
store i32 %33, i32* %6
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %25
%36 = call i32 @warnx(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %53
37: ; preds = %25
%38 = load i32, i32* %6
%39 = load i8*, i8** %5
%40 = load i8*, i8** %5
%41 = call i32 @strlen(i8* %40)
%42 = call i64 @write(i32 %38, i8* %39, i32 %41)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %37
%45 = call i32 @warnx(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %53
46: ; preds = %37
%47 = load i32, i32* %6
%48 = call i64 @close(i32 %47)
%49 = icmp slt i64 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %46
%51 = call i32 @warnx(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %3
br label %53
52: ; preds = %46
store i32 0, i32* %3
br label %53
53: ; preds = %52, %50, %44, %35, %24, %15
%54 = load i32, i32* %3
ret i32 %54
}

LLVM IR (-O2):
@.str = [22 x i8] c"/tmp/gpio/gpio%s/edge\00"
@O_WRONLY = global i32 0
@.str.1 = [27 x i8] c"open(SYS_GPIO_EDGE) failed\00"
@.str.2 = [33 x i8] c"write(SYS_GPIO_DIRECTION) failed\00"
@.str.3 = [33 x i8] c"close(SYS_GPIO_DIRECTION) failed\00"
define i32 @set_gpio_trigger_mode(i8* %0, i8* %1) {
%3 = alloca [48 x i8]
%4 = getelementptr inbounds [48 x i8], [48 x i8]* %3, i64 0, i64 0
%5 = icmp eq i8* %0, null
br i1 %5, label %30, label %6
6: ; preds = %2
%7 = load i8, i8* %0
%8 = icmp eq i8 %7, 0
%9 = icmp eq i8* %1, null
%10 = or i1 %9, %8
br i1 %10, label %30, label %11
11: ; preds = %6
%12 = load i8, i8* %1
%13 = icmp eq i8 %12, 0
br i1 %13, label %30, label %14
14: ; preds = %11
%15 = call i32 @memset(i8* %4, i32 0, i32 48)
%16 = call i32 @snprintf(i8* %4, i32 47, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %0)
%17 = load i32, i32* @O_WRONLY
%18 = call i32 @open(i8* %4, i32 %17)
%19 = icmp slt i32 %18, 0
br i1 %19, label %27, label %20
20: ; preds = %14
%21 = call i32 @strlen(i8* %1)
%22 = call i64 @write(i32 %18, i8* %1, i32 %21)
%23 = icmp slt i64 %22, 0
br i1 %23, label %27, label %24
24: ; preds = %20
%25 = call i64 @close(i32 %18)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %24, %20, %14
%28 = phi i8* [ getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), %14 ], [ getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), %20 ], [ getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), %24 ]
%29 = call i32 @warnx(i8* %28)
br label %30
30: ; preds = %27, %24, %11, %2, %6
%31 = phi i32 [ -1, %6 ], [ -1, %2 ], [ -1, %11 ], [ 0, %24 ], [ -1, %27 ]
ret i32 %31
}