#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*/ filename;

/* Variables and functions */
int EINVAL;
int /*<<< orphan*/ O_WRONLY;
scalar_t__ close(int);
int errno;
int open(char *, int /*<<< orphan*/);
int snprintf(char *, int, char *, int);
int write(int, char *, int);
int gpio_set_irq(int gpio, char mode) {
  int ret;
  char filename[128];
  ret =
      snprintf(filename, sizeof(filename), "/sys/class/gpio/gpio%d/edge", gpio);
  if (ret < 0)
    goto out;
  int fd = open(filename, O_WRONLY);
  if (fd < 0) {
    ret = -errno;
    goto out;
  }
  switch (mode) {
  case 0:
    if (write(fd, "none", 4) != 4) {
      ret = -errno;
      goto out_close;
    }
    break;
  case 1:
    if (write(fd, "rising", 6) != 6) {
      ret = -errno;
      goto out_close;
    }
    break;
  case 2:
    if (write(fd, "falling", 7) != 7) {
      ret = -errno;
      goto out_close;
    }
    break;
  case 3:
    if (write(fd, "both", 4) != 4) {
      ret = -errno;
      goto out_close;
    }
    break;
  default:
    ret = -EINVAL;
  }
out_close:
  if (close(fd) < 0)
    ret = -errno;
out:
  return ret;
}


x86 assembly (clang, -O0):
<gpio_set_irq>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %sil,%al
mov    %edi,-0x4(%rbp)
mov    %al,-0x5(%rbp)
lea    -0x90(%rbp),%rdi
mov    -0x4(%rbp),%ecx
mov    $0x80,%esi
lea    0xea6(%rip),%rdx
call   1040 <snprintf@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1181 <gpio_set_irq+0x41>
jmp    1304 <gpio_set_irq+0x1c4>
lea    -0x90(%rbp),%rdi
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x94(%rbp)
cmpl   $0x0,-0x94(%rbp)
jge    11bc <gpio_set_irq+0x7c>
xor    %eax,%eax
mov    0x2e2e(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0xc(%rbp)
jmp    1304 <gpio_set_irq+0x1c4>
movsbl -0x5(%rbp),%eax
mov    %eax,%ecx
mov    %rcx,-0xa0(%rbp)
sub    $0x3,%eax
ja     12c9 <gpio_set_irq+0x189>
mov    -0xa0(%rbp),%rax
lea    0xe20(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    -0x94(%rbp),%edi
lea    0xe36(%rip),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
cmp    $0x4,%eax
je     121c <gpio_set_irq+0xdc>
xor    %eax,%eax
mov    0x2dce(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0xc(%rbp)
jmp    12dc <gpio_set_irq+0x19c>
jmp    12d7 <gpio_set_irq+0x197>
mov    -0x94(%rbp),%edi
lea    0xe03(%rip),%rsi
mov    $0x6,%edx
call   1030 <write@plt>
cmp    $0x6,%eax
je     1254 <gpio_set_irq+0x114>
xor    %eax,%eax
mov    0x2d96(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0xc(%rbp)
jmp    12dc <gpio_set_irq+0x19c>
jmp    12d7 <gpio_set_irq+0x197>
mov    -0x94(%rbp),%edi
lea    0xdd2(%rip),%rsi
mov    $0x7,%edx
call   1030 <write@plt>
cmp    $0x7,%eax
je     128c <gpio_set_irq+0x14c>
xor    %eax,%eax
mov    0x2d5e(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0xc(%rbp)
jmp    12dc <gpio_set_irq+0x19c>
jmp    12d7 <gpio_set_irq+0x197>
mov    -0x94(%rbp),%edi
lea    0xda2(%rip),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
cmp    $0x4,%eax
je     12c4 <gpio_set_irq+0x184>
xor    %eax,%eax
mov    0x2d26(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0xc(%rbp)
jmp    12dc <gpio_set_irq+0x19c>
jmp    12d7 <gpio_set_irq+0x197>
xor    %eax,%eax
mov    0x2d06(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0xc(%rbp)
jmp    12dc <gpio_set_irq+0x19c>
mov    -0x94(%rbp),%edi
call   1050 <close@plt>
cmp    $0x0,%rax
jge    12ff <gpio_set_irq+0x1bf>
xor    %eax,%eax
mov    0x2ce6(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0xc(%rbp)
jmp    1304 <gpio_set_irq+0x1c4>
mov    -0xc(%rbp),%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gpio_set_irq>:
push   %rbp
push   %r14
push   %rbx
sub    $0x80,%rsp
mov    %esi,%ebp
mov    %edi,%ecx
lea    0xeba(%rip),%rdx
mov    %rsp,%rdi
mov    $0x80,%esi
call   1040 <snprintf@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1226 <gpio_set_irq+0xe6>
mov    0x2e54(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdi
call   1060 <open@plt>
test   %eax,%eax
js     121b <gpio_set_irq+0xdb>
mov    %eax,%r14d
movsbl %bpl,%eax
cmp    $0x3,%eax
ja     11ab <gpio_set_irq+0x6b>
lea    0xe67(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
lea    0xe83(%rip),%rsi
jmp    11f1 <gpio_set_irq+0xb1>
mov    0x2e26(%rip),%rax
jmp    120a <gpio_set_irq+0xca>
lea    0xe76(%rip),%rsi
mov    %r14d,%edi
mov    $0x6,%edx
call   1030 <write@plt>
cmp    $0x6,%eax
jne    1203 <gpio_set_irq+0xc3>
jmp    120e <gpio_set_irq+0xce>
lea    0xe62(%rip),%rsi
mov    %r14d,%edi
mov    $0x7,%edx
call   1030 <write@plt>
cmp    $0x7,%eax
jne    1203 <gpio_set_irq+0xc3>
jmp    120e <gpio_set_irq+0xce>
lea    0xe4f(%rip),%rsi
mov    %r14d,%edi
mov    $0x4,%edx
call   1030 <write@plt>
cmp    $0x4,%eax
je     120e <gpio_set_irq+0xce>
mov    0x2dd6(%rip),%rax
xor    %ebx,%ebx
sub    (%rax),%ebx
mov    %r14d,%edi
call   1050 <close@plt>
test   %rax,%rax
jns    1226 <gpio_set_irq+0xe6>
mov    0x2dbe(%rip),%rax
xor    %ebx,%ebx
sub    (%rax),%ebx
mov    %ebx,%eax
add    $0x80,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [28 x i8] c"/sys/class/gpio/gpio%d/edge\00"
@O_WRONLY = global i32 0
@errno = global i32 0
@.str.1 = [5 x i8] c"none\00"
@.str.2 = [7 x i8] c"rising\00"
@.str.3 = [8 x i8] c"falling\00"
@.str.4 = [5 x i8] c"both\00"
@EINVAL = global i32 0
define i32 @gpio_set_irq(i32 %0, i8 signext %1) {
%3 = alloca i32
%4 = alloca i8
%5 = alloca i32
%6 = alloca [128 x i8]
%7 = alloca i32
store i32 %0, i32* %3
store i8 %1, i8* %4
%8 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%9 = load i32, i32* %3
%10 = call i32 @snprintf(i8* %8, i32 128, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %9)
store i32 %10, i32* %5
%11 = load i32, i32* %5
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %2
br label %70
14: ; preds = %2
%15 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%16 = load i32, i32* @O_WRONLY
%17 = call i32 @open(i8* %15, i32 %16)
store i32 %17, i32* %7
%18 = load i32, i32* %7
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %14
%21 = load i32, i32* @errno
%22 = sub nsw i32 0, %21
store i32 %22, i32* %5
br label %70
23: ; preds = %14
%24 = load i8, i8* %4
%25 = sext i8 %24 to i32
switch i32 %25, label %58 [
i32 0, label %26
i32 1, label %34
i32 2, label %42
i32 3, label %50
]
26: ; preds = %23
%27 = load i32, i32* %7
%28 = call i32 @write(i32 %27, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 4)
%29 = icmp ne i32 %28, 4
br i1 %29, label %30, label %33
30: ; preds = %26
%31 = load i32, i32* @errno
%32 = sub nsw i32 0, %31
store i32 %32, i32* %5
br label %62
33: ; preds = %26
br label %61
34: ; preds = %23
%35 = load i32, i32* %7
%36 = call i32 @write(i32 %35, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 6)
%37 = icmp ne i32 %36, 6
br i1 %37, label %38, label %41
38: ; preds = %34
%39 = load i32, i32* @errno
%40 = sub nsw i32 0, %39
store i32 %40, i32* %5
br label %62
41: ; preds = %34
br label %61
42: ; preds = %23
%43 = load i32, i32* %7
%44 = call i32 @write(i32 %43, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32 7)
%45 = icmp ne i32 %44, 7
br i1 %45, label %46, label %49
46: ; preds = %42
%47 = load i32, i32* @errno
%48 = sub nsw i32 0, %47
store i32 %48, i32* %5
br label %62
49: ; preds = %42
br label %61
50: ; preds = %23
%51 = load i32, i32* %7
%52 = call i32 @write(i32 %51, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32 4)
%53 = icmp ne i32 %52, 4
br i1 %53, label %54, label %57
54: ; preds = %50
%55 = load i32, i32* @errno
%56 = sub nsw i32 0, %55
store i32 %56, i32* %5
br label %62
57: ; preds = %50
br label %61
58: ; preds = %23
%59 = load i32, i32* @EINVAL
%60 = sub nsw i32 0, %59
store i32 %60, i32* %5
br label %61
61: ; preds = %58, %57, %49, %41, %33
br label %62
62: ; preds = %61, %54, %46, %38, %30
%63 = load i32, i32* %7
%64 = call i64 @close(i32 %63)
%65 = icmp slt i64 %64, 0
br i1 %65, label %66, label %69
66: ; preds = %62
%67 = load i32, i32* @errno
%68 = sub nsw i32 0, %67
store i32 %68, i32* %5
br label %69
69: ; preds = %66, %62
br label %70
70: ; preds = %69, %20, %13
%71 = load i32, i32* %5
ret i32 %71
}

LLVM IR (-O2):
@.str = [28 x i8] c"/sys/class/gpio/gpio%d/edge\00"
@O_WRONLY = global i32 0
@errno = global i32 0
@.str.1 = [5 x i8] c"none\00"
@.str.2 = [7 x i8] c"rising\00"
@.str.3 = [8 x i8] c"falling\00"
@.str.4 = [5 x i8] c"both\00"
@EINVAL = global i32 0
define i32 @gpio_set_irq(i32 %0, i8 signext %1) {
%3 = alloca [128 x i8]
%4 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%5 = call i32 @snprintf(i8* %4, i32 128, 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 %50, 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 %14
11: ; preds = %7
%12 = load i32, i32* @errno
%13 = sub nsw i32 0, %12
br label %50
14: ; preds = %7
%15 = sext i8 %1 to i32
switch i32 %15, label %40 [
i32 0, label %16
i32 1, label %22
i32 2, label %28
i32 3, label %34
]
16: ; preds = %14
%17 = call i32 @write(i32 %9, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 4)
%18 = icmp eq i32 %17, 4
br i1 %18, label %43, label %19
19: ; preds = %16
%20 = load i32, i32* @errno
%21 = sub nsw i32 0, %20
br label %43
22: ; preds = %14
%23 = call i32 @write(i32 %9, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 6)
%24 = icmp eq i32 %23, 6
br i1 %24, label %43, label %25
25: ; preds = %22
%26 = load i32, i32* @errno
%27 = sub nsw i32 0, %26
br label %43
28: ; preds = %14
%29 = call i32 @write(i32 %9, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32 7)
%30 = icmp eq i32 %29, 7
br i1 %30, label %43, label %31
31: ; preds = %28
%32 = load i32, i32* @errno
%33 = sub nsw i32 0, %32
br label %43
34: ; preds = %14
%35 = call i32 @write(i32 %9, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32 4)
%36 = icmp eq i32 %35, 4
br i1 %36, label %43, label %37
37: ; preds = %34
%38 = load i32, i32* @errno
%39 = sub nsw i32 0, %38
br label %43
40: ; preds = %14
%41 = load i32, i32* @EINVAL
%42 = sub nsw i32 0, %41
br label %43
43: ; preds = %40, %16, %22, %28, %34, %37, %31, %25, %19
%44 = phi i32 [ %42, %40 ], [ %39, %37 ], [ %5, %34 ], [ %33, %31 ], [ %5, %28 ], [ %27, %25 ], [ %5, %22 ], [ %21, %19 ], [ %5, %16 ]
%45 = call i64 @close(i32 %9)
%46 = icmp slt i64 %45, 0
br i1 %46, label %47, label %50
47: ; preds = %43
%48 = load i32, i32* @errno
%49 = sub nsw i32 0, %48
br label %50
50: ; preds = %43, %47, %2, %11
%51 = phi i32 [ %5, %2 ], [ %13, %11 ], [ %49, %47 ], [ %44, %43 ]
ret i32 %51
}