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

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ exit_if(int, char *);
int lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ overwrite_bytes(int, int, void (*)(void *, size_t));
void overwrite_file(int file_descriptor, int size,
                    void (*generate)(void *, size_t)) {
  int block, blocks, bytes, result;
  blocks = size / 4096;
  bytes = size % 4096;
  result = lseek(file_descriptor, 0, SEEK_SET);
  exit_if(result == -1, "Error reading file");
  for (block = 0; block < blocks; block++) {
    overwrite_bytes(file_descriptor, 4096, generate);
  }
  overwrite_bytes(file_descriptor, bytes, generate);
}


x86 assembly (clang, -O0):
<overwrite_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    -0x8(%rbp),%eax
mov    $0x1000,%ecx
cltd
idiv   %ecx
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%eax
mov    $0x1000,%ecx
cltd
idiv   %ecx
mov    %edx,-0x1c(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e78(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
lea    0xe79(%rip),%rsi
call   1030 <exit_if@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    11be <overwrite_file+0x8e>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rdx
mov    $0x1000,%esi
call   1050 <overwrite_bytes@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1193 <overwrite_file+0x63>
mov    -0x4(%rbp),%edi
mov    -0x1c(%rbp),%esi
mov    -0x10(%rbp),%rdx
call   1050 <overwrite_bytes@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<overwrite_file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r12
mov    %esi,%r15d
mov    %edi,%ebp
lea    0xfff(%r15),%ebx
test   %esi,%esi
cmovns %esi,%ebx
mov    %ebx,%eax
and    $0xfffff000,%eax
mov    %esi,%r14d
sub    %eax,%r14d
mov    0x2e80(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
xor    %edi,%edi
cmp    $0xffffffff,%eax
sete   %dil
lea    0xe87(%rip),%rsi
call   1030 <exit_if@plt>
cmp    $0x1000,%r15d
jl     11a4 <overwrite_file+0x74>
sar    $0xc,%ebx
nopw   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    $0x1000,%esi
mov    %r12,%rdx
call   1050 <overwrite_bytes@plt>
add    $0xffffffff,%ebx
jne    1190 <overwrite_file+0x60>
mov    %ebp,%edi
mov    %r14d,%esi
mov    %r12,%rdx
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <overwrite_bytes@plt>

LLVM IR (-O0):
@SEEK_SET = global i32 0
@.str = [19 x i8] c"Error reading file\00"
define void @overwrite_file(i32 %0, i32 %1, void (i8*, i64)* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca void (i8*, i64)*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store void (i8*, i64)* %2, void (i8*, i64)** %6
%11 = load i32, i32* %5
%12 = sdiv i32 %11, 4096
store i32 %12, i32* %8
%13 = load i32, i32* %5
%14 = srem i32 %13, 4096
store i32 %14, i32* %9
%15 = load i32, i32* %4
%16 = load i32, i32* @SEEK_SET
%17 = call i32 @lseek(i32 %15, i32 0, i32 %16)
store i32 %17, i32* %10
%18 = load i32, i32* %10
%19 = icmp eq i32 %18, -1
%20 = zext i1 %19 to i32
%21 = call i32 @exit_if(i32 %20, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %7
br label %22
22: ; preds = %30, %3
%23 = load i32, i32* %7
%24 = load i32, i32* %8
%25 = icmp slt i32 %23, %24
br i1 %25, label %26, label %33
26: ; preds = %22
%27 = load i32, i32* %4
%28 = load void (i8*, i64)*, void (i8*, i64)** %6
%29 = call i32 @overwrite_bytes(i32 %27, i32 4096, void (i8*, i64)* %28)
br label %30
30: ; preds = %26
%31 = load i32, i32* %7
%32 = add nsw i32 %31, 1
store i32 %32, i32* %7
br label %22
33: ; preds = %22
%34 = load i32, i32* %4
%35 = load i32, i32* %9
%36 = load void (i8*, i64)*, void (i8*, i64)** %6
%37 = call i32 @overwrite_bytes(i32 %34, i32 %35, void (i8*, i64)* %36)
ret void
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
@.str = [19 x i8] c"Error reading file\00"
define void @overwrite_file(i32 %0, i32 %1, void (i8*, i64)* %2) {
%4 = sdiv i32 %1, 4096
%5 = srem i32 %1, 4096
%6 = load i32, i32* @SEEK_SET
%7 = tail call i32 @lseek(i32 %0, i32 0, i32 %6)
%8 = icmp eq i32 %7, -1
%9 = zext i1 %8 to i32
%10 = tail call i32 @exit_if(i32 %9, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%11 = icmp sgt i32 %1, 4095
br i1 %11, label %12, label %17
12: ; preds = %3, %12
%13 = phi i32 [ %15, %12 ], [ 0, %3 ]
%14 = tail call i32 @overwrite_bytes(i32 %0, i32 4096, void (i8*, i64)* %2)
%15 = add nuw nsw i32 %13, 1
%16 = icmp eq i32 %15, %4
br i1 %16, label %17, label %12
17: ; preds = %12, %3
%18 = tail call i32 @overwrite_bytes(i32 %0, i32 %5, void (i8*, i64)* %2)
ret void
}