#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 /*<<< orphan*/ free(int *);
int *malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ printf_A(int *);
int /*<<< orphan*/ quicksort(int *, int /*<<< orphan*/, int);
int rand();
int rand_select(int *, int /*<<< orphan*/, int, int);
int main(void) {
  int *A;
  int i;
  if ((A = malloc(1000 * sizeof(int))) == NULL) {
    printf("malloc error\n");
    return (-1);
  }
  for (i = 0; i < 1000; i++) {
    A[i] = rand() % 1000 + 1;
  }
  printf("i = %d\n", rand_select(A, 0, 999, 999));
  quicksort(A, 0, 999);
  printf_A(A);
  free(A);
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0xfa0,%edi
call   1080 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    11b1 <main+0x41>
lea    0xe62(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    123b <main+0xcb>
movl   $0x0,-0x14(%rbp)
cmpl   $0x3e8,-0x14(%rbp)
jge    11f0 <main+0x80>
mov    $0x0,%al
call   1090 <rand@plt>
mov    $0x3e8,%ecx
cltd
idiv   %ecx
add    $0x1,%edx
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    11b8 <main+0x48>
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x3e7,%ecx
mov    %ecx,%edx
call   1070 <rand_select@plt>
mov    %eax,%esi
lea    0xe03(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x3e7,%edx
call   1040 <quicksort@plt>
mov    -0x10(%rbp),%rdi
call   1060 <printf_A@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    $0xfa0,%edi
call   1090 <malloc@plt>
test   %rax,%rax
je     1221 <main+0xa1>
mov    %rax,%rbx
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   10a0 <rand@plt>
cltq
imul   $0x10624dd3,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rcx
add    %edx,%ecx
imul   $0x3e8,%ecx,%ecx
neg    %ecx
add    %ecx,%eax
add    $0x1,%eax
mov    %eax,(%rbx,%rbp,4)
add    $0x1,%rbp
cmp    $0x3e8,%rbp
jne    11a0 <main+0x20>
xor    %ebp,%ebp
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x3e7,%edx
mov    $0x3e7,%ecx
call   1080 <rand_select@plt>
lea    0xe09(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x3e7,%edx
call   1050 <quicksort@plt>
mov    %rbx,%rdi
call   1070 <printf_A@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
jmp    1232 <main+0xb2>
lea    0xde0(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"malloc error\0A\00"
@.str.1 = [8 x i8] c"i = %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32
store i32 0, i32* %1
%4 = call i32* @malloc(i32 4000)
store i32* %4, i32** %2
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %33
8: ; preds = %0
store i32 0, i32* %3
br label %9
9: ; preds = %20, %8
%10 = load i32, i32* %3
%11 = icmp slt i32 %10, 1000
br i1 %11, label %12, label %23
12: ; preds = %9
%13 = call i32 (...) @rand()
%14 = srem i32 %13, 1000
%15 = add nsw i32 %14, 1
%16 = load i32*, i32** %2
%17 = load i32, i32* %3
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i32, i32* %16, i64 %18
store i32 %15, i32* %19
br label %20
20: ; preds = %12
%21 = load i32, i32* %3
%22 = add nsw i32 %21, 1
store i32 %22, i32* %3
br label %9
23: ; preds = %9
%24 = load i32*, i32** %2
%25 = call i32 @rand_select(i32* %24, i32 0, i32 999, i32 999)
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %25)
%27 = load i32*, i32** %2
%28 = call i32 @quicksort(i32* %27, i32 0, i32 999)
%29 = load i32*, i32** %2
%30 = call i32 @printf_A(i32* %29)
%31 = load i32*, i32** %2
%32 = call i32 @free(i32* %31)
store i32 0, i32* %1
br label %33
33: ; preds = %23, %6
%34 = load i32, i32* %1
ret i32 %34
}

LLVM IR (-O2):
@.str.1 = [8 x i8] c"i = %d\0A\00"
@str = [13 x i8] c"malloc error\00"
define i32 @main() {
%1 = tail call i32* @malloc(i32 4000)
%2 = icmp eq i32* %1, null
br i1 %2, label %3, label %5
3: ; preds = %0
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
br label %19
5: ; preds = %0, %5
%6 = phi i64 [ %11, %5 ], [ 0, %0 ]
%7 = tail call i32 (...) @rand()
%8 = srem i32 %7, 1000
%9 = add nsw i32 %8, 1
%10 = getelementptr inbounds i32, i32* %1, i64 %6
store i32 %9, i32* %10
%11 = add nuw nsw i64 %6, 1
%12 = icmp eq i64 %11, 1000
br i1 %12, label %13, label %5
13: ; preds = %5
%14 = tail call i32 @rand_select(i32* %1, i32 0, i32 999, i32 999)
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %14)
%16 = tail call i32 @quicksort(i32* %1, i32 0, i32 999)
%17 = tail call i32 @printf_A(i32* %1)
%18 = tail call i32 @free(i32* %1)
br label %19
19: ; preds = %13, %3
%20 = phi i32 [ -1, %3 ], [ 0, %13 ]
ret i32 %20
}