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


x86 assembly (clang, -O0):
<writeSector>:
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    0x2e39(%rip),%rax
mov    (%rax),%esi
mov    0x2e60(%rip),%rax
or     (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    11c2 <writeSector+0x72>
mov    -0x10(%rbp),%rsi
lea    0xe59(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%eax
mov    0x2e18(%rip),%rcx
mov    %eax,(%rcx)
mov    %eax,-0x4(%rbp)
jmp    1284 <writeSector+0x134>
mov    -0x28(%rbp),%edi
mov    -0x14(%rbp),%esi
imul   -0x18(%rbp),%esi
mov    0x2df5(%rip),%rax
mov    (%rax),%edx
call   1050 <lseek@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1214 <writeSector+0xc4>
mov    -0x14(%rbp),%esi
lea    0xe25(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%edi
call   1060 <close@plt>
mov    -0x24(%rbp),%eax
mov    0x2dc6(%rip),%rcx
mov    %eax,(%rcx)
mov    %eax,-0x4(%rbp)
jmp    1284 <writeSector+0x134>
mov    -0x28(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x24(%rbp)
mov    -0x28(%rbp),%edi
call   1060 <close@plt>
cmpl   $0x0,-0x24(%rbp)
jl     1244 <writeSector+0xf4>
mov    -0x24(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    1276 <writeSector+0x126>
lea    0xdeb(%rip),%rdi
lea    0xde7(%rip),%rsi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2d78(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2d64(%rip),%rcx
mov    %eax,(%rcx)
mov    %eax,-0x4(%rbp)
jmp    1284 <writeSector+0x134>
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writeSector>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rcx,%r15
mov    %edx,%r12d
mov    %esi,%r14d
mov    %rdi,%rbp
mov    0x2e3d(%rip),%rax
mov    0x2e66(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1080 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11e4 <writeSector+0x84>
mov    %r12d,%esi
imul   %r14d,%esi
mov    0x2e29(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
call   1060 <lseek@plt>
test   %eax,%eax
js     11f7 <writeSector+0x97>
mov    %ebx,%edi
mov    %r15,%rsi
mov    %r12d,%edx
call   1040 <write@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1070 <close@plt>
test   %ebp,%ebp
js     11cd <writeSector+0x6d>
xor    %ebx,%ebx
cmp    %r12d,%ebp
jge    121c <writeSector+0xbc>
lea    0xe62(%rip),%rdi
call   1030 <puts@plt>
mov    0x2df8(%rip),%rax
mov    (%rax),%ebx
jmp    1213 <writeSector+0xb3>
lea    0xe15(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1213 <writeSector+0xb3>
mov    %eax,%ebp
lea    0xe16(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%edi
call   1070 <close@plt>
mov    %ebp,%ebx
mov    0x2db6(%rip),%rax
mov    %ebx,(%rax)
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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