#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*/ _IO(char, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ ioctl(int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int main(void) {
  int fd;
  fd = open("/dev/hello", O_RDWR);
  if (fd < 0) {
    printf("fd open failed\n");
    exit(0);
  }
  printf("\n/dev/hello opened, fd=%d\n", fd);
  ioctl(fd, _IO('k', 1));
  ioctl(fd, _IO('k', 2));
  close(fd);
  printf("/dev/hello closed :)\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e72(%rip),%rax
mov    (%rax),%esi
lea    0xe81(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11a6 <main+0x46>
lea    0xe73(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>
mov    -0x8(%rbp),%esi
lea    0xe6b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    $0x6b,%edi
mov    $0x1,%esi
call   1040 <_IO@plt>
mov    -0x10(%rbp),%edi
mov    %eax,%esi
call   1050 <ioctl@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    $0x6b,%edi
mov    $0x2,%esi
call   1040 <_IO@plt>
mov    -0xc(%rbp),%edi
mov    %eax,%esi
call   1050 <ioctl@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
lea    0xe32(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x2e70(%rip),%rax
mov    (%rax),%esi
lea    0xe7f(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
js     11e3 <main+0x73>
mov    %eax,%ebx
lea    0xe78(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x6b,%edi
mov    $0x1,%esi
call   1050 <_IO@plt>
mov    %ebx,%edi
mov    %eax,%esi
call   1060 <ioctl@plt>
mov    $0x6b,%edi
mov    $0x2,%esi
call   1050 <_IO@plt>
mov    %ebx,%edi
mov    %eax,%esi
call   1060 <ioctl@plt>
mov    %ebx,%edi
call   1070 <close@plt>
lea    0xe4c(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
pop    %rbx
ret
lea    0xe51(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"/dev/hello\00"
@O_RDWR = global i32 0
@.str.1 = [16 x i8] c"fd open failed\0A\00"
@.str.2 = [27 x i8] c"\0A/dev/hello opened, fd=%d\0A\00"
@.str.3 = [22 x i8] c"/dev/hello closed :)\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
store i32 0, i32* %1
%3 = load i32, i32* @O_RDWR
%4 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %3)
store i32 %4, i32* %2
%5 = load i32, i32* %2
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%9 = call i32 @exit(i32 0)
unreachable
10: ; preds = %0
%11 = load i32, i32* %2
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 %11)
%13 = load i32, i32* %2
%14 = call i32 @_IO(i8 signext 107, i32 1)
%15 = call i32 @ioctl(i32 %13, i32 %14)
%16 = load i32, i32* %2
%17 = call i32 @_IO(i8 signext 107, i32 2)
%18 = call i32 @ioctl(i32 %16, i32 %17)
%19 = load i32, i32* %2
%20 = call i32 @close(i32 %19)
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str = [11 x i8] c"/dev/hello\00"
@O_RDWR = global i32 0
@.str.2 = [27 x i8] c"\0A/dev/hello opened, fd=%d\0A\00"
@str = [21 x i8] c"/dev/hello closed :)\00"
@str.4 = [15 x i8] c"fd open failed\00"
define i32 @main() {
%1 = load i32, i32* @O_RDWR
%2 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = icmp slt i32 %2, 0
br i1 %3, label %4, label %7
4: ; preds = %0
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.4, i64 0, i64 0))
%6 = tail call i32 @exit(i32 0)
unreachable
7: ; preds = %0
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 %2)
%9 = tail call i32 @_IO(i8 signext 107, i32 1)
%10 = tail call i32 @ioctl(i32 %2, i32 %9)
%11 = tail call i32 @_IO(i8 signext 107, i32 2)
%12 = tail call i32 @ioctl(i32 %2, i32 %11)
%13 = tail call i32 @close(i32 %2)
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
ret i32 0
}