#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();
void getrandom(int n, int *d) {
  int i;
  d[0] = n;
  d[n] = 1;
  for (i = n - 1; i >= 1; i--)
    d[i] = rand() % 2;
}


x86 assembly (clang, -O0):
<getrandom>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x4(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
movslq -0x4(%rbp),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    -0x4(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x1,-0x14(%rbp)
jl     1172 <getrandom+0x62>
mov    $0x0,%al
call   1030 <rand@plt>
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    1140 <getrandom+0x30>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getrandom>:
push   %r15
push   %r14
push   %rbx
mov    %edi,(%rsi)
movslq %edi,%rax
movl   $0x1,(%rsi,%rax,4)
cmp    $0x2,%eax
jl     1155 <getrandom+0x45>
mov    %rsi,%r14
mov    %edi,%ebx
add    $0x1,%rbx
nop
lea    -0x2(%rbx),%r15d
xor    %eax,%eax
call   1030 <rand@plt>
mov    %eax,%ecx
shr    $0x1f,%ecx
add    %eax,%ecx
and    $0xfffffffe,%ecx
sub    %ecx,%eax
mov    %eax,(%r14,%r15,4)
add    $0xffffffffffffffff,%rbx
cmp    $0x2,%rbx
ja     1130 <getrandom+0x20>
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @getrandom(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
store i32 %0, i32* %3
store i32* %1, i32** %4
%6 = load i32, i32* %3
%7 = load i32*, i32** %4
%8 = getelementptr inbounds i32, i32* %7, i64 0
store i32 %6, i32* %8
%9 = load i32*, i32** %4
%10 = load i32, i32* %3
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds i32, i32* %9, i64 %11
store i32 1, i32* %12
%13 = load i32, i32* %3
%14 = sub nsw i32 %13, 1
store i32 %14, i32* %5
br label %15
15: ; preds = %25, %2
%16 = load i32, i32* %5
%17 = icmp sge i32 %16, 1
br i1 %17, label %18, label %28
18: ; preds = %15
%19 = call i32 (...) @rand()
%20 = srem i32 %19, 2
%21 = load i32*, i32** %4
%22 = load i32, i32* %5
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i32, i32* %21, i64 %23
store i32 %20, i32* %24
br label %25
25: ; preds = %18
%26 = load i32, i32* %5
%27 = add nsw i32 %26, -1
store i32 %27, i32* %5
br label %15
28: ; preds = %15
ret void
}

LLVM IR (-O2):
define void @getrandom(i32 %0, i32* nocapture writeonly %1) {
store i32 %0, i32* %1
%3 = sext i32 %0 to i64
%4 = getelementptr inbounds i32, i32* %1, i64 %3
store i32 1, i32* %4
%5 = icmp sgt i32 %0, 1
br i1 %5, label %6, label %16
6: ; preds = %2
%7 = zext i32 %0 to i64
br label %8
8: ; preds = %6, %8
%9 = phi i64 [ %7, %6 ], [ %10, %8 ]
%10 = add nsw i64 %9, -1
%11 = tail call i32 (...) @rand()
%12 = srem i32 %11, 2
%13 = and i64 %10, 4294967295
%14 = getelementptr inbounds i32, i32* %1, i64 %13
store i32 %12, i32* %14
%15 = icmp ugt i64 %9, 2
br i1 %15, label %8, label %16
16: ; preds = %8, %2
ret void
}