#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;
typedef char *off_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int lseek(int, char *, int /*<<< orphan*/);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, char *, ...);
char *strerror(int /*<<< orphan*/);
int write(int, char *, unsigned int);
void zero_data(int to_fd, off_t to_offset, ssize_t size) {
  char buf[4096];
  int ret;
  unsigned int to_write;
  memset(buf, 0, sizeof(buf));
  ret = lseek(to_fd, to_offset, SEEK_SET);
  if (ret < 0) {
    printf("error: "
           "failed to seek output file to %lx"
           ": %s"
           "\n",
           to_offset, strerror(errno));
    exit(-1);
  };
  while (size != 0) {
    to_write = size;
    if (to_write > sizeof(buf))
      to_write = sizeof(buf);
    ret = write(to_fd, buf, to_write);
    if (ret < 0) {
      printf("error: "
             "failed to write to output file"
             ": %s"
             "\n",
             strerror(errno));
      exit(-1);
    };
    size -= ret;
  }
}


x86 assembly (clang, -O0):
<zero_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
lea    -0x1020(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1060 <memset@plt>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2e42(%rip),%rax
mov    (%rax),%edx
call   1050 <lseek@plt>
mov    %eax,-0x1024(%rbp)
cmpl   $0x0,-0x1024(%rbp)
jge    11eb <zero_data+0x8b>
mov    -0x10(%rbp),%rax
mov    %rax,-0x1030(%rbp)
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x1030(%rbp),%rsi
mov    %rax,%rdx
lea    0xe26(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
jmp    11f0 <zero_data+0x90>
cmpl   $0x0,-0x14(%rbp)
je     1283 <zero_data+0x123>
mov    -0x14(%rbp),%eax
mov    %eax,-0x1028(%rbp)
mov    -0x1028(%rbp),%eax
cmp    $0x1000,%rax
jbe    121f <zero_data+0xbf>
movl   $0x1000,-0x1028(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    -0x1028(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x1024(%rbp)
cmpl   $0x0,-0x1024(%rbp)
jge    1270 <zero_data+0x110>
mov    0x2d92(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %rax,%rsi
lea    0xdcf(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    -0x1024(%rbp),%ecx
mov    -0x14(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
jmp    11f0 <zero_data+0x90>
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<zero_data>:
push   %rbp
push   %r14
push   %rbx
sub    $0x1000,%rsp
mov    %edx,%ebx
mov    %rsi,%r14
mov    %edi,%ebp
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1060 <memset@plt>
mov    0x2e50(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
mov    %r14,%rsi
call   1050 <lseek@plt>
test   %eax,%eax
js     11f5 <zero_data+0x95>
test   %ebx,%ebx
je     11c0 <zero_data+0x60>
mov    %rsp,%r14
nop
cmp    $0x1000,%ebx
mov    $0x1000,%edx
cmovb  %ebx,%edx
mov    %ebp,%edi
mov    %r14,%rsi
call   1030 <write@plt>
test   %eax,%eax
js     11cc <zero_data+0x6c>
sub    %eax,%ebx
jne    11a0 <zero_data+0x40>
add    $0x1000,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2e0d(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe4d(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    0x2de4(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xdf6(%rip),%rdi
mov    %r14,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@SEEK_SET = global i32 0
@.str = [46 x i8] c"error: failed to seek output file to %lx: %s\0A\00"
@errno = global i32 0
@.str.1 = [43 x i8] c"error: failed to write to output file: %s\0A\00"
define void @zero_data(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca [4096 x i8]
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%10 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%11 = call i32 @memset(i8* %10, i32 0, i32 4096)
%12 = load i32, i32* %4
%13 = load i8*, i8** %5
%14 = load i32, i32* @SEEK_SET
%15 = call i32 @lseek(i32 %12, i8* %13, i32 %14)
store i32 %15, i32* %8
%16 = load i32, i32* %8
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %24
18: ; preds = %3
%19 = load i8*, i8** %5
%20 = load i32, i32* @errno
%21 = call i8* @strerror(i32 %20)
%22 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), i8* %19, i8* %21)
%23 = call i32 @exit(i32 -1)
unreachable
24: ; preds = %3
br label %25
25: ; preds = %46, %24
%26 = load i32, i32* %6
%27 = icmp ne i32 %26, 0
br i1 %27, label %28, label %50
28: ; preds = %25
%29 = load i32, i32* %6
store i32 %29, i32* %9
%30 = load i32, i32* %9
%31 = zext i32 %30 to i64
%32 = icmp ugt i64 %31, 4096
br i1 %32, label %33, label %34
33: ; preds = %28
store i32 4096, i32* %9
br label %34
34: ; preds = %33, %28
%35 = load i32, i32* %4
%36 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%37 = load i32, i32* %9
%38 = call i32 @write(i32 %35, i8* %36, i32 %37)
store i32 %38, i32* %8
%39 = load i32, i32* %8
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %46
41: ; preds = %34
%42 = load i32, i32* @errno
%43 = call i8* @strerror(i32 %42)
%44 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0), i8* %43)
%45 = call i32 @exit(i32 -1)
unreachable
46: ; preds = %34
%47 = load i32, i32* %8
%48 = load i32, i32* %6
%49 = sub nsw i32 %48, %47
store i32 %49, i32* %6
br label %25
50: ; preds = %25
ret void
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
@.str = [46 x i8] c"error: failed to seek output file to %lx: %s\0A\00"
@errno = global i32 0
@.str.1 = [43 x i8] c"error: failed to write to output file: %s\0A\00"
define void @zero_data(i32 %0, i8* %1, i32 %2) {
%4 = alloca [4096 x i8]
%5 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%6 = call i32 @memset(i8* %5, i32 0, i32 4096)
%7 = load i32, i32* @SEEK_SET
%8 = call i32 @lseek(i32 %0, i8* %1, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %3
%11 = icmp eq i32 %2, 0
br i1 %11, label %31, label %19
12: ; preds = %3
%13 = load i32, i32* @errno
%14 = call i8* @strerror(i32 %13)
%15 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), i8* %1, i8* %14)
%16 = call i32 @exit(i32 -1)
unreachable
17: ; preds = %19
%18 = icmp eq i32 %25, 0
br i1 %18, label %31, label %19
19: ; preds = %10, %17
%20 = phi i32 [ %25, %17 ], [ %2, %10 ]
%21 = icmp ult i32 %20, 4096
%22 = select i1 %21, i32 %20, i32 4096
%23 = call i32 @write(i32 %0, i8* %5, i32 %22)
%24 = icmp slt i32 %23, 0
%25 = sub nsw i32 %20, %23
br i1 %24, label %26, label %17
26: ; preds = %19
%27 = load i32, i32* @errno
%28 = call i8* @strerror(i32 %27)
%29 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0), i8* %28)
%30 = call i32 @exit(i32 -1)
unreachable
31: ; preds = %17, %10
ret void
}