#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 */
scalar_t__ 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, 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, (int)(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    11ee <blockDeviceSize+0x9e>
movq   $0x0,-0x20(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2e46(%rip),%rax
mov    (%rax),%rax
mov    %eax,%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    11e6 <blockDeviceSize+0x96>
mov    0x2e06(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe29(%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 i64 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 %29
12: ; preds = %1
store i64 0, i64* %5
%13 = load i32, i32* %4
%14 = load i64, i64* @BLKGETSIZE64
%15 = trunc i64 %14 to i32
%16 = call i32 @ioctl(i32 %13, i32 %15, i64* %5)
%17 = load i32, i32* %4
%18 = call i32 @fsync(i32 %17)
%19 = load i32, i32* %4
%20 = call i32 @close(i32 %19)
%21 = load i64, i64* %5
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %12
%24 = load i32, i32* @stderr
%25 = load i8*, i8** %3
%26 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0), i8* %25, i32 0)
store i64 1, i64* %5
br label %27
27: ; preds = %23, %12
%28 = load i64, i64* %5
store i64 %28, i64* %2
br label %29
29: ; preds = %27, %11
%30 = load i64, i64* %2
ret i64 %30
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@BLKGETSIZE64 = global i64 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 %20, label %6
6: ; preds = %1
%7 = bitcast i64* %2 to i8*
store i64 0, i64* %2
%8 = load i64, i64* @BLKGETSIZE64
%9 = trunc i64 %8 to i32
%10 = call i32 @ioctl(i32 %4, i32 %9, i64* %2)
%11 = call i32 @fsync(i32 %4)
%12 = call i32 @close(i32 %4)
%13 = load i64, i64* %2
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %6
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0), i8* %0, i32 0)
br label %18
18: ; preds = %15, %6
%19 = phi i64 [ 1, %15 ], [ %13, %6 ]
br label %20
20: ; preds = %1, %18
%21 = phi i64 [ %19, %18 ], [ 0, %1 ]
ret i64 %21
}