#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 *);
void *malloc(size_t);
int /*<<< orphan*/ memcpy(void *, void *, size_t);
void *extend_array(void *data, size_t elem_size, size_t current_size,
                   size_t new_size) {
  void *old_data = data;
  data = malloc(elem_size * new_size);
  if (data == NULL)
    return NULL;
  memcpy(data, old_data, elem_size * current_size);
  free(old_data);
  return data;
}


x86 assembly (clang, -O0):
<extend_array>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
imul   -0x28(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    117a <extend_array+0x4a>
movq   $0x0,-0x8(%rbp)
jmp    11a1 <extend_array+0x71>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x18(%rbp),%rdx
imul   -0x20(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<extend_array>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%r14
imul   %rsi,%rcx
mov    %rcx,%rdi
call   1050 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     116f <extend_array+0x3f>
imul   %r12,%r15
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1040 <memcpy@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @extend_array(i8* %0, i64 %1, i64 %2, i64 %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
%10 = alloca i8*
store i8* %0, i8** %6
store i64 %1, i64* %7
store i64 %2, i64* %8
store i64 %3, i64* %9
%11 = load i8*, i8** %6
store i8* %11, i8** %10
%12 = load i64, i64* %7
%13 = load i64, i64* %9
%14 = mul i64 %12, %13
%15 = call i8* @malloc(i64 %14)
store i8* %15, i8** %6
%16 = load i8*, i8** %6
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %19
18: ; preds = %4
store i8* null, i8** %5
br label %29
19: ; preds = %4
%20 = load i8*, i8** %6
%21 = load i8*, i8** %10
%22 = load i64, i64* %7
%23 = load i64, i64* %8
%24 = mul i64 %22, %23
%25 = call i32 @memcpy(i8* %20, i8* %21, i64 %24)
%26 = load i8*, i8** %10
%27 = call i32 @free(i8* %26)
%28 = load i8*, i8** %6
store i8* %28, i8** %5
br label %29
29: ; preds = %19, %18
%30 = load i8*, i8** %5
ret i8* %30
}

LLVM IR (-O2):
define i8* @extend_array(i8* %0, i64 %1, i64 %2, i64 %3) {
%5 = mul i64 %3, %1
%6 = tail call i8* @malloc(i64 %5)
%7 = icmp eq i8* %6, null
br i1 %7, label %12, label %8
8: ; preds = %4
%9 = mul i64 %2, %1
%10 = tail call i32 @memcpy(i8* %6, i8* %0, i64 %9)
%11 = tail call i32 @free(i8* %0)
br label %12
12: ; preds = %4, %8
ret i8* %6
}