#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;
int /*<<< orphan*/ CDSL_CURRENT;
int CDS_DISC_OK;
int O_NONBLOCK;
int O_RDONLY;
int /*<<< orphan*/ close(int);
int ioctl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int);
int disc_is_inserted(char *device) {
  int fd, status;
  fd = open(device, O_RDONLY | O_NONBLOCK);
  if (fd < 0) {
    return 0;
  }
  status = ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
  close(fd);
  if (status == CDS_DISC_OK) {
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<disc_is_inserted>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e91(%rip),%rax
mov    (%rax),%esi
mov    0x2e90(%rip),%rax
or     (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1170 <disc_is_inserted+0x40>
movl   $0x0,-0x4(%rbp)
jmp    11ba <disc_is_inserted+0x8a>
mov    -0x14(%rbp),%edi
mov    0x2e4e(%rip),%rax
mov    (%rax),%esi
mov    0x2e3d(%rip),%rax
mov    (%rax),%edx
call   1030 <ioctl@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%eax
mov    0x2e49(%rip),%rcx
cmp    (%rcx),%eax
jne    11b3 <disc_is_inserted+0x83>
movl   $0x1,-0x4(%rbp)
jmp    11ba <disc_is_inserted+0x8a>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<disc_is_inserted>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e9e(%rip),%rax
mov    0x2e9f(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1050 <open@plt>
test   %eax,%eax
js     1182 <disc_is_inserted+0x52>
mov    %eax,%ebx
mov    0x2e71(%rip),%rax
mov    (%rax),%esi
mov    0x2e60(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
call   1030 <ioctl@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1040 <close@plt>
mov    0x2e6f(%rip),%rcx
xor    %eax,%eax
cmp    (%rcx),%ebp
sete   %al
jmp    1184 <disc_is_inserted+0x54>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@CDROM_DRIVE_STATUS = global i32 0
@CDSL_CURRENT = global i32 0
@CDS_DISC_OK = global i32 0
define i32 @disc_is_inserted(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = load i32, i32* @O_RDONLY
%8 = load i32, i32* @O_NONBLOCK
%9 = or i32 %7, %8
%10 = call i32 @open(i8* %6, i32 %9)
store i32 %10, i32* %4
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %1
store i32 0, i32* %2
br label %26
14: ; preds = %1
%15 = load i32, i32* %4
%16 = load i32, i32* @CDROM_DRIVE_STATUS
%17 = load i32, i32* @CDSL_CURRENT
%18 = call i32 @ioctl(i32 %15, i32 %16, i32 %17)
store i32 %18, i32* %5
%19 = load i32, i32* %4
%20 = call i32 @close(i32 %19)
%21 = load i32, i32* %5
%22 = load i32, i32* @CDS_DISC_OK
%23 = icmp eq i32 %21, %22
br i1 %23, label %24, label %25
24: ; preds = %14
store i32 1, i32* %2
br label %26
25: ; preds = %14
store i32 0, i32* %2
br label %26
26: ; preds = %25, %24, %13
%27 = load i32, i32* %2
ret i32 %27
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@CDROM_DRIVE_STATUS = global i32 0
@CDSL_CURRENT = global i32 0
@CDS_DISC_OK = global i32 0
define i32 @disc_is_inserted(i8* nocapture %0) {
%2 = load i32, i32* @O_RDONLY
%3 = load i32, i32* @O_NONBLOCK
%4 = or i32 %3, %2
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %15, label %7
7: ; preds = %1
%8 = load i32, i32* @CDROM_DRIVE_STATUS
%9 = load i32, i32* @CDSL_CURRENT
%10 = tail call i32 @ioctl(i32 %5, i32 %8, i32 %9)
%11 = tail call i32 @close(i32 %5)
%12 = load i32, i32* @CDS_DISC_OK
%13 = icmp eq i32 %10, %12
%14 = zext i1 %13 to i32
br label %15
15: ; preds = %7, %1
%16 = phi i32 [ 0, %1 ], [ %14, %7 ]
ret i32 %16
}