#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 *);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, int);
int func(int n) {
  int i = 0;
  int ret = 0;
  int *p = (int *)malloc(sizeof(int) * n);
  do {
    if (NULL == p)
      break;
    if (n < 0)
      break;
    for (i = 0; i < n; i++) {
      p[i] = i;
      printf("%d\n", p[i]);
    }
    ret = 1;
  } while (0);
  free(p);
  return ret;
}


x86 assembly (clang, -O0):
<func>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movslq -0x4(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
xor    %eax,%eax
cmp    -0x18(%rbp),%rax
jne    116d <func+0x3d>
jmp    11c9 <func+0x99>
cmpl   $0x0,-0x4(%rbp)
jge    117c <func+0x4c>
jmp    11c9 <func+0x99>
movl   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    11c2 <func+0x92>
mov    -0x8(%rbp),%edx
mov    -0x18(%rbp),%rax
movslq -0x8(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x18(%rbp),%rax
movslq -0x8(%rbp),%rcx
mov    (%rax,%rcx,4),%esi
lea    0xe51(%rip),%rdi
call   1040 <printf@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1183 <func+0x53>
movl   $0x1,-0xc(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<func>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edi,%ebp
lea    0x0(,%rbp,4),%edi
call   1050 <malloc@plt>
mov    %rax,%r12
xor    %r14d,%r14d
test   %ebp,%ebp
js     1187 <func+0x57>
test   %r12,%r12
je     1187 <func+0x57>
mov    $0x1,%r14d
test   %ebp,%ebp
je     1187 <func+0x57>
mov    %ebp,%ebx
xor    %ebp,%ebp
lea    0xe96(%rip),%r15
nopw   0x0(%rax,%rax,1)
mov    %ebp,(%r12,%rbp,4)
mov    %r15,%rdi
mov    %ebp,%esi
call   1040 <printf@plt>
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    1170 <func+0x40>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"%d\0A\00"
define i32 @func(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32*
store i32 %0, i32* %2
store i32 0, i32* %3
store i32 0, i32* %4
%6 = load i32, i32* %2
%7 = sext i32 %6 to i64
%8 = mul i64 4, %7
%9 = trunc i64 %8 to i32
%10 = call i64 @malloc(i32 %9)
%11 = inttoptr i64 %10 to i32*
store i32* %11, i32** %5
br label %12
12: ; preds = %1
%13 = load i32*, i32** %5
%14 = icmp eq i32* null, %13
br i1 %14, label %15, label %16
15: ; preds = %12
br label %41
16: ; preds = %12
%17 = load i32, i32* %2
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %16
br label %41
20: ; preds = %16
store i32 0, i32* %3
br label %21
21: ; preds = %37, %20
%22 = load i32, i32* %3
%23 = load i32, i32* %2
%24 = icmp slt i32 %22, %23
br i1 %24, label %25, label %40
25: ; preds = %21
%26 = load i32, i32* %3
%27 = load i32*, i32** %5
%28 = load i32, i32* %3
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i32, i32* %27, i64 %29
store i32 %26, i32* %30
%31 = load i32*, i32** %5
%32 = load i32, i32* %3
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i32, i32* %31, i64 %33
%35 = load i32, i32* %34
%36 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %35)
br label %37
37: ; preds = %25
%38 = load i32, i32* %3
%39 = add nsw i32 %38, 1
store i32 %39, i32* %3
br label %21
40: ; preds = %21
store i32 1, i32* %4
br label %41
41: ; preds = %40, %19, %15
%42 = load i32*, i32** %5
%43 = call i32 @free(i32* %42)
%44 = load i32, i32* %4
ret i32 %44
}

LLVM IR (-O2):
@.str = [4 x i8] c"%d\0A\00"
define i32 @func(i32 %0) {
%2 = shl i32 %0, 2
%3 = tail call i64 @malloc(i32 %2)
%4 = inttoptr i64 %3 to i32*
%5 = icmp eq i64 %3, 0
%6 = icmp slt i32 %0, 0
%7 = or i1 %6, %5
br i1 %7, label %19, label %8
8: ; preds = %1
%9 = icmp eq i32 %0, 0
br i1 %9, label %19, label %10
10: ; preds = %8
%11 = zext i32 %0 to i64
br label %12
12: ; preds = %10, %12
%13 = phi i64 [ 0, %10 ], [ %17, %12 ]
%14 = getelementptr inbounds i32, i32* %4, i64 %13
%15 = trunc i64 %13 to i32
store i32 %15, i32* %14
%16 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %15)
%17 = add nuw nsw i64 %13, 1
%18 = icmp eq i64 %17, %11
br i1 %18, label %19, label %12
19: ; preds = %12, %8, %1
%20 = phi i32 [ 0, %1 ], [ 1, %8 ], [ 1, %12 ]
%21 = tail call i32 @free(i32* %4)
ret i32 %20
}