#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;
typedef int uint32_t;
struct stat {
  int st_size;
  int /*<<< orphan*/ st_mode;
};

/* Variables and functions */
int /*<<< orphan*/ BLKGETSIZE;
int /*<<< orphan*/ BLKGETSIZE64;
int EINVAL;
int /*<<< orphan*/ LOG_INFO;
scalar_t__ S_ISBLK(int /*<<< orphan*/);
scalar_t__ fstat(int, struct stat *);
scalar_t__ ioctl(int, int /*<<< orphan*/, int *);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *);
int tapdisk_get_image_size(int fd, uint64_t *_sectors, uint32_t *_sector_size) {
  struct stat stat;
  uint64_t sectors, bytes;
  uint32_t sector_size;
  sectors = 0;
  sector_size = 0;
  *_sectors = 0;
  *_sector_size = 0;
  if (fstat(fd, &stat)) {
    syslog(LOG_INFO, "ERROR: fstat failed, Couldn't stat image");
    return -EINVAL;
  }
  if (S_ISBLK(stat.st_mode)) {
    if (ioctl(fd, BLKGETSIZE64, &bytes) == 0) {
      sectors = bytes >> 9;
    } else if (ioctl(fd, BLKGETSIZE, &sectors) != 0) {
      syslog(LOG_INFO,
             "ERR: BLKGETSIZE and BLKGETSIZE64 failed, couldn't stat image");
      return -EINVAL;
    }
    sector_size = 512;
  } else {
    sectors = (stat.st_size >> 9);
    sector_size = 512;
  }
  if (sectors == 0) {
    sectors = 16836057ULL;
    sector_size = 512;
  }
  return 0;
}


x86 assembly (clang, -O0):
<tapdisk_get_image_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1060 <fstat@plt>
cmp    $0x0,%rax
je     11b3 <tapdisk_get_image_size+0x73>
mov    0x2e36(%rip),%rax
mov    (%rax),%edi
lea    0xe65(%rip),%rsi
call   1040 <syslog@plt>
xor    %eax,%eax
mov    0x2e27(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1279 <tapdisk_get_image_size+0x139>
mov    -0x1c(%rbp),%edi
call   1050 <S_ISBLK@plt>
cmp    $0x0,%rax
je     124a <tapdisk_get_image_size+0x10a>
mov    -0x8(%rbp),%edi
mov    0x2e19(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
jne    11f2 <tapdisk_get_image_size+0xb2>
mov    -0x28(%rbp),%eax
sar    $0x9,%eax
mov    %eax,-0x24(%rbp)
jmp    123e <tapdisk_get_image_size+0xfe>
mov    -0x8(%rbp),%edi
mov    0x2ddc(%rip),%rax
mov    (%rax),%esi
lea    -0x24(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
je     1239 <tapdisk_get_image_size+0xf9>
mov    0x2db0(%rip),%rax
mov    (%rax),%edi
lea    0xe08(%rip),%rsi
call   1040 <syslog@plt>
xor    %eax,%eax
mov    0x2da1(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1279 <tapdisk_get_image_size+0x139>
jmp    123e <tapdisk_get_image_size+0xfe>
movl   $0x200,-0x2c(%rbp)
jmp    125a <tapdisk_get_image_size+0x11a>
mov    -0x20(%rbp),%eax
sar    $0x9,%eax
mov    %eax,-0x24(%rbp)
movl   $0x200,-0x2c(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    1272 <tapdisk_get_image_size+0x132>
movl   $0x100e5d9,-0x24(%rbp)
movl   $0x200,-0x2c(%rbp)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tapdisk_get_image_size>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebp
movl   $0x0,0x4(%rsp)
movl   $0x0,(%rsi)
movl   $0x0,(%rdx)
lea    0x8(%rsp),%rsi
call   1060 <fstat@plt>
test   %rax,%rax
je     1194 <tapdisk_get_image_size+0x54>
mov    0x2e56(%rip),%rax
mov    (%rax),%edi
lea    0xe85(%rip),%rsi
call   1040 <syslog@plt>
mov    0x2e49(%rip),%rax
xor    %ebx,%ebx
sub    (%rax),%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
mov    0xc(%rsp),%edi
call   1050 <S_ISBLK@plt>
xor    %ebx,%ebx
test   %rax,%rax
je     118b <tapdisk_get_image_size+0x4b>
mov    0x2e3d(%rip),%rax
mov    (%rax),%esi
lea    0x14(%rsp),%rdx
mov    %ebp,%edi
call   1030 <ioctl@plt>
test   %rax,%rax
je     118b <tapdisk_get_image_size+0x4b>
mov    0x2e13(%rip),%rax
mov    (%rax),%esi
lea    0x4(%rsp),%rdx
mov    %ebp,%edi
call   1030 <ioctl@plt>
test   %rax,%rax
je     118b <tapdisk_get_image_size+0x4b>
mov    0x2de9(%rip),%rax
mov    (%rax),%edi
lea    0xe41(%rip),%rsi
jmp    117b <tapdisk_get_image_size+0x3b>

LLVM IR (-O0):
%struct.stat = type { i32, i32 }
@LOG_INFO = global i32 0
@.str = [41 x i8] c"ERROR: fstat failed, Couldn't stat image\00"
@EINVAL = global i32 0
@BLKGETSIZE64 = global i32 0
@BLKGETSIZE = global i32 0
@.str.1 = [61 x i8] c"ERR: BLKGETSIZE and BLKGETSIZE64 failed, couldn't stat image\00"
define i32 @tapdisk_get_image_size(i32 %0, i32* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca %struct.stat
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %5
store i32* %1, i32** %6
store i32* %2, i32** %7
store i32 0, i32* %9
store i32 0, i32* %11
%12 = load i32*, i32** %6
store i32 0, i32* %12
%13 = load i32*, i32** %7
store i32 0, i32* %13
%14 = load i32, i32* %5
%15 = call i64 @fstat(i32 %14, %struct.stat* %8)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %22
17: ; preds = %3
%18 = load i32, i32* @LOG_INFO
%19 = call i32 @syslog(i32 %18, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
%20 = load i32, i32* @EINVAL
%21 = sub nsw i32 0, %20
store i32 %21, i32* %4
br label %56
22: ; preds = %3
%23 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 1
%24 = load i32, i32* %23
%25 = call i64 @S_ISBLK(i32 %24)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %47
27: ; preds = %22
%28 = load i32, i32* %5
%29 = load i32, i32* @BLKGETSIZE64
%30 = call i64 @ioctl(i32 %28, i32 %29, i32* %10)
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %27
%33 = load i32, i32* %10
%34 = ashr i32 %33, 9
store i32 %34, i32* %9
br label %46
35: ; preds = %27
%36 = load i32, i32* %5
%37 = load i32, i32* @BLKGETSIZE
%38 = call i64 @ioctl(i32 %36, i32 %37, i32* %9)
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %45
40: ; preds = %35
%41 = load i32, i32* @LOG_INFO
%42 = call i32 @syslog(i32 %41, i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.1, i64 0, i64 0))
%43 = load i32, i32* @EINVAL
%44 = sub nsw i32 0, %43
store i32 %44, i32* %4
br label %56
45: ; preds = %35
br label %46
46: ; preds = %45, %32
store i32 512, i32* %11
br label %51
47: ; preds = %22
%48 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%49 = load i32, i32* %48
%50 = ashr i32 %49, 9
store i32 %50, i32* %9
store i32 512, i32* %11
br label %51
51: ; preds = %47, %46
%52 = load i32, i32* %9
%53 = icmp eq i32 %52, 0
br i1 %53, label %54, label %55
54: ; preds = %51
store i32 16836057, i32* %9
store i32 512, i32* %11
br label %55
55: ; preds = %54, %51
store i32 0, i32* %4
br label %56
56: ; preds = %55, %40, %17
%57 = load i32, i32* %4
ret i32 %57
}

LLVM IR (-O2):
%struct.stat = type { i32, i32 }
@LOG_INFO = global i32 0
@.str = [41 x i8] c"ERROR: fstat failed, Couldn't stat image\00"
@EINVAL = global i32 0
@BLKGETSIZE64 = global i32 0
@BLKGETSIZE = global i32 0
@.str.1 = [61 x i8] c"ERR: BLKGETSIZE and BLKGETSIZE64 failed, couldn't stat image\00"
define i32 @tapdisk_get_image_size(i32 %0, i32* nocapture writeonly %1, i32* nocapture writeonly %2) {
%4 = alloca %struct.stat
%5 = alloca i32
%6 = alloca i32
%7 = bitcast %struct.stat* %4 to i8*
%8 = bitcast i32* %5 to i8*
%9 = bitcast i32* %6 to i8*
store i32 0, i32* %5
store i32 0, i32* %1
store i32 0, i32* %2
%10 = call i64 @fstat(i32 %0, %struct.stat* %4)
%11 = icmp eq i64 %10, 0
br i1 %11, label %17, label %12
12: ; preds = %3
%13 = load i32, i32* @LOG_INFO
%14 = tail call i32 @syslog(i32 %13, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
%15 = load i32, i32* @EINVAL
%16 = sub nsw i32 0, %15
br label %35
17: ; preds = %3
%18 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 1
%19 = load i32, i32* %18
%20 = tail call i64 @S_ISBLK(i32 %19)
%21 = icmp eq i64 %20, 0
br i1 %21, label %35, label %22
22: ; preds = %17
%23 = load i32, i32* @BLKGETSIZE64
%24 = call i64 @ioctl(i32 %0, i32 %23, i32* %6)
%25 = icmp eq i64 %24, 0
br i1 %25, label %35, label %26
26: ; preds = %22
%27 = load i32, i32* @BLKGETSIZE
%28 = call i64 @ioctl(i32 %0, i32 %27, i32* %5)
%29 = icmp eq i64 %28, 0
br i1 %29, label %35, label %30
30: ; preds = %26
%31 = load i32, i32* @LOG_INFO
%32 = call i32 @syslog(i32 %31, i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.1, i64 0, i64 0))
%33 = load i32, i32* @EINVAL
%34 = sub nsw i32 0, %33
br label %35
35: ; preds = %26, %17, %22, %30, %12
%36 = phi i32 [ %16, %12 ], [ %34, %30 ], [ 0, %22 ], [ 0, %17 ], [ 0, %26 ]
ret i32 %36
}