#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*/ exit_if(int, char *);
int write(int, char *, int);
void overwrite_bytes(int file_descriptor, int size,
                     void (*generate)(void *, size_t)) {
  char buffer[size];
  int wrote;
  generate(buffer, size);
  wrote = write(file_descriptor, buffer, size);
  exit_if(wrote != size, "Error writing to file");
}


x86 assembly (clang, -O0):
<overwrite_bytes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    -0x8(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x30(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x8(%rbp),%rsi
call   *%rax
mov    -0x30(%rbp),%rsi
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%edx
call   1040 <write@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x8(%rbp),%eax
setne  %al
and    $0x1,%al
movzbl %al,%edi
lea    0xe77(%rip),%rsi
call   1030 <exit_if@plt>
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<overwrite_bytes>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%r14d
mov    %esi,%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
movslq %esi,%r15
mov    %rbx,%rdi
mov    %r15,%rsi
call   *%rdx
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1040 <write@plt>
xor    %edi,%edi
cmp    %r15d,%eax
setne  %dil
lea    0xe97(%rip),%rsi
call   1030 <exit_if@plt>
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [22 x i8] c"Error writing to file\00"
define void @overwrite_bytes(i32 %0, i32 %1, void (i8*, i64)* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca void (i8*, i64)*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store void (i8*, i64)* %2, void (i8*, i64)** %6
%10 = load i32, i32* %5
%11 = zext i32 %10 to i64
%12 = call i8* @llvm.stacksave()
store i8* %12, i8** %7
%13 = alloca i8, i64 %11
store i64 %11, i64* %8
%14 = load void (i8*, i64)*, void (i8*, i64)** %6
%15 = load i32, i32* %5
%16 = sext i32 %15 to i64
%17 = load i32, i32* %4
%18 = load i32, i32* %5
%19 = call i32 @write(i32 %17, i8* %13, i32 %18)
store i32 %19, i32* %9
%20 = load i32, i32* %9
%21 = load i32, i32* %5
%22 = icmp ne i32 %20, %21
%23 = zext i1 %22 to i32
%24 = call i32 @exit_if(i32 %23, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%25 = load i8*, i8** %7
ret void
}

LLVM IR (-O2):
@.str = [22 x i8] c"Error writing to file\00"
define void @overwrite_bytes(i32 %0, i32 %1, void (i8*, i64)* nocapture %2) {
%4 = zext i32 %1 to i64
%5 = alloca i8, i64 %4
%6 = sext i32 %1 to i64
%7 = call i32 @write(i32 %0, i8* %5, i32 %1)
%8 = icmp ne i32 %7, %1
%9 = zext i1 %8 to i32
%10 = call i32 @exit_if(i32 %9, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
ret void
}