#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 *findNextGreater(int *, int);
int /*<<< orphan*/ free(int *);
int /*<<< orphan*/ printf(char *, ...);
void problem1() {
  printf("###### Problem 1 ######\n\n");
  int findNextGreaterInput[] = {12, 15, 22, 9, 7, 2, 18, 23, 27};
  int findNextGreaterLength = 9;
  int i;
  printf("Input:\t");
  for (i = 0; i < findNextGreaterLength; i++) {
    printf("%i\t", findNextGreaterInput[i]);
  }
  printf("\n");
  int *findNextGreaterOutput =
      findNextGreater(findNextGreaterInput, findNextGreaterLength);
  printf("Output:\t");
  for (i = 0; i < findNextGreaterLength; i++) {
    printf("%i\t", findNextGreaterOutput[i]);
  }
  free(findNextGreaterOutput);
  printf("\n\n");
}


x86 assembly (clang, -O0):
<problem1>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
lea    0xeb1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x30(%rbp),%rdi
lea    0xecf(%rip),%rsi
mov    $0x24,%edx
call   1050 <memcpy@plt>
movl   $0x9,-0x34(%rbp)
lea    0xea1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%eax
cmp    -0x34(%rbp),%eax
jge    11b7 <problem1+0x77>
movslq -0x38(%rbp),%rax
mov    -0x30(%rbp,%rax,4),%esi
lea    0xe80(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    1187 <problem1+0x47>
lea    0xe5a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x30(%rbp),%rdi
mov    -0x34(%rbp),%esi
call   1060 <findNextGreater@plt>
mov    %rax,-0x40(%rbp)
lea    0xe4a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%eax
cmp    -0x34(%rbp),%eax
jge    121d <problem1+0xdd>
mov    -0x40(%rbp),%rax
movslq -0x38(%rbp),%rcx
mov    (%rax,%rcx,4),%esi
lea    0xe1a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    11ea <problem1+0xaa>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
lea    0xdea(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<problem1>:
push   %r14
push   %rbx
sub    $0x28,%rsp
lea    0xedb(%rip),%rdi
call   1050 <puts@plt>
mov    0xeb7(%rip),%eax
mov    %eax,0x20(%rsp)
movaps 0xe9c(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movaps 0xe80(%rip),%xmm0
movaps %xmm0,(%rsp)
lea    0xe99(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe93(%rip),%r14
mov    %r14,%rdi
mov    $0xc,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r14,%rdi
mov    $0xf,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r14,%rdi
mov    $0x16,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r14,%rdi
mov    $0x9,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r14,%rdi
mov    $0x7,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r14,%rdi
mov    $0x2,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r14,%rdi
mov    $0x12,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r14,%rdi
mov    $0x17,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r14,%rdi
mov    $0x1b,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xa,%edi
call   1040 <putchar@plt>
mov    %rsp,%rdi
mov    $0x9,%esi
call   1070 <findNextGreater@plt>
mov    %rax,%rbx
lea    0xdef(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    (%rbx),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x4(%rbx),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x8(%rbx),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0xc(%rbx),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x10(%rbx),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x14(%rbx),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x18(%rbx),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x1c(%rbx),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x20(%rbx),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
lea    0xd85(%rip),%rdi
call   1050 <puts@plt>
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [26 x i8] c"###### Problem 1 ######\0A\0A\00"
@__const.problem1.findNextGreaterInput = [9 x i32] [i32 12, i32 15, i32 22, i32 9, i32 7, i32 2, i32 18, i32 23, i32 27]
@.str.1 = [8 x i8] c"Input:\09\00"
@.str.2 = [4 x i8] c"%i\09\00"
@.str.3 = [2 x i8] c"\0A\00"
@.str.4 = [9 x i8] c"Output:\09\00"
@.str.5 = [3 x i8] c"\0A\0A\00"
define void @problem1() {
%1 = alloca [9 x i32]
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32*
%5 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%6 = bitcast [9 x i32]* %1 to i8*
store i32 9, i32* %2
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %3
br label %8
8: ; preds = %18, %0
%9 = load i32, i32* %3
%10 = load i32, i32* %2
%11 = icmp slt i32 %9, %10
br i1 %11, label %12, label %21
12: ; preds = %8
%13 = load i32, i32* %3
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds [9 x i32], [9 x i32]* %1, i64 0, i64 %14
%16 = load i32, i32* %15
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %16)
br label %18
18: ; preds = %12
%19 = load i32, i32* %3
%20 = add nsw i32 %19, 1
store i32 %20, i32* %3
br label %8
21: ; preds = %8
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%23 = getelementptr inbounds [9 x i32], [9 x i32]* %1, i64 0, i64 0
%24 = load i32, i32* %2
%25 = call i32* @findNextGreater(i32* %23, i32 %24)
store i32* %25, i32** %4
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %3
br label %27
27: ; preds = %38, %21
%28 = load i32, i32* %3
%29 = load i32, i32* %2
%30 = icmp slt i32 %28, %29
br i1 %30, label %31, label %41
31: ; preds = %27
%32 = load i32*, i32** %4
%33 = load i32, i32* %3
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i32, i32* %32, i64 %34
%36 = load i32, i32* %35
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %36)
br label %38
38: ; preds = %31
%39 = load i32, i32* %3
%40 = add nsw i32 %39, 1
store i32 %40, i32* %3
br label %27
41: ; preds = %27
%42 = load i32*, i32** %4
%43 = call i32 @free(i32* %42)
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@__const.problem1.findNextGreaterInput = [9 x i32] [i32 12, i32 15, i32 22, i32 9, i32 7, i32 2, i32 18, i32 23, i32 27]
@.str.1 = [8 x i8] c"Input:\09\00"
@.str.2 = [4 x i8] c"%i\09\00"
@.str.4 = [9 x i8] c"Output:\09\00"
@str = [25 x i8] c"###### Problem 1 ######\0A\00"
@str.6 = [2 x i8] c"\0A\00"
define void @problem1() {
%1 = alloca [9 x i32]
%2 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
%3 = bitcast [9 x i32]* %1 to i8*
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%5 = getelementptr inbounds [9 x i32], [9 x i32]* %1, i64 0, i64 0
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 12)
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 15)
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 22)
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 9)
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 7)
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 2)
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 18)
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 23)
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 27)
%15 = tail call i32 @putchar(i32 10)
%16 = call i32* @findNextGreater(i32* %5, i32 9)
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
%18 = load i32, i32* %16
%19 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %18)
%20 = getelementptr inbounds i32, i32* %16, i64 1
%21 = load i32, i32* %20
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %21)
%23 = getelementptr inbounds i32, i32* %16, i64 2
%24 = load i32, i32* %23
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %24)
%26 = getelementptr inbounds i32, i32* %16, i64 3
%27 = load i32, i32* %26
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %27)
%29 = getelementptr inbounds i32, i32* %16, i64 4
%30 = load i32, i32* %29
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %30)
%32 = getelementptr inbounds i32, i32* %16, i64 5
%33 = load i32, i32* %32
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %33)
%35 = getelementptr inbounds i32, i32* %16, i64 6
%36 = load i32, i32* %35
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %36)
%38 = getelementptr inbounds i32, i32* %16, i64 7
%39 = load i32, i32* %38
%40 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %39)
%41 = getelementptr inbounds i32, i32* %16, i64 8
%42 = load i32, i32* %41
%43 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %42)
%44 = call i32 @free(i32* %16)
%45 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([2 x i8], [2 x i8]* @str.6, i64 0, i64 0))
ret void
}