#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_DRIVE_NOT_READY;
scalar_t__ CDS_TRAY_OPEN;
int O_NONBLOCK;
int O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
scalar_t__ ioctl(int, int /*<<< orphan*/);
int open(char *, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stdout;
int main(int argc, char **argv) {
  int cdrom;
  int status = 1;
  int sleeptime = 0;
  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;
  }
  while (ioctl(cdrom, CDROM_DRIVE_STATUS) == CDS_DRIVE_NOT_READY &&
         sleeptime < 15) {
    printf("\rWaiting for the drive to become ready...");
    fflush(stdout);
    sleep(1);
  }
  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)
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
cmpq   $0x0,0x8(%rax)
jne    11c9 <main+0x59>
lea    0xe56(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe63(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e08(%rip),%rax
mov    (%rax),%esi
mov    0x2e07(%rip),%rax
or     (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1214 <main+0xa4>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe59(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
mov    -0x14(%rbp),%edi
mov    0x2db2(%rip),%rax
mov    (%rax),%esi
call   1040 <ioctl@plt>
mov    0x2d8c(%rip),%rcx
cmp    (%rcx),%rax
jne    123c <main+0xcc>
movl   $0x0,-0x18(%rbp)
jmp    1241 <main+0xd1>
mov    -0x14(%rbp),%edi
mov    0x2d85(%rip),%rax
mov    (%rax),%esi
call   1040 <ioctl@plt>
mov    %rax,%rcx
xor    %eax,%eax
mov    0x2d6a(%rip),%rdx
cmp    (%rdx),%rcx
mov    %al,-0x1d(%rbp)
jne    1274 <main+0x104>
cmpl   $0xf,-0x1c(%rbp)
setl   %al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    1284 <main+0x114>
jmp    12af <main+0x13f>
lea    0xe29(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2d27(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
jmp    1241 <main+0xd1>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x8(%rsi),%rdi
test   %rdi,%rdi
jne    11b6 <main+0x36>
lea    0xee7(%rip),%rdi
call   1030 <puts@plt>
lea    0xef5(%rip),%rdi
call   1030 <puts@plt>
mov    $0x2,%edi
call   1090 <exit@plt>
mov    %rsi,%r14
mov    0x2e20(%rip),%rax
mov    0x2e21(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1080 <open@plt>
test   %eax,%eax
jns    11f0 <main+0x70>
mov    0x8(%r14),%rsi
lea    0xe21(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x2,%edi
call   1090 <exit@plt>
mov    %eax,%ebx
mov    0x2dd7(%rip),%r13
mov    0x0(%r13),%esi
mov    %eax,%edi
call   1050 <ioctl@plt>
mov    %rax,(%rsp)
mov    0x2da9(%rip),%rax
mov    (%rax),%r12
mov    0x0(%r13),%esi
mov    %ebx,%edi
call   1050 <ioctl@plt>
mov    0x2da4(%rip),%rbp
cmp    0x0(%rbp),%rax
jne    126d <main+0xed>
lea    0xe27(%rip),%r15
mov    0x2d88(%rip),%r14
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    (%r14),%edi
call   1070 <fflush@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    0x0(%r13),%esi
mov    %ebx,%edi
call   1050 <ioctl@plt>
cmp    0x0(%rbp),%rax
je     1240 <main+0xc0>
xor    %ebp,%ebp
cmp    %r12,(%rsp)
setne  %bpl
mov    %ebx,%edi
call   1060 <close@plt>
mov    %ebp,%edi
call   1090 <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
@CDS_DRIVE_NOT_READY = global i64 0
@.str.3 = [42 x i8] c"\0DWaiting for the drive to become ready...\00"
@stdout = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 1, i32* %7
store i32 0, i32* %8
%9 = load i8**, i8*** %5
%10 = getelementptr inbounds i8*, i8** %9, i64 1
%11 = load i8*, i8** %10
%12 = icmp ne i8* %11, null
br i1 %12, label %17, label %13
13: ; preds = %2
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.1, i64 0, i64 0))
%16 = call i32 @exit(i32 2)
unreachable
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = load i32, i32* @O_RDONLY
%22 = load i32, i32* @O_NONBLOCK
%23 = or i32 %21, %22
%24 = call i32 @open(i8* %20, i32 %23)
store i32 %24, i32* %6
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %32
26: ; preds = %17
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 1
%29 = load i8*, i8** %28
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([88 x i8], [88 x i8]* @.str.2, i64 0, i64 0), i8* %29)
%31 = call i32 @exit(i32 2)
unreachable
32: ; preds = %17
%33 = load i32, i32* %6
%34 = load i32, i32* @CDROM_DRIVE_STATUS
%35 = call i64 @ioctl(i32 %33, i32 %34)
%36 = load i64, i64* @CDS_TRAY_OPEN
%37 = icmp eq i64 %35, %36
br i1 %37, label %38, label %39
38: ; preds = %32
store i32 0, i32* %7
br label %39
39: ; preds = %38, %32
br label %40
40: ; preds = %51, %39
%41 = load i32, i32* %6
%42 = load i32, i32* @CDROM_DRIVE_STATUS
%43 = call i64 @ioctl(i32 %41, i32 %42)
%44 = load i64, i64* @CDS_DRIVE_NOT_READY
%45 = icmp eq i64 %43, %44
br i1 %45, label %46, label %49
46: ; preds = %40
%47 = load i32, i32* %8
%48 = icmp slt i32 %47, 15
br label %49
49: ; preds = %46, %40
%50 = phi i1 [ false, %40 ], [ %48, %46 ]
br i1 %50, label %51, label %56
51: ; preds = %49
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0))
%53 = load i32, i32* @stdout
%54 = call i32 @fflush(i32 %53)
%55 = call i32 @sleep(i32 1)
br label %40
56: ; preds = %49
%57 = load i32, i32* %6
%58 = call i32 @close(i32 %57)
%59 = load i32, i32* %7
%60 = call i32 @exit(i32 %59)
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
@CDS_DRIVE_NOT_READY = global i64 0
@.str.3 = [42 x i8] c"\0DWaiting for the drive to become ready...\00"
@stdout = global i32 0
@str = [26 x i8] c"Usage : trayopen [device]\00"
@str.4 = [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.4, 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 = load i32, i32* @CDROM_DRIVE_STATUS
%25 = tail call i64 @ioctl(i32 %14, i32 %24)
%26 = load i64, i64* @CDS_DRIVE_NOT_READY
%27 = icmp eq i64 %25, %26
br i1 %27, label %28, label %37
28: ; preds = %20, %28
%29 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0))
%30 = load i32, i32* @stdout
%31 = tail call i32 @fflush(i32 %30)
%32 = tail call i32 @sleep(i32 1)
%33 = load i32, i32* @CDROM_DRIVE_STATUS
%34 = tail call i64 @ioctl(i32 %14, i32 %33)
%35 = load i64, i64* @CDS_DRIVE_NOT_READY
%36 = icmp eq i64 %34, %35
br i1 %36, label %28, label %37
37: ; preds = %28, %20
%38 = icmp ne i64 %22, %23
%39 = zext i1 %38 to i32
%40 = tail call i32 @close(i32 %14)
%41 = tail call i32 @exit(i32 %39)
unreachable
}