#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 EBADF;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ _IO(char, int);
int /*<<< orphan*/ dasd_close_device(int);
int dasd_open_device(char const *, int /*<<< orphan*/);
int errno;
scalar_t__ ioctl(int, int /*<<< orphan*/, int /*<<< orphan*/ *);
int dasd_disk_reserve(const char *device) {
  int fd;
  fd = dasd_open_device(device, O_RDONLY);
  do {
    if (ioctl(fd, _IO('D', 2), NULL) != 0) {
      int err = errno;
      if (err != EBADF)
        dasd_close_device(fd);
      return err;
    }
  } while (0);
  dasd_close_device(fd);
  return 0;
}


x86 assembly (clang, -O0):
<dasd_disk_reserve>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e91(%rip),%rax
mov    (%rax),%esi
call   1060 <dasd_open_device@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    $0x44,%edi
mov    $0x2,%esi
call   1030 <_IO@plt>
mov    -0x1c(%rbp),%edi
mov    %eax,%esi
xor    %eax,%eax
mov    %eax,%edx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
je     11bf <dasd_disk_reserve+0x7f>
mov    0x2e43(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    0x2e2c(%rip),%rcx
cmp    (%rcx),%eax
je     11b4 <dasd_disk_reserve+0x74>
mov    -0x14(%rbp),%edi
call   1040 <dasd_close_device@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11d3 <dasd_disk_reserve+0x93>
jmp    11c4 <dasd_disk_reserve+0x84>
mov    -0x14(%rbp),%edi
call   1040 <dasd_close_device@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dasd_disk_reserve>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e9e(%rip),%rax
mov    (%rax),%esi
call   1060 <dasd_open_device@plt>
mov    %eax,%ebp
mov    $0x44,%edi
mov    $0x2,%esi
call   1030 <_IO@plt>
xor    %ebx,%ebx
mov    %ebp,%edi
mov    %eax,%esi
xor    %edx,%edx
call   1050 <ioctl@plt>
test   %rax,%rax
je     1188 <dasd_disk_reserve+0x48>
mov    0x2e5d(%rip),%rax
mov    (%rax),%ebx
mov    0x2e4c(%rip),%rax
cmp    (%rax),%ebx
je     118f <dasd_disk_reserve+0x4f>
mov    %ebp,%edi
call   1040 <dasd_close_device@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@errno = global i32 0
@EBADF = global i32 0
define i32 @dasd_disk_reserve(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 = call i32 @dasd_open_device(i8* %6, i32 %7)
store i32 %8, i32* %4
br label %9
9: ; preds = %1
%10 = load i32, i32* %4
%11 = call i32 @_IO(i8 signext 68, i32 2)
%12 = call i64 @ioctl(i32 %10, i32 %11, i32* null)
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %24
14: ; preds = %9
%15 = load i32, i32* @errno
store i32 %15, i32* %5
%16 = load i32, i32* %5
%17 = load i32, i32* @EBADF
%18 = icmp ne i32 %16, %17
br i1 %18, label %19, label %22
19: ; preds = %14
%20 = load i32, i32* %4
%21 = call i32 @dasd_close_device(i32 %20)
br label %22
22: ; preds = %19, %14
%23 = load i32, i32* %5
store i32 %23, i32* %2
br label %28
24: ; preds = %9
br label %25
25: ; preds = %24
%26 = load i32, i32* %4
%27 = call i32 @dasd_close_device(i32 %26)
store i32 0, i32* %2
br label %28
28: ; preds = %25, %22
%29 = load i32, i32* %2
ret i32 %29
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@errno = global i32 0
@EBADF = global i32 0
define i32 @dasd_disk_reserve(i8* %0) {
%2 = load i32, i32* @O_RDONLY
%3 = tail call i32 @dasd_open_device(i8* %0, i32 %2)
%4 = tail call i32 @_IO(i8 signext 68, i32 2)
%5 = tail call i64 @ioctl(i32 %3, i32 %4, i32* null)
%6 = icmp eq i64 %5, 0
br i1 %6, label %11, label %7
7: ; preds = %1
%8 = load i32, i32* @errno
%9 = load i32, i32* @EBADF
%10 = icmp eq i32 %8, %9
br i1 %10, label %14, label %11
11: ; preds = %1, %7
%12 = phi i32 [ %8, %7 ], [ 0, %1 ]
%13 = tail call i32 @dasd_close_device(i32 %3)
br label %14
14: ; preds = %11, %7
%15 = phi i32 [ %8, %7 ], [ %12, %11 ]
ret i32 %15
}