#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*/ BLKGETSIZE64;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *,
                           int /*<<< orphan*/);
int /*<<< orphan*/ fsync(int);
int /*<<< orphan*/ ioctl(int, int /*<<< orphan*/, size_t *);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
size_t blockDeviceSize(const char *path) {
  int fd = open(path, O_RDONLY);
  if (fd < 0) {
    return 0;
  }
  size_t file_size_in_bytes = 0;
  ioctl(fd, BLKGETSIZE64, &file_size_in_bytes);
  fsync(fd);
  close(fd);
  if (file_size_in_bytes == 0) {
    fprintf(stderr, "*warning*: block device '%s' has a size of %d bytes.\n",
            path, 0);
    file_size_in_bytes = 1;
  }
  return file_size_in_bytes;
}


x86 assembly (clang, -O0):
<blockDeviceSize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e81(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1188 <blockDeviceSize+0x38>
movq   $0x0,-0x8(%rbp)
jmp    11eb <blockDeviceSize+0x9b>
movq   $0x0,-0x20(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2e46(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1030 <ioctl@plt>
mov    -0x14(%rbp),%edi
call   1060 <fsync@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
cmpq   $0x0,-0x20(%rbp)
jne    11e3 <blockDeviceSize+0x93>
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe2c(%rip),%rsi
xor    %ecx,%ecx
call   1050 <fprintf@plt>
movq   $0x1,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<blockDeviceSize>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2e8b(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
test   %eax,%eax
js     11bd <blockDeviceSize+0x6d>
mov    %eax,%ebp
movq   $0x0,(%rsp)
mov    0x2e67(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1030 <ioctl@plt>
mov    %ebp,%edi
call   1060 <fsync@plt>
mov    %ebp,%edi
call   1040 <close@plt>
mov    (%rsp),%rax
test   %rax,%rax
jne    11bf <blockDeviceSize+0x6f>
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
lea    0xe54(%rip),%rsi
mov    %rbx,%rdx
xor    %ecx,%ecx
call   1050 <fprintf@plt>
mov    $0x1,%eax
jmp    11bf <blockDeviceSize+0x6f>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@BLKGETSIZE64 = global i32 0
@stderr = global i32 0
@.str = [54 x i8] c"*warning*: block device '%s' has a size of %d bytes.\0A\00"
define i64 @blockDeviceSize(i8* %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i64
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = load i32, i32* @O_RDONLY
%8 = call i32 @open(i8* %6, i32 %7)
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %1
store i64 0, i64* %2
br label %28
12: ; preds = %1
store i64 0, i64* %5
%13 = load i32, i32* %4
%14 = load i32, i32* @BLKGETSIZE64
%15 = call i32 @ioctl(i32 %13, i32 %14, i64* %5)
%16 = load i32, i32* %4
%17 = call i32 @fsync(i32 %16)
%18 = load i32, i32* %4
%19 = call i32 @close(i32 %18)
%20 = load i64, i64* %5
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %12
%23 = load i32, i32* @stderr
%24 = load i8*, i8** %3
%25 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0), i8* %24, i32 0)
store i64 1, i64* %5
br label %26
26: ; preds = %22, %12
%27 = load i64, i64* %5
store i64 %27, i64* %2
br label %28
28: ; preds = %26, %11
%29 = load i64, i64* %2
ret i64 %29
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@BLKGETSIZE64 = global i32 0
@stderr = global i32 0
@.str = [54 x i8] c"*warning*: block device '%s' has a size of %d bytes.\0A\00"
define i64 @blockDeviceSize(i8* %0) {
%2 = alloca i64
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* %0, i32 %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %19, label %6
6: ; preds = %1
%7 = bitcast i64* %2 to i8*
store i64 0, i64* %2
%8 = load i32, i32* @BLKGETSIZE64
%9 = call i32 @ioctl(i32 %4, i32 %8, i64* %2)
%10 = call i32 @fsync(i32 %4)
%11 = call i32 @close(i32 %4)
%12 = load i64, i64* %2
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %6
%15 = load i32, i32* @stderr
%16 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0), i8* %0, i32 0)
br label %17
17: ; preds = %14, %6
%18 = phi i64 [ 1, %14 ], [ %12, %6 ]
br label %19
19: ; preds = %1, %17
%20 = phi i64 [ %18, %17 ], [ 0, %1 ]
ret i64 %20
}