#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 */
typedef int uint64_t;

/* Variables and functions */
int /*<<< orphan*/ BLKGETSIZE64;
int EINVAL;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int ioctl(int, int /*<<< orphan*/, int *);
int open(char const *, int /*<<< orphan*/);
int get_dev_size(const char *path, uint64_t *sectors) {
  int r, fd;
  fd = open(path, O_RDONLY);
  if (fd < 0)
    return -EINVAL;
  r = ioctl(fd, BLKGETSIZE64, sectors);
  (*sectors) /= 512;
  close(fd);
  return r;
}


x86 assembly (clang, -O0):
<get_dev_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e9d(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1172 <get_dev_size+0x42>
xor    %eax,%eax
mov    0x2e68(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11b2 <get_dev_size+0x82>
mov    -0x20(%rbp),%edi
mov    0x2e64(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%rdx
call   1030 <ioctl@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    (%rax),%eax
mov    $0x200,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_dev_size>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    0x2eaa(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
test   %eax,%eax
js     117f <get_dev_size+0x4f>
mov    %eax,%ebp
mov    0x2e8e(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %rbx,%rdx
call   1030 <ioctl@plt>
mov    %eax,%r14d
mov    (%rbx),%eax
lea    0x1ff(%rax),%ecx
test   %eax,%eax
cmovns %eax,%ecx
sar    $0x9,%ecx
mov    %ecx,(%rbx)
mov    %ebp,%edi
call   1040 <close@plt>
mov    %r14d,%eax
jmp    118a <get_dev_size+0x5a>
mov    0x2e4a(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@EINVAL = global i32 0
@BLKGETSIZE64 = global i32 0
define i32 @get_dev_size(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 @open(i8* %8, i32 %9)
store i32 %10, i32* %7
%11 = load i32, i32* %7
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = load i32, i32* @EINVAL
%15 = sub nsw i32 0, %14
store i32 %15, i32* %3
br label %27
16: ; preds = %2
%17 = load i32, i32* %7
%18 = load i32, i32* @BLKGETSIZE64
%19 = load i32*, i32** %5
%20 = call i32 @ioctl(i32 %17, i32 %18, i32* %19)
store i32 %20, i32* %6
%21 = load i32*, i32** %5
%22 = load i32, i32* %21
%23 = sdiv i32 %22, 512
store i32 %23, i32* %21
%24 = load i32, i32* %7
%25 = call i32 @close(i32 %24)
%26 = load i32, i32* %6
store i32 %26, i32* %3
br label %27
27: ; preds = %16, %13
%28 = load i32, i32* %3
ret i32 %28
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@EINVAL = global i32 0
@BLKGETSIZE64 = global i32 0
define i32 @get_dev_size(i8* nocapture %0, i32* %1) {
%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 %6, label %9
6: ; preds = %2
%7 = load i32, i32* @EINVAL
%8 = sub nsw i32 0, %7
br label %15
9: ; preds = %2
%10 = load i32, i32* @BLKGETSIZE64
%11 = tail call i32 @ioctl(i32 %4, i32 %10, i32* %1)
%12 = load i32, i32* %1
%13 = sdiv i32 %12, 512
store i32 %13, i32* %1
%14 = tail call i32 @close(i32 %4)
br label %15
15: ; preds = %9, %6
%16 = phi i32 [ %8, %6 ], [ %11, %9 ]
ret i32 %16
}