#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 allocate_memory_for_weights(long max_precincts, int max_quality_layers,
                                long ***weights) {
  int i;
  *weights = NULL;
  *weights = (long **)malloc(max_precincts * (sizeof(long *)));
  if (*weights == NULL)
    return -1;
  for (i = 0; i < max_precincts; i++) {
    (*weights)[i] = (long *)malloc(max_quality_layers * (sizeof(long)));
  }
  return 1;
}


x86 assembly (clang, -O0):
<allocate_memory_for_weights>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1030 <malloc@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1161 <allocate_memory_for_weights+0x51>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ac <allocate_memory_for_weights+0x9c>
movl   $0x0,-0x24(%rbp)
movslq -0x24(%rbp),%rax
cmp    -0x10(%rbp),%rax
jge    11a5 <allocate_memory_for_weights+0x95>
movslq -0x14(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1030 <malloc@plt>
mov    %rax,%rdx
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
movslq -0x24(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1168 <allocate_memory_for_weights+0x58>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<allocate_memory_for_weights>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r12
mov    %esi,%r14d
mov    %rdi,%rbp
movq   $0x0,(%rdx)
lea    0x0(,%rdi,8),%edi
call   1030 <malloc@plt>
mov    %rax,(%r12)
test   %rax,%rax
je     116b <allocate_memory_for_weights+0x5b>
mov    $0x1,%r15d
test   %rbp,%rbp
jle    1171 <allocate_memory_for_weights+0x61>
shl    $0x3,%r14d
xor    %ebx,%ebx
xchg   %ax,%ax
mov    %r14d,%edi
call   1030 <malloc@plt>
mov    (%r12),%rcx
mov    %rax,(%rcx,%rbx,8)
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1150 <allocate_memory_for_weights+0x40>
jmp    1171 <allocate_memory_for_weights+0x61>
mov    $0xffffffff,%r15d
mov    %r15d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @allocate_memory_for_weights(i64 %0, i32 %1, i64*** nocapture %2) {
store i64** null, i64*** %2
%4 = trunc i64 %0 to i32
%5 = shl i32 %4, 3
%6 = tail call i64 @malloc(i32 %5)
%7 = inttoptr i64 %6 to i64**
store i64** %7, i64*** %2
%8 = icmp eq i64 %6, 0
br i1 %8, label %21, label %9
9: ; preds = %3
%10 = icmp sgt i64 %0, 0
br i1 %10, label %11, label %21
11: ; preds = %9
%12 = shl i32 %1, 3
br label %13
13: ; preds = %11, %13
%14 = phi i64 [ 0, %11 ], [ %19, %13 ]
%15 = tail call i64 @malloc(i32 %12)
%16 = inttoptr i64 %15 to i64*
%17 = load i64**, i64*** %2
%18 = getelementptr inbounds i64*, i64** %17, i64 %14
store i64* %16, i64** %18
%19 = add nuw nsw i64 %14, 1
%20 = icmp eq i64 %19, %0
br i1 %20, label %21, label %13
21: ; preds = %13, %9, %3
%22 = phi i32 [ -1, %3 ], [ 1, %9 ], [ 1, %13 ]
ret i32 %22
}