#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 */
scalar_t__ __VERIFIER_nondet_int();
int /*<<< orphan*/ free(int *);
int *malloc(int);
int main() {
  int n = 1;
  while (__VERIFIER_nondet_int() && n < 5) {
    n++;
  }
  int *p = malloc(n * sizeof(*p));
  *p++;
  int ret = p[n - 2];
  free(p - 1);
  return ret;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x8(%rbp)
mov    $0x0,%al
call   1040 <__VERIFIER_nondet_int@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x15(%rbp)
je     1169 <main+0x39>
cmpl   $0x5,-0x8(%rbp)
setl   %al
mov    %al,-0x15(%rbp)
mov    -0x15(%rbp),%al
test   $0x1,%al
jne    1179 <main+0x49>
jmp    1187 <main+0x57>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1146 <main+0x16>
movslq -0x8(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%ecx
sub    $0x2,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
add    $0xfffffffffffffffc,%rdi
call   1030 <free@plt>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    $0x1,%r14d
xor    %ebx,%ebx
nopl   0x0(%rax)
xor    %eax,%eax
call   1040 <__VERIFIER_nondet_int@plt>
add    $0x4,%rbx
test   %rax,%rax
je     115d <main+0x2d>
lea    0x1(%r14),%eax
cmp    $0x5,%r14d
mov    %eax,%r14d
jb     1140 <main+0x10>
mov    %ebx,%edi
call   1050 <malloc@plt>
mov    -0x4(%rax,%rbx,1),%ebx
mov    %rax,%rdi
call   1030 <free@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i32
store i32 0, i32* %1
store i32 1, i32* %2
br label %5
5: ; preds = %13, %0
%6 = call i64 (...) @__VERIFIER_nondet_int()
%7 = icmp ne i64 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %5
%9 = load i32, i32* %2
%10 = icmp slt i32 %9, 5
br label %11
11: ; preds = %8, %5
%12 = phi i1 [ false, %5 ], [ %10, %8 ]
br i1 %12, label %13, label %16
13: ; preds = %11
%14 = load i32, i32* %2
%15 = add nsw i32 %14, 1
store i32 %15, i32* %2
br label %5
16: ; preds = %11
%17 = load i32, i32* %2
%18 = sext i32 %17 to i64
%19 = mul i64 %18, 4
%20 = trunc i64 %19 to i32
%21 = call i32* @malloc(i32 %20)
store i32* %21, i32** %3
%22 = load i32*, i32** %3
%23 = getelementptr inbounds i32, i32* %22, i32 1
store i32* %23, i32** %3
%24 = load i32, i32* %22
%25 = load i32*, i32** %3
%26 = load i32, i32* %2
%27 = sub nsw i32 %26, 2
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i32, i32* %25, i64 %28
%30 = load i32, i32* %29
store i32 %30, i32* %4
%31 = load i32*, i32** %3
%32 = getelementptr inbounds i32, i32* %31, i64 -1
%33 = call i32 @free(i32* %32)
%34 = load i32, i32* %4
ret i32 %34
}

LLVM IR (-O2):
define i32 @main() {
br label %1
1: ; preds = %1, %0
%2 = phi i32 [ 1, %0 ], [ %7, %1 ]
%3 = tail call i64 (...) @__VERIFIER_nondet_int()
%4 = icmp ne i64 %3, 0
%5 = icmp ult i32 %2, 5
%6 = select i1 %4, i1 %5, i1 false
%7 = add nuw nsw i32 %2, 1
br i1 %6, label %1, label %8
8: ; preds = %1
%9 = shl nuw nsw i32 %2, 2
%10 = tail call i32* @malloc(i32 %9)
%11 = getelementptr inbounds i32, i32* %10, i64 1
%12 = add nsw i32 %2, -2
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i32, i32* %11, i64 %13
%15 = load i32, i32* %14
%16 = tail call i32 @free(i32* %10)
ret i32 %15
}