#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 /*<<< orphan*/ st_mode;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ S_ISBLK(int /*<<< orphan*/);
int atoi(char *);
int /*<<< orphan*/ die(char *, char *, ...);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ fstat(int, struct stat *);
scalar_t__ lseek(int, int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *, int);
int read(int, unsigned char *, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int write(int, unsigned char *, int);
int main(int argc, char **argv) {
  struct stat st;
  int fd, number, count;
  unsigned char flag;
  if (argc != 2 && argc != 3) {
    fprintf(stderr, "usage: %s device [ partition ]\n", argv[0]);
    fprintf(stderr, "  i.e. %s /dev/hda 2\n", argv[0]);
    exit(1);
  }
  if ((fd = open(argv[1], argc == 2 ? O_RDONLY : O_WRONLY)) < 0)
    die("open %s: %s", argv[1], strerror(errno));
  if (fstat(fd, &st) < 0)
    die("stat %s: %s", argv[1], strerror(errno));
  if (!S_ISBLK(st.st_mode))
    die("%s: not a block device", argv[1]);
  if (argc == 2) {
    for (count = 1; count <= 4; count++) {
      if (lseek(fd, 0x1be + (count - 1) * 16, SEEK_SET) < 0)
        die("lseek: %s", strerror(errno));
      if (read(fd, &flag, 1) != 1)
        die("read: %s", strerror(errno));
      if (flag == 0x80) {
        printf("%s%d\n", argv[1], count);
        exit(0);
      }
    }
    die("No active partition found on %s", argv[1]);
  }
  number = atoi(argv[2]);
  if (number < 1 || number > 4)
    die("%s: not a valid partition number (1-4)", argv[2]);
  for (count = 1; count <= 4; count++) {
    if (lseek(fd, 0x1be + (count - 1) * 16, SEEK_SET) < 0)
      die("lseek: %s", strerror(errno));
    flag = count == number ? 0x80 : 0;
    if (write(fd, &flag, 1) != 1)
      die("write: %s", strerror(errno));
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     122c <main+0x6c>
cmpl   $0x3,-0x8(%rbp)
je     122c <main+0x6c>
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xdff(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2dcb(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe03(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x30(%rbp)
cmpl   $0x2,-0x8(%rbp)
jne    1253 <main+0x93>
mov    0x2d9f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
jmp    125f <main+0x9f>
mov    0x2d5e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x34(%rbp),%esi
call   1090 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    12a6 <main+0xe6>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x40(%rbp)
mov    0x2d46(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    -0x40(%rbp),%rsi
mov    %rax,%rdx
lea    0xd97(%rip),%rdi
mov    $0x0,%al
call   10a0 <die@plt>
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   10e0 <fstat@plt>
cmp    $0x0,%rax
jge    12eb <main+0x12b>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x48(%rbp)
mov    0x2d01(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    -0x48(%rbp),%rsi
mov    %rax,%rdx
lea    0xd5e(%rip),%rdi
mov    $0x0,%al
call   10a0 <die@plt>
mov    -0x18(%rbp),%edi
call   1080 <S_ISBLK@plt>
cmp    $0x0,%eax
jne    1312 <main+0x152>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xd43(%rip),%rdi
mov    $0x0,%al
call   10a0 <die@plt>
cmpl   $0x2,-0x8(%rbp)
jne    1405 <main+0x245>
movl   $0x1,-0x24(%rbp)
cmpl   $0x4,-0x24(%rbp)
jg     13ef <main+0x22f>
mov    -0x1c(%rbp),%edi
mov    -0x24(%rbp),%esi
sub    $0x1,%esi
shl    $0x4,%esi
add    $0x1be,%esi
mov    0x2c82(%rip),%rax
mov    (%rax),%edx
call   1050 <lseek@plt>
cmp    $0x0,%rax
jge    1376 <main+0x1b6>
mov    0x2c72(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    %rax,%rsi
lea    0xcf6(%rip),%rdi
mov    $0x0,%al
call   10a0 <die@plt>
mov    -0x1c(%rbp),%edi
lea    -0x25(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
cmp    $0x1,%eax
je     13af <main+0x1ef>
mov    0x2c39(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    %rax,%rsi
lea    0xcc7(%rip),%rdi
mov    $0x0,%al
call   10a0 <die@plt>
movzbl -0x25(%rbp),%eax
cmp    $0x80,%eax
jne    13dc <main+0x21c>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
mov    -0x24(%rbp),%edx
lea    0xca8(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
jmp    13e1 <main+0x221>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1323 <main+0x163>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xc80(%rip),%rdi
mov    $0x0,%al
call   10a0 <die@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10b0 <atoi@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x1,-0x20(%rbp)
jl     1429 <main+0x269>
cmpl   $0x4,-0x20(%rbp)
jle    143f <main+0x27f>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
lea    0xc66(%rip),%rdi
mov    $0x0,%al
call   10a0 <die@plt>
movl   $0x1,-0x24(%rbp)
cmpl   $0x4,-0x24(%rbp)
jg     14fa <main+0x33a>
mov    -0x1c(%rbp),%edi
mov    -0x24(%rbp),%esi
sub    $0x1,%esi
shl    $0x4,%esi
add    $0x1be,%esi
mov    0x2b5f(%rip),%rax
mov    (%rax),%edx
call   1050 <lseek@plt>
cmp    $0x0,%rax
jge    1499 <main+0x2d9>
mov    0x2b4f(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    %rax,%rsi
lea    0xbd3(%rip),%rdi
mov    $0x0,%al
call   10a0 <die@plt>
mov    -0x24(%rbp),%edx
mov    -0x20(%rbp),%esi
xor    %eax,%eax
mov    $0x80,%ecx
cmp    %esi,%edx
cmove  %ecx,%eax
mov    %al,-0x25(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x25(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
je     14e7 <main+0x327>
mov    0x2b01(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    %rax,%rsi
lea    0xbe5(%rip),%rdi
mov    $0x0,%al
call   10a0 <die@plt>
jmp    14ec <main+0x32c>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1446 <main+0x286>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    %edi,%eax
and    $0xfffffffe,%eax
cmp    $0x2,%eax
jne    1691 <main+0x4d1>
mov    %edi,%ebp
mov    0x8(%r14),%rdi
cmp    $0x2,%ebp
jne    11ef <main+0x2f>
mov    0x2dfb(%rip),%rax
jmp    11f6 <main+0x36>
mov    0x2dc2(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1229 <main+0x69>
mov    0x8(%r14),%r15
mov    0x2dc2(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xe1a(%rip),%rdi
mov    %r15,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   10a0 <die@plt>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
call   10e0 <fstat@plt>
test   %rax,%rax
jns    1260 <main+0xa0>
mov    0x8(%r14),%r15
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xdef(%rip),%rdi
mov    %r15,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   10a0 <die@plt>
mov    0x10(%rsp),%edi
call   1080 <S_ISBLK@plt>
test   %eax,%eax
je     1277 <main+0xb7>
cmp    $0x2,%ebp
je     1292 <main+0xd2>
jmp    147c <main+0x2bc>
mov    0x8(%r14),%rsi
lea    0xdcc(%rip),%rdi
xor    %eax,%eax
call   10a0 <die@plt>
cmp    $0x2,%ebp
jne    147c <main+0x2bc>
mov    0x2d2f(%rip),%r15
mov    (%r15),%edx
mov    %ebx,%edi
mov    $0x1be,%esi
call   1050 <lseek@plt>
test   %rax,%rax
jns    12cc <main+0x10c>
mov    0x2d1c(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xda3(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   10a0 <die@plt>
lea    0xf(%rsp),%rsi
mov    $0x1,%ebp
mov    %ebx,%edi
mov    $0x1,%edx
call   1060 <read@plt>
cmp    $0x1,%eax
je     1306 <main+0x146>
mov    0x2ce2(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xd73(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   10a0 <die@plt>
cmpb   $0x80,0xf(%rsp)
je     16d7 <main+0x517>
mov    (%r15),%edx
mov    %ebx,%edi
mov    $0x1ce,%esi
call   1050 <lseek@plt>
test   %rax,%rax
jns    1344 <main+0x184>
mov    0x2ca4(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xd2b(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   10a0 <die@plt>
lea    0xf(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1060 <read@plt>
cmp    $0x1,%eax
je     1379 <main+0x1b9>
mov    0x2c6f(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xd00(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   10a0 <die@plt>
cmpb   $0x80,0xf(%rsp)
je     16c4 <main+0x504>
mov    (%r15),%edx
mov    %ebx,%edi
mov    $0x1de,%esi
call   1050 <lseek@plt>
test   %rax,%rax
jns    13b7 <main+0x1f7>
mov    0x2c31(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xcb8(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   10a0 <die@plt>
lea    0xf(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1060 <read@plt>
cmp    $0x1,%eax
je     13ec <main+0x22c>
mov    0x2bfc(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xc8d(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   10a0 <die@plt>
cmpb   $0x80,0xf(%rsp)
je     16cb <main+0x50b>
mov    (%r15),%edx
mov    %ebx,%edi
mov    $0x1ee,%esi
call   1050 <lseek@plt>
test   %rax,%rax
jns    142a <main+0x26a>
mov    0x2bbe(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xc45(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   10a0 <die@plt>
lea    0xf(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1060 <read@plt>
cmp    $0x1,%eax
je     145f <main+0x29f>
mov    0x2b89(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xc1a(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   10a0 <die@plt>
cmpb   $0x80,0xf(%rsp)
je     16d2 <main+0x512>
mov    0x8(%r14),%rsi
lea    0xc09(%rip),%rdi
xor    %eax,%eax
call   10a0 <die@plt>
mov    0x10(%r14),%rbp
mov    %rbp,%rdi
call   10b0 <atoi@plt>
mov    %eax,%r14d
lea    -0x5(%r14),%eax
cmp    $0xfffffffb,%eax
ja     14a5 <main+0x2e5>
lea    0xc03(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   10a0 <die@plt>
mov    0x2b1c(%rip),%rbp
mov    0x0(%rbp),%edx
mov    %ebx,%edi
mov    $0x1be,%esi
call   1050 <lseek@plt>
test   %rax,%rax
jns    14df <main+0x31f>
mov    0x2b09(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xb90(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   10a0 <die@plt>
cmp    $0x1,%r14d
sete   %al
shl    $0x7,%al
mov    %al,0xf(%rsp)
lea    0xf(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
je     1522 <main+0x362>
mov    0x2ac6(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xbad(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   10a0 <die@plt>
mov    0x0(%rbp),%edx
mov    %ebx,%edi
mov    $0x1ce,%esi
call   1050 <lseek@plt>
test   %rax,%rax
jns    1555 <main+0x395>
mov    0x2a93(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xb1a(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   10a0 <die@plt>
cmp    $0x2,%r14d
sete   %al
shl    $0x7,%al
mov    %al,0xf(%rsp)
lea    0xf(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
je     1598 <main+0x3d8>
mov    0x2a50(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xb37(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   10a0 <die@plt>
mov    0x0(%rbp),%edx
mov    %ebx,%edi
mov    $0x1de,%esi
call   1050 <lseek@plt>
test   %rax,%rax
jns    15cb <main+0x40b>
mov    0x2a1d(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xaa4(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   10a0 <die@plt>
cmp    $0x3,%r14d
sete   %al
shl    $0x7,%al
mov    %al,0xf(%rsp)
lea    0xf(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
je     160e <main+0x44e>
mov    0x29da(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xac1(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   10a0 <die@plt>
mov    0x0(%rbp),%edx
mov    %ebx,%edi
mov    $0x1ee,%esi
call   1050 <lseek@plt>
test   %rax,%rax
jns    1641 <main+0x481>
mov    0x29a7(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xa2e(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   10a0 <die@plt>
cmp    $0x4,%r14d
sete   %al
shl    $0x7,%al
mov    %al,0xf(%rsp)
lea    0xf(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
je     1684 <main+0x4c4>
mov    0x2964(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xa4b(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   10a0 <die@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2940(%rip),%rbx
mov    (%rbx),%edi
mov    (%r14),%rdx
lea    0x95c(%rip),%rsi
call   1070 <fprintf@plt>
mov    (%rbx),%edi
mov    (%r14),%rdx
lea    0x96b(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    $0x2,%ebp
jmp    16d7 <main+0x517>
mov    $0x3,%ebp
jmp    16d7 <main+0x517>
mov    $0x4,%ebp
mov    0x8(%r14),%rsi
lea    0x996(%rip),%rdi
mov    %ebp,%edx
call   1040 <printf@plt>
xor    %edi,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [32 x i8] c"usage: %s device [ partition ]\0A\00"
@.str.1 = [22 x i8] c" i.e. %s /dev/hda 2\0A\00"
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@.str.2 = [12 x i8] c"open %s: %s\00"
@errno = global i32 0
@.str.3 = [12 x i8] c"stat %s: %s\00"
@.str.4 = [23 x i8] c"%s: not a block device\00"
@SEEK_SET = global i32 0
@.str.5 = [10 x i8] c"lseek: %s\00"
@.str.6 = [9 x i8] c"read: %s\00"
@.str.7 = [6 x i8] c"%s%d\0A\00"
@.str.8 = [32 x i8] c"No active partition found on %s\00"
@.str.9 = [39 x i8] c"%s: not a valid partition number (1-4)\00"
@.str.10 = [10 x i8] c"write: %s\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.stat
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 2
br i1 %12, label %13, label %28
13: ; preds = %2
%14 = load i32, i32* %4
%15 = icmp ne i32 %14, 3
br i1 %15, label %16, label %28
16: ; preds = %13
%17 = load i32, i32* @stderr
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 0
%20 = load i8*, i8** %19
%21 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %20)
%22 = load i32, i32* @stderr
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 0
%25 = load i8*, i8** %24
%26 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %25)
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %13, %2
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 1
%31 = load i8*, i8** %30
%32 = load i32, i32* %4
%33 = icmp eq i32 %32, 2
br i1 %33, label %34, label %36
34: ; preds = %28
%35 = load i32, i32* @O_RDONLY
br label %38
36: ; preds = %28
%37 = load i32, i32* @O_WRONLY
br label %38
38: ; preds = %36, %34
%39 = phi i32 [ %35, %34 ], [ %37, %36 ]
%40 = call i32 @open(i8* %31, i32 %39)
store i32 %40, i32* %7
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %49
42: ; preds = %38
%43 = load i8**, i8*** %5
%44 = getelementptr inbounds i8*, i8** %43, i64 1
%45 = load i8*, i8** %44
%46 = load i32, i32* @errno
%47 = call i8* @strerror(i32 %46)
%48 = call i32 (i8*, i8*, ...) @die(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* %45, i8* %47)
br label %49
49: ; preds = %42, %38
%50 = load i32, i32* %7
%51 = call i64 @fstat(i32 %50, %struct.stat* %6)
%52 = icmp slt i64 %51, 0
br i1 %52, label %53, label %60
53: ; preds = %49
%54 = load i8**, i8*** %5
%55 = getelementptr inbounds i8*, i8** %54, i64 1
%56 = load i8*, i8** %55
%57 = load i32, i32* @errno
%58 = call i8* @strerror(i32 %57)
%59 = call i32 (i8*, i8*, ...) @die(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i8* %56, i8* %58)
br label %60
60: ; preds = %53, %49
%61 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%62 = load i32, i32* %61
%63 = call i32 @S_ISBLK(i32 %62)
%64 = icmp ne i32 %63, 0
br i1 %64, label %70, label %65
65: ; preds = %60
%66 = load i8**, i8*** %5
%67 = getelementptr inbounds i8*, i8** %66, i64 1
%68 = load i8*, i8** %67
%69 = call i32 (i8*, i8*, ...) @die(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i8* %68)
br label %70
70: ; preds = %65, %60
%71 = load i32, i32* %4
%72 = icmp eq i32 %71, 2
br i1 %72, label %73, label %118
73: ; preds = %70
store i32 1, i32* %9
br label %74
74: ; preds = %110, %73
%75 = load i32, i32* %9
%76 = icmp sle i32 %75, 4
br i1 %76, label %77, label %113
77: ; preds = %74
%78 = load i32, i32* %7
%79 = load i32, i32* %9
%80 = sub nsw i32 %79, 1
%81 = mul nsw i32 %80, 16
%82 = add nsw i32 446, %81
%83 = load i32, i32* @SEEK_SET
%84 = call i64 @lseek(i32 %78, i32 %82, i32 %83)
%85 = icmp slt i64 %84, 0
br i1 %85, label %86, label %90
86: ; preds = %77
%87 = load i32, i32* @errno
%88 = call i8* @strerror(i32 %87)
%89 = call i32 (i8*, i8*, ...) @die(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i8* %88)
br label %90
90: ; preds = %86, %77
%91 = load i32, i32* %7
%92 = call i32 @read(i32 %91, i8* %10, i32 1)
%93 = icmp ne i32 %92, 1
br i1 %93, label %94, label %98
94: ; preds = %90
%95 = load i32, i32* @errno
%96 = call i8* @strerror(i32 %95)
%97 = call i32 (i8*, i8*, ...) @die(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0), i8* %96)
br label %98
98: ; preds = %94, %90
%99 = load i8, i8* %10
%100 = zext i8 %99 to i32
%101 = icmp eq i32 %100, 128
br i1 %101, label %102, label %109
102: ; preds = %98
%103 = load i8**, i8*** %5
%104 = getelementptr inbounds i8*, i8** %103, i64 1
%105 = load i8*, i8** %104
%106 = load i32, i32* %9
%107 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0), i8* %105, i32 %106)
%108 = call i32 @exit(i32 0)
unreachable
109: ; preds = %98
br label %110
110: ; preds = %109
%111 = load i32, i32* %9
%112 = add nsw i32 %111, 1
store i32 %112, i32* %9
br label %74
113: ; preds = %74
%114 = load i8**, i8*** %5
%115 = getelementptr inbounds i8*, i8** %114, i64 1
%116 = load i8*, i8** %115
%117 = call i32 (i8*, i8*, ...) @die(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.8, i64 0, i64 0), i8* %116)
br label %118
118: ; preds = %113, %70
%119 = load i8**, i8*** %5
%120 = getelementptr inbounds i8*, i8** %119, i64 2
%121 = load i8*, i8** %120
%122 = call i32 @atoi(i8* %121)
store i32 %122, i32* %8
%123 = load i32, i32* %8
%124 = icmp slt i32 %123, 1
br i1 %124, label %128, label %125
125: ; preds = %118
%126 = load i32, i32* %8
%127 = icmp sgt i32 %126, 4
br i1 %127, label %128, label %133
128: ; preds = %125, %118
%129 = load i8**, i8*** %5
%130 = getelementptr inbounds i8*, i8** %129, i64 2
%131 = load i8*, i8** %130
%132 = call i32 (i8*, i8*, ...) @die(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.9, i64 0, i64 0), i8* %131)
br label %133
133: ; preds = %128, %125
store i32 1, i32* %9
br label %134
134: ; preds = %165, %133
%135 = load i32, i32* %9
%136 = icmp sle i32 %135, 4
br i1 %136, label %137, label %168
137: ; preds = %134
%138 = load i32, i32* %7
%139 = load i32, i32* %9
%140 = sub nsw i32 %139, 1
%141 = mul nsw i32 %140, 16
%142 = add nsw i32 446, %141
%143 = load i32, i32* @SEEK_SET
%144 = call i64 @lseek(i32 %138, i32 %142, i32 %143)
%145 = icmp slt i64 %144, 0
br i1 %145, label %146, label %150
146: ; preds = %137
%147 = load i32, i32* @errno
%148 = call i8* @strerror(i32 %147)
%149 = call i32 (i8*, i8*, ...) @die(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i8* %148)
br label %150
150: ; preds = %146, %137
%151 = load i32, i32* %9
%152 = load i32, i32* %8
%153 = icmp eq i32 %151, %152
%154 = zext i1 %153 to i64
%155 = select i1 %153, i32 128, i32 0
%156 = trunc i32 %155 to i8
store i8 %156, i8* %10
%157 = load i32, i32* %7
%158 = call i32 @write(i32 %157, i8* %10, i32 1)
%159 = icmp ne i32 %158, 1
br i1 %159, label %160, label %164
160: ; preds = %150
%161 = load i32, i32* @errno
%162 = call i8* @strerror(i32 %161)
%163 = call i32 (i8*, i8*, ...) @die(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.10, i64 0, i64 0), i8* %162)
br label %164
164: ; preds = %160, %150
br label %165
165: ; preds = %164
%166 = load i32, i32* %9
%167 = add nsw i32 %166, 1
store i32 %167, i32* %9
br label %134
168: ; preds = %134
ret i32 0
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [32 x i8] c"usage: %s device [ partition ]\0A\00"
@.str.1 = [22 x i8] c" i.e. %s /dev/hda 2\0A\00"
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@.str.2 = [12 x i8] c"open %s: %s\00"
@errno = global i32 0
@.str.3 = [12 x i8] c"stat %s: %s\00"
@.str.4 = [23 x i8] c"%s: not a block device\00"
@SEEK_SET = global i32 0
@.str.5 = [10 x i8] c"lseek: %s\00"
@.str.6 = [9 x i8] c"read: %s\00"
@.str.7 = [6 x i8] c"%s%d\0A\00"
@.str.8 = [32 x i8] c"No active partition found on %s\00"
@.str.9 = [39 x i8] c"%s: not a valid partition number (1-4)\00"
@.str.10 = [10 x i8] c"write: %s\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = alloca i8
%5 = bitcast %struct.stat* %3 to i8*
%6 = and i32 %0, -2
%7 = icmp eq i32 %6, 2
br i1 %7, label %16, label %8
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = load i8*, i8** %1
%11 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = load i32, i32* @stderr
%13 = load i8*, i8** %1
%14 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %13)
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %2
%17 = getelementptr inbounds i8*, i8** %1, i64 1
%18 = load i8*, i8** %17
%19 = icmp eq i32 %0, 2
%20 = load i32, i32* @O_RDONLY
%21 = load i32, i32* @O_WRONLY
%22 = select i1 %19, i32 %20, i32 %21
%23 = tail call i32 @open(i8* %18, i32 %22)
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %30
25: ; preds = %16
%26 = load i8*, i8** %17
%27 = load i32, i32* @errno
%28 = tail call i8* @strerror(i32 %27)
%29 = tail call i32 (i8*, i8*, ...) @die(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* %26, i8* %28)
br label %30
30: ; preds = %25, %16
%31 = call i64 @fstat(i32 %23, %struct.stat* %3)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %38
33: ; preds = %30
%34 = load i8*, i8** %17
%35 = load i32, i32* @errno
%36 = tail call i8* @strerror(i32 %35)
%37 = tail call i32 (i8*, i8*, ...) @die(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i8* %34, i8* %36)
br label %38
38: ; preds = %33, %30
%39 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%40 = load i32, i32* %39
%41 = tail call i32 @S_ISBLK(i32 %40)
%42 = icmp eq i32 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %38
%44 = load i8*, i8** %17
%45 = tail call i32 (i8*, i8*, ...) @die(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i8* %44)
br label %46
46: ; preds = %43, %38
br i1 %19, label %47, label %127
47: ; preds = %46
%48 = load i32, i32* @SEEK_SET
%49 = tail call i64 @lseek(i32 %23, i32 446, i32 %48)
%50 = icmp slt i64 %49, 0
br i1 %50, label %51, label %55
51: ; preds = %47
%52 = load i32, i32* @errno
%53 = tail call i8* @strerror(i32 %52)
%54 = tail call i32 (i8*, i8*, ...) @die(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i8* %53)
br label %55
55: ; preds = %51, %47
%56 = call i32 @read(i32 %23, i8* %4, i32 1)
%57 = icmp eq i32 %56, 1
br i1 %57, label %62, label %58
58: ; preds = %55
%59 = load i32, i32* @errno
%60 = tail call i8* @strerror(i32 %59)
%61 = tail call i32 (i8*, i8*, ...) @die(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0), i8* %60)
br label %62
62: ; preds = %58, %55
%63 = load i8, i8* %4
%64 = icmp eq i8 %63, -128
br i1 %64, label %65, label %70
65: ; preds = %121, %103, %85, %62
%66 = phi i32 [ 1, %62 ], [ 2, %85 ], [ 3, %103 ], [ 4, %121 ]
%67 = load i8*, i8** %17
%68 = tail call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0), i8* %67, i32 %66)
%69 = tail call i32 @exit(i32 0)
unreachable
70: ; preds = %62
%71 = load i32, i32* @SEEK_SET
%72 = tail call i64 @lseek(i32 %23, i32 462, i32 %71)
%73 = icmp slt i64 %72, 0
br i1 %73, label %74, label %78
74: ; preds = %70
%75 = load i32, i32* @errno
%76 = tail call i8* @strerror(i32 %75)
%77 = tail call i32 (i8*, i8*, ...) @die(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i8* %76)
br label %78
78: ; preds = %74, %70
%79 = call i32 @read(i32 %23, i8* %4, i32 1)
%80 = icmp eq i32 %79, 1
br i1 %80, label %85, label %81
81: ; preds = %78
%82 = load i32, i32* @errno
%83 = tail call i8* @strerror(i32 %82)
%84 = tail call i32 (i8*, i8*, ...) @die(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0), i8* %83)
br label %85
85: ; preds = %81, %78
%86 = load i8, i8* %4
%87 = icmp eq i8 %86, -128
br i1 %87, label %65, label %88
88: ; preds = %85
%89 = load i32, i32* @SEEK_SET
%90 = tail call i64 @lseek(i32 %23, i32 478, i32 %89)
%91 = icmp slt i64 %90, 0
br i1 %91, label %92, label %96
92: ; preds = %88
%93 = load i32, i32* @errno
%94 = tail call i8* @strerror(i32 %93)
%95 = tail call i32 (i8*, i8*, ...) @die(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i8* %94)
br label %96
96: ; preds = %92, %88
%97 = call i32 @read(i32 %23, i8* %4, i32 1)
%98 = icmp eq i32 %97, 1
br i1 %98, label %103, label %99
99: ; preds = %96
%100 = load i32, i32* @errno
%101 = tail call i8* @strerror(i32 %100)
%102 = tail call i32 (i8*, i8*, ...) @die(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0), i8* %101)
br label %103
103: ; preds = %99, %96
%104 = load i8, i8* %4
%105 = icmp eq i8 %104, -128
br i1 %105, label %65, label %106
106: ; preds = %103
%107 = load i32, i32* @SEEK_SET
%108 = tail call i64 @lseek(i32 %23, i32 494, i32 %107)
%109 = icmp slt i64 %108, 0
br i1 %109, label %110, label %114
110: ; preds = %106
%111 = load i32, i32* @errno
%112 = tail call i8* @strerror(i32 %111)
%113 = tail call i32 (i8*, i8*, ...) @die(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i8* %112)
br label %114
114: ; preds = %110, %106
%115 = call i32 @read(i32 %23, i8* %4, i32 1)
%116 = icmp eq i32 %115, 1
br i1 %116, label %121, label %117
117: ; preds = %114
%118 = load i32, i32* @errno
%119 = tail call i8* @strerror(i32 %118)
%120 = tail call i32 (i8*, i8*, ...) @die(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0), i8* %119)
br label %121
121: ; preds = %117, %114
%122 = load i8, i8* %4
%123 = icmp eq i8 %122, -128
br i1 %123, label %65, label %124
124: ; preds = %121
%125 = load i8*, i8** %17
%126 = tail call i32 (i8*, i8*, ...) @die(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.8, i64 0, i64 0), i8* %125)
br label %127
127: ; preds = %124, %46
%128 = getelementptr inbounds i8*, i8** %1, i64 2
%129 = load i8*, i8** %128
%130 = tail call i32 @atoi(i8* %129)
%131 = add i32 %130, -5
%132 = icmp ult i32 %131, -4
br i1 %132, label %133, label %135
133: ; preds = %127
%134 = tail call i32 (i8*, i8*, ...) @die(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.9, i64 0, i64 0), i8* %129)
br label %135
135: ; preds = %127, %133
%136 = load i32, i32* @SEEK_SET
%137 = tail call i64 @lseek(i32 %23, i32 446, i32 %136)
%138 = icmp slt i64 %137, 0
br i1 %138, label %139, label %143
139: ; preds = %135
%140 = load i32, i32* @errno
%141 = tail call i8* @strerror(i32 %140)
%142 = tail call i32 (i8*, i8*, ...) @die(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i8* %141)
br label %143
143: ; preds = %139, %135
%144 = icmp eq i32 %130, 1
%145 = select i1 %144, i8 -128, i8 0
store i8 %145, i8* %4
%146 = call i32 @write(i32 %23, i8* %4, i32 1)
%147 = icmp eq i32 %146, 1
br i1 %147, label %152, label %148
148: ; preds = %143
%149 = load i32, i32* @errno
%150 = tail call i8* @strerror(i32 %149)
%151 = tail call i32 (i8*, i8*, ...) @die(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.10, i64 0, i64 0), i8* %150)
br label %152
152: ; preds = %143, %148
%153 = load i32, i32* @SEEK_SET
%154 = tail call i64 @lseek(i32 %23, i32 462, i32 %153)
%155 = icmp slt i64 %154, 0
br i1 %155, label %156, label %160
156: ; preds = %152
%157 = load i32, i32* @errno
%158 = tail call i8* @strerror(i32 %157)
%159 = tail call i32 (i8*, i8*, ...) @die(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i8* %158)
br label %160
160: ; preds = %156, %152
%161 = icmp eq i32 %130, 2
%162 = select i1 %161, i8 -128, i8 0
store i8 %162, i8* %4
%163 = call i32 @write(i32 %23, i8* %4, i32 1)
%164 = icmp eq i32 %163, 1
br i1 %164, label %169, label %165
165: ; preds = %160
%166 = load i32, i32* @errno
%167 = tail call i8* @strerror(i32 %166)
%168 = tail call i32 (i8*, i8*, ...) @die(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.10, i64 0, i64 0), i8* %167)
br label %169
169: ; preds = %165, %160
%170 = load i32, i32* @SEEK_SET
%171 = tail call i64 @lseek(i32 %23, i32 478, i32 %170)
%172 = icmp slt i64 %171, 0
br i1 %172, label %173, label %177
173: ; preds = %169
%174 = load i32, i32* @errno
%175 = tail call i8* @strerror(i32 %174)
%176 = tail call i32 (i8*, i8*, ...) @die(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i8* %175)
br label %177
177: ; preds = %173, %169
%178 = icmp eq i32 %130, 3
%179 = select i1 %178, i8 -128, i8 0
store i8 %179, i8* %4
%180 = call i32 @write(i32 %23, i8* %4, i32 1)
%181 = icmp eq i32 %180, 1
br i1 %181, label %186, label %182
182: ; preds = %177
%183 = load i32, i32* @errno
%184 = tail call i8* @strerror(i32 %183)
%185 = tail call i32 (i8*, i8*, ...) @die(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.10, i64 0, i64 0), i8* %184)
br label %186
186: ; preds = %182, %177
%187 = load i32, i32* @SEEK_SET
%188 = tail call i64 @lseek(i32 %23, i32 494, i32 %187)
%189 = icmp slt i64 %188, 0
br i1 %189, label %190, label %194
190: ; preds = %186
%191 = load i32, i32* @errno
%192 = tail call i8* @strerror(i32 %191)
%193 = tail call i32 (i8*, i8*, ...) @die(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i8* %192)
br label %194
194: ; preds = %190, %186
%195 = icmp eq i32 %130, 4
%196 = select i1 %195, i8 -128, i8 0
store i8 %196, i8* %4
%197 = call i32 @write(i32 %23, i8* %4, i32 1)
%198 = icmp eq i32 %197, 1
br i1 %198, label %203, label %199
199: ; preds = %194
%200 = load i32, i32* @errno
%201 = tail call i8* @strerror(i32 %200)
%202 = tail call i32 (i8*, i8*, ...) @die(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.10, i64 0, i64 0), i8* %201)
br label %203
203: ; preds = %199, %194
ret i32 0
}