#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 */
typedef int /*<<< orphan*/ uint8_t;
typedef scalar_t__ ssize_t;
typedef scalar_t__ off_t;

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ memset(int /*<<< orphan*/ *, int /*<<< orphan*/, int);
scalar_t__ read(int, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
scalar_t__ write(int, int /*<<< orphan*/ *, size_t);
void writefile(int infd, int outfd, size_t len, size_t padlen) {
  off_t offset;
  uint8_t buffer[1024];
  ssize_t nbytesread;
  ssize_t nbyteswritten;
  size_t totalread;
  offset = lseek(infd, 0, SEEK_SET);
  if (offset == (off_t)-1) {
    fprintf(stderr, "lseek failed: %s\n", strerror(errno));
    exit(4);
  }
  for (totalread = 0; totalread < len; totalread += nbytesread) {
    nbytesread = read(infd, buffer, 1024);
    if (nbytesread < 0) {
      fprintf(stderr, "read failed: %s\n", strerror(errno));
      exit(4);
    } else if (nbytesread == 0) {
      fprintf(stderr, "Unexpected end-of-file: %s\n", strerror(errno));
      exit(4);
    } else {
      nbyteswritten = write(outfd, buffer, nbytesread);
      if (nbyteswritten < 0) {
        fprintf(stderr, "write failed: %s\n", strerror(errno));
        exit(4);
      } else if (nbyteswritten != nbytesread) {
        fprintf(stderr, "Short writes not handled\n");
        exit(4);
      }
    }
  }
  memset(buffer, 0, 1024);
  nbyteswritten = write(outfd, buffer, padlen);
  if (nbyteswritten < 0) {
    fprintf(stderr, "write failed: %s\n", strerror(errno));
    exit(4);
  } else if (nbyteswritten != padlen) {
    fprintf(stderr, "Short writes not handled\n");
    exit(4);
  }
}


x86 assembly (clang, -O0):
<writefile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1050,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e3d(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0xffffffffffffffff,-0x20(%rbp)
jne    11e9 <writefile+0x79>
mov    0x2e2e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x103c(%rbp)
mov    0x2e17(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x103c(%rbp),%edi
mov    %rax,%rdx
lea    0xe28(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x4,%edi
call   1080 <exit@plt>
movq   $0x0,-0x1038(%rbp)
mov    -0x1038(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    1384 <writefile+0x214>
mov    -0x4(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x400,%edx
call   1060 <read@plt>
mov    %rax,-0x1028(%rbp)
cmpq   $0x0,-0x1028(%rbp)
jge    126c <writefile+0xfc>
mov    0x2dab(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1040(%rbp)
mov    0x2d94(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x1040(%rbp),%edi
mov    %rax,%rdx
lea    0xdb7(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x4,%edi
call   1080 <exit@plt>
cmpq   $0x0,-0x1028(%rbp)
jne    12b8 <writefile+0x148>
mov    0x2d5f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1044(%rbp)
mov    0x2d48(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x1044(%rbp),%edi
mov    %rax,%rdx
lea    0xd7c(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x4,%edi
call   1080 <exit@plt>
mov    -0x8(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    -0x1028(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x1030(%rbp)
cmpq   $0x0,-0x1030(%rbp)
jge    1321 <writefile+0x1b1>
mov    0x2cf6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1048(%rbp)
mov    0x2cdf(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x1048(%rbp),%edi
mov    %rax,%rdx
lea    0xd2f(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x4,%edi
call   1080 <exit@plt>
mov    -0x1030(%rbp),%rax
cmp    -0x1028(%rbp),%rax
je     1356 <writefile+0x1e6>
mov    0x2ca4(%rip),%rax
mov    (%rax),%edi
lea    0xd0c(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x4,%edi
call   1080 <exit@plt>
jmp    135b <writefile+0x1eb>
jmp    1360 <writefile+0x1f0>
jmp    1365 <writefile+0x1f5>
jmp    136a <writefile+0x1fa>
mov    -0x1028(%rbp),%rax
add    -0x1038(%rbp),%rax
mov    %rax,-0x1038(%rbp)
jmp    11f4 <writefile+0x84>
lea    -0x1020(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
mov    -0x8(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x1030(%rbp)
cmpq   $0x0,-0x1030(%rbp)
jge    13fd <writefile+0x28d>
mov    0x2c1a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x104c(%rbp)
mov    0x2c03(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x104c(%rbp),%edi
mov    %rax,%rdx
lea    0xc53(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x4,%edi
call   1080 <exit@plt>
mov    -0x1030(%rbp),%rax
cmp    -0x18(%rbp),%rax
je     142f <writefile+0x2bf>
mov    0x2bcb(%rip),%rax
mov    (%rax),%edi
lea    0xc33(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x4,%edi
call   1080 <exit@plt>
jmp    1434 <writefile+0x2c4>
add    $0x1050,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writefile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1018,%rsp
mov    %rcx,%rbx
mov    %rdx,%r12
mov    %esi,%r15d
mov    %edi,%r13d
mov    0x2e3c(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
cmp    $0xffffffffffffffff,%rax
je     12da <writefile+0x16a>
mov    %rbx,0x8(%rsp)
test   %r12,%r12
je     1202 <writefile+0x92>
xor    %r14d,%r14d
lea    0x10(%rsp),%rbx
nopl   0x0(%rax)
mov    %r13d,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1060 <read@plt>
test   %rax,%rax
js     1286 <writefile+0x116>
je     12a6 <writefile+0x136>
mov    %rax,%rbp
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1030 <write@plt>
test   %rax,%rax
js     1245 <writefile+0xd5>
cmp    %rbp,%rax
jne    1265 <writefile+0xf5>
add    %rbp,%r14
cmp    %r12,%r14
jb     11c0 <writefile+0x50>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    0x8(%rsp),%rbx
mov    %rbx,%rdx
call   1030 <write@plt>
test   %rax,%rax
js     1245 <writefile+0xd5>
cmp    %rbx,%rax
jne    1265 <writefile+0xf5>
add    $0x1018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d94(%rip),%rax
mov    (%rax),%ebx
mov    0x2d83(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xddc(%rip),%rsi
jmp    12c4 <writefile+0x154>
mov    0x2d74(%rip),%rax
mov    (%rax),%edi
lea    0xddc(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x4,%edi
call   1080 <exit@plt>
mov    0x2d53(%rip),%rax
mov    (%rax),%ebx
mov    0x2d42(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xd6e(%rip),%rsi
jmp    12c4 <writefile+0x154>
mov    0x2d33(%rip),%rax
mov    (%rax),%ebx
mov    0x2d22(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xd5f(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x4,%edi
call   1080 <exit@plt>
mov    0x2cff(%rip),%rax
mov    (%rax),%ebx
mov    0x2cee(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xd08(%rip),%rsi
jmp    12c4 <writefile+0x154>

LLVM IR (-O0):
@SEEK_SET = global i32 0
@stderr = global i32 0
@.str = [18 x i8] c"lseek failed: %s\0A\00"
@errno = global i32 0
@.str.1 = [17 x i8] c"read failed: %s\0A\00"
@.str.2 = [28 x i8] c"Unexpected end-of-file: %s\0A\00"
@.str.3 = [18 x i8] c"write failed: %s\0A\00"
@.str.4 = [26 x i8] c"Short writes not handled\0A\00"
define void @writefile(i32 %0, i32 %1, i64 %2, i64 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
%10 = alloca [1024 x i32]
%11 = alloca i64
%12 = alloca i64
%13 = alloca i64
store i32 %0, i32* %5
store i32 %1, i32* %6
store i64 %2, i64* %7
store i64 %3, i64* %8
%14 = load i32, i32* %5
%15 = load i32, i32* @SEEK_SET
%16 = call i64 @lseek(i32 %14, i32 0, i32 %15)
store i64 %16, i64* %9
%17 = load i64, i64* %9
%18 = icmp eq i64 %17, -1
br i1 %18, label %19, label %25
19: ; preds = %4
%20 = load i32, i32* @stderr
%21 = load i32, i32* @errno
%22 = call i8* @strerror(i32 %21)
%23 = call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %22)
%24 = call i32 @exit(i32 4)
unreachable
25: ; preds = %4
store i64 0, i64* %13
br label %26
26: ; preds = %76, %25
%27 = load i64, i64* %13
%28 = load i64, i64* %7
%29 = icmp ult i64 %27, %28
br i1 %29, label %30, label %80
30: ; preds = %26
%31 = load i32, i32* %5
%32 = getelementptr inbounds [1024 x i32], [1024 x i32]* %10, i64 0, i64 0
%33 = call i64 @read(i32 %31, i32* %32, i32 1024)
store i64 %33, i64* %11
%34 = load i64, i64* %11
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %42
36: ; preds = %30
%37 = load i32, i32* @stderr
%38 = load i32, i32* @errno
%39 = call i8* @strerror(i32 %38)
%40 = call i32 (i32, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %39)
%41 = call i32 @exit(i32 4)
unreachable
42: ; preds = %30
%43 = load i64, i64* %11
%44 = icmp eq i64 %43, 0
br i1 %44, label %45, label %51
45: ; preds = %42
%46 = load i32, i32* @stderr
%47 = load i32, i32* @errno
%48 = call i8* @strerror(i32 %47)
%49 = call i32 (i32, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %48)
%50 = call i32 @exit(i32 4)
unreachable
51: ; preds = %42
%52 = load i32, i32* %6
%53 = getelementptr inbounds [1024 x i32], [1024 x i32]* %10, i64 0, i64 0
%54 = load i64, i64* %11
%55 = call i64 @write(i32 %52, i32* %53, i64 %54)
store i64 %55, i64* %12
%56 = load i64, i64* %12
%57 = icmp slt i64 %56, 0
br i1 %57, label %58, label %64
58: ; preds = %51
%59 = load i32, i32* @stderr
%60 = load i32, i32* @errno
%61 = call i8* @strerror(i32 %60)
%62 = call i32 (i32, i8*, ...) @fprintf(i32 %59, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* %61)
%63 = call i32 @exit(i32 4)
unreachable
64: ; preds = %51
%65 = load i64, i64* %12
%66 = load i64, i64* %11
%67 = icmp ne i64 %65, %66
br i1 %67, label %68, label %72
68: ; preds = %64
%69 = load i32, i32* @stderr
%70 = call i32 (i32, i8*, ...) @fprintf(i32 %69, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
%71 = call i32 @exit(i32 4)
unreachable
72: ; preds = %64
br label %73
73: ; preds = %72
br label %74
74: ; preds = %73
br label %75
75: ; preds = %74
br label %76
76: ; preds = %75
%77 = load i64, i64* %11
%78 = load i64, i64* %13
%79 = add i64 %78, %77
store i64 %79, i64* %13
br label %26
80: ; preds = %26
%81 = getelementptr inbounds [1024 x i32], [1024 x i32]* %10, i64 0, i64 0
%82 = call i32 @memset(i32* %81, i32 0, i32 1024)
%83 = load i32, i32* %6
%84 = getelementptr inbounds [1024 x i32], [1024 x i32]* %10, i64 0, i64 0
%85 = load i64, i64* %8
%86 = call i64 @write(i32 %83, i32* %84, i64 %85)
store i64 %86, i64* %12
%87 = load i64, i64* %12
%88 = icmp slt i64 %87, 0
br i1 %88, label %89, label %95
89: ; preds = %80
%90 = load i32, i32* @stderr
%91 = load i32, i32* @errno
%92 = call i8* @strerror(i32 %91)
%93 = call i32 (i32, i8*, ...) @fprintf(i32 %90, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* %92)
%94 = call i32 @exit(i32 4)
unreachable
95: ; preds = %80
%96 = load i64, i64* %12
%97 = load i64, i64* %8
%98 = icmp ne i64 %96, %97
br i1 %98, label %99, label %103
99: ; preds = %95
%100 = load i32, i32* @stderr
%101 = call i32 (i32, i8*, ...) @fprintf(i32 %100, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
%102 = call i32 @exit(i32 4)
unreachable
103: ; preds = %95
br label %104
104: ; preds = %103
ret void
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
@stderr = global i32 0
@.str = [18 x i8] c"lseek failed: %s\0A\00"
@errno = global i32 0
@.str.1 = [17 x i8] c"read failed: %s\0A\00"
@.str.2 = [28 x i8] c"Unexpected end-of-file: %s\0A\00"
@.str.3 = [18 x i8] c"write failed: %s\0A\00"
@.str.4 = [26 x i8] c"Short writes not handled\0A\00"
define void @writefile(i32 %0, i32 %1, i64 %2, i64 %3) {
%5 = alloca [1024 x i32]
%6 = bitcast [1024 x i32]* %5 to i8*
%7 = load i32, i32* @SEEK_SET
%8 = tail call i64 @lseek(i32 %0, i32 0, i32 %7)
%9 = icmp eq i64 %8, -1
br i1 %9, label %14, label %10
10: ; preds = %4
%11 = icmp eq i64 %2, 0
br i1 %11, label %56, label %12
12: ; preds = %10
%13 = getelementptr inbounds [1024 x i32], [1024 x i32]* %5, i64 0, i64 0
br label %22
14: ; preds = %4
%15 = load i32, i32* @stderr
%16 = load i32, i32* @errno
%17 = tail call i8* @strerror(i32 %16)
%18 = tail call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = tail call i32 @exit(i32 4)
unreachable
20: ; preds = %49
%21 = icmp ult i64 %51, %2
br i1 %21, label %22, label %56
22: ; preds = %12, %20
%23 = phi i64 [ 0, %12 ], [ %51, %20 ]
%24 = call i64 @read(i32 %0, i32* %13, i32 1024)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %32
26: ; preds = %22
%27 = load i32, i32* @stderr
%28 = load i32, i32* @errno
%29 = tail call i8* @strerror(i32 %28)
%30 = tail call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %29)
%31 = tail call i32 @exit(i32 4)
unreachable
32: ; preds = %22
%33 = icmp eq i64 %24, 0
br i1 %33, label %34, label %40
34: ; preds = %32
%35 = load i32, i32* @stderr
%36 = load i32, i32* @errno
%37 = tail call i8* @strerror(i32 %36)
%38 = tail call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %37)
%39 = tail call i32 @exit(i32 4)
unreachable
40: ; preds = %32
%41 = call i64 @write(i32 %1, i32* %13, i64 %24)
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %49
43: ; preds = %40
%44 = load i32, i32* @stderr
%45 = load i32, i32* @errno
%46 = tail call i8* @strerror(i32 %45)
%47 = tail call i32 (i32, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* %46)
%48 = tail call i32 @exit(i32 4)
unreachable
49: ; preds = %40
%50 = icmp eq i64 %41, %24
%51 = add i64 %24, %23
br i1 %50, label %20, label %52
52: ; preds = %49
%53 = load i32, i32* @stderr
%54 = tail call i32 (i32, i8*, ...) @fprintf(i32 %53, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
%55 = tail call i32 @exit(i32 4)
unreachable
56: ; preds = %20, %10
%57 = getelementptr inbounds [1024 x i32], [1024 x i32]* %5, i64 0, i64 0
%58 = call i32 @memset(i32* %57, i32 0, i32 1024)
%59 = call i64 @write(i32 %1, i32* %57, i64 %3)
%60 = icmp slt i64 %59, 0
br i1 %60, label %61, label %67
61: ; preds = %56
%62 = load i32, i32* @stderr
%63 = load i32, i32* @errno
%64 = call i8* @strerror(i32 %63)
%65 = call i32 (i32, i8*, ...) @fprintf(i32 %62, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* %64)
%66 = call i32 @exit(i32 4)
unreachable
67: ; preds = %56
%68 = icmp eq i64 %59, %3
br i1 %68, label %73, label %69
69: ; preds = %67
%70 = load i32, i32* @stderr
%71 = call i32 (i32, i8*, ...) @fprintf(i32 %70, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
%72 = call i32 @exit(i32 4)
unreachable
73: ; preds = %67
ret void
}