#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*/ fprintf(int /*<<< orphan*/, char *);
int *realloc(int *, int);
int /*<<< orphan*/ stderr;
int append_int(int **arr, int appi, int pos, int size) {
  if (pos >= size - 1) {
    size = size * 2;
    int *newplace = realloc(*arr, sizeof(int) * size);
    if (newplace != NULL) {
      *arr = newplace;
    } else {
      fprintf(stderr,
              "append_int failed to reallocate memory when necessary.\n");
    }
  }
  (*arr)[pos] = appi;
  return size;
}


x86 assembly (clang, -O0):
<append_int>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    -0x10(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jl     119e <append_int+0x7e>
mov    -0x14(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
movslq -0x14(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%esi
call   1040 <realloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1184 <append_int+0x64>
mov    -0x20(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
jmp    1199 <append_int+0x79>
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
lea    0xe6c(%rip),%rsi
call   1030 <fprintf@plt>
jmp    119e <append_int+0x7e>
mov    -0xc(%rbp),%edx
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
movslq -0x10(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<append_int>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %esi,%r14d
mov    %rdi,%rbx
lea    -0x1(%rcx),%eax
cmp    %edx,%eax
jle    113b <append_int+0x1b>
mov    %ecx,%r15d
jmp    116b <append_int+0x4b>
lea    (%rcx,%rcx,1),%r15d
mov    (%rbx),%rdi
shl    $0x3,%ecx
mov    %ecx,%esi
call   1040 <realloc@plt>
test   %rax,%rax
je     1156 <append_int+0x36>
mov    %rax,(%rbx)
jmp    116b <append_int+0x4b>
mov    0x2e83(%rip),%rax
mov    (%rax),%edi
lea    0xe9a(%rip),%rsi
call   1030 <fprintf@plt>
mov    (%rbx),%rax
movslq %ebp,%rcx
mov    %r14d,(%rax,%rcx,4)
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [56 x i8] c"append_int failed to reallocate memory when necessary.\0A\00"
define i32 @append_int(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* %8
%12 = sub nsw i32 %11, 1
%13 = icmp sge i32 %10, %12
br i1 %13, label %14, label %33
14: ; preds = %4
%15 = load i32, i32* %8
%16 = mul nsw i32 %15, 2
store i32 %16, i32* %8
%17 = load i32**, i32*** %5
%18 = load i32*, i32** %17
%19 = load i32, i32* %8
%20 = sext i32 %19 to i64
%21 = mul i64 4, %20
%22 = trunc i64 %21 to i32
%23 = call i32* @realloc(i32* %18, i32 %22)
store i32* %23, i32** %9
%24 = load i32*, i32** %9
%25 = icmp ne i32* %24, null
br i1 %25, label %26, label %29
26: ; preds = %14
%27 = load i32*, i32** %9
%28 = load i32**, i32*** %5
store i32* %27, i32** %28
br label %32
29: ; preds = %14
%30 = load i32, i32* @stderr
%31 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0))
br label %32
32: ; preds = %29, %26
br label %33
33: ; preds = %32, %4
%34 = load i32, i32* %6
%35 = load i32**, i32*** %5
%36 = load i32*, i32** %35
%37 = load i32, i32* %7
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i32, i32* %36, i64 %38
store i32 %34, i32* %39
%40 = load i32, i32* %8
ret i32 %40
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [56 x i8] c"append_int failed to reallocate memory when necessary.\0A\00"
define i32 @append_int(i32** nocapture %0, i32 %1, i32 %2, i32 %3) {
%5 = add nsw i32 %3, -1
%6 = icmp sgt i32 %5, %2
br i1 %6, label %17, label %7
7: ; preds = %4
%8 = shl nsw i32 %3, 1
%9 = load i32*, i32** %0
%10 = shl i32 %3, 3
%11 = tail call i32* @realloc(i32* %9, i32 %10)
%12 = icmp eq i32* %11, null
br i1 %12, label %14, label %13
13: ; preds = %7
store i32* %11, i32** %0
br label %17
14: ; preds = %7
%15 = load i32, i32* @stderr
%16 = tail call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %13, %14, %4
%18 = phi i32 [ %3, %4 ], [ %8, %14 ], [ %8, %13 ]
%19 = load i32*, i32** %0
%20 = sext i32 %2 to i64
%21 = getelementptr inbounds i32, i32* %19, i64 %20
store i32 %1, i32* %21
ret i32 %18
}