#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 *malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, int *);
int main() {
  char *ptr = "foo";
  printf("Addr of ptr (heap?): %p \nAddr of ptr[1]: %p\n", ptr, &ptr[1]);
  char ptr2[3] = "foo";
  printf("Addr of ptr2 (stack?): %p \nAddr of ptr2[1]: %p\n", ptr2, &ptr2[1]);
  int *ptr3;
  int n;
  n = 3;
  ptr3 = malloc(n * sizeof(int));
  printf("Addr of ptr3 (heap!): %p\n", ptr3);
  printf("So, *ptr = \"foo\" get allocated on the heap, while ptr[3] gets "
         "allocated on the stack\n");
  scanf("%d", &n);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeba(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x10(%rbp),%rdx
add    $0x1,%rdx
lea    0xea7(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0xf69(%rip),%ax
mov    %ax,-0x13(%rbp)
mov    0xf61(%rip),%al
mov    %al,-0x11(%rbp)
lea    -0x13(%rbp),%rsi
lea    -0x13(%rbp),%rdx
add    $0x1,%rdx
lea    0xea6(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x3,-0x24(%rbp)
movslq -0x24(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rsi
lea    0xeaa(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xeb6(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xefe(%rip),%rdi
lea    -0x24(%rbp),%rsi
call   1050 <scanf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
lea    0xebc(%rip),%rdi
lea    0xeb1(%rip),%rsi
lea    0xeab(%rip),%rdx
xor    %eax,%eax
call   1040 <printf@plt>
movb   $0x6f,0x2(%rsp)
movw   $0x6f66,(%rsp)
lea    0x1(%rsp),%rdx
lea    0xebd(%rip),%rdi
mov    %rsp,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
movl   $0x3,0x4(%rsp)
mov    $0xc,%edi
call   1050 <malloc@plt>
lea    0xeca(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xed6(%rip),%rdi
call   1030 <puts@plt>
lea    0xec7(%rip),%rdi
lea    0x4(%rsp),%rsi
call   1060 <scanf@plt>
xor    %eax,%eax
pop    %rcx
ret

LLVM IR (-O0):
@.str = [4 x i8] c"foo\00"
@.str.1 = [45 x i8] c"Addr of ptr (heap?): %p \0AAddr of ptr[1]: %p\0A\00"
@__const.main.ptr2 = [3 x i8] c"foo"
@.str.2 = [48 x i8] c"Addr of ptr2 (stack?): %p \0AAddr of ptr2[1]: %p\0A\00"
@.str.3 = [26 x i8] c"Addr of ptr3 (heap!): %p\0A\00"
@.str.4 = [86 x i8] c"So, *ptr = \22foo\22 get allocated on the heap, while ptr[3] gets allocated on the stack\0A\00"
@.str.5 = [3 x i8] c"%d\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca [3 x i8]
%4 = alloca i32*
%5 = alloca i32
store i32 0, i32* %1
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8** %2
%6 = load i8*, i8** %2
%7 = load i8*, i8** %2
%8 = getelementptr inbounds i8, i8* %7, i64 1
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i8* %6, i8* %8)
%10 = bitcast [3 x i8]* %3 to i8*
%11 = getelementptr inbounds [3 x i8], [3 x i8]* %3, i64 0, i64 0
%12 = getelementptr inbounds [3 x i8], [3 x i8]* %3, i64 0, i64 1
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0), i8* %11, i8* %12)
store i32 3, i32* %5
%14 = load i32, i32* %5
%15 = sext i32 %14 to i64
%16 = mul i64 %15, 4
%17 = trunc i64 %16 to i32
%18 = call i32* @malloc(i32 %17)
store i32* %18, i32** %4
%19 = load i32*, i32** %4
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i32* %19)
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([86 x i8], [86 x i8]* @.str.4, i64 0, i64 0))
%22 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32* %5)
ret i32 0
}

LLVM IR (-O2):
@.str = [4 x i8] c"foo\00"
@.str.1 = [45 x i8] c"Addr of ptr (heap?): %p \0AAddr of ptr[1]: %p\0A\00"
@__const.main.ptr2 = [3 x i8] c"foo"
@.str.2 = [48 x i8] c"Addr of ptr2 (stack?): %p \0AAddr of ptr2[1]: %p\0A\00"
@.str.3 = [26 x i8] c"Addr of ptr3 (heap!): %p\0A\00"
@.str.5 = [3 x i8] c"%d\00"
@str = [85 x i8] c"So, *ptr = \22foo\22 get allocated on the heap, while ptr[3] gets allocated on the stack\00"
define i32 @main() {
%1 = alloca [3 x i8]
%2 = alloca i32
%3 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 1))
%4 = getelementptr inbounds [3 x i8], [3 x i8]* %1, i64 0, i64 0
%5 = getelementptr inbounds [3 x i8], [3 x i8]* %1, i64 0, i64 1
%6 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0), i8* %4, i8* %5)
%7 = bitcast i32* %2 to i8*
store i32 3, i32* %2
%8 = call i32* @malloc(i32 12)
%9 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i32* %8)
%10 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([85 x i8], [85 x i8]* @str, i64 0, i64 0))
%11 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32* %2)
ret i32 0
}