#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 {
  scalar_t__ st_size;
  int /*<<< orphan*/ st_mode;
};
typedef int ssize_t;
typedef scalar_t__ off_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ S_ISREG(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ error_reason(char *);
int /*<<< orphan*/ free(void *);
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, size_t);
void *misc_malloc(size_t);
scalar_t__ misc_read(int, void *, size_t);
int open(char const *, int /*<<< orphan*/);
scalar_t__ stat(char const *, struct stat *);
char *strerror(int /*<<< orphan*/);
int write(int, void *, size_t);
int write_tapefile(int fd, const char *filename, size_t blocksize) {
  struct stat stats;
  ssize_t written;
  off_t offset;
  size_t chunk;
  void *buffer;
  int read_fd;
  if (stat(filename, &stats)) {
    error_reason(strerror(errno));
    return -1;
  }
  if (!S_ISREG(stats.st_mode)) {
    error_reason("Not a regular file");
    return -1;
  }
  buffer = misc_malloc(blocksize);
  if (buffer == NULL)
    return -1;
  read_fd = open(filename, O_RDONLY);
  if (fd == -1) {
    error_reason(strerror(errno));
    free(buffer);
    return -1;
  }
  for (offset = 0; offset < stats.st_size; offset += chunk) {
    chunk = stats.st_size - offset;
    if (chunk > blocksize)
      chunk = blocksize;
    else
      memset(buffer, 0, blocksize);
    if (misc_read(read_fd, buffer, chunk)) {
      close(read_fd);
      free(buffer);
      return -1;
    }
    written = write(fd, buffer, chunk);
    if (written != (ssize_t)chunk) {
      if (written == -1)
        error_reason(strerror(errno));
      else
        error_reason("Write error");
      close(read_fd);
      free(buffer);
      return -1;
    }
  }
  close(read_fd);
  free(buffer);
  return 0;
}


x86 assembly (clang, -O0):
<write_tapefile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
je     11fc <write_tapefile+0x4c>
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    %rax,%rdi
call   1030 <error_reason@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13b4 <write_tapefile+0x204>
mov    -0x20(%rbp),%edi
call   1090 <S_ISREG@plt>
cmp    $0x0,%eax
jne    1225 <write_tapefile+0x75>
lea    0xdec(%rip),%rdi
call   1030 <error_reason@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13b4 <write_tapefile+0x204>
mov    -0x18(%rbp),%rdi
call   10a0 <misc_malloc@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    1249 <write_tapefile+0x99>
movl   $0xffffffff,-0x4(%rbp)
jmp    13b4 <write_tapefile+0x204>
mov    -0x10(%rbp),%rdi
mov    0x2d94(%rip),%rax
mov    (%rax),%esi
call   10b0 <open@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1293 <write_tapefile+0xe3>
mov    0x2d69(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    %rax,%rdi
call   1030 <error_reason@plt>
mov    -0x48(%rbp),%rdi
call   1040 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13b4 <write_tapefile+0x204>
movq   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x28(%rbp),%rax
jge    139c <write_tapefile+0x1ec>
mov    -0x28(%rbp),%rax
sub    -0x38(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
cmp    -0x18(%rbp),%rax
jbe    12d0 <write_tapefile+0x120>
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
jmp    12df <write_tapefile+0x12f>
mov    -0x48(%rbp),%rdi
mov    -0x18(%rbp),%rdx
xor    %esi,%esi
call   1060 <memset@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rsi
mov    -0x40(%rbp),%rdx
call   10d0 <misc_read@plt>
cmp    $0x0,%rax
je     1316 <write_tapefile+0x166>
mov    -0x4c(%rbp),%edi
call   1070 <close@plt>
mov    -0x48(%rbp),%rdi
call   1040 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13b4 <write_tapefile+0x204>
mov    -0x8(%rbp),%edi
mov    -0x48(%rbp),%rsi
mov    -0x40(%rbp),%rdx
call   1050 <write@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    -0x40(%rbp),%rcx
cmp    %ecx,%eax
je     1386 <write_tapefile+0x1d6>
cmpl   $0xffffffff,-0x2c(%rbp)
jne    135d <write_tapefile+0x1ad>
mov    0x2c8f(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    %rax,%rdi
call   1030 <error_reason@plt>
jmp    1369 <write_tapefile+0x1b9>
lea    0xcaf(%rip),%rdi
call   1030 <error_reason@plt>
mov    -0x4c(%rbp),%edi
call   1070 <close@plt>
mov    -0x48(%rbp),%rdi
call   1040 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13b4 <write_tapefile+0x204>
jmp    138b <write_tapefile+0x1db>
mov    -0x40(%rbp),%rax
add    -0x38(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    129b <write_tapefile+0xeb>
mov    -0x4c(%rbp),%edi
call   1070 <close@plt>
mov    -0x48(%rbp),%rdi
call   1040 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_tapefile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%r15d
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1080 <stat@plt>
test   %rax,%rax
je     1205 <write_tapefile+0x55>
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    %rax,%rdi
call   1030 <error_reason@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x10(%rsp),%edi
call   1090 <S_ISREG@plt>
test   %eax,%eax
je     12c6 <write_tapefile+0x116>
mov    %r14,%rdi
call   10a0 <misc_malloc@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
je     11f4 <write_tapefile+0x44>
mov    %rax,%r13
mov    0x2db6(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   10b0 <open@plt>
mov    %eax,0x4(%rsp)
cmp    $0xffffffff,%r15d
je     12d2 <write_tapefile+0x122>
mov    0x8(%rsp),%rbx
test   %rbx,%rbx
jle    12ae <write_tapefile+0xfe>
xor    %r12d,%r12d
nopw   0x0(%rax,%rax,1)
sub    %r12,%rbx
mov    %r14,%rbp
cmp    %r14,%rbx
ja     127b <write_tapefile+0xcb>
mov    %r13,%rdi
xor    %esi,%esi
mov    %r14,%rdx
call   1060 <memset@plt>
mov    %rbx,%rbp
mov    0x4(%rsp),%edi
mov    %r13,%rsi
mov    %rbp,%rdx
call   10d0 <misc_read@plt>
test   %rax,%rax
jne    12ea <write_tapefile+0x13a>
mov    %r15d,%edi
mov    %r13,%rsi
mov    %rbp,%rdx
call   1050 <write@plt>
cmp    %ebp,%eax
jne    1300 <write_tapefile+0x150>
add    %rbp,%r12
mov    0x8(%rsp),%rbx
cmp    %r12,%rbx
jg     1260 <write_tapefile+0xb0>
mov    0x4(%rsp),%edi
call   1070 <close@plt>
mov    %r13,%rdi
call   1040 <free@plt>
xor    %ebp,%ebp
jmp    11f4 <write_tapefile+0x44>
lea    0xd33(%rip),%rdi
jmp    11ea <write_tapefile+0x3a>
mov    0x2cff(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    %rax,%rdi
call   1030 <error_reason@plt>
jmp    1332 <write_tapefile+0x182>
mov    0x4(%rsp),%edi
call   1070 <close@plt>
mov    %r13,%rdi
call   1040 <free@plt>
jmp    11ef <write_tapefile+0x3f>
cmp    $0xffffffff,%eax
je     130e <write_tapefile+0x15e>
lea    0xd07(%rip),%rdi
jmp    131f <write_tapefile+0x16f>
mov    0x2cc3(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    %rax,%rdi
mov    $0xffffffff,%ebp
call   1030 <error_reason@plt>
mov    0x4(%rsp),%edi
call   1070 <close@plt>
mov    %r13,%rdi
call   1040 <free@plt>
jmp    11f4 <write_tapefile+0x44>

LLVM IR (-O0):
%struct.stat = type { i64, i32 }
@errno = global i32 0
@.str = [19 x i8] c"Not a regular file\00"
@O_RDONLY = global i32 0
@.str.1 = [12 x i8] c"Write error\00"
define i32 @write_tapefile(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca %struct.stat
%9 = alloca i32
%10 = alloca i64
%11 = alloca i64
%12 = alloca i8*
%13 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%14 = load i8*, i8** %6
%15 = call i64 @stat(i8* %14, %struct.stat* %8)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %3
%18 = load i32, i32* @errno
%19 = call i8* @strerror(i32 %18)
%20 = call i32 @error_reason(i8* %19)
store i32 -1, i32* %4
br label %110
21: ; preds = %3
%22 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 1
%23 = load i32, i32* %22
%24 = call i32 @S_ISREG(i32 %23)
%25 = icmp ne i32 %24, 0
br i1 %25, label %28, label %26
26: ; preds = %21
%27 = call i32 @error_reason(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %110
28: ; preds = %21
%29 = load i64, i64* %7
%30 = call i8* @misc_malloc(i64 %29)
store i8* %30, i8** %12
%31 = load i8*, i8** %12
%32 = icmp eq i8* %31, null
br i1 %32, label %33, label %34
33: ; preds = %28
store i32 -1, i32* %4
br label %110
34: ; preds = %28
%35 = load i8*, i8** %6
%36 = load i32, i32* @O_RDONLY
%37 = call i32 @open(i8* %35, i32 %36)
store i32 %37, i32* %13
%38 = load i32, i32* %5
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %46
40: ; preds = %34
%41 = load i32, i32* @errno
%42 = call i8* @strerror(i32 %41)
%43 = call i32 @error_reason(i8* %42)
%44 = load i8*, i8** %12
%45 = call i32 @free(i8* %44)
store i32 -1, i32* %4
br label %110
46: ; preds = %34
store i64 0, i64* %10
br label %47
47: ; preds = %101, %46
%48 = load i64, i64* %10
%49 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%50 = load i64, i64* %49
%51 = icmp slt i64 %48, %50
br i1 %51, label %52, label %105
52: ; preds = %47
%53 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%54 = load i64, i64* %53
%55 = load i64, i64* %10
%56 = sub nsw i64 %54, %55
store i64 %56, i64* %11
%57 = load i64, i64* %11
%58 = load i64, i64* %7
%59 = icmp ugt i64 %57, %58
br i1 %59, label %60, label %62
60: ; preds = %52
%61 = load i64, i64* %7
store i64 %61, i64* %11
br label %66
62: ; preds = %52
%63 = load i8*, i8** %12
%64 = load i64, i64* %7
%65 = call i32 @memset(i8* %63, i32 0, i64 %64)
br label %66
66: ; preds = %62, %60
%67 = load i32, i32* %13
%68 = load i8*, i8** %12
%69 = load i64, i64* %11
%70 = call i64 @misc_read(i32 %67, i8* %68, i64 %69)
%71 = icmp ne i64 %70, 0
br i1 %71, label %72, label %77
72: ; preds = %66
%73 = load i32, i32* %13
%74 = call i32 @close(i32 %73)
%75 = load i8*, i8** %12
%76 = call i32 @free(i8* %75)
store i32 -1, i32* %4
br label %110
77: ; preds = %66
%78 = load i32, i32* %5
%79 = load i8*, i8** %12
%80 = load i64, i64* %11
%81 = call i32 @write(i32 %78, i8* %79, i64 %80)
store i32 %81, i32* %9
%82 = load i32, i32* %9
%83 = load i64, i64* %11
%84 = trunc i64 %83 to i32
%85 = icmp ne i32 %82, %84
br i1 %85, label %86, label %100
86: ; preds = %77
%87 = load i32, i32* %9
%88 = icmp eq i32 %87, -1
br i1 %88, label %89, label %93
89: ; preds = %86
%90 = load i32, i32* @errno
%91 = call i8* @strerror(i32 %90)
%92 = call i32 @error_reason(i8* %91)
br label %95
93: ; preds = %86
%94 = call i32 @error_reason(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %95
95: ; preds = %93, %89
%96 = load i32, i32* %13
%97 = call i32 @close(i32 %96)
%98 = load i8*, i8** %12
%99 = call i32 @free(i8* %98)
store i32 -1, i32* %4
br label %110
100: ; preds = %77
br label %101
101: ; preds = %100
%102 = load i64, i64* %11
%103 = load i64, i64* %10
%104 = add i64 %103, %102
store i64 %104, i64* %10
br label %47
105: ; preds = %47
%106 = load i32, i32* %13
%107 = call i32 @close(i32 %106)
%108 = load i8*, i8** %12
%109 = call i32 @free(i8* %108)
store i32 0, i32* %4
br label %110
110: ; preds = %105, %95, %72, %40, %33, %26, %17
%111 = load i32, i32* %4
ret i32 %111
}

LLVM IR (-O2):
%struct.stat = type { i64, i32 }
@errno = global i32 0
@.str = [19 x i8] c"Not a regular file\00"
@O_RDONLY = global i32 0
@.str.1 = [12 x i8] c"Write error\00"
define i32 @write_tapefile(i32 %0, i8* %1, i64 %2) {
%4 = alloca %struct.stat
%5 = bitcast %struct.stat* %4 to i8*
%6 = call i64 @stat(i8* %1, %struct.stat* %4)
%7 = icmp eq i64 %6, 0
br i1 %7, label %12, label %8
8: ; preds = %3
%9 = load i32, i32* @errno
%10 = call i8* @strerror(i32 %9)
%11 = call i32 @error_reason(i8* %10)
br label %70
12: ; preds = %3
%13 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 1
%14 = load i32, i32* %13
%15 = call i32 @S_ISREG(i32 %14)
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %12
%18 = call i32 @error_reason(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %70
19: ; preds = %12
%20 = call i8* @misc_malloc(i64 %2)
%21 = icmp eq i8* %20, null
br i1 %21, label %70, label %22
22: ; preds = %19
%23 = load i32, i32* @O_RDONLY
%24 = call i32 @open(i8* %1, i32 %23)
%25 = icmp eq i32 %0, -1
br i1 %25, label %30, label %26
26: ; preds = %22
%27 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%28 = load i64, i64* %27
%29 = icmp sgt i64 %28, 0
br i1 %29, label %38, label %67
30: ; preds = %22
%31 = load i32, i32* @errno
%32 = call i8* @strerror(i32 %31)
%33 = call i32 @error_reason(i8* %32)
%34 = call i32 @free(i8* %20)
br label %70
35: ; preds = %52
%36 = load i64, i64* %27
%37 = icmp sgt i64 %36, %56
br i1 %37, label %38, label %67
38: ; preds = %26, %35
%39 = phi i64 [ %36, %35 ], [ %28, %26 ]
%40 = phi i64 [ %56, %35 ], [ 0, %26 ]
%41 = sub nsw i64 %39, %40
%42 = icmp ugt i64 %41, %2
br i1 %42, label %45, label %43
43: ; preds = %38
%44 = call i32 @memset(i8* %20, i32 0, i64 %2)
br label %45
45: ; preds = %38, %43
%46 = phi i64 [ %41, %43 ], [ %2, %38 ]
%47 = call i64 @misc_read(i32 %24, i8* %20, i64 %46)
%48 = icmp eq i64 %47, 0
br i1 %48, label %52, label %49
49: ; preds = %45
%50 = call i32 @close(i32 %24)
%51 = call i32 @free(i8* %20)
br label %70
52: ; preds = %45
%53 = call i32 @write(i32 %0, i8* %20, i64 %46)
%54 = trunc i64 %46 to i32
%55 = icmp eq i32 %53, %54
%56 = add i64 %46, %40
br i1 %55, label %35, label %57
57: ; preds = %52
%58 = icmp eq i32 %53, -1
br i1 %58, label %59, label %62
59: ; preds = %57
%60 = load i32, i32* @errno
%61 = call i8* @strerror(i32 %60)
br label %62
62: ; preds = %57, %59
%63 = phi i8* [ %61, %59 ], [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), %57 ]
%64 = call i32 @error_reason(i8* %63)
%65 = call i32 @close(i32 %24)
%66 = call i32 @free(i8* %20)
br label %70
67: ; preds = %35, %26
%68 = call i32 @close(i32 %24)
%69 = call i32 @free(i8* %20)
br label %70
70: ; preds = %19, %67, %62, %49, %30, %17, %8
%71 = phi i32 [ -1, %8 ], [ -1, %30 ], [ -1, %49 ], [ -1, %62 ], [ 0, %67 ], [ -1, %17 ], [ -1, %19 ]
ret i32 %71
}