#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*/ init_stack(int *, size_t *, int, int);
int /*<<< orphan*/ peek_safe(int *, size_t *, int, int *, int);
int /*<<< orphan*/ print_stack_debug(int *, size_t *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ push(int *, size_t *, int, int *);
int main() {
  printf("Stack via array, universal version v1.\n\n");
  int data[2];
  size_t count = 0;
  init_stack(data, &count, sizeof(int), 2);
  int val = 1;
  push(data, &count, sizeof(int), &val);
  int retVal = 0;
  peek_safe(data, &count, sizeof(int), &retVal, 2);
  printf("Val %d\n", retVal);
  printf("\n");
  print_stack_debug(data, &count);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe9a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movq   $0x0,-0x18(%rbp)
lea    -0xc(%rbp),%rdi
lea    -0x18(%rbp),%rsi
mov    $0x4,%edx
mov    $0x2,%ecx
call   1060 <init_stack@plt>
movl   $0x1,-0x1c(%rbp)
lea    -0xc(%rbp),%rdi
lea    -0x18(%rbp),%rsi
mov    $0x4,%edx
lea    -0x1c(%rbp),%rcx
call   1070 <push@plt>
movl   $0x0,-0x20(%rbp)
lea    -0xc(%rbp),%rdi
lea    -0x18(%rbp),%rsi
mov    $0x4,%edx
lea    -0x20(%rbp),%rcx
mov    $0x2,%r8d
call   1050 <peek_safe@plt>
mov    -0x20(%rbp),%esi
lea    0xe53(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe43(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0xc(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1030 <print_stack_debug@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x18,%rsp
lea    0xe8a(%rip),%rdi
call   1040 <puts@plt>
movq   $0x0,0x8(%rsp)
lea    0x10(%rsp),%rbx
lea    0x8(%rsp),%r14
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x4,%edx
mov    $0x2,%ecx
call   1080 <init_stack@plt>
movl   $0x1,0x4(%rsp)
lea    0x4(%rsp),%rcx
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x4,%edx
call   1090 <push@plt>
movl   $0x0,(%rsp)
mov    %rsp,%rcx
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x4,%edx
mov    $0x2,%r8d
call   1070 <peek_safe@plt>
mov    (%rsp),%esi
lea    0xe0e(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1050 <print_stack_debug@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [41 x i8] c"Stack via array, universal version v1.\0A\0A\00"
@.str.1 = [8 x i8] c"Val %d\0A\00"
@.str.2 = [2 x i8] c"\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca i64
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
%6 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
store i64 0, i64* %3
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%8 = call i32 @init_stack(i32* %7, i64* %3, i32 4, i32 2)
store i32 1, i32* %4
%9 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%10 = call i32 @push(i32* %9, i64* %3, i32 4, i32* %4)
store i32 0, i32* %5
%11 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%12 = call i32 @peek_safe(i32* %11, i64* %3, i32 4, i32* %5, i32 2)
%13 = load i32, i32* %5
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %13)
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%16 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%17 = call i32 @print_stack_debug(i32* %16, i64* %3)
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [8 x i8] c"Val %d\0A\00"
@str = [40 x i8] c"Stack via array, universal version v1.\0A\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca i64
%3 = alloca i32
%4 = alloca i32
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @str, i64 0, i64 0))
%6 = bitcast [2 x i32]* %1 to i8*
%7 = bitcast i64* %2 to i8*
store i64 0, i64* %2
%8 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%9 = call i32 @init_stack(i32* %8, i64* %2, i32 4, i32 2)
%10 = bitcast i32* %3 to i8*
store i32 1, i32* %3
%11 = call i32 @push(i32* %8, i64* %2, i32 4, i32* %3)
%12 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%13 = call i32 @peek_safe(i32* %8, i64* %2, i32 4, i32* %4, i32 2)
%14 = load i32, i32* %4
%15 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %14)
%16 = call i32 @putchar(i32 10)
%17 = call i32 @print_stack_debug(i32* %8, i64* %2)
ret i32 0
}