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


x86 assembly (clang, -O0):
<dasd_disk_disable>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e8d(%rip),%rax
mov    (%rax),%esi
call   1060 <dasd_open_device@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    $0x44,%edi
xor    %esi,%esi
call   1030 <_IO@plt>
mov    -0x20(%rbp),%edi
mov    %eax,%esi
xor    %eax,%eax
mov    %eax,%edx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
je     11cb <dasd_disk_disable+0x8b>
mov    0x2e3a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    0x2e23(%rip),%rcx
cmp    (%rcx),%eax
je     11c0 <dasd_disk_disable+0x80>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1040 <dasd_close_device@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11d7 <dasd_disk_disable+0x97>
jmp    11d0 <dasd_disk_disable+0x90>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dasd_disk_disable>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
mov    0x2e9a(%rip),%rax
mov    (%rax),%esi
call   1060 <dasd_open_device@plt>
mov    %eax,%ebp
mov    %eax,(%r14)
xor    %ebx,%ebx
mov    $0x44,%edi
xor    %esi,%esi
call   1030 <_IO@plt>
mov    %ebp,%edi
mov    %eax,%esi
xor    %edx,%edx
call   1050 <ioctl@plt>
test   %rax,%rax
je     1194 <dasd_disk_disable+0x54>
mov    0x2e59(%rip),%rax
mov    (%rax),%ebx
mov    0x2e48(%rip),%rax
cmp    (%rax),%ebx
je     1194 <dasd_disk_disable+0x54>
mov    (%r14),%edi
call   1040 <dasd_close_device@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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