#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*/ exit(int);
int *malloc(int);
int /*<<< orphan*/ memcpy(int *, int *, int);
int omp_get_num_threads();
double omp_get_wtime();
int /*<<< orphan*/ omp_set_dynamic(int /*<<< orphan*/);
int /*<<< orphan*/ omp_set_nested(int);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ puts(char *);
int rand();
int /*<<< orphan*/ sort1(int *, int);
int /*<<< orphan*/ sort2(int *, int);
int /*<<< orphan*/ srand(int);
int strtoul(char *, int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ verify(char *, double, int *, int);
int main(int argc, char **argv) {
  int i, count = 1000000;
  double stime;
  int *unsorted, *sorted, num_threads;
  if (argc >= 2)
    count = strtoul(argv[1], NULL, 0);
  unsorted = malloc(count * sizeof(int));
  sorted = malloc(count * sizeof(int));
  if (unsorted == NULL || sorted == NULL) {
    puts("allocation failure");
    exit(1);
  }
  srand(0xdeadbeef);
  for (i = 0; i < count; i++)
    unsorted[i] = rand();
  omp_set_nested(1);
  omp_set_dynamic(0);
#pragma omp parallel
#pragma omp single nowait
  num_threads = omp_get_num_threads();
  printf("Threads: %d\n", num_threads);
  memcpy(sorted, unsorted, count * sizeof(int));
  stime = omp_get_wtime();
  sort1(sorted, count);
  verify("sort1", stime, sorted, count);
  memcpy(sorted, unsorted, count * sizeof(int));
  stime = omp_get_wtime();
  sort2(sorted, count);
  verify("sort2", stime, sorted, count);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xf4240,-0x18(%rbp)
cmpl   $0x2,-0x8(%rbp)
jl     122d <main+0x3d>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   10e0 <strtoul@plt>
mov    %eax,-0x18(%rbp)
movslq -0x18(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   10c0 <malloc@plt>
mov    %rax,-0x28(%rbp)
movslq -0x18(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   10c0 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1269 <main+0x79>
cmpq   $0x0,-0x30(%rbp)
jne    127f <main+0x8f>
lea    0xd90(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
mov    $0xdeadbeef,%edi
call   1070 <srand@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    12be <main+0xce>
mov    $0x0,%al
call   1100 <rand@plt>
mov    %eax,%edx
mov    -0x28(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1290 <main+0xa0>
mov    $0x1,%edi
call   1090 <omp_set_nested@plt>
xor    %edi,%edi
call   1080 <omp_set_dynamic@plt>
mov    $0x0,%al
call   1060 <omp_get_num_threads@plt>
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%esi
lea    0xd30(%rip),%rdi
call   1050 <printf@plt>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
movslq -0x18(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edx
call   10b0 <memcpy@plt>
mov    $0x0,%al
call   1040 <omp_get_wtime@plt>
movsd  %xmm0,-0x20(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%esi
call   1110 <sort1@plt>
movsd  -0x20(%rbp),%xmm0
mov    -0x30(%rbp),%rsi
mov    -0x18(%rbp),%edx
lea    0xcf6(%rip),%rdi
call   10d0 <verify@plt>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
movslq -0x18(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edx
call   10b0 <memcpy@plt>
mov    $0x0,%al
call   1040 <omp_get_wtime@plt>
movsd  %xmm0,-0x20(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%esi
call   10a0 <sort2@plt>
movsd  -0x20(%rbp),%xmm0
mov    -0x30(%rbp),%rsi
mov    -0x18(%rbp),%edx
lea    0xcb5(%rip),%rdi
call   10d0 <verify@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0xf4240,%r14d
cmp    $0x2,%edi
jl     1216 <main+0x26>
mov    0x8(%rsi),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   10e0 <strtoul@plt>
mov    %eax,%r14d
lea    0x0(,%r14,4),%r15d
mov    %r15d,%edi
call   10c0 <malloc@plt>
mov    %rax,%r13
mov    %r15d,%edi
call   10c0 <malloc@plt>
test   %r13,%r13
je     1324 <main+0x134>
mov    %rax,%r12
test   %rax,%rax
je     1324 <main+0x134>
mov    $0xdeadbeef,%edi
call   1070 <srand@plt>
test   %r14d,%r14d
jle    1275 <main+0x85>
mov    %r14d,%ebx
xor    %ebp,%ebp
nopw   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1100 <rand@plt>
mov    %eax,0x0(%r13,%rbp,4)
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    1260 <main+0x70>
mov    $0x1,%edi
call   1090 <omp_set_nested@plt>
xor    %edi,%edi
call   1080 <omp_set_dynamic@plt>
xor    %eax,%eax
call   1060 <omp_get_num_threads@plt>
lea    0xd7f(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
mov    %r12,%rdi
mov    %r13,%rsi
mov    %r15d,%edx
call   10b0 <memcpy@plt>
xor    %eax,%eax
call   1040 <omp_get_wtime@plt>
movsd  %xmm0,(%rsp)
mov    %r12,%rdi
mov    %r14d,%esi
call   1110 <sort1@plt>
lea    0xd59(%rip),%rdi
movsd  (%rsp),%xmm0
mov    %r12,%rsi
mov    %r14d,%edx
call   10d0 <verify@plt>
mov    %r12,%rdi
mov    %r13,%rsi
mov    %r15d,%edx
call   10b0 <memcpy@plt>
xor    %eax,%eax
call   1040 <omp_get_wtime@plt>
movsd  %xmm0,(%rsp)
mov    %r12,%rdi
mov    %r14d,%esi
call   10a0 <sort2@plt>
lea    0xd23(%rip),%rdi
movsd  (%rsp),%xmm0
mov    %r12,%rsi
mov    %r14d,%edx
call   10d0 <verify@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcd5(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>

LLVM IR (-O0):
@.str = [19 x i8] c"allocation failure\00"
@.str.1 = [13 x i8] c"Threads: %d\0A\00"
@.str.2 = [6 x i8] c"sort1\00"
@.str.3 = [6 x i8] c"sort2\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca double
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 1000000, i32* %7
%12 = load i32, i32* %4
%13 = icmp sge i32 %12, 2
br i1 %13, label %14, label %19
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = call i32 @strtoul(i8* %17, i32* null, i32 0)
store i32 %18, i32* %7
br label %19
19: ; preds = %14, %2
%20 = load i32, i32* %7
%21 = sext i32 %20 to i64
%22 = mul i64 %21, 4
%23 = trunc i64 %22 to i32
%24 = call i32* @malloc(i32 %23)
store i32* %24, i32** %9
%25 = load i32, i32* %7
%26 = sext i32 %25 to i64
%27 = mul i64 %26, 4
%28 = trunc i64 %27 to i32
%29 = call i32* @malloc(i32 %28)
store i32* %29, i32** %10
%30 = load i32*, i32** %9
%31 = icmp eq i32* %30, null
br i1 %31, label %35, label %32
32: ; preds = %19
%33 = load i32*, i32** %10
%34 = icmp eq i32* %33, null
br i1 %34, label %35, label %38
35: ; preds = %32, %19
%36 = call i32 @puts(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %32
%39 = call i32 @srand(i32 -559038737)
store i32 0, i32* %6
br label %40
40: ; preds = %50, %38
%41 = load i32, i32* %6
%42 = load i32, i32* %7
%43 = icmp slt i32 %41, %42
br i1 %43, label %44, label %53
44: ; preds = %40
%45 = call i32 (...) @rand()
%46 = load i32*, i32** %9
%47 = load i32, i32* %6
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i32, i32* %46, i64 %48
store i32 %45, i32* %49
br label %50
50: ; preds = %44
%51 = load i32, i32* %6
%52 = add nsw i32 %51, 1
store i32 %52, i32* %6
br label %40
53: ; preds = %40
%54 = call i32 @omp_set_nested(i32 1)
%55 = call i32 @omp_set_dynamic(i32 0)
%56 = call i32 (...) @omp_get_num_threads()
store i32 %56, i32* %11
%57 = load i32, i32* %11
%58 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %57)
%59 = load i32*, i32** %10
%60 = load i32*, i32** %9
%61 = load i32, i32* %7
%62 = sext i32 %61 to i64
%63 = mul i64 %62, 4
%64 = trunc i64 %63 to i32
%65 = call i32 @memcpy(i32* %59, i32* %60, i32 %64)
%66 = call double (...) @omp_get_wtime()
store double %66, double* %8
%67 = load i32*, i32** %10
%68 = load i32, i32* %7
%69 = call i32 @sort1(i32* %67, i32 %68)
%70 = load double, double* %8
%71 = load i32*, i32** %10
%72 = load i32, i32* %7
%73 = call i32 @verify(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), double %70, i32* %71, i32 %72)
%74 = load i32*, i32** %10
%75 = load i32*, i32** %9
%76 = load i32, i32* %7
%77 = sext i32 %76 to i64
%78 = mul i64 %77, 4
%79 = trunc i64 %78 to i32
%80 = call i32 @memcpy(i32* %74, i32* %75, i32 %79)
%81 = call double (...) @omp_get_wtime()
store double %81, double* %8
%82 = load i32*, i32** %10
%83 = load i32, i32* %7
%84 = call i32 @sort2(i32* %82, i32 %83)
%85 = load double, double* %8
%86 = load i32*, i32** %10
%87 = load i32, i32* %7
%88 = call i32 @verify(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), double %85, i32* %86, i32 %87)
ret i32 0
}

LLVM IR (-O2):
@.str = [19 x i8] c"allocation failure\00"
@.str.1 = [13 x i8] c"Threads: %d\0A\00"
@.str.2 = [6 x i8] c"sort1\00"
@.str.3 = [6 x i8] c"sort2\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp sgt i32 %0, 1
br i1 %3, label %4, label %8
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = tail call i32 @strtoul(i8* nocapture %6, i32* null, i32 0)
br label %8
8: ; preds = %4, %2
%9 = phi i32 [ %7, %4 ], [ 1000000, %2 ]
%10 = shl i32 %9, 2
%11 = tail call i32* @malloc(i32 %10)
%12 = tail call i32* @malloc(i32 %10)
%13 = icmp eq i32* %11, null
%14 = icmp eq i32* %12, null
%15 = or i1 %13, %14
br i1 %15, label %16, label %19
16: ; preds = %8
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%18 = tail call i32 @exit(i32 1)
unreachable
19: ; preds = %8
%20 = tail call i32 @srand(i32 -559038737)
%21 = icmp sgt i32 %9, 0
br i1 %21, label %22, label %30
22: ; preds = %19
%23 = zext i32 %9 to i64
br label %24
24: ; preds = %22, %24
%25 = phi i64 [ 0, %22 ], [ %28, %24 ]
%26 = tail call i32 (...) @rand()
%27 = getelementptr inbounds i32, i32* %11, i64 %25
store i32 %26, i32* %27
%28 = add nuw nsw i64 %25, 1
%29 = icmp eq i64 %28, %23
br i1 %29, label %30, label %24
30: ; preds = %24, %19
%31 = tail call i32 @omp_set_nested(i32 1)
%32 = tail call i32 @omp_set_dynamic(i32 0)
%33 = tail call i32 (...) @omp_get_num_threads()
%34 = tail call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %33)
%35 = tail call i32 @memcpy(i32* %12, i32* %11, i32 %10)
%36 = tail call double (...) @omp_get_wtime()
%37 = tail call i32 @sort1(i32* %12, i32 %9)
%38 = tail call i32 @verify(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), double %36, i32* %12, i32 %9)
%39 = tail call i32 @memcpy(i32* %12, i32* %11, i32 %10)
%40 = tail call double (...) @omp_get_wtime()
%41 = tail call i32 @sort2(i32* %12, i32 %9)
%42 = tail call i32 @verify(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), double %40, i32* %12, i32 %9)
ret i32 0
}