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

/* Variables and functions */
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *malloc(size_t);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/ *, size_t);
void build_post_bin(uint8_t **post, size_t *post_len, uint8_t *newdata,
                    size_t datalen) {
  uint8_t *newpost = NULL;
  newpost = malloc((*post_len + datalen) * sizeof(uint8_t));
  if (*post) {
    memcpy(newpost, *post, *post_len);
  } else {
    *post_len = 0;
  }
  memcpy(newpost + *post_len, newdata, datalen);
  *post_len += datalen;
  if (*post)
    free(*post);
  *post = newpost;
}


x86 assembly (clang, -O0):
<build_post_bin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
add    -0x20(%rbp),%rdi
shl    $0x2,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rax
cmpq   $0x0,(%rax)
je     1192 <build_post_bin+0x62>
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rax
mov    (%rax),%rsi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
call   1040 <memcpy@plt>
jmp    119d <build_post_bin+0x6d>
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x20(%rbp),%rcx
mov    -0x10(%rbp),%rax
add    (%rax),%rcx
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
cmpq   $0x0,(%rax)
je     11e4 <build_post_bin+0xb4>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<build_post_bin>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r12
mov    %rdx,%r13
mov    %rsi,%rbx
mov    %rdi,%r14
mov    (%rsi),%rbp
lea    (%rcx,%rbp,1),%rdi
shl    $0x2,%rdi
call   1050 <malloc@plt>
mov    %rax,%r15
mov    (%r14),%rsi
test   %rsi,%rsi
je     1172 <build_post_bin+0x42>
mov    %r15,%rdi
mov    %rbp,%rdx
call   1040 <memcpy@plt>
mov    (%rbx),%rax
jmp    117b <build_post_bin+0x4b>
movq   $0x0,(%rbx)
xor    %eax,%eax
lea    (%r15,%rax,4),%rdi
mov    %r13,%rsi
mov    %r12,%rdx
call   1040 <memcpy@plt>
add    %r12,(%rbx)
mov    (%r14),%rdi
test   %rdi,%rdi
je     119a <build_post_bin+0x6a>
call   1030 <free@plt>
mov    %r15,(%r14)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define void @build_post_bin(i32** nocapture %0, i64* nocapture %1, i32* %2, i64 %3) {
%5 = load i64, i64* %1
%6 = add i64 %5, %3
%7 = shl i64 %6, 2
%8 = tail call i32* @malloc(i64 %7)
%9 = load i32*, i32** %0
%10 = icmp eq i32* %9, null
br i1 %10, label %14, label %11
11: ; preds = %4
%12 = tail call i32 @memcpy(i32* %8, i32* %9, i64 %5)
%13 = load i64, i64* %1
br label %15
14: ; preds = %4
store i64 0, i64* %1
br label %15
15: ; preds = %14, %11
%16 = phi i64 [ 0, %14 ], [ %13, %11 ]
%17 = getelementptr inbounds i32, i32* %8, i64 %16
%18 = tail call i32 @memcpy(i32* %17, i32* %2, i64 %3)
%19 = load i64, i64* %1
%20 = add i64 %19, %3
store i64 %20, i64* %1
%21 = load i32*, i32** %0
%22 = icmp eq i32* %21, null
br i1 %22, label %25, label %23
23: ; preds = %15
%24 = tail call i32 @free(i32* %21)
br label %25
25: ; preds = %23, %15
store i32* %8, i32** %0
ret void
}