#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 */
unsigned char *lib_realloc(unsigned char *, size_t);
int /*<<< orphan*/ memcpy(unsigned char *, unsigned char const *, int);
unsigned char *util_bufcat(unsigned char *buf, int *buf_size,
                           size_t *max_buf_size, const unsigned char *src,
                           int src_size) {
  if (*buf_size + src_size > (int)(*max_buf_size)) {
    unsigned char *new_buf;
    *max_buf_size = (((*buf_size + src_size) / 0x1000 + 1) * 0x1000);
    new_buf = lib_realloc(buf, *max_buf_size);
    buf = new_buf;
  }
  memcpy(buf + *buf_size, src, src_size);
  *buf_size += src_size;
  return buf;
}


x86 assembly (clang, -O0):
<util_bufcat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
add    -0x24(%rbp),%eax
mov    -0x18(%rbp),%rcx
mov    (%rcx),%rcx
cmp    %ecx,%eax
jle    1191 <util_bufcat+0x71>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
add    -0x24(%rbp),%eax
mov    $0x1000,%ecx
cltd
idiv   %ecx
add    $0x1,%eax
shl    $0xc,%eax
movslq %eax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
call   1040 <lib_realloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rax
movslq (%rax),%rax
add    %rax,%rdi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1030 <memcpy@plt>
mov    -0x24(%rbp),%ecx
mov    -0x10(%rbp),%rax
add    (%rax),%ecx
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<util_bufcat>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %r8d,%r12d
mov    %rcx,%r15
mov    %rsi,%r14
mov    %rdi,%rbx
mov    (%rsi),%eax
lea    (%rax,%r12,1),%ecx
cmp    (%rdx),%ecx
jle    1167 <util_bufcat+0x47>
lea    0xfff(%rcx),%eax
test   %ecx,%ecx
cmovns %ecx,%eax
and    $0xfffff000,%eax
add    $0x1000,%eax
movslq %eax,%rsi
mov    %rsi,(%rdx)
mov    %rbx,%rdi
call   1040 <lib_realloc@plt>
mov    %rax,%rbx
mov    (%r14),%eax
movslq %eax,%rdi
add    %rbx,%rdi
mov    %r15,%rsi
mov    %r12d,%edx
call   1030 <memcpy@plt>
add    %r12d,(%r14)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @util_bufcat(i8* %0, i32* %1, i64* %2, i8* %3, i32 %4) {
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i64*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i8*
store i8* %0, i8** %6
store i32* %1, i32** %7
store i64* %2, i64** %8
store i8* %3, i8** %9
store i32 %4, i32* %10
%12 = load i32*, i32** %7
%13 = load i32, i32* %12
%14 = load i32, i32* %10
%15 = add nsw i32 %13, %14
%16 = load i64*, i64** %8
%17 = load i64, i64* %16
%18 = trunc i64 %17 to i32
%19 = icmp sgt i32 %15, %18
br i1 %19, label %20, label %35
20: ; preds = %5
%21 = load i32*, i32** %7
%22 = load i32, i32* %21
%23 = load i32, i32* %10
%24 = add nsw i32 %22, %23
%25 = sdiv i32 %24, 4096
%26 = add nsw i32 %25, 1
%27 = mul nsw i32 %26, 4096
%28 = sext i32 %27 to i64
%29 = load i64*, i64** %8
store i64 %28, i64* %29
%30 = load i8*, i8** %6
%31 = load i64*, i64** %8
%32 = load i64, i64* %31
%33 = call i8* @lib_realloc(i8* %30, i64 %32)
store i8* %33, i8** %11
%34 = load i8*, i8** %11
store i8* %34, i8** %6
br label %35
35: ; preds = %20, %5
%36 = load i8*, i8** %6
%37 = load i32*, i32** %7
%38 = load i32, i32* %37
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %36, i64 %39
%41 = load i8*, i8** %9
%42 = load i32, i32* %10
%43 = call i32 @memcpy(i8* %40, i8* %41, i32 %42)
%44 = load i32, i32* %10
%45 = load i32*, i32** %7
%46 = load i32, i32* %45
%47 = add nsw i32 %46, %44
store i32 %47, i32* %45
%48 = load i8*, i8** %6
ret i8* %48
}

LLVM IR (-O2):
define i8* @util_bufcat(i8* %0, i32* nocapture %1, i64* nocapture %2, i8* %3, i32 %4) {
%6 = load i32, i32* %1
%7 = add nsw i32 %6, %4
%8 = load i64, i64* %2
%9 = trunc i64 %8 to i32
%10 = icmp sgt i32 %7, %9
br i1 %10, label %11, label %18
11: ; preds = %5
%12 = sdiv i32 %7, 4096
%13 = shl nsw i32 %12, 12
%14 = add i32 %13, 4096
%15 = sext i32 %14 to i64
store i64 %15, i64* %2
%16 = tail call i8* @lib_realloc(i8* %0, i64 %15)
%17 = load i32, i32* %1
br label %18
18: ; preds = %11, %5
%19 = phi i32 [ %17, %11 ], [ %6, %5 ]
%20 = phi i8* [ %16, %11 ], [ %0, %5 ]
%21 = sext i32 %19 to i64
%22 = getelementptr inbounds i8, i8* %20, i64 %21
%23 = tail call i32 @memcpy(i8* %22, i8* %3, i32 %4)
%24 = load i32, i32* %1
%25 = add nsw i32 %24, %4
store i32 %25, i32* %1
ret i8* %20
}