#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*/ buffer;

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int write(int, char *, int);
void zerofill(int fd, int size) {
  char buffer[4096];
  memset(buffer, 0, sizeof(buffer));
  int offset = 0;
  while (offset < size) {
    int toWrite = sizeof(buffer);
    if (offset + toWrite > size) {
      toWrite = size - offset;
    }
    int written = write(fd, buffer, toWrite);
    assert(written == toWrite);
    offset += toWrite;
  }
}


x86 assembly (clang, -O0):
<zerofill>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
lea    -0x1010(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1040 <memset@plt>
movl   $0x0,-0x1014(%rbp)
mov    -0x1014(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    11e6 <zerofill+0xb6>
movl   $0x1000,-0x1018(%rbp)
mov    -0x1014(%rbp),%eax
add    -0x1018(%rbp),%eax
cmp    -0x8(%rbp),%eax
jle    119b <zerofill+0x6b>
mov    -0x8(%rbp),%eax
sub    -0x1014(%rbp),%eax
mov    %eax,-0x1018(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    -0x1018(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x101c(%rbp)
mov    -0x101c(%rbp),%eax
cmp    -0x1018(%rbp),%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x1018(%rbp),%eax
add    -0x1014(%rbp),%eax
mov    %eax,-0x1014(%rbp)
jmp    115e <zerofill+0x2e>
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<zerofill>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1008,%rsp
mov    %esi,%r13d
mov    %edi,%r15d
mov    %rsp,%rdi
xor    %ebp,%ebp
mov    $0x1000,%r12d
xor    %esi,%esi
mov    $0x1000,%edx
call   1040 <memset@plt>
test   %r13d,%r13d
jle    11a5 <zerofill+0x75>
mov    %rsp,%r14
cs nopw 0x0(%rax,%rax,1)
lea    0x1000(%rbp),%eax
mov    %r13d,%ebx
sub    %ebp,%ebx
cmp    %r13d,%eax
cmovle %r12d,%ebx
mov    %r15d,%edi
mov    %r14,%rsi
mov    %ebx,%edx
call   1030 <write@plt>
xor    %edi,%edi
cmp    %ebx,%eax
sete   %dil
call   1050 <assert@plt>
add    %ebp,%ebx
mov    %ebx,%ebp
cmp    %r13d,%ebx
jl     1170 <zerofill+0x40>
add    $0x1008,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @zerofill(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca [4096 x i8]
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%9 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%10 = call i32 @memset(i8* %9, i32 0, i32 4096)
store i32 0, i32* %6
br label %11
11: ; preds = %25, %2
%12 = load i32, i32* %6
%13 = load i32, i32* %4
%14 = icmp slt i32 %12, %13
br i1 %14, label %15, label %38
15: ; preds = %11
store i32 4096, i32* %7
%16 = load i32, i32* %6
%17 = load i32, i32* %7
%18 = add nsw i32 %16, %17
%19 = load i32, i32* %4
%20 = icmp sgt i32 %18, %19
br i1 %20, label %21, label %25
21: ; preds = %15
%22 = load i32, i32* %4
%23 = load i32, i32* %6
%24 = sub nsw i32 %22, %23
store i32 %24, i32* %7
br label %25
25: ; preds = %21, %15
%26 = load i32, i32* %3
%27 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%28 = load i32, i32* %7
%29 = call i32 @write(i32 %26, i8* %27, i32 %28)
store i32 %29, i32* %8
%30 = load i32, i32* %8
%31 = load i32, i32* %7
%32 = icmp eq i32 %30, %31
%33 = zext i1 %32 to i32
%34 = call i32 @assert(i32 %33)
%35 = load i32, i32* %7
%36 = load i32, i32* %6
%37 = add nsw i32 %36, %35
store i32 %37, i32* %6
br label %11
38: ; preds = %11
ret void
}

LLVM IR (-O2):
define void @zerofill(i32 %0, i32 %1) {
%3 = alloca [4096 x i8]
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%5 = call i32 @memset(i8* %4, i32 0, i32 4096)
%6 = icmp sgt i32 %1, 0
br i1 %6, label %7, label %19
7: ; preds = %2, %7
%8 = phi i32 [ %17, %7 ], [ 0, %2 ]
%9 = add nsw i32 %8, 4096
%10 = icmp sgt i32 %9, %1
%11 = sub nsw i32 %1, %8
%12 = select i1 %10, i32 %11, i32 4096
%13 = call i32 @write(i32 %0, i8* %4, i32 %12)
%14 = icmp eq i32 %13, %12
%15 = zext i1 %14 to i32
%16 = call i32 @assert(i32 %15)
%17 = add nsw i32 %12, %8
%18 = icmp slt i32 %17, %1
br i1 %18, label %7, label %19
19: ; preds = %7, %2
ret void
}