#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 *, int);
int main(void) {
  int i, j;
  int **p = (int **)malloc(2 * sizeof(int *));
  p[0] = (int *)malloc(2 * sizeof(int));
  p[1] = (int *)malloc(2 * sizeof(int));
  for (i = 0; i < 2; i++)
    for (j = 0; j < 2; j++)
      p[i][j] = i + j;
  printf("%d", p[0][0]);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x10,%edi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    $0x8,%edi
call   1040 <malloc@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    $0x8,%edi
call   1040 <malloc@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,0x8(%rax)
movl   $0x0,-0x8(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11c2 <main+0xa2>
movl   $0x0,-0xc(%rbp)
cmpl   $0x2,-0xc(%rbp)
jge    11af <main+0x8f>
mov    -0x8(%rbp),%edx
add    -0xc(%rbp),%edx
mov    -0x18(%rbp),%rax
movslq -0x8(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
movslq -0xc(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    117e <main+0x5e>
jmp    11b4 <main+0x94>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    116d <main+0x4d>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%esi
lea    0xe2e(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    $0x10,%edi
call   1040 <malloc@plt>
mov    %rax,%rbx
mov    $0x8,%edi
call   1040 <malloc@plt>
mov    %rax,(%rbx)
mov    $0x8,%edi
call   1040 <malloc@plt>
mov    %rax,0x8(%rbx)
mov    (%rbx),%rcx
movabs $0x100000000,%rdx
mov    %rdx,(%rcx)
movabs $0x200000001,%rdx
mov    %rdx,(%rax)
mov    (%rcx),%esi
lea    0xe91(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
pop    %rbx
ret

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

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
define i32 @main() {
%1 = tail call i64 @malloc(i32 16)
%2 = inttoptr i64 %1 to i32**
%3 = tail call i64 @malloc(i32 8)
%4 = inttoptr i64 %3 to i32*
store i32* %4, i32** %2
%5 = tail call i64 @malloc(i32 8)
%6 = inttoptr i64 %5 to i32*
%7 = getelementptr inbounds i32*, i32** %2, i64 1
store i32* %6, i32** %7
%8 = load i32*, i32** %2
store i32 0, i32* %8
%9 = getelementptr inbounds i32, i32* %8, i64 1
store i32 1, i32* %9
store i32 1, i32* %6
%10 = getelementptr inbounds i32, i32* %6, i64 1
store i32 2, i32* %10
%11 = load i32, i32* %8
%12 = tail call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %11)
ret i32 0
}