#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_RDWR;
int /*<<< orphan*/ close(int);
int ioctl(int, unsigned long, unsigned long *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int __api(unsigned long cmd, unsigned long val) {
  int fd, ret;
  fd = open("/dev/resch", O_RDWR);
  if (fd < 0) {
    printf("Error: failed to access the module!\n");
    return 1;
  }
  ret = ioctl(fd, cmd, &val);
  if (ret == -1) {
    printf("Error: failed ioctl: cmd %lu, val: %lu\n", cmd, val);
  }
  close(fd);
  return ret;
}


x86 assembly (clang, -O0):
<__api>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e91(%rip),%rax
mov    (%rax),%esi
lea    0xea0(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    118c <__api+0x4c>
lea    0xe92(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    11cd <__api+0x8d>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    -0x18(%rbp),%rdx
call   1040 <ioctl@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    11bf <__api+0x7f>
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xe78(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__api>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
mov    %rsi,0x8(%rsp)
mov    0x2e81(%rip),%rax
mov    (%rax),%esi
lea    0xe90(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     11b0 <__api+0x60>
mov    %eax,%ebp
lea    0x8(%rsp),%rdx
mov    %eax,%edi
mov    %r14,%rsi
call   1050 <ioctl@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    11a7 <__api+0x57>
mov    0x8(%rsp),%rdx
lea    0xe6e(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebp,%edi
call   1060 <close@plt>
jmp    11c1 <__api+0x71>
lea    0xe7c(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [11 x i8] c"/dev/resch\00"
@O_RDWR = global i32 0
@.str.1 = [37 x i8] c"Error: failed to access the module!\0A\00"
@.str.2 = [40 x i8] c"Error: failed ioctl: cmd %lu, val: %lu\0A\00"
define i32 @__api(i64 %0, i64 %1) {
%3 = alloca i32
%4 = alloca i64
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32
store i64 %0, i64* %4
store i64 %1, i64* %5
%8 = load i32, i32* @O_RDWR
%9 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %8)
store i32 %9, i32* %6
%10 = load i32, i32* %6
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %28
14: ; preds = %2
%15 = load i32, i32* %6
%16 = load i64, i64* %4
%17 = call i32 @ioctl(i32 %15, i64 %16, i64* %5)
store i32 %17, i32* %7
%18 = load i32, i32* %7
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %24
20: ; preds = %14
%21 = load i64, i64* %4
%22 = load i64, i64* %5
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i64 %21, i64 %22)
br label %24
24: ; preds = %20, %14
%25 = load i32, i32* %6
%26 = call i32 @close(i32 %25)
%27 = load i32, i32* %7
store i32 %27, i32* %3
br label %28
28: ; preds = %24, %12
%29 = load i32, i32* %3
ret i32 %29
}

LLVM IR (-O2):
@.str = [11 x i8] c"/dev/resch\00"
@O_RDWR = global i32 0
@.str.2 = [40 x i8] c"Error: failed ioctl: cmd %lu, val: %lu\0A\00"
@str = [36 x i8] c"Error: failed to access the module!\00"
define i32 @__api(i64 %0, i64 %1) {
%3 = alloca i64
store i64 %1, i64* %3
%4 = load i32, i32* @O_RDWR
%5 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str, i64 0, i64 0))
br label %17
9: ; preds = %2
%10 = call i32 @ioctl(i32 %5, i64 %0, i64* %3)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %9
%13 = load i64, i64* %3
%14 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i64 %0, i64 %13)
br label %15
15: ; preds = %12, %9
%16 = call i32 @close(i32 %5)
br label %17
17: ; preds = %15, %7
%18 = phi i32 [ 1, %7 ], [ %10, %15 ]
ret i32 %18
}