#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] = p[0];
  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    -0x18(%rbp),%rax
mov    (%rax),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,0x8(%rax)
movl   $0x0,-0x8(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11bc <main+0x9c>
movl   $0x0,-0xc(%rbp)
cmpl   $0x2,-0xc(%rbp)
jge    11a9 <main+0x89>
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    1178 <main+0x58>
jmp    11ae <main+0x8e>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1167 <main+0x47>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%esi
lea    0xe34(%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    %rax,0x8(%rbx)
movabs $0x200000001,%rcx
mov    %rcx,(%rax)
lea    0xead(%rip),%rdi
mov    $0x1,%esi
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 = load i32**, i32*** %4
%12 = getelementptr inbounds i32*, i32** %11, i64 0
%13 = load i32*, i32** %12
%14 = load i32**, i32*** %4
%15 = getelementptr inbounds i32*, i32** %14, i64 1
store i32* %13, i32** %15
store i32 0, i32* %2
br label %16
16: ; preds = %39, %0
%17 = load i32, i32* %2
%18 = icmp slt i32 %17, 2
br i1 %18, label %19, label %42
19: ; preds = %16
store i32 0, i32* %3
br label %20
20: ; preds = %35, %19
%21 = load i32, i32* %3
%22 = icmp slt i32 %21, 2
br i1 %22, label %23, label %38
23: ; preds = %20
%24 = load i32, i32* %2
%25 = load i32, i32* %3
%26 = add nsw i32 %24, %25
%27 = load i32**, i32*** %4
%28 = load i32, i32* %2
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i32*, i32** %27, i64 %29
%31 = load i32*, i32** %30
%32 = load i32, i32* %3
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i32, i32* %31, i64 %33
store i32 %26, i32* %34
br label %35
35: ; preds = %23
%36 = load i32, i32* %3
%37 = add nsw i32 %36, 1
store i32 %37, i32* %3
br label %20
38: ; preds = %20
br label %39
39: ; preds = %38
%40 = load i32, i32* %2
%41 = add nsw i32 %40, 1
store i32 %41, i32* %2
br label %16
42: ; preds = %16
%43 = load i32**, i32*** %4
%44 = getelementptr inbounds i32*, i32** %43, i64 0
%45 = load i32*, i32** %44
%46 = getelementptr inbounds i32, i32* %45, i64 0
%47 = load i32, i32* %46
%48 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %47)
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 = getelementptr inbounds i32*, i32** %2, i64 1
store i32* %4, i32** %5
%6 = getelementptr inbounds i32, i32* %4, i64 1
store i32 1, i32* %4
store i32 2, i32* %6
%7 = tail call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 1)
ret i32 0
}