#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 hd_geometry {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ HDIO_GETGEO;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int ioctl(int, int /*<<< orphan*/, struct hd_geometry *);
int open(char const *, int /*<<< orphan*/);
int getDriveGeometry(const char *dev, struct hd_geometry *geom) {
  int r;
  int fd = open(dev, O_RDONLY);
  if (fd < 0)
    return -1;
  r = ioctl(fd, HDIO_GETGEO, geom);
  close(fd);
  return r;
}


x86 assembly (clang, -O0):
<getDriveGeometry>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%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    116b <getDriveGeometry+0x3b>
movl   $0xffffffff,-0x4(%rbp)
jmp    1191 <getDriveGeometry+0x61>
mov    -0x20(%rbp),%edi
mov    0x2e6b(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%rdx
call   1030 <ioctl@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getDriveGeometry>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    0x2eab(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
test   %eax,%eax
js     116a <getDriveGeometry+0x3a>
mov    %eax,%ebp
mov    0x2e8f(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %rbx,%rdx
call   1030 <ioctl@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1040 <close@plt>
mov    %ebx,%eax
jmp    116f <getDriveGeometry+0x3f>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.hd_geometry = type { i32 }
@O_RDONLY = global i32 0
@HDIO_GETGEO = global i32 0
define i32 @getDriveGeometry(i8* %0, %struct.hd_geometry* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca %struct.hd_geometry*
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %4
store %struct.hd_geometry* %1, %struct.hd_geometry** %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 %14
13: ; preds = %2
store i32 -1, i32* %3
br label %22
14: ; preds = %2
%15 = load i32, i32* %7
%16 = load i32, i32* @HDIO_GETGEO
%17 = load %struct.hd_geometry*, %struct.hd_geometry** %5
%18 = call i32 @ioctl(i32 %15, i32 %16, %struct.hd_geometry* %17)
store i32 %18, i32* %6
%19 = load i32, i32* %7
%20 = call i32 @close(i32 %19)
%21 = load i32, i32* %6
store i32 %21, i32* %3
br label %22
22: ; preds = %14, %13
%23 = load i32, i32* %3
ret i32 %23
}

LLVM IR (-O2):
%struct.hd_geometry = type { i32 }
@O_RDONLY = global i32 0
@HDIO_GETGEO = global i32 0
define i32 @getDriveGeometry(i8* nocapture %0, %struct.hd_geometry* %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 %10, label %6
6: ; preds = %2
%7 = load i32, i32* @HDIO_GETGEO
%8 = tail call i32 @ioctl(i32 %4, i32 %7, %struct.hd_geometry* %1)
%9 = tail call i32 @close(i32 %4)
br label %10
10: ; preds = %2, %6
%11 = phi i32 [ %8, %6 ], [ -1, %2 ]
ret i32 %11
}