#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*/ HDIO_DRIVE_CMD;
int /*<<< orphan*/ O_RDONLY;
int WIN_IDENTIFY;
int /*<<< orphan*/ close(int);
int ioctl(int, int /*<<< orphan*/, unsigned char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
unsigned long getDiskSize(char *szDrive) {
  unsigned char args[4 + 512] = {
      WIN_IDENTIFY,
      0,
      0,
      1,
  };
  unsigned short *drive_info = &args[4];
  int rc;
  int dev = open(szDrive, O_RDONLY);
  if (dev <= 0) {
    printf("Unable to open device %s\n", szDrive);
    return 0;
  }
  rc = ioctl(dev, HDIO_DRIVE_CMD, args);
  if (rc != 0) {
    printf("HDIO_DRIVE_CMD(identify) failed - is drive frozen?\n");
    return 0;
  }
  close(dev);
  unsigned long sectorsTotal = *((unsigned int *)&(drive_info[60]));
  if (drive_info[86] & 1ul << 10)
    sectorsTotal = *((unsigned int *)&(drive_info[100]));
  return sectorsTotal;
}


x86 assembly (clang, -O0):
<getDiskSize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x240,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x220(%rbp),%rdi
xor    %esi,%esi
mov    $0x204,%edx
call   1040 <memset@plt>
mov    0x2e5f(%rip),%rax
mov    (%rax),%eax
mov    %al,-0x220(%rbp)
movb   $0x1,-0x21d(%rbp)
lea    -0x220(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x228(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e43(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x230(%rbp)
cmpl   $0x0,-0x230(%rbp)
jg     11de <getDiskSize+0x8e>
mov    -0x10(%rbp),%rsi
lea    0xe36(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1280 <getDiskSize+0x130>
mov    -0x230(%rbp),%edi
mov    0x2df5(%rip),%rax
mov    (%rax),%esi
lea    -0x220(%rbp),%rdx
call   1050 <ioctl@plt>
mov    %eax,-0x22c(%rbp)
cmpl   $0x0,-0x22c(%rbp)
je     1227 <getDiskSize+0xd7>
lea    0xe07(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1280 <getDiskSize+0x130>
mov    -0x230(%rbp),%edi
call   1060 <close@plt>
mov    -0x228(%rbp),%rax
mov    0x78(%rax),%eax
mov    %rax,-0x238(%rbp)
mov    -0x228(%rbp),%rax
movzwl 0xac(%rax),%eax
and    $0x400,%rax
cmp    $0x0,%rax
je     1275 <getDiskSize+0x125>
mov    -0x228(%rbp),%rax
mov    0xc8(%rax),%eax
mov    %rax,-0x238(%rbp)
mov    -0x238(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x240,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getDiskSize>:
push   %rbp
push   %rbx
sub    $0x208,%rsp
mov    %rdi,%rbx
mov    %rsp,%rdi
mov    $0x204,%edx
xor    %esi,%esi
call   1050 <memset@plt>
mov    0x2e56(%rip),%rax
mov    (%rax),%al
mov    %al,(%rsp)
movb   $0x1,0x3(%rsp)
mov    0x2e55(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1080 <open@plt>
test   %eax,%eax
jle    11ca <getDiskSize+0x6a>
mov    %eax,%ebp
mov    0x2e36(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1060 <ioctl@plt>
test   %eax,%eax
je     11df <getDiskSize+0x7f>
lea    0xe59(%rip),%rdi
call   1030 <puts@plt>
xor    %ebp,%ebp
jmp    1201 <getDiskSize+0xa1>
lea    0xe2f(%rip),%rdi
xor    %ebp,%ebp
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1201 <getDiskSize+0xa1>
mov    %ebp,%edi
call   1070 <close@plt>
testb  $0x4,0xb1(%rsp)
04
lea    0x7c(%rsp),%rax
lea    0xcc(%rsp),%rcx
cmove  %rax,%rcx
mov    (%rcx),%ebp
mov    %rbp,%rax
add    $0x208,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@WIN_IDENTIFY = global i32 0
@O_RDONLY = global i32 0
@.str = [26 x i8] c"Unable to open device %s\0A\00"
@HDIO_DRIVE_CMD = global i32 0
@.str.1 = [52 x i8] c"HDIO_DRIVE_CMD(identify) failed - is drive frozen?\0A\00"
define i64 @getDiskSize(i8* %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = alloca [516 x i8]
%5 = alloca i16*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i64
store i8* %0, i8** %3
%9 = bitcast [516 x i8]* %4 to i8*
%10 = getelementptr inbounds [516 x i8], [516 x i8]* %4, i64 0, i64 0
%11 = load i32, i32* @WIN_IDENTIFY
%12 = trunc i32 %11 to i8
store i8 %12, i8* %10
%13 = getelementptr inbounds i8, i8* %10, i64 1
%14 = getelementptr inbounds i8, i8* %13, i64 1
%15 = getelementptr inbounds i8, i8* %14, i64 1
store i8 1, i8* %15
%16 = getelementptr inbounds [516 x i8], [516 x i8]* %4, i64 0, i64 4
%17 = bitcast i8* %16 to i16*
store i16* %17, i16** %5
%18 = load i8*, i8** %3
%19 = load i32, i32* @O_RDONLY
%20 = call i32 @open(i8* %18, i32 %19)
store i32 %20, i32* %7
%21 = load i32, i32* %7
%22 = icmp sle i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %1
%24 = load i8*, i8** %3
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %24)
store i64 0, i64* %2
br label %57
26: ; preds = %1
%27 = load i32, i32* %7
%28 = load i32, i32* @HDIO_DRIVE_CMD
%29 = getelementptr inbounds [516 x i8], [516 x i8]* %4, i64 0, i64 0
%30 = call i32 @ioctl(i32 %27, i32 %28, i8* %29)
store i32 %30, i32* %6
%31 = load i32, i32* %6
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %26
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.1, i64 0, i64 0))
store i64 0, i64* %2
br label %57
35: ; preds = %26
%36 = load i32, i32* %7
%37 = call i32 @close(i32 %36)
%38 = load i16*, i16** %5
%39 = getelementptr inbounds i16, i16* %38, i64 60
%40 = bitcast i16* %39 to i32*
%41 = load i32, i32* %40
%42 = zext i32 %41 to i64
store i64 %42, i64* %8
%43 = load i16*, i16** %5
%44 = getelementptr inbounds i16, i16* %43, i64 86
%45 = load i16, i16* %44
%46 = zext i16 %45 to i64
%47 = and i64 %46, 1024
%48 = icmp ne i64 %47, 0
br i1 %48, label %49, label %55
49: ; preds = %35
%50 = load i16*, i16** %5
%51 = getelementptr inbounds i16, i16* %50, i64 100
%52 = bitcast i16* %51 to i32*
%53 = load i32, i32* %52
%54 = zext i32 %53 to i64
store i64 %54, i64* %8
br label %55
55: ; preds = %49, %35
%56 = load i64, i64* %8
store i64 %56, i64* %2
br label %57
57: ; preds = %55, %33, %23
%58 = load i64, i64* %2
ret i64 %58
}

LLVM IR (-O2):
@WIN_IDENTIFY = global i32 0
@O_RDONLY = global i32 0
@.str = [26 x i8] c"Unable to open device %s\0A\00"
@HDIO_DRIVE_CMD = global i32 0
@str = [51 x i8] c"HDIO_DRIVE_CMD(identify) failed - is drive frozen?\00"
define i64 @getDiskSize(i8* %0) {
%2 = alloca [516 x i8]
%3 = getelementptr inbounds [516 x i8], [516 x i8]* %2, i64 0, i64 0
%4 = load i32, i32* @WIN_IDENTIFY
%5 = trunc i32 %4 to i8
store i8 %5, i8* %3
%6 = getelementptr inbounds [516 x i8], [516 x i8]* %2, i64 0, i64 3
store i8 1, i8* %6
%7 = load i32, i32* @O_RDONLY
%8 = tail call i32 @open(i8* %0, i32 %7)
%9 = icmp slt i32 %8, 1
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %31
12: ; preds = %1
%13 = load i32, i32* @HDIO_DRIVE_CMD
%14 = call i32 @ioctl(i32 %8, i32 %13, i8* %3)
%15 = icmp eq i32 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %12
%17 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([51 x i8], [51 x i8]* @str, i64 0, i64 0))
br label %31
18: ; preds = %12
%19 = call i32 @close(i32 %8)
%20 = getelementptr inbounds [516 x i8], [516 x i8]* %2, i64 0, i64 124
%21 = getelementptr inbounds [516 x i8], [516 x i8]* %2, i64 0, i64 176
%22 = bitcast i8* %21 to i16*
%23 = load i16, i16* %22
%24 = and i16 %23, 1024
%25 = icmp eq i16 %24, 0
%26 = getelementptr inbounds [516 x i8], [516 x i8]* %2, i64 0, i64 204
%27 = select i1 %25, i8* %20, i8* %26
%28 = bitcast i8* %27 to i32*
%29 = load i32, i32* %28
%30 = zext i32 %29 to i64
br label %31
31: ; preds = %18, %16, %10
%32 = phi i64 [ 0, %10 ], [ 0, %16 ], [ %30, %18 ]
ret i64 %32
}