#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 rand();
int foo(int n) {
  int arr[1000] = {};
  for (int i = 0; i < 1000; ++i) {
    arr[i] = rand() % 3;
    i += rand() % 100;
  }
  int res = 0;
  while (rand() % 3)
    ++res;
  if (n > 0)
    res += foo(n - 1);
  res += arr[res % 1000];
  return res + (rand() % 3);
}


x86 assembly (clang, -O0):
<foo>:
push   %rbp
mov    %rsp,%rbp
sub    $0xfc0,%rsp
mov    %edi,-0x4(%rbp)
lea    -0xfb0(%rbp),%rdi
xor    %esi,%esi
mov    $0xfa0,%edx
call   1030 <memset@plt>
movl   $0x0,-0xfb4(%rbp)
cmpl   $0x3e8,-0xfb4(%rbp)
jge    11b7 <foo+0x87>
mov    $0x0,%al
call   1050 <rand@plt>
mov    $0x3,%ecx
cltd
idiv   %ecx
movslq -0xfb4(%rbp),%rax
mov    %edx,-0xfb0(%rbp,%rax,4)
mov    $0x0,%al
call   1050 <rand@plt>
mov    $0x64,%ecx
cltd
idiv   %ecx
add    -0xfb4(%rbp),%edx
mov    %edx,-0xfb4(%rbp)
mov    -0xfb4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xfb4(%rbp)
jmp    115b <foo+0x2b>
movl   $0x0,-0xfb8(%rbp)
mov    $0x0,%al
call   1050 <rand@plt>
mov    $0x3,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
je     11ed <foo+0xbd>
mov    -0xfb8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xfb8(%rbp)
jmp    11c1 <foo+0x91>
cmpl   $0x0,-0x4(%rbp)
jle    120e <foo+0xde>
mov    -0x4(%rbp),%edi
sub    $0x1,%edi
call   1040 <foo@plt>
add    -0xfb8(%rbp),%eax
mov    %eax,-0xfb8(%rbp)
mov    -0xfb8(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
movslq %edx,%rax
mov    -0xfb0(%rbp,%rax,4),%eax
add    -0xfb8(%rbp),%eax
mov    %eax,-0xfb8(%rbp)
mov    -0xfb8(%rbp),%eax
mov    %eax,-0xfbc(%rbp)
mov    $0x0,%al
call   1050 <rand@plt>
mov    $0x3,%ecx
cltd
idiv   %ecx
mov    -0xfbc(%rbp),%eax
add    %edx,%eax
add    $0xfc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<foo>:
push   %rbp
push   %rbx
sub    $0xfa8,%rsp
mov    %edi,%ebx
mov    %rsp,%rdi
mov    $0xfa0,%edx
xor    %esi,%esi
call   1030 <memset@plt>
xor    %ebp,%ebp
nopl   0x0(%rax)
xor    %eax,%eax
call   1050 <rand@plt>
cltq
imul   $0x55555556,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
shr    $0x20,%rcx
add    %edx,%ecx
lea    (%rcx,%rcx,2),%ecx
sub    %ecx,%eax
movslq %ebp,%rbp
mov    %eax,(%rsp,%rbp,4)
xor    %eax,%eax
call   1050 <rand@plt>
cltq
imul   $0x51eb851f,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x25,%rcx
add    %edx,%ecx
imul   $0x64,%ecx,%ecx
sub    %ecx,%eax
add    %eax,%ebp
add    $0x1,%ebp
cmp    $0x3e8,%ebp
jl     1150 <foo+0x20>
mov    $0xffffffff,%ebp
nopl   0x0(%rax)
xor    %eax,%eax
call   1050 <rand@plt>
imul   $0xaaaaaaab,%eax,%eax
add    $0x2aaaaaaa,%eax
add    $0x1,%ebp
cmp    $0x55555554,%eax
ja     11b0 <foo+0x80>
test   %ebx,%ebx
jle    11dc <foo+0xac>
add    $0xffffffff,%ebx
mov    %ebx,%edi
call   1040 <foo@plt>
add    %eax,%ebp
movslq %ebp,%rbx
imul   $0x10624dd3,%rbx,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
shr    $0x20,%rax
sar    $0x6,%eax
add    %ecx,%eax
imul   $0x3e8,%eax,%eax
mov    %ebx,%ecx
sub    %eax,%ecx
movslq %ecx,%rax
add    (%rsp,%rax,4),%ebx
xor    %eax,%eax
call   1050 <rand@plt>
cltq
imul   $0x55555556,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
shr    $0x20,%rcx
add    %edx,%ecx
lea    (%rcx,%rcx,2),%ecx
sub    %ecx,%eax
add    %ebx,%eax
add    $0xfa8,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @foo(i32 %0) {
%2 = alloca [1000 x i32]
%3 = bitcast [1000 x i32]* %2 to i8*
br label %4
4: ; preds = %1, %4
%5 = phi i32 [ 0, %1 ], [ %13, %4 ]
%6 = tail call i32 (...) @rand()
%7 = srem i32 %6, 3
%8 = sext i32 %5 to i64
%9 = getelementptr inbounds [1000 x i32], [1000 x i32]* %2, i64 0, i64 %8
store i32 %7, i32* %9
%10 = tail call i32 (...) @rand()
%11 = srem i32 %10, 100
%12 = add nsw i32 %5, 1
%13 = add i32 %12, %11
%14 = icmp slt i32 %13, 1000
br i1 %14, label %4, label %15
15: ; preds = %4, %15
%16 = phi i32 [ %20, %15 ], [ 0, %4 ]
%17 = tail call i32 (...) @rand()
%18 = srem i32 %17, 3
%19 = icmp eq i32 %18, 0
%20 = add nuw nsw i32 %16, 1
br i1 %19, label %21, label %15
21: ; preds = %15
%22 = icmp sgt i32 %0, 0
br i1 %22, label %23, label %27
23: ; preds = %21
%24 = add nsw i32 %0, -1
%25 = tail call i32 @foo(i32 %24)
%26 = add nsw i32 %25, %16
br label %27
27: ; preds = %23, %21
%28 = phi i32 [ %26, %23 ], [ %16, %21 ]
%29 = srem i32 %28, 1000
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds [1000 x i32], [1000 x i32]* %2, i64 0, i64 %30
%32 = load i32, i32* %31
%33 = add nsw i32 %32, %28
%34 = tail call i32 (...) @rand()
%35 = srem i32 %34, 3
%36 = add nsw i32 %33, %35
ret i32 %36
}