#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 EIO;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int errno;
int lseek(int, unsigned int, int /*<<< orphan*/);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int read(int, void *, int);
int readSector(const char *inputName, unsigned sector, int bytesPerSector,
               void *buffer) {
  int status = 0;
  int fd = 0;
  {
    fd = open(inputName, O_RDONLY);
    if (fd < 0) {
      printf("Can't open device %s\n", inputName);
      return (errno = fd);
    }
    status = lseek(fd, (sector * bytesPerSector), SEEK_SET);
    if (status < 0) {
      printf("Can't seek device to sector %u\n", sector);
      close(fd);
      return (errno = status);
    }
    status = read(fd, buffer, bytesPerSector);
    close(fd);
    if ((status < 0) || (status < bytesPerSector)) {
      printf("%s", "Can't read sector\n");
      return (errno = status = EIO);
    }
  }
  return (status = 0);
}


x86 assembly (clang, -O0):
<readSector>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    11b9 <readSector+0x69>
mov    -0x10(%rbp),%rsi
lea    0xe62(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x28(%rbp),%eax
mov    0x2e21(%rip),%rcx
mov    %eax,(%rcx)
mov    %eax,-0x4(%rbp)
jmp    127b <readSector+0x12b>
mov    -0x28(%rbp),%edi
mov    -0x14(%rbp),%esi
imul   -0x18(%rbp),%esi
mov    0x2dfe(%rip),%rax
mov    (%rax),%edx
call   1040 <lseek@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    120b <readSector+0xbb>
mov    -0x14(%rbp),%esi
lea    0xe2e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x28(%rbp),%edi
call   1050 <close@plt>
mov    -0x24(%rbp),%eax
mov    0x2dcf(%rip),%rcx
mov    %eax,(%rcx)
mov    %eax,-0x4(%rbp)
jmp    127b <readSector+0x12b>
mov    -0x28(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1060 <read@plt>
mov    %eax,-0x24(%rbp)
mov    -0x28(%rbp),%edi
call   1050 <close@plt>
cmpl   $0x0,-0x24(%rbp)
jl     123b <readSector+0xeb>
mov    -0x24(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    126d <readSector+0x11d>
lea    0xdf4(%rip),%rdi
lea    0xdf0(%rip),%rsi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2d81(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2d6d(%rip),%rcx
mov    %eax,(%rcx)
mov    %eax,-0x4(%rbp)
jmp    127b <readSector+0x12b>
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readSector>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rcx,%r15
mov    %edx,%r12d
mov    %esi,%r14d
mov    %rdi,%rbp
mov    0x2e6d(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11db <readSector+0x7b>
mov    %r12d,%esi
imul   %r14d,%esi
mov    0x2e32(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
call   1050 <lseek@plt>
test   %eax,%eax
js     11ee <readSector+0x8e>
mov    %ebx,%edi
mov    %r15,%rsi
mov    %r12d,%edx
call   1070 <read@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1060 <close@plt>
test   %ebp,%ebp
js     11c4 <readSector+0x64>
xor    %ebx,%ebx
cmp    %r12d,%ebp
jge    1213 <readSector+0xb3>
lea    0xe6b(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e01(%rip),%rax
mov    (%rax),%ebx
jmp    120a <readSector+0xaa>
lea    0xe1e(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    120a <readSector+0xaa>
mov    %eax,%ebp
lea    0xe1f(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    %ebp,%ebx
mov    0x2dbf(%rip),%rax
mov    %ebx,(%rax)
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [22 x i8] c"Can't open device %s\0A\00"
@errno = global i32 0
@SEEK_SET = global i32 0
@.str.1 = [32 x i8] c"Can't seek device to sector %u\0A\00"
@.str.2 = [3 x i8] c"%s\00"
@.str.3 = [19 x i8] c"Can't read sector\0A\00"
@EIO = global i32 0
define i32 @readSector(i8* %0, i32 %1, i32 %2, i8* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i8* %3, i8** %9
store i32 0, i32* %10
store i32 0, i32* %11
%12 = load i8*, i8** %6
%13 = load i32, i32* @O_RDONLY
%14 = call i32 @open(i8* %12, i32 %13)
store i32 %14, i32* %11
%15 = load i32, i32* %11
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %4
%18 = load i8*, i8** %6
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %18)
%20 = load i32, i32* %11
store i32 %20, i32* @errno
store i32 %20, i32* %5
br label %53
21: ; preds = %4
%22 = load i32, i32* %11
%23 = load i32, i32* %7
%24 = load i32, i32* %8
%25 = mul i32 %23, %24
%26 = load i32, i32* @SEEK_SET
%27 = call i32 @lseek(i32 %22, i32 %25, i32 %26)
store i32 %27, i32* %10
%28 = load i32, i32* %10
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %36
30: ; preds = %21
%31 = load i32, i32* %7
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i32 %31)
%33 = load i32, i32* %11
%34 = call i32 @close(i32 %33)
%35 = load i32, i32* %10
store i32 %35, i32* @errno
store i32 %35, i32* %5
br label %53
36: ; preds = %21
%37 = load i32, i32* %11
%38 = load i8*, i8** %9
%39 = load i32, i32* %8
%40 = call i32 @read(i32 %37, i8* %38, i32 %39)
store i32 %40, i32* %10
%41 = load i32, i32* %11
%42 = call i32 @close(i32 %41)
%43 = load i32, i32* %10
%44 = icmp slt i32 %43, 0
br i1 %44, label %49, label %45
45: ; preds = %36
%46 = load i32, i32* %10
%47 = load i32, i32* %8
%48 = icmp slt i32 %46, %47
br i1 %48, label %49, label %52
49: ; preds = %45, %36
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
%51 = load i32, i32* @EIO
store i32 %51, i32* %10
store i32 %51, i32* @errno
store i32 %51, i32* %5
br label %53
52: ; preds = %45
store i32 0, i32* %10
store i32 0, i32* %5
br label %53
53: ; preds = %52, %49, %30, %17
%54 = load i32, i32* %5
ret i32 %54
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [22 x i8] c"Can't open device %s\0A\00"
@errno = global i32 0
@SEEK_SET = global i32 0
@.str.1 = [32 x i8] c"Can't seek device to sector %u\0A\00"
@EIO = global i32 0
@str = [18 x i8] c"Can't read sector\00"
define i32 @readSector(i8* %0, i32 %1, i32 %2, i8* nocapture %3) {
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i8* %0, i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %4
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %27
10: ; preds = %4
%11 = mul i32 %2, %1
%12 = load i32, i32* @SEEK_SET
%13 = tail call i32 @lseek(i32 %6, i32 %11, i32 %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %10
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i32 %1)
%17 = tail call i32 @close(i32 %6)
br label %27
18: ; preds = %10
%19 = tail call i32 @read(i32 %6, i8* %3, i32 %2)
%20 = tail call i32 @close(i32 %6)
%21 = icmp slt i32 %19, 0
%22 = icmp slt i32 %19, %2
%23 = or i1 %21, %22
br i1 %23, label %24, label %29
24: ; preds = %18
%25 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
%26 = load i32, i32* @EIO
br label %27
27: ; preds = %8, %15, %24
%28 = phi i32 [ %26, %24 ], [ %13, %15 ], [ %6, %8 ]
store i32 %28, i32* @errno
br label %29
29: ; preds = %27, %18
%30 = phi i32 [ 0, %18 ], [ %28, %27 ]
ret i32 %30
}