#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*/ CDROMEJECT;
scalar_t__ ENOTTY;
int /*<<< orphan*/ FDEJECT;
int /*<<< orphan*/ FDRESET;
int /*<<< orphan*/ FD_RESET_IF_NEEDED;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
scalar_t__ ioctl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ sync();
int /*<<< orphan*/ wait_for_key();
int change_disk(const char *devpath, int rfd, int disk) {
  sync();
  if (ioctl(rfd, FDEJECT, 0)) {
    if (errno == ENOTTY) {
      ioctl(rfd, CDROMEJECT, 0);
    } else {
      ioctl(rfd, FDRESET, FD_RESET_IF_NEEDED);
    }
  }
  close(rfd);
  fprintf(stderr, "\nPlease insert disk %d for ramdisk and press Enter...",
          disk);
  wait_for_key();
  return open(devpath, O_RDONLY);
}


x86 assembly (clang, -O0):
<change_disk>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    $0x0,%al
call   1070 <sync@plt>
mov    -0xc(%rbp),%edi
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
je     11e7 <change_disk+0x87>
mov    0x2e13(%rip),%rax
mov    (%rax),%rax
mov    0x2e41(%rip),%rcx
cmp    (%rcx),%rax
jne    11c8 <change_disk+0x68>
mov    -0xc(%rbp),%edi
mov    0x2dfe(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <ioctl@plt>
jmp    11e2 <change_disk+0x82>
mov    -0xc(%rbp),%edi
mov    0x2df6(%rip),%rax
mov    (%rax),%esi
mov    0x2dcd(%rip),%rax
mov    (%rax),%edx
call   1030 <ioctl@plt>
jmp    11e7 <change_disk+0x87>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
mov    0x2dca(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%edx
lea    0xdfe(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x0,%al
call   1060 <wait_for_key@plt>
mov    -0x8(%rbp),%rdi
mov    0x2dbf(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<change_disk>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %esi,%ebx
mov    %rdi,%r14
xor    %ebp,%ebp
xor    %eax,%eax
call   1070 <sync@plt>
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1030 <ioctl@plt>
test   %rax,%rax
je     11c7 <change_disk+0x67>
mov    0x2e1a(%rip),%rax
mov    (%rax),%rax
mov    0x2e48(%rip),%rcx
cmp    (%rcx),%rax
mov    0x2e1e(%rip),%rax
mov    0x2df7(%rip),%rcx
cmovne (%rcx),%ebp
cmove  0x2dfc(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %ebp,%edx
call   1030 <ioctl@plt>
mov    %ebx,%edi
call   1040 <close@plt>
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
lea    0xe22(%rip),%rsi
mov    %r15d,%edx
call   1050 <fprintf@plt>
xor    %eax,%eax
call   1060 <wait_for_key@plt>
mov    0x2de4(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1080 <open@plt>

LLVM IR (-O0):
@FDEJECT = global i32 0
@errno = global i64 0
@ENOTTY = global i64 0
@CDROMEJECT = global i32 0
@FDRESET = global i32 0
@FD_RESET_IF_NEEDED = global i32 0
@stderr = global i32 0
@.str = [54 x i8] c"\0APlease insert disk %d for ramdisk and press Enter...\00"
@O_RDONLY = global i32 0
define i32 @change_disk(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%7 = call i32 (...) @sync()
%8 = load i32, i32* %5
%9 = load i32, i32* @FDEJECT
%10 = call i64 @ioctl(i32 %8, i32 %9, i32 0)
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %26
12: ; preds = %3
%13 = load i64, i64* @errno
%14 = load i64, i64* @ENOTTY
%15 = icmp eq i64 %13, %14
br i1 %15, label %16, label %20
16: ; preds = %12
%17 = load i32, i32* %5
%18 = load i32, i32* @CDROMEJECT
%19 = call i64 @ioctl(i32 %17, i32 %18, i32 0)
br label %25
20: ; preds = %12
%21 = load i32, i32* %5
%22 = load i32, i32* @FDRESET
%23 = load i32, i32* @FD_RESET_IF_NEEDED
%24 = call i64 @ioctl(i32 %21, i32 %22, i32 %23)
br label %25
25: ; preds = %20, %16
br label %26
26: ; preds = %25, %3
%27 = load i32, i32* %5
%28 = call i32 @close(i32 %27)
%29 = load i32, i32* @stderr
%30 = load i32, i32* %6
%31 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0), i32 %30)
%32 = call i32 (...) @wait_for_key()
%33 = load i8*, i8** %4
%34 = load i32, i32* @O_RDONLY
%35 = call i32 @open(i8* %33, i32 %34)
ret i32 %35
}

LLVM IR (-O2):
@FDEJECT = global i32 0
@errno = global i64 0
@ENOTTY = global i64 0
@CDROMEJECT = global i32 0
@FDRESET = global i32 0
@FD_RESET_IF_NEEDED = global i32 0
@stderr = global i32 0
@.str = [54 x i8] c"\0APlease insert disk %d for ramdisk and press Enter...\00"
@O_RDONLY = global i32 0
define i32 @change_disk(i8* nocapture %0, i32 %1, i32 %2) {
%4 = tail call i32 (...) @sync()
%5 = load i32, i32* @FDEJECT
%6 = tail call i64 @ioctl(i32 %1, i32 %5, i32 0)
%7 = icmp eq i64 %6, 0
br i1 %7, label %18, label %8
8: ; preds = %3
%9 = load i64, i64* @errno
%10 = load i64, i64* @ENOTTY
%11 = icmp eq i64 %9, %10
%12 = load i32, i32* @CDROMEJECT
%13 = load i32, i32* @FDRESET
%14 = load i32, i32* @FD_RESET_IF_NEEDED
%15 = select i1 %11, i32 0, i32 %14
%16 = select i1 %11, i32 %12, i32 %13
%17 = tail call i64 @ioctl(i32 %1, i32 %16, i32 %15)
br label %18
18: ; preds = %8, %3
%19 = tail call i32 @close(i32 %1)
%20 = load i32, i32* @stderr
%21 = tail call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0), i32 %2)
%22 = tail call i32 (...) @wait_for_key()
%23 = load i32, i32* @O_RDONLY
%24 = tail call i32 @open(i8* %0, i32 %23)
ret i32 %24
}