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

/* Variables and functions */
int /*<<< orphan*/ BLKGETSIZE;
int /*<<< orphan*/ EXIT_FAILURE;
scalar_t__ S_ISBLK(int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fstat(int, struct stat *);
scalar_t__ ioctl(int, int /*<<< orphan*/, long *);
int /*<<< orphan*/ perror(char *);
long getblocks(int fd) {
  long ret;
  struct stat statbuf;
  if (fstat(fd, &statbuf)) {
    perror("stat error");
    exit(EXIT_FAILURE);
  }
  ret = statbuf.st_size / 512;
  if (S_ISBLK(statbuf.st_mode))
    if (ioctl(fd, BLKGETSIZE, &ret)) {
      perror("ioctl error");
      exit(EXIT_FAILURE);
    }
  return ret;
}


x86 assembly (clang, -O0):
<getblocks>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1070 <fstat@plt>
cmp    $0x0,%rax
je     118b <getblocks+0x3b>
lea    0xe88(%rip),%rdi
call   1050 <perror@plt>
mov    0x2e54(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    -0x18(%rbp),%eax
mov    $0x200,%ecx
cltd
idiv   %ecx
cltq
mov    %rax,-0x10(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <S_ISBLK@plt>
cmp    $0x0,%rax
je     11ec <getblocks+0x9c>
mov    -0x4(%rbp),%edi
mov    0x2e28(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
je     11e7 <getblocks+0x97>
lea    0xe37(%rip),%rdi
call   1050 <perror@plt>
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
jmp    11ec <getblocks+0x9c>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getblocks>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebp
lea    0x8(%rsp),%rsi
call   1070 <fstat@plt>
test   %rax,%rax
jne    11b8 <getblocks+0x68>
mov    0x8(%rsp),%eax
lea    0x1ff(%rax),%ecx
test   %eax,%eax
cmovns %eax,%ecx
mov    0xc(%rsp),%edi
sar    $0x9,%ecx
movslq %ecx,%rbx
mov    %rbx,0x10(%rsp)
call   1040 <S_ISBLK@plt>
test   %rax,%rax
je     11ae <getblocks+0x5e>
mov    0x2e4a(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdx
mov    %ebp,%edi
call   1030 <ioctl@plt>
test   %rax,%rax
jne    11c1 <getblocks+0x71>
mov    0x10(%rsp),%rbx
mov    %rbx,%rax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe41(%rip),%rdi
jmp    11c8 <getblocks+0x78>
lea    0xe43(%rip),%rdi
call   1050 <perror@plt>
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32, i32 }
@.str = [11 x i8] c"stat error\00"
@EXIT_FAILURE = global i32 0
@BLKGETSIZE = global i32 0
@.str.1 = [12 x i8] c"ioctl error\00"
define i64 @getblocks(i32 %0) {
%2 = alloca i32
%3 = alloca i64
%4 = alloca %struct.stat
store i32 %0, i32* %2
%5 = load i32, i32* %2
%6 = call i64 @fstat(i32 %5, %struct.stat* %4)
%7 = icmp ne i64 %6, 0
br i1 %7, label %8, label %12
8: ; preds = %1
%9 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%10 = load i32, i32* @EXIT_FAILURE
%11 = call i32 @exit(i32 %10)
unreachable
12: ; preds = %1
%13 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%14 = load i32, i32* %13
%15 = sdiv i32 %14, 512
%16 = sext i32 %15 to i64
store i64 %16, i64* %3
%17 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 1
%18 = load i32, i32* %17
%19 = call i64 @S_ISBLK(i32 %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %31
21: ; preds = %12
%22 = load i32, i32* %2
%23 = load i32, i32* @BLKGETSIZE
%24 = call i64 @ioctl(i32 %22, i32 %23, i64* %3)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %21
%27 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%28 = load i32, i32* @EXIT_FAILURE
%29 = call i32 @exit(i32 %28)
unreachable
30: ; preds = %21
br label %31
31: ; preds = %30, %12
%32 = load i64, i64* %3
ret i64 %32
}

LLVM IR (-O2):
%struct.stat = type { i32, i32 }
@.str = [11 x i8] c"stat error\00"
@EXIT_FAILURE = global i32 0
@BLKGETSIZE = global i32 0
@.str.1 = [12 x i8] c"ioctl error\00"
define i64 @getblocks(i32 %0) {
%2 = alloca i64
%3 = alloca %struct.stat
%4 = bitcast i64* %2 to i8*
%5 = bitcast %struct.stat* %3 to i8*
%6 = call i64 @fstat(i32 %0, %struct.stat* %3)
%7 = icmp eq i64 %6, 0
br i1 %7, label %12, label %8
8: ; preds = %1
%9 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%10 = load i32, i32* @EXIT_FAILURE
%11 = tail call i32 @exit(i32 %10)
unreachable
12: ; preds = %1
%13 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%14 = load i32, i32* %13
%15 = sdiv i32 %14, 512
%16 = sext i32 %15 to i64
store i64 %16, i64* %2
%17 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 1
%18 = load i32, i32* %17
%19 = tail call i64 @S_ISBLK(i32 %18)
%20 = icmp eq i64 %19, 0
br i1 %20, label %31, label %21
21: ; preds = %12
%22 = load i32, i32* @BLKGETSIZE
%23 = call i64 @ioctl(i32 %0, i32 %22, i64* %2)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = load i64, i64* %2
br label %31
27: ; preds = %21
%28 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%29 = load i32, i32* @EXIT_FAILURE
%30 = call i32 @exit(i32 %29)
unreachable
31: ; preds = %25, %12
%32 = phi i64 [ %26, %25 ], [ %16, %12 ]
ret i64 %32
}