#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*/ memcpy(unsigned char *, void *, long);
unsigned char *realloc(unsigned char *, long);
unsigned char *dgl_mempush(unsigned char *pstack, long *istack, long size,
                           void *pv) {
  if (*istack == 0)
    pstack = NULL;
  pstack = realloc(pstack, size * (1 + *istack));
  if (pstack == NULL)
    return NULL;
  memcpy(&pstack[(*istack) * size], pv, size);
  (*istack)++;
  return pstack;
}


x86 assembly (clang, -O0):
<dgl_mempush>:
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    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
jne    114e <dgl_mempush+0x2e>
movq   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
add    $0x1,%rax
imul   %rax,%rsi
call   1040 <realloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1186 <dgl_mempush+0x66>
movq   $0x0,-0x8(%rbp)
jmp    11bc <dgl_mempush+0x9c>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
imul   -0x20(%rbp),%rax
add    %rax,%rdi
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <memcpy@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rcx
add    $0x1,%rcx
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dgl_mempush>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r15
mov    %rdx,%r12
mov    %rsi,%r14
mov    (%rsi),%rsi
test   %rsi,%rsi
cmove  %rsi,%rdi
add    $0x1,%rsi
imul   %rdx,%rsi
call   1040 <realloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     116c <dgl_mempush+0x4c>
mov    (%r14),%rax
imul   %r12,%rax
mov    %rbx,%rdi
add    %rax,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1030 <memcpy@plt>
addq   $0x1,(%r14)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @dgl_mempush(i8* %0, i64* %1, i64 %2, i8* %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64*
%8 = alloca i64
%9 = alloca i8*
store i8* %0, i8** %6
store i64* %1, i64** %7
store i64 %2, i64* %8
store i8* %3, i8** %9
%10 = load i64*, i64** %7
%11 = load i64, i64* %10
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %4
store i8* null, i8** %6
br label %14
14: ; preds = %13, %4
%15 = load i8*, i8** %6
%16 = load i64, i64* %8
%17 = load i64*, i64** %7
%18 = load i64, i64* %17
%19 = add nsw i64 1, %18
%20 = mul nsw i64 %16, %19
%21 = call i8* @realloc(i8* %15, i64 %20)
store i8* %21, i8** %6
%22 = load i8*, i8** %6
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %25
24: ; preds = %14
store i8* null, i8** %5
br label %39
25: ; preds = %14
%26 = load i8*, i8** %6
%27 = load i64*, i64** %7
%28 = load i64, i64* %27
%29 = load i64, i64* %8
%30 = mul nsw i64 %28, %29
%31 = getelementptr inbounds i8, i8* %26, i64 %30
%32 = load i8*, i8** %9
%33 = load i64, i64* %8
%34 = call i32 @memcpy(i8* %31, i8* %32, i64 %33)
%35 = load i64*, i64** %7
%36 = load i64, i64* %35
%37 = add nsw i64 %36, 1
store i64 %37, i64* %35
%38 = load i8*, i8** %6
store i8* %38, i8** %5
br label %39
39: ; preds = %25, %24
%40 = load i8*, i8** %5
ret i8* %40
}

LLVM IR (-O2):
define i8* @dgl_mempush(i8* nocapture %0, i64* nocapture %1, i64 %2, i8* %3) {
%5 = load i64, i64* %1
%6 = icmp eq i64 %5, 0
%7 = select i1 %6, i8* null, i8* %0
%8 = add nsw i64 %5, 1
%9 = mul nsw i64 %8, %2
%10 = tail call i8* @realloc(i8* %7, i64 %9)
%11 = icmp eq i8* %10, null
br i1 %11, label %19, label %12
12: ; preds = %4
%13 = load i64, i64* %1
%14 = mul nsw i64 %13, %2
%15 = getelementptr inbounds i8, i8* %10, i64 %14
%16 = tail call i32 @memcpy(i8* %15, i8* %3, i64 %2)
%17 = load i64, i64* %1
%18 = add nsw i64 %17, 1
store i64 %18, i64* %1
br label %19
19: ; preds = %4, %12
ret i8* %10
}