#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*/ free(void *);
int /*<<< orphan*/ memcpy(void *, void *, size_t);
void *xmalloc(size_t);
void *augment_array(void *arr, size_t elem_sz, size_t cnt, void *aug,
                    size_t aug_cnt) {
  void *p = xmalloc(elem_sz * (cnt + aug_cnt));
  memcpy(p, arr, elem_sz * cnt);
  memcpy(p + elem_sz * cnt, aug, elem_sz * aug_cnt);
  free(arr);
  return p;
}


x86 assembly (clang, -O0):
<augment_array>:
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    %r8,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
add    -0x28(%rbp),%rax
imul   %rax,%rdi
call   1040 <xmalloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
imul   -0x18(%rbp),%rdx
call   1050 <memcpy@plt>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rax
imul   -0x18(%rbp),%rax
add    %rax,%rdi
mov    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%rdx
imul   -0x28(%rbp),%rdx
call   1050 <memcpy@plt>
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<augment_array>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8,%r15
mov    %rcx,%r14
mov    %rdx,%rbx
mov    %rsi,%r12
mov    %rdi,%r13
lea    (%r8,%rdx,1),%rdi
imul   %rsi,%rdi
call   1040 <xmalloc@plt>
mov    %rax,%rbp
imul   %r12,%rbx
mov    %rax,%rdi
mov    %r13,%rsi
mov    %rbx,%rdx
call   1050 <memcpy@plt>
add    %rbp,%rbx
imul   %r12,%r15
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1050 <memcpy@plt>
mov    %r13,%rdi
call   1030 <free@plt>
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @augment_array(i8* %0, i64 %1, i64 %2, i8* %3, i64 %4) {
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i8*
store i8* %0, i8** %6
store i64 %1, i64* %7
store i64 %2, i64* %8
store i8* %3, i8** %9
store i64 %4, i64* %10
%12 = load i64, i64* %7
%13 = load i64, i64* %8
%14 = load i64, i64* %10
%15 = add i64 %13, %14
%16 = mul i64 %12, %15
%17 = call i8* @xmalloc(i64 %16)
store i8* %17, i8** %11
%18 = load i8*, i8** %11
%19 = load i8*, i8** %6
%20 = load i64, i64* %7
%21 = load i64, i64* %8
%22 = mul i64 %20, %21
%23 = call i32 @memcpy(i8* %18, i8* %19, i64 %22)
%24 = load i8*, i8** %11
%25 = load i64, i64* %7
%26 = load i64, i64* %8
%27 = mul i64 %25, %26
%28 = getelementptr i8, i8* %24, i64 %27
%29 = load i8*, i8** %9
%30 = load i64, i64* %7
%31 = load i64, i64* %10
%32 = mul i64 %30, %31
%33 = call i32 @memcpy(i8* %28, i8* %29, i64 %32)
%34 = load i8*, i8** %6
%35 = call i32 @free(i8* %34)
%36 = load i8*, i8** %11
ret i8* %36
}

LLVM IR (-O2):
define i8* @augment_array(i8* %0, i64 %1, i64 %2, i8* %3, i64 %4) {
%6 = add i64 %4, %2
%7 = mul i64 %6, %1
%8 = tail call i8* @xmalloc(i64 %7)
%9 = mul i64 %2, %1
%10 = tail call i32 @memcpy(i8* %8, i8* %0, i64 %9)
%11 = getelementptr i8, i8* %8, i64 %9
%12 = mul i64 %4, %1
%13 = tail call i32 @memcpy(i8* %11, i8* %3, i64 %12)
%14 = tail call i32 @free(i8* %0)
ret i8* %8
}