#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__ malloc(int);
int /*<<< orphan*/ printf(char *, ...);
void foo(void) {
  static int num = 0;
  int autovar;
  int *ptr_foo = (int *)malloc(sizeof(int));
  if (++num == 4)
    return;
  printf("Stack frame number %d: address of autovar: %p\n", num, &autovar);
  printf("Address of heap allocated inside foo() %p\n", ptr_foo);
  foo();
}


x86 assembly (clang, -O0):
<foo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x4,%edi
call   1040 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    0x2ef0(%rip),%eax
add    $0x1,%eax
mov    %eax,0x2ee7(%rip)
cmp    $0x4,%eax
jne    1163 <foo+0x33>
jmp    1192 <foo+0x62>
mov    0x2ed3(%rip),%esi
lea    0xe90(%rip),%rdi
lea    -0x4(%rbp),%rdx
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%rsi
lea    0xea9(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
call   1050 <foo@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<foo>:
push   %rbx
sub    $0x10,%rsp
mov    $0x4,%edi
call   1040 <malloc@plt>
mov    0x2ef7(%rip),%esi
add    $0x1,%esi
mov    %esi,0x2eee(%rip)
cmp    $0x4,%esi
je     117f <foo+0x4f>
mov    %rax,%rbx
lea    0xea3(%rip),%rdi
lea    0xc(%rsp),%rdx
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xebf(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1030 <printf@plt>
call   1050 <foo@plt>
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@foo.num = internal global i32 0
@.str = [47 x i8] c"Stack frame number %d: address of autovar: %p\0A\00"
@.str.1 = [43 x i8] c"Address of heap allocated inside foo() %p\0A\00"
define void @foo() {
%1 = alloca i32
%2 = alloca i32*
%3 = call i64 @malloc(i32 4)
%4 = inttoptr i64 %3 to i32*
store i32* %4, i32** %2
%5 = load i32, i32* @foo.num
%6 = add nsw i32 %5, 1
store i32 %6, i32* @foo.num
%7 = icmp eq i32 %6, 4
br i1 %7, label %8, label %9
8: ; preds = %0
br label %14
9: ; preds = %0
%10 = load i32, i32* @foo.num
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i32 %10, i32* %1)
%12 = load i32*, i32** %2
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0), i32* %12)
br label %14
14: ; preds = %9, %8
ret void
}

LLVM IR (-O2):
@foo.num = internal unnamed_addr global i32 0
@.str = [47 x i8] c"Stack frame number %d: address of autovar: %p\0A\00"
@.str.1 = [43 x i8] c"Address of heap allocated inside foo() %p\0A\00"
define void @foo() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i64 @malloc(i32 4)
%4 = load i32, i32* @foo.num
%5 = add nsw i32 %4, 1
store i32 %5, i32* @foo.num
%6 = icmp eq i32 %5, 4
br i1 %6, label %11, label %7
7: ; preds = %0
%8 = inttoptr i64 %3 to i32*
%9 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i32 %5, i32* %1)
%10 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0), i32* %8)
br label %11
11: ; preds = %0, %7
ret void
}