#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 */
struct timespec {
  scalar_t__ tv_sec;
  scalar_t__ tv_nsec;
};

/* Variables and functions */
int /*<<< orphan*/ CLOCK_REALTIME;
int /*<<< orphan*/ assert(int);
scalar_t__ clock_gettime(int /*<<< orphan*/, struct timespec *);
int *dl_malloc(size_t);
int rand();
int /*<<< orphan*/ srand(unsigned int);
int /*<<< orphan*/ usleep(int);
int *rand_perm(int n) {
  int *v = dl_malloc((size_t)n * sizeof(int)), i;
  usleep(1);
  struct timespec tv;
  assert(clock_gettime(CLOCK_REALTIME, &tv) >= 0);
  srand((unsigned int)tv.tv_nsec * 123456789u +
        (unsigned int)tv.tv_sec * 987654321u);
  for (i = 0; i < n; i++) {
    v[i] = rand() % (i + 1);
  }
  return v;
}


x86 assembly (clang, -O0):
<rand_perm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
movslq -0x4(%rbp),%rdi
shl    $0x2,%rdi
call   1030 <dl_malloc@plt>
mov    %rax,-0x10(%rbp)
mov    $0x1,%edi
call   1080 <usleep@plt>
mov    0x2e53(%rip),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsi
call   1040 <clock_gettime@plt>
cmp    $0x0,%rax
setge  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x20(%rbp),%rax
imul   $0x75bcd15,%eax,%edi
mov    -0x28(%rbp),%rax
imul   $0x3ade68b1,%eax,%eax
add    %eax,%edi
call   1050 <srand@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    1200 <rand_perm+0xa0>
mov    $0x0,%al
call   1070 <rand@plt>
mov    -0x14(%rbp),%ecx
add    $0x1,%ecx
cltd
idiv   %ecx
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11cb <rand_perm+0x6b>
mov    -0x10(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rand_perm>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edi,%r14d
movslq %edi,%rbp
lea    0x0(,%rbp,4),%rdi
call   1030 <dl_malloc@plt>
mov    %rax,%r15
mov    $0x1,%edi
call   1080 <usleep@plt>
mov    0x2e4f(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
call   1040 <clock_gettime@plt>
shr    $0x3f,%rax
xor    $0x1,%eax
mov    %eax,%edi
call   1060 <assert@plt>
imul   $0x75bcd15,0x10(%rsp),%eax
07
imul   $0x3ade68b1,0x8(%rsp),%edi
3a
add    %eax,%edi
call   1050 <srand@plt>
test   %ebp,%ebp
jle    11ea <rand_perm+0x8a>
mov    %r14d,%ebp
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1070 <rand@plt>
lea    0x1(%rbx),%rcx
cltd
idiv   %ecx
mov    %edx,(%r15,%rbx,4)
mov    %rcx,%rbx
cmp    %rcx,%rbp
jne    11d0 <rand_perm+0x70>
mov    %r15,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timespec = type { i64, i64 }
@CLOCK_REALTIME = global i32 0
define i32* @rand_perm(i32 %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i32
%5 = alloca %struct.timespec
store i32 %0, i32* %2
%6 = load i32, i32* %2
%7 = sext i32 %6 to i64
%8 = mul i64 %7, 4
%9 = call i32* @dl_malloc(i64 %8)
store i32* %9, i32** %3
%10 = call i32 @usleep(i32 1)
%11 = load i32, i32* @CLOCK_REALTIME
%12 = call i64 @clock_gettime(i32 %11, %struct.timespec* %5)
%13 = icmp sge i64 %12, 0
%14 = zext i1 %13 to i32
%15 = call i32 @assert(i32 %14)
%16 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 1
%17 = load i64, i64* %16
%18 = trunc i64 %17 to i32
%19 = mul i32 %18, 123456789
%20 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 0
%21 = load i64, i64* %20
%22 = trunc i64 %21 to i32
%23 = mul i32 %22, 987654321
%24 = add i32 %19, %23
%25 = call i32 @srand(i32 %24)
store i32 0, i32* %4
br label %26
26: ; preds = %39, %1
%27 = load i32, i32* %4
%28 = load i32, i32* %2
%29 = icmp slt i32 %27, %28
br i1 %29, label %30, label %42
30: ; preds = %26
%31 = call i32 (...) @rand()
%32 = load i32, i32* %4
%33 = add nsw i32 %32, 1
%34 = srem i32 %31, %33
%35 = load i32*, i32** %3
%36 = load i32, i32* %4
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i32, i32* %35, i64 %37
store i32 %34, i32* %38
br label %39
39: ; preds = %30
%40 = load i32, i32* %4
%41 = add nsw i32 %40, 1
store i32 %41, i32* %4
br label %26
42: ; preds = %26
%43 = load i32*, i32** %3
ret i32* %43
}

LLVM IR (-O2):
%struct.timespec = type { i64, i64 }
@CLOCK_REALTIME = global i32 0
define i32* @rand_perm(i32 %0) {
%2 = alloca %struct.timespec
%3 = sext i32 %0 to i64
%4 = shl nsw i64 %3, 2
%5 = tail call i32* @dl_malloc(i64 %4)
%6 = tail call i32 @usleep(i32 1)
%7 = bitcast %struct.timespec* %2 to i8*
%8 = load i32, i32* @CLOCK_REALTIME
%9 = call i64 @clock_gettime(i32 %8, %struct.timespec* %2)
%10 = lshr i64 %9, 63
%11 = trunc i64 %10 to i32
%12 = xor i32 %11, 1
%13 = call i32 @assert(i32 %12)
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
%15 = load i64, i64* %14
%16 = trunc i64 %15 to i32
%17 = mul i32 %16, 123456789
%18 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
%19 = load i64, i64* %18
%20 = trunc i64 %19 to i32
%21 = mul i32 %20, 987654321
%22 = add i32 %21, %17
%23 = call i32 @srand(i32 %22)
%24 = icmp sgt i32 %0, 0
br i1 %24, label %25, label %35
25: ; preds = %1
%26 = zext i32 %0 to i64
br label %27
27: ; preds = %25, %27
%28 = phi i64 [ 0, %25 ], [ %30, %27 ]
%29 = call i32 (...) @rand()
%30 = add nuw nsw i64 %28, 1
%31 = trunc i64 %30 to i32
%32 = srem i32 %29, %31
%33 = getelementptr inbounds i32, i32* %5, i64 %28
store i32 %32, i32* %33
%34 = icmp eq i64 %30, %26
br i1 %34, label %35, label %27
35: ; preds = %27, %1
ret i32* %5
}