#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 O_NDELAY;
int O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int ioctl(int, int, int);
int open(char *, int);
int /*<<< orphan*/ printf(char *, int, int);
int /*<<< orphan*/ stderr;
int ioctl_chr(int cmd, int arg) {
  int fd = open("/dev/char_dev", O_RDONLY | O_NDELAY);
  if (fd < 0) {
    fprintf(stderr, "Ftiled to open device %s!\n", "/dev/char_dev");
    return -1;
  }
  if (ioctl(fd, cmd, arg) == -1) {
    fprintf(stderr, "Failed to ioctl device %s!\n", "/dev/char_dev");
    return -1;
  }
  printf("ioctl ( %d, %d)\n", cmd, arg);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<ioctl_chr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0x2e83(%rip),%rax
mov    (%rax),%esi
mov    0x2e62(%rip),%rax
or     (%rax),%esi
lea    0xe89(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    11b1 <ioctl_chr+0x61>
mov    0x2e48(%rip),%rax
mov    (%rax),%edi
lea    0xe75(%rip),%rsi
lea    0xe60(%rip),%rdx
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1211 <ioctl_chr+0xc1>
mov    -0x10(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    -0xc(%rbp),%edx
call   1040 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    11f0 <ioctl_chr+0xa0>
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
lea    0xe51(%rip),%rsi
lea    0xe21(%rip),%rdx
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1211 <ioctl_chr+0xc1>
mov    -0x8(%rbp),%esi
mov    -0xc(%rbp),%edx
lea    0xe48(%rip),%rdi
call   1030 <printf@plt>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ioctl_chr>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%r14d
mov    %edi,%ebp
mov    0x2e88(%rip),%rax
mov    0x2e69(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe8e(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     11aa <ioctl_chr+0x5a>
mov    %eax,%ebx
mov    %eax,%edi
mov    %ebp,%esi
mov    %r14d,%edx
call   1040 <ioctl@plt>
cmp    $0xffffffff,%eax
je     11bc <ioctl_chr+0x6c>
lea    0xeb0(%rip),%rdi
mov    %ebp,%esi
mov    %r14d,%edx
call   1030 <printf@plt>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %eax,%eax
jmp    11dd <ioctl_chr+0x8d>
mov    0x2e27(%rip),%rax
mov    (%rax),%edi
lea    0xe54(%rip),%rsi
jmp    11cc <ioctl_chr+0x7c>
mov    0x2e15(%rip),%rax
mov    (%rax),%edi
lea    0xe5d(%rip),%rsi
lea    0xe2d(%rip),%rdx
call   1060 <fprintf@plt>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"/dev/char_dev\00"
@O_RDONLY = global i32 0
@O_NDELAY = global i32 0
@stderr = global i32 0
@.str.1 = [27 x i8] c"Ftiled to open device %s!\0A\00"
@.str.2 = [28 x i8] c"Failed to ioctl device %s!\0A\00"
@.str.3 = [17 x i8] c"ioctl ( %d, %d)\0A\00"
define i32 @ioctl_chr(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%7 = load i32, i32* @O_RDONLY
%8 = load i32, i32* @O_NDELAY
%9 = or i32 %7, %8
%10 = call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %9)
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %31
16: ; preds = %2
%17 = load i32, i32* %6
%18 = load i32, i32* %4
%19 = load i32, i32* %5
%20 = call i32 @ioctl(i32 %17, i32 %18, i32 %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %25
22: ; preds = %16
%23 = load i32, i32* @stderr
%24 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %31
25: ; preds = %16
%26 = load i32, i32* %4
%27 = load i32, i32* %5
%28 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %26, i32 %27)
%29 = load i32, i32* %6
%30 = call i32 @close(i32 %29)
store i32 0, i32* %3
br label %31
31: ; preds = %25, %22, %13
%32 = load i32, i32* %3
ret i32 %32
}

LLVM IR (-O2):
@.str = [14 x i8] c"/dev/char_dev\00"
@O_RDONLY = global i32 0
@O_NDELAY = global i32 0
@stderr = global i32 0
@.str.1 = [27 x i8] c"Ftiled to open device %s!\0A\00"
@.str.2 = [28 x i8] c"Failed to ioctl device %s!\0A\00"
@.str.3 = [17 x i8] c"ioctl ( %d, %d)\0A\00"
define i32 @ioctl_chr(i32 %0, i32 %1) {
%3 = load i32, i32* @O_RDONLY
%4 = load i32, i32* @O_NDELAY
%5 = or i32 %4, %3
%6 = tail call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %20
11: ; preds = %2
%12 = tail call i32 @ioctl(i32 %6, i32 %0, i32 %1)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = load i32, i32* @stderr
%16 = tail call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %20
17: ; preds = %11
%18 = tail call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %0, i32 %1)
%19 = tail call i32 @close(i32 %6)
br label %20
20: ; preds = %17, %14, %8
%21 = phi i32 [ -1, %8 ], [ -1, %14 ], [ 0, %17 ]
ret i32 %21
}