#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 next_lcg();
void *x_malloc(int);
int **allocate_random_triangle(int size) {
  int **triangle = x_malloc(size * sizeof(int *));
  for (int i = 0; i < size; i++) {
    int *row = x_malloc((i + 1) * sizeof(int));
    for (int j = 0; j <= i; j++)
      row[j] = next_lcg();
    triangle[i] = row;
  }
  return triangle;
}


x86 assembly (clang, -O0):
<allocate_random_triangle>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
movslq -0x4(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1030 <x_malloc@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    11bb <allocate_random_triangle+0x9b>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x2,%rax
mov    %eax,%edi
call   1030 <x_malloc@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jg     119d <allocate_random_triangle+0x7d>
mov    $0x0,%al
call   1040 <next_lcg@plt>
mov    %eax,%edx
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    116f <allocate_random_triangle+0x4f>
mov    -0x20(%rbp),%rdx
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1145 <allocate_random_triangle+0x25>
mov    -0x10(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<allocate_random_triangle>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%ebx
lea    0x0(,%rbx,8),%edi
call   1030 <x_malloc@plt>
mov    %rax,(%rsp)
test   %ebx,%ebx
jle    1198 <allocate_random_triangle+0x78>
mov    %ebx,%r15d
mov    $0x1,%r12d
xor    %r13d,%r13d
nopl   (%rax)
lea    0x1(%r13),%rbp
lea    0x0(,%rbp,4),%edi
call   1030 <x_malloc@plt>
mov    %rax,%rbx
xor    %r14d,%r14d
cs nopw 0x0(%rax,%rax,1)
xor    %eax,%eax
call   1040 <next_lcg@plt>
mov    %eax,(%rbx,%r14,4)
add    $0x1,%r14
cmp    %r14,%r12
jne    1170 <allocate_random_triangle+0x50>
mov    (%rsp),%rax
mov    %rbx,(%rax,%r13,8)
add    $0x1,%r12
mov    %rbp,%r13
cmp    %r15,%rbp
jne    1150 <allocate_random_triangle+0x30>
mov    (%rsp),%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32** @allocate_random_triangle(i32 %0) {
%2 = shl i32 %0, 3
%3 = tail call i8* @x_malloc(i32 %2)
%4 = bitcast i8* %3 to i32**
%5 = icmp sgt i32 %0, 0
br i1 %5, label %6, label %8
6: ; preds = %1
%7 = zext i32 %0 to i64
br label %9
8: ; preds = %17, %1
ret i32** %4
9: ; preds = %6, %17
%10 = phi i64 [ 0, %6 ], [ %12, %17 ]
%11 = phi i64 [ 1, %6 ], [ %20, %17 ]
%12 = add nuw nsw i64 %10, 1
%13 = trunc i64 %12 to i32
%14 = shl i32 %13, 2
%15 = tail call i8* @x_malloc(i32 %14)
%16 = bitcast i8* %15 to i32*
br label %22
17: ; preds = %22
%18 = getelementptr inbounds i32*, i32** %4, i64 %10
%19 = bitcast i32** %18 to i8**
store i8* %15, i8** %19
%20 = add nuw nsw i64 %11, 1
%21 = icmp eq i64 %12, %7
br i1 %21, label %8, label %9
22: ; preds = %9, %22
%23 = phi i64 [ 0, %9 ], [ %26, %22 ]
%24 = tail call i32 (...) @next_lcg()
%25 = getelementptr inbounds i32, i32* %16, i64 %23
store i32 %24, i32* %25
%26 = add nuw nsw i64 %23, 1
%27 = icmp eq i64 %26, %11
br i1 %27, label %17, label %22
}