#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 */
typedef int int64_t;

/* Variables and functions */
int *malloc(int);
int rand();
int64_t *rand_array(int64_t length, int64_t mod) {
  int64_t *arr = malloc(length * sizeof(int64_t));
  for (int64_t i = 0; i < 1000; i++) {
    for (int64_t j = 0; j < length; j++) {
      arr[j] = rand() % mod;
    }
  }
  return arr;
}


x86 assembly (clang, -O0):
<rand_array>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movslq -0x4(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1030 <malloc@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x3e8,-0x14(%rbp)
jge    119f <rand_array+0x7f>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    118c <rand_array+0x6c>
mov    $0x0,%al
call   1040 <rand@plt>
cltd
idivl  -0x8(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    115c <rand_array+0x3c>
jmp    1191 <rand_array+0x71>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1148 <rand_array+0x28>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rand_array>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r15d
mov    %edi,%r14d
lea    0x0(,%r14,4),%edi
call   1030 <malloc@plt>
mov    %rax,%r13
mov    %r14d,%ebp
xor    %r12d,%r12d
jmp    115d <rand_array+0x3d>
nopl   0x0(%rax)
add    $0x1,%r12d
cmp    $0x3e8,%r12d
je     118b <rand_array+0x6b>
test   %r14d,%r14d
jle    1150 <rand_array+0x30>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
xor    %eax,%eax
call   1040 <rand@plt>
cltd
idiv   %r15d
mov    %edx,0x0(%r13,%rbx,4)
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1170 <rand_array+0x50>
jmp    1150 <rand_array+0x30>
mov    %r13,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define noalias i32* @rand_array(i32 %0, i32 %1) {
%3 = shl i32 %0, 2
%4 = tail call i32* @malloc(i32 %3)
%5 = icmp sgt i32 %0, 0
%6 = zext i32 %0 to i64
br label %7
7: ; preds = %2, %10
%8 = phi i32 [ 0, %2 ], [ %11, %10 ]
br i1 %5, label %13, label %10
9: ; preds = %10
ret i32* %4
10: ; preds = %13, %7
%11 = add nuw nsw i32 %8, 1
%12 = icmp eq i32 %11, 1000
br i1 %12, label %9, label %7
13: ; preds = %7, %13
%14 = phi i64 [ %18, %13 ], [ 0, %7 ]
%15 = tail call i32 (...) @rand()
%16 = srem i32 %15, %1
%17 = getelementptr inbounds i32, i32* %4, i64 %14
store i32 %16, i32* %17
%18 = add nuw nsw i64 %14, 1
%19 = icmp eq i64 %18, %6
br i1 %19, label %10, label %13
}