#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(int *);
int *malloc(int);
int /*<<< orphan*/ memcpy(int *, int *, int);
void stack_push(int **stack, int *capacity, int *stack_top, int data) {
  if (*stack_top == *capacity) {
    int *new_stack = malloc(sizeof(int) * 2 * *capacity);
    memcpy(new_stack, *stack, sizeof(int) * *capacity);
    free(*stack);
    *stack = new_stack;
    *capacity *= 2;
  }
  (*stack)[*stack_top] = data;
  (*stack_top)++;
}


x86 assembly (clang, -O0):
<stack_push>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    -0x10(%rbp),%rcx
cmp    (%rcx),%eax
jne    11ae <stack_push+0x7e>
mov    -0x10(%rbp),%rax
movslq (%rax),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rax
mov    (%rax),%rsi
mov    -0x10(%rbp),%rax
movslq (%rax),%rax
shl    $0x2,%rax
mov    %eax,%edx
call   1040 <memcpy@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%ecx
shl    $0x1,%ecx
mov    %ecx,(%rax)
mov    -0x1c(%rbp),%edx
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    -0x18(%rbp),%rcx
movslq (%rcx),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x18(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<stack_push>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%r14d
mov    %rdx,%r12
mov    %rdi,%r13
mov    (%rdx),%ebp
cmp    (%rsi),%ebp
jne    1183 <stack_push+0x53>
mov    %rsi,%r15
lea    0x0(,%rbp,8),%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
mov    0x0(%r13),%rsi
shl    $0x2,%ebp
mov    %rax,%rdi
mov    %ebp,%edx
call   1040 <memcpy@plt>
mov    0x0(%r13),%rdi
call   1030 <free@plt>
mov    %rbx,0x0(%r13)
shll   (%r15)
mov    (%r12),%ebp
jmp    1187 <stack_push+0x57>
mov    0x0(%r13),%rbx
movslq %ebp,%rax
mov    %r14d,(%rbx,%rax,4)
addl   $0x1,(%r12)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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