#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*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ memcpy(char *, void *, int);
int /*<<< orphan*/ stderr;
char *xrealloc(char *, int);
int write_to_header(char *hdr, int *hdr_nr, int *hdr_alloc, void *buf, int sz) {
  int i;
  int alloc, nr;
  nr = *hdr_nr;
  alloc = *hdr_alloc;
  if (nr + sz >= alloc) {
    int new_alloc = (alloc + 16) * 2;
    if (new_alloc <= alloc) {
      fprintf(stderr, "write_to_header: int overflow\n");
      return -1;
    }
    hdr = xrealloc(hdr, new_alloc);
    alloc = new_alloc;
    *hdr_alloc = new_alloc;
  }
  memcpy(hdr + nr, buf, sz);
  *hdr_nr = nr + sz;
  return 0;
}


x86 assembly (clang, -O0):
<write_to_header>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8d,-0x2c(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x38(%rbp),%eax
add    -0x2c(%rbp),%eax
cmp    -0x34(%rbp),%eax
jl     11c5 <write_to_header+0x95>
mov    -0x34(%rbp),%eax
add    $0x10,%eax
shl    $0x1,%eax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0x34(%rbp),%eax
jg     11a6 <write_to_header+0x76>
mov    0x2e54(%rip),%rax
mov    (%rax),%edi
lea    0xe6b(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ef <write_to_header+0xbf>
mov    -0x10(%rbp),%rdi
mov    -0x3c(%rbp),%esi
call   1030 <xrealloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x3c(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x3c(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rdi
movslq -0x38(%rbp),%rax
add    %rax,%rdi
mov    -0x28(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   1050 <memcpy@plt>
mov    -0x38(%rbp),%ecx
add    -0x2c(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_to_header>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8d,%r14d
mov    %rsi,%r12
movslq (%rsi),%r15
mov    (%rdx),%eax
lea    (%r15,%r14,1),%ebp
cmp    %eax,%ebp
jl     1174 <write_to_header+0x44>
lea    (%rax,%rax,1),%r13d
add    $0x20,%r13d
cmp    %eax,%r13d
jle    118a <write_to_header+0x5a>
mov    %rdx,%rbx
mov    %r13d,%esi
mov    %rcx,(%rsp)
call   1030 <xrealloc@plt>
mov    (%rsp),%rcx
mov    %rax,%rdi
mov    %r13d,(%rbx)
add    %r15,%rdi
mov    %rcx,%rsi
mov    %r14d,%edx
call   1050 <memcpy@plt>
mov    %ebp,(%r12)
xor    %eax,%eax
jmp    11a4 <write_to_header+0x74>
mov    0x2e4f(%rip),%rax
mov    (%rax),%edi
lea    0xe66(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [31 x i8] c"write_to_header: int overflow\0A\00"
define i32 @write_to_header(i8* %0, i32* %1, i32* %2, i8* %3, i32 %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
store i8* %0, i8** %7
store i32* %1, i32** %8
store i32* %2, i32** %9
store i8* %3, i8** %10
store i32 %4, i32* %11
%16 = load i32*, i32** %8
%17 = load i32, i32* %16
store i32 %17, i32* %14
%18 = load i32*, i32** %9
%19 = load i32, i32* %18
store i32 %19, i32* %13
%20 = load i32, i32* %14
%21 = load i32, i32* %11
%22 = add nsw i32 %20, %21
%23 = load i32, i32* %13
%24 = icmp sge i32 %22, %23
br i1 %24, label %25, label %42
25: ; preds = %5
%26 = load i32, i32* %13
%27 = add nsw i32 %26, 16
%28 = mul nsw i32 %27, 2
store i32 %28, i32* %15
%29 = load i32, i32* %15
%30 = load i32, i32* %13
%31 = icmp sle i32 %29, %30
br i1 %31, label %32, label %35
32: ; preds = %25
%33 = load i32, i32* @stderr
%34 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %6
br label %54
35: ; preds = %25
%36 = load i8*, i8** %7
%37 = load i32, i32* %15
%38 = call i8* @xrealloc(i8* %36, i32 %37)
store i8* %38, i8** %7
%39 = load i32, i32* %15
store i32 %39, i32* %13
%40 = load i32, i32* %15
%41 = load i32*, i32** %9
store i32 %40, i32* %41
br label %42
42: ; preds = %35, %5
%43 = load i8*, i8** %7
%44 = load i32, i32* %14
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i8, i8* %43, i64 %45
%47 = load i8*, i8** %10
%48 = load i32, i32* %11
%49 = call i32 @memcpy(i8* %46, i8* %47, i32 %48)
%50 = load i32, i32* %14
%51 = load i32, i32* %11
%52 = add nsw i32 %50, %51
%53 = load i32*, i32** %8
store i32 %52, i32* %53
store i32 0, i32* %6
br label %54
54: ; preds = %42, %32
%55 = load i32, i32* %6
ret i32 %55
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [31 x i8] c"write_to_header: int overflow\0A\00"
define i32 @write_to_header(i8* %0, i32* nocapture %1, i32* nocapture %2, i8* %3, i32 %4) {
%6 = load i32, i32* %1
%7 = load i32, i32* %2
%8 = add nsw i32 %6, %4
%9 = icmp slt i32 %8, %7
br i1 %9, label %19, label %10
10: ; preds = %5
%11 = shl i32 %7, 1
%12 = add i32 %11, 32
%13 = icmp sgt i32 %12, %7
br i1 %13, label %14, label %16
14: ; preds = %10
%15 = tail call i8* @xrealloc(i8* %0, i32 %12)
store i32 %12, i32* %2
br label %19
16: ; preds = %10
%17 = load i32, i32* @stderr
%18 = tail call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
br label %24
19: ; preds = %14, %5
%20 = phi i8* [ %0, %5 ], [ %15, %14 ]
%21 = sext i32 %6 to i64
%22 = getelementptr inbounds i8, i8* %20, i64 %21
%23 = tail call i32 @memcpy(i8* %22, i8* %3, i32 %4)
store i32 %8, i32* %1
br label %24
24: ; preds = %16, %19
%25 = phi i32 [ 0, %19 ], [ -1, %16 ]
ret i32 %25
}