#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*/ find_kth_element_nr(int *, int /*<<< orphan*/, int, int);
int /*<<< orphan*/ find_kth_element_r(int *, int /*<<< orphan*/, int, int);
int /*<<< orphan*/ print_list(int *, int);
int /*<<< orphan*/ printf(char *, ...);
int rand();
int main() {
  int l[30];
  int size = 30;
  int k = 15;
  int c;
  for (c = 0; c < size; c++)
    l[c] = rand() % 100;
  printf("first list (solve recursively):\n");
  print_list(l, size - 1);
  find_kth_element_r(l, 0, size - 1, k);
  printf("\nelement in position %d is: %d\n", k, l[k]);
  for (c = 0; c < size; c++)
    l[c] = rand() % 100;
  printf("second list (solve non-recursively):\n");
  print_list(l, size - 1);
  find_kth_element_nr(l, 0, size - 1, k);
  printf("\nelement in position %d is: %d\n", k, l[k]);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x1e,-0x84(%rbp)
movl   $0xf,-0x88(%rbp)
movl   $0x0,-0x8c(%rbp)
mov    -0x8c(%rbp),%eax
cmp    -0x84(%rbp),%eax
jge    11c0 <main+0x70>
mov    $0x0,%al
call   1070 <rand@plt>
mov    $0x64,%ecx
cltd
idiv   %ecx
movslq -0x8c(%rbp),%rax
mov    %edx,-0x80(%rbp,%rax,4)
mov    -0x8c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8c(%rbp)
jmp    1180 <main+0x30>
lea    0xe39(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x80(%rbp),%rdi
mov    -0x84(%rbp),%esi
sub    $0x1,%esi
call   1040 <print_list@plt>
lea    -0x80(%rbp),%rdi
mov    -0x84(%rbp),%edx
sub    $0x1,%edx
mov    -0x88(%rbp),%ecx
xor    %esi,%esi
call   1060 <find_kth_element_r@plt>
mov    -0x88(%rbp),%esi
movslq -0x88(%rbp),%rax
mov    -0x80(%rbp,%rax,4),%edx
lea    0xe0f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x8c(%rbp)
mov    -0x8c(%rbp),%eax
cmp    -0x84(%rbp),%eax
jge    1263 <main+0x113>
mov    $0x0,%al
call   1070 <rand@plt>
mov    $0x64,%ecx
cltd
idiv   %ecx
movslq -0x8c(%rbp),%rax
mov    %edx,-0x80(%rbp,%rax,4)
mov    -0x8c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8c(%rbp)
jmp    1223 <main+0xd3>
lea    0xdd7(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x80(%rbp),%rdi
mov    -0x84(%rbp),%esi
sub    $0x1,%esi
call   1040 <print_list@plt>
lea    -0x80(%rbp),%rdi
mov    -0x84(%rbp),%edx
sub    $0x1,%edx
mov    -0x88(%rbp),%ecx
xor    %esi,%esi
call   1030 <find_kth_element_nr@plt>
mov    -0x88(%rbp),%esi
movslq -0x88(%rbp),%rax
mov    -0x80(%rbp,%rax,4),%edx
lea    0xd6c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x80,%rsp
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1080 <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
mov    %eax,(%rsp,%rbx,4)
add    $0x1,%rbx
cmp    $0x1e,%rbx
jne    1170 <main+0x10>
lea    0xe7a(%rip),%rdi
call   1030 <puts@plt>
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x1d,%esi
call   1050 <print_list@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x1d,%edx
mov    $0xf,%ecx
call   1070 <find_kth_element_r@plt>
mov    0x3c(%rsp),%edx
lea    0xe26(%rip),%rdi
mov    $0xf,%esi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1080 <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
mov    %eax,(%rsp,%rbx,4)
add    $0x1,%rbx
cmp    $0x1e,%rbx
jne    11f0 <main+0x90>
lea    0xe1a(%rip),%rdi
call   1030 <puts@plt>
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x1d,%esi
call   1050 <print_list@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x1d,%edx
mov    $0xf,%ecx
call   1040 <find_kth_element_nr@plt>
mov    0x3c(%rsp),%edx
lea    0xda6(%rip),%rdi
mov    $0xf,%esi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
add    $0x80,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [33 x i8] c"first list (solve recursively):\0A\00"
@.str.1 = [32 x i8] c"\0Aelement in position %d is: %d\0A\00"
@.str.2 = [38 x i8] c"second list (solve non-recursively):\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [30 x i32]
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
store i32 30, i32* %3
store i32 15, i32* %4
store i32 0, i32* %5
br label %6
6: ; preds = %16, %0
%7 = load i32, i32* %5
%8 = load i32, i32* %3
%9 = icmp slt i32 %7, %8
br i1 %9, label %10, label %19
10: ; preds = %6
%11 = call i32 (...) @rand()
%12 = srem i32 %11, 100
%13 = load i32, i32* %5
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds [30 x i32], [30 x i32]* %2, i64 0, i64 %14
store i32 %12, i32* %15
br label %16
16: ; preds = %10
%17 = load i32, i32* %5
%18 = add nsw i32 %17, 1
store i32 %18, i32* %5
br label %6
19: ; preds = %6
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
%21 = getelementptr inbounds [30 x i32], [30 x i32]* %2, i64 0, i64 0
%22 = load i32, i32* %3
%23 = sub nsw i32 %22, 1
%24 = call i32 @print_list(i32* %21, i32 %23)
%25 = getelementptr inbounds [30 x i32], [30 x i32]* %2, i64 0, i64 0
%26 = load i32, i32* %3
%27 = sub nsw i32 %26, 1
%28 = load i32, i32* %4
%29 = call i32 @find_kth_element_r(i32* %25, i32 0, i32 %27, i32 %28)
%30 = load i32, i32* %4
%31 = load i32, i32* %4
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds [30 x i32], [30 x i32]* %2, i64 0, i64 %32
%34 = load i32, i32* %33
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i32 %30, i32 %34)
store i32 0, i32* %5
br label %36
36: ; preds = %46, %19
%37 = load i32, i32* %5
%38 = load i32, i32* %3
%39 = icmp slt i32 %37, %38
br i1 %39, label %40, label %49
40: ; preds = %36
%41 = call i32 (...) @rand()
%42 = srem i32 %41, 100
%43 = load i32, i32* %5
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds [30 x i32], [30 x i32]* %2, i64 0, i64 %44
store i32 %42, i32* %45
br label %46
46: ; preds = %40
%47 = load i32, i32* %5
%48 = add nsw i32 %47, 1
store i32 %48, i32* %5
br label %36
49: ; preds = %36
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0))
%51 = getelementptr inbounds [30 x i32], [30 x i32]* %2, i64 0, i64 0
%52 = load i32, i32* %3
%53 = sub nsw i32 %52, 1
%54 = call i32 @print_list(i32* %51, i32 %53)
%55 = getelementptr inbounds [30 x i32], [30 x i32]* %2, i64 0, i64 0
%56 = load i32, i32* %3
%57 = sub nsw i32 %56, 1
%58 = load i32, i32* %4
%59 = call i32 @find_kth_element_nr(i32* %55, i32 0, i32 %57, i32 %58)
%60 = load i32, i32* %4
%61 = load i32, i32* %4
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds [30 x i32], [30 x i32]* %2, i64 0, i64 %62
%64 = load i32, i32* %63
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i32 %60, i32 %64)
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [32 x i8] c"\0Aelement in position %d is: %d\0A\00"
@str = [32 x i8] c"first list (solve recursively):\00"
@str.3 = [37 x i8] c"second list (solve non-recursively):\00"
define i32 @main() {
%1 = alloca [30 x i32]
%2 = bitcast [30 x i32]* %1 to i8*
br label %3
3: ; preds = %0, %3
%4 = phi i64 [ 0, %0 ], [ %8, %3 ]
%5 = tail call i32 (...) @rand()
%6 = srem i32 %5, 100
%7 = getelementptr inbounds [30 x i32], [30 x i32]* %1, i64 0, i64 %4
store i32 %6, i32* %7
%8 = add nuw nsw i64 %4, 1
%9 = icmp eq i64 %8, 30
br i1 %9, label %10, label %3
10: ; preds = %3
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str, i64 0, i64 0))
%12 = getelementptr inbounds [30 x i32], [30 x i32]* %1, i64 0, i64 0
%13 = call i32 @print_list(i32* %12, i32 29)
%14 = call i32 @find_kth_element_r(i32* %12, i32 0, i32 29, i32 15)
%15 = getelementptr inbounds [30 x i32], [30 x i32]* %1, i64 0, i64 15
%16 = load i32, i32* %15
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i32 15, i32 %16)
br label %18
18: ; preds = %10, %18
%19 = phi i64 [ 0, %10 ], [ %23, %18 ]
%20 = call i32 (...) @rand()
%21 = srem i32 %20, 100
%22 = getelementptr inbounds [30 x i32], [30 x i32]* %1, i64 0, i64 %19
store i32 %21, i32* %22
%23 = add nuw nsw i64 %19, 1
%24 = icmp eq i64 %23, 30
br i1 %24, label %25, label %18
25: ; preds = %18
%26 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @str.3, i64 0, i64 0))
%27 = call i32 @print_list(i32* %12, i32 29)
%28 = call i32 @find_kth_element_nr(i32* %12, i32 0, i32 29, i32 15)
%29 = load i32, i32* %15
%30 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i32 15, i32 %29)
ret i32 0
}