#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*/ CDROM_DRIVE_STATUS;
scalar_t__ CDS_TRAY_OPEN;
int O_NONBLOCK;
int O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
scalar_t__ ioctl(int, int /*<<< orphan*/);
int open(char *, int);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char **argv) {
  int cdrom;
  int status = 1;
  if (!argv[1]) {
    printf("Usage : trayopen [device]\n");
    printf("Result: Returns a 0 if the tray was open and 1 if it was closed\n");
    exit(2);
  }
  if ((cdrom = open(argv[1], O_RDONLY | O_NONBLOCK)) < 0) {
    printf("Unable to open device %s. Provide a device name (/dev/sr0, "
           "/dev/cdrom) as a parameter.\n",
           argv[1]);
    exit(2);
  }
  if (ioctl(cdrom, CDROM_DRIVE_STATUS) == CDS_TRAY_OPEN) {
    status = 0;
  }
  close(cdrom);
  exit(status);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1,-0x18(%rbp)
mov    -0x10(%rbp),%rax
cmpq   $0x0,0x8(%rax)
jne    11a2 <main+0x52>
lea    0xe7d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe8a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x2,%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e2f(%rip),%rax
mov    (%rax),%esi
mov    0x2e2e(%rip),%rax
or     (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11ed <main+0x9d>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe80(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x2,%edi
call   1070 <exit@plt>
mov    -0x14(%rbp),%edi
mov    0x2dd9(%rip),%rax
mov    (%rax),%esi
call   1040 <ioctl@plt>
mov    0x2dc3(%rip),%rcx
cmp    (%rcx),%rax
jne    1215 <main+0xc5>
movl   $0x0,-0x18(%rbp)
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    0x8(%rsi),%rdi
test   %rdi,%rdi
jne    118e <main+0x2e>
lea    0xee5(%rip),%rdi
call   1030 <puts@plt>
lea    0xef3(%rip),%rdi
call   1030 <puts@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
mov    %rsi,%rbx
mov    0x2e48(%rip),%rax
mov    0x2e49(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1070 <open@plt>
test   %eax,%eax
jns    11c8 <main+0x68>
mov    0x8(%rbx),%rsi
lea    0xe49(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
mov    %eax,%ebp
mov    0x2dff(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1050 <ioctl@plt>
mov    0x2de7(%rip),%rcx
xor    %ebx,%ebx
cmp    (%rcx),%rax
setne  %bl
mov    %ebp,%edi
call   1060 <close@plt>
mov    %ebx,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [27 x i8] c"Usage : trayopen [device]\0A\00"
@.str.1 = [65 x i8] c"Result: Returns a 0 if the tray was open and 1 if it was closed\0A\00"
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@.str.2 = [88 x i8] c"Unable to open device %s. Provide a device name (/dev/sr0, /dev/cdrom) as a parameter.\0A\00"
@CDROM_DRIVE_STATUS = global i32 0
@CDS_TRAY_OPEN = global i64 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 1, i32* %7
%8 = load i8**, i8*** %5
%9 = getelementptr inbounds i8*, i8** %8, i64 1
%10 = load i8*, i8** %9
%11 = icmp ne i8* %10, null
br i1 %11, label %16, label %12
12: ; preds = %2
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.1, i64 0, i64 0))
%15 = call i32 @exit(i32 2)
unreachable
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = load i32, i32* @O_RDONLY
%21 = load i32, i32* @O_NONBLOCK
%22 = or i32 %20, %21
%23 = call i32 @open(i8* %19, i32 %22)
store i32 %23, i32* %6
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %31
25: ; preds = %16
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([88 x i8], [88 x i8]* @.str.2, i64 0, i64 0), i8* %28)
%30 = call i32 @exit(i32 2)
unreachable
31: ; preds = %16
%32 = load i32, i32* %6
%33 = load i32, i32* @CDROM_DRIVE_STATUS
%34 = call i64 @ioctl(i32 %32, i32 %33)
%35 = load i64, i64* @CDS_TRAY_OPEN
%36 = icmp eq i64 %34, %35
br i1 %36, label %37, label %38
37: ; preds = %31
store i32 0, i32* %7
br label %38
38: ; preds = %37, %31
%39 = load i32, i32* %6
%40 = call i32 @close(i32 %39)
%41 = load i32, i32* %7
%42 = call i32 @exit(i32 %41)
unreachable
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@.str.2 = [88 x i8] c"Unable to open device %s. Provide a device name (/dev/sr0, /dev/cdrom) as a parameter.\0A\00"
@CDROM_DRIVE_STATUS = global i32 0
@CDS_TRAY_OPEN = global i64 0
@str = [26 x i8] c"Usage : trayopen [device]\00"
@str.3 = [64 x i8] c"Result: Returns a 0 if the tray was open and 1 if it was closed\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = getelementptr inbounds i8*, i8** %1, i64 1
%4 = load i8*, i8** %3
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %10
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([64 x i8], [64 x i8]* @str.3, i64 0, i64 0))
%9 = tail call i32 @exit(i32 2)
unreachable
10: ; preds = %2
%11 = load i32, i32* @O_RDONLY
%12 = load i32, i32* @O_NONBLOCK
%13 = or i32 %12, %11
%14 = tail call i32 @open(i8* %4, i32 %13)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %10
%17 = load i8*, i8** %3
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([88 x i8], [88 x i8]* @.str.2, i64 0, i64 0), i8* %17)
%19 = tail call i32 @exit(i32 2)
unreachable
20: ; preds = %10
%21 = load i32, i32* @CDROM_DRIVE_STATUS
%22 = tail call i64 @ioctl(i32 %14, i32 %21)
%23 = load i64, i64* @CDS_TRAY_OPEN
%24 = icmp ne i64 %22, %23
%25 = zext i1 %24 to i32
%26 = tail call i32 @close(i32 %14)
%27 = tail call i32 @exit(i32 %25)
unreachable
}