#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 ssize_t;

/* Variables and functions */
int /*<<< orphan*/ errno;
int /*<<< orphan*/ error_reason(char *);
int /*<<< orphan*/ free(void *);
int /*<<< orphan*/ memcpy(void *, void *, size_t);
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, size_t);
void *misc_malloc(size_t);
char *strerror(int /*<<< orphan*/);
int write(int, void *, size_t);
int write_tapebuffer(int fd, const char *data, size_t size, size_t blocksize) {
  ssize_t written;
  size_t offset;
  size_t chunk;
  void *buffer;
  buffer = misc_malloc(blocksize);
  if (buffer == NULL)
    return -1;
  for (offset = 0; offset < size; offset += chunk) {
    chunk = size - offset;
    if (chunk > blocksize)
      chunk = blocksize;
    else
      memset(buffer, 0, blocksize);
    memcpy(buffer, ((void *)(((unsigned long)data) + ((unsigned long)offset))),
           chunk);
    written = write(fd, buffer, chunk);
    if (written != (ssize_t)chunk) {
      if (written == -1)
        error_reason(strerror(errno));
      else
        error_reason("Write error");
      free(buffer);
      return -1;
    }
  }
  free(buffer);
  return 0;
}


x86 assembly (clang, -O0):
<write_tapebuffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1080 <misc_malloc@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    11ab <write_tapebuffer+0x3b>
movl   $0xffffffff,-0x4(%rbp)
jmp    129a <write_tapebuffer+0x12a>
movq   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    128a <write_tapebuffer+0x11a>
mov    -0x18(%rbp),%rax
sub    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x20(%rbp),%rax
jbe    11e8 <write_tapebuffer+0x78>
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    11f7 <write_tapebuffer+0x87>
mov    -0x40(%rbp),%rdi
mov    -0x20(%rbp),%rdx
xor    %esi,%esi
call   1060 <memset@plt>
mov    -0x40(%rbp),%rdi
mov    -0x10(%rbp),%rsi
add    -0x30(%rbp),%rsi
mov    -0x38(%rbp),%rdx
call   1070 <memcpy@plt>
mov    -0x8(%rbp),%edi
mov    -0x40(%rbp),%rsi
mov    -0x38(%rbp),%rdx
call   1050 <write@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    -0x38(%rbp),%rcx
cmp    %ecx,%eax
je     1274 <write_tapebuffer+0x104>
cmpl   $0xffffffff,-0x24(%rbp)
jne    1253 <write_tapebuffer+0xe3>
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %rax,%rdi
call   1030 <error_reason@plt>
jmp    125f <write_tapebuffer+0xef>
lea    0xda6(%rip),%rdi
call   1030 <error_reason@plt>
mov    -0x40(%rbp),%rdi
call   1040 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    129a <write_tapebuffer+0x12a>
jmp    1279 <write_tapebuffer+0x109>
mov    -0x38(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    11b3 <write_tapebuffer+0x43>
mov    -0x40(%rbp),%rdi
call   1040 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_tapebuffer>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%rbp
mov    %rdx,%r13
mov    %rsi,0x10(%rsp)
mov    %edi,0xc(%rsp)
mov    %rcx,%rdi
call   1080 <misc_malloc@plt>
test   %rax,%rax
je     1201 <write_tapebuffer+0x91>
mov    %rax,%r14
test   %r13,%r13
je     11fd <write_tapebuffer+0x8d>
xor    %r12d,%r12d
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r13,%r15
sub    %r12,%r15
mov    %rbp,%rbx
cmp    %rbp,%r15
ja     11ce <write_tapebuffer+0x5e>
mov    %r14,%rdi
xor    %esi,%esi
mov    %rbp,%rdx
call   1060 <memset@plt>
mov    %r15,%rbx
mov    0x10(%rsp),%rax
lea    (%r12,%rax,1),%rsi
mov    %r14,%rdi
mov    %rbx,%rdx
call   1070 <memcpy@plt>
mov    0xc(%rsp),%edi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1050 <write@plt>
cmp    %ebx,%eax
jne    1208 <write_tapebuffer+0x98>
add    %rbx,%r12
cmp    %r13,%r12
jb     11b0 <write_tapebuffer+0x40>
xor    %ebx,%ebx
jmp    1231 <write_tapebuffer+0xc1>
mov    $0xffffffff,%ebx
jmp    1239 <write_tapebuffer+0xc9>
cmp    $0xffffffff,%eax
je     1216 <write_tapebuffer+0xa6>
lea    0xdec(%rip),%rdi
jmp    1227 <write_tapebuffer+0xb7>
mov    0x2dc3(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %rax,%rdi
call   1030 <error_reason@plt>
mov    $0xffffffff,%ebx
mov    %r14,%rdi
call   1040 <free@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@errno = global i32 0
@.str = [12 x i8] c"Write error\00"
define i32 @write_tapebuffer(i32 %0, i8* %1, i64 %2, i64 %3) {
%5 = tail call i8* @misc_malloc(i64 %3)
%6 = icmp eq i8* %5, null
br i1 %6, label %39, label %7
7: ; preds = %4
%8 = icmp eq i64 %2, 0
br i1 %8, label %36, label %9
9: ; preds = %7
%10 = ptrtoint i8* %1 to i64
br label %13
11: ; preds = %19
%12 = icmp ult i64 %27, %2
br i1 %12, label %13, label %36
13: ; preds = %9, %11
%14 = phi i64 [ 0, %9 ], [ %27, %11 ]
%15 = sub i64 %2, %14
%16 = icmp ugt i64 %15, %3
br i1 %16, label %19, label %17
17: ; preds = %13
%18 = tail call i32 @memset(i8* %5, i32 0, i64 %3)
br label %19
19: ; preds = %13, %17
%20 = phi i64 [ %15, %17 ], [ %3, %13 ]
%21 = add i64 %14, %10
%22 = inttoptr i64 %21 to i8*
%23 = tail call i32 @memcpy(i8* %5, i8* %22, i64 %20)
%24 = tail call i32 @write(i32 %0, i8* %5, i64 %20)
%25 = trunc i64 %20 to i32
%26 = icmp eq i32 %24, %25
%27 = add i64 %20, %14
br i1 %26, label %11, label %28
28: ; preds = %19
%29 = icmp eq i32 %24, -1
br i1 %29, label %30, label %33
30: ; preds = %28
%31 = load i32, i32* @errno
%32 = tail call i8* @strerror(i32 %31)
br label %33
33: ; preds = %28, %30
%34 = phi i8* [ %32, %30 ], [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), %28 ]
%35 = tail call i32 @error_reason(i8* %34)
br label %36
36: ; preds = %11, %7, %33
%37 = phi i32 [ -1, %33 ], [ 0, %7 ], [ 0, %11 ]
%38 = tail call i32 @free(i8* %5)
br label %39
39: ; preds = %36, %4
%40 = phi i32 [ -1, %4 ], [ %37, %36 ]
ret i32 %40
}