#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(int, int);
int /*<<< orphan*/ dasd_close_device(int);
int dasd_open_device(char const *, int /*<<< orphan*/);
int errno;
scalar_t__ ioctl(int, int /*<<< orphan*/, unsigned int *);
int dasd_get_blocksize(const char *device, unsigned int *blksize) {
  int fd;
  fd = dasd_open_device(device, O_RDONLY);
  do {
    if (ioctl(fd, _IO(0x12, 104), blksize) != 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_get_blocksize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%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,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    $0x12,%edi
mov    $0x68,%esi
call   1030 <_IO@plt>
mov    -0x24(%rbp),%edi
mov    %eax,%esi
mov    -0x18(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
je     11c3 <dasd_get_blocksize+0x83>
mov    0x2e3f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    0x2e28(%rip),%rcx
cmp    (%rcx),%eax
je     11b8 <dasd_get_blocksize+0x78>
mov    -0x1c(%rbp),%edi
call   1040 <dasd_close_device@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11d7 <dasd_get_blocksize+0x97>
jmp    11c8 <dasd_get_blocksize+0x88>
mov    -0x1c(%rbp),%edi
call   1040 <dasd_close_device@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dasd_get_blocksize>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbp
mov    0x2e9b(%rip),%rax
mov    (%rax),%esi
call   1060 <dasd_open_device@plt>
mov    %eax,%ebx
mov    $0x12,%edi
mov    $0x68,%esi
call   1030 <_IO@plt>
mov    %ebx,%edi
mov    %eax,%esi
mov    %rbp,%rdx
call   1050 <ioctl@plt>
test   %rax,%rax
je     118c <dasd_get_blocksize+0x4c>
mov    0x2e5b(%rip),%rax
mov    (%rax),%ebp
mov    0x2e4a(%rip),%rax
cmp    (%rax),%ebp
jne    118e <dasd_get_blocksize+0x4e>
jmp    1195 <dasd_get_blocksize+0x55>
xor    %ebp,%ebp
mov    %ebx,%edi
call   1040 <dasd_close_device@plt>
mov    %ebp,%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_get_blocksize(i8* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %4
store i32* %1, i32** %5
%8 = load i8*, i8** %4
%9 = load i32, i32* @O_RDONLY
%10 = call i32 @dasd_open_device(i8* %8, i32 %9)
store i32 %10, i32* %6
br label %11
11: ; preds = %2
%12 = load i32, i32* %6
%13 = call i32 @_IO(i32 18, i32 104)
%14 = load i32*, i32** %5
%15 = call i64 @ioctl(i32 %12, i32 %13, i32* %14)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %27
17: ; preds = %11
%18 = load i32, i32* @errno
store i32 %18, i32* %7
%19 = load i32, i32* %7
%20 = load i32, i32* @EBADF
%21 = icmp ne i32 %19, %20
br i1 %21, label %22, label %25
22: ; preds = %17
%23 = load i32, i32* %6
%24 = call i32 @dasd_close_device(i32 %23)
br label %25
25: ; preds = %22, %17
%26 = load i32, i32* %7
store i32 %26, i32* %3
br label %31
27: ; preds = %11
br label %28
28: ; preds = %27
%29 = load i32, i32* %6
%30 = call i32 @dasd_close_device(i32 %29)
store i32 0, i32* %3
br label %31
31: ; preds = %28, %25
%32 = load i32, i32* %3
ret i32 %32
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@errno = global i32 0
@EBADF = global i32 0
define i32 @dasd_get_blocksize(i8* %0, i32* %1) {
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @dasd_open_device(i8* %0, i32 %3)
%5 = tail call i32 @_IO(i32 18, i32 104)
%6 = tail call i64 @ioctl(i32 %4, i32 %5, i32* %1)
%7 = icmp eq i64 %6, 0
br i1 %7, label %12, 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 = %2, %8
%13 = phi i32 [ %9, %8 ], [ 0, %2 ]
%14 = tail call i32 @dasd_close_device(i32 %4)
br label %15
15: ; preds = %12, %8
%16 = phi i32 [ %9, %8 ], [ %13, %12 ]
ret i32 %16
}