#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 */
typedef int /*<<< orphan*/ arr;

/* Variables and functions */
int /*<<< orphan*/ printf(char *, int, ...);
int main(void) {
  int arr[] = {10, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15, 0};
  int len = sizeof(arr) / sizeof(int);
  int i = 0;
  int maxSum, maxIndex, buyIndex, lowIndex, sum;
  maxSum = maxIndex = buyIndex = lowIndex = sum = 0;
  printf("arr = %d ", arr[0]);
  for (i = 1; i < len; i++) {
    printf("%d ", arr[i]);
    sum += arr[i] - arr[i - 1];
    if (maxSum < sum) {
      maxSum = sum;
      maxIndex = i;
      lowIndex = buyIndex;
    }
    if (arr[i] < arr[buyIndex]) {
      buyIndex = i;
    }
  }
  printf("\nbuy at %d, sell at %d\n", arr[lowIndex], arr[maxIndex]);
  printf("buy at index %d, sell at index %d\n", lowIndex, maxIndex);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x50(%rbp),%rdi
lea    0xec6(%rip),%rsi
mov    $0x44,%edx
call   1040 <memcpy@plt>
movl   $0x11,-0x54(%rbp)
movl   $0x0,-0x58(%rbp)
movl   $0x0,-0x6c(%rbp)
movl   $0x0,-0x68(%rbp)
movl   $0x0,-0x64(%rbp)
movl   $0x0,-0x60(%rbp)
movl   $0x0,-0x5c(%rbp)
mov    -0x50(%rbp),%esi
lea    0xec5(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x58(%rbp)
mov    -0x58(%rbp),%eax
cmp    -0x54(%rbp),%eax
jge    1217 <main+0xf7>
movslq -0x58(%rbp),%rax
mov    -0x50(%rbp,%rax,4),%esi
lea    0xea2(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movslq -0x58(%rbp),%rax
mov    -0x50(%rbp,%rax,4),%eax
mov    -0x58(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
sub    -0x50(%rbp,%rcx,4),%eax
add    -0x6c(%rbp),%eax
mov    %eax,-0x6c(%rbp)
mov    -0x5c(%rbp),%eax
cmp    -0x6c(%rbp),%eax
jge    11e8 <main+0xc8>
mov    -0x6c(%rbp),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x58(%rbp),%eax
mov    %eax,-0x60(%rbp)
mov    -0x64(%rbp),%eax
mov    %eax,-0x68(%rbp)
movslq -0x58(%rbp),%rax
mov    -0x50(%rbp,%rax,4),%eax
movslq -0x64(%rbp),%rcx
cmp    -0x50(%rbp,%rcx,4),%eax
jge    1204 <main+0xe4>
mov    -0x58(%rbp),%eax
mov    %eax,-0x64(%rbp)
jmp    1209 <main+0xe9>
mov    -0x58(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x58(%rbp)
jmp    118d <main+0x6d>
movslq -0x68(%rbp),%rax
mov    -0x50(%rbp,%rax,4),%esi
movslq -0x60(%rbp),%rax
mov    -0x50(%rbp,%rax,4),%edx
lea    0xe20(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x68(%rbp),%esi
mov    -0x60(%rbp),%edx
lea    0xe24(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
lea    0xee8(%rip),%rdi
mov    $0xa,%esi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xedb(%rip),%rbx
mov    %rbx,%rdi
mov    $0x8,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %rbx,%rdi
mov    $0x4,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %rbx,%rdi
mov    $0xc,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %rbx,%rdi
mov    $0x2,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %rbx,%rdi
mov    $0xa,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %rbx,%rdi
mov    $0x6,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %rbx,%rdi
mov    $0xe,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %rbx,%rdi
mov    $0x9,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %rbx,%rdi
mov    $0x5,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %rbx,%rdi
mov    $0xd,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %rbx,%rdi
mov    $0x3,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %rbx,%rdi
mov    $0xb,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %rbx,%rdi
mov    $0x7,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %rbx,%rdi
mov    $0xf,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %rbx,%rdi
xor    %esi,%esi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xdeb(%rip),%rdi
mov    $0x1,%esi
mov    $0xf,%edx
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xdeb(%rip),%rdi
mov    $0x8,%esi
mov    $0xf,%edx
xor    %eax,%eax
call   1030 <printf@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@__const.main.arr = [17 x i32] [i32 10, i32 8, i32 4, i32 12, i32 2, i32 10, i32 6, i32 14, i32 1, i32 9, i32 5, i32 13, i32 3, i32 11, i32 7, i32 15, i32 0]
@.str = [10 x i8] c"arr = %d \00"
@.str.1 = [4 x i8] c"%d \00"
@.str.2 = [24 x i8] c"\0Abuy at %d, sell at %d\0A\00"
@.str.3 = [35 x i8] c"buy at index %d, sell at index %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [17 x i32]
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %1
%10 = bitcast [17 x i32]* %2 to i8*
store i32 17, i32* %3
store i32 0, i32* %4
store i32 0, i32* %9
store i32 0, i32* %8
store i32 0, i32* %7
store i32 0, i32* %6
store i32 0, i32* %5
%11 = getelementptr inbounds [17 x i32], [17 x i32]* %2, i64 0, i64 0
%12 = load i32, i32* %11
%13 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %12)
store i32 1, i32* %4
br label %14
14: ; preds = %56, %0
%15 = load i32, i32* %4
%16 = load i32, i32* %3
%17 = icmp slt i32 %15, %16
br i1 %17, label %18, label %59
18: ; preds = %14
%19 = load i32, i32* %4
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds [17 x i32], [17 x i32]* %2, i64 0, i64 %20
%22 = load i32, i32* %21
%23 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %22)
%24 = load i32, i32* %4
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds [17 x i32], [17 x i32]* %2, i64 0, i64 %25
%27 = load i32, i32* %26
%28 = load i32, i32* %4
%29 = sub nsw i32 %28, 1
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds [17 x i32], [17 x i32]* %2, i64 0, i64 %30
%32 = load i32, i32* %31
%33 = sub nsw i32 %27, %32
%34 = load i32, i32* %9
%35 = add nsw i32 %34, %33
store i32 %35, i32* %9
%36 = load i32, i32* %5
%37 = load i32, i32* %9
%38 = icmp slt i32 %36, %37
br i1 %38, label %39, label %43
39: ; preds = %18
%40 = load i32, i32* %9
store i32 %40, i32* %5
%41 = load i32, i32* %4
store i32 %41, i32* %6
%42 = load i32, i32* %7
store i32 %42, i32* %8
br label %43
43: ; preds = %39, %18
%44 = load i32, i32* %4
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds [17 x i32], [17 x i32]* %2, i64 0, i64 %45
%47 = load i32, i32* %46
%48 = load i32, i32* %7
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds [17 x i32], [17 x i32]* %2, i64 0, i64 %49
%51 = load i32, i32* %50
%52 = icmp slt i32 %47, %51
br i1 %52, label %53, label %55
53: ; preds = %43
%54 = load i32, i32* %4
store i32 %54, i32* %7
br label %55
55: ; preds = %53, %43
br label %56
56: ; preds = %55
%57 = load i32, i32* %4
%58 = add nsw i32 %57, 1
store i32 %58, i32* %4
br label %14
59: ; preds = %14
%60 = load i32, i32* %8
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds [17 x i32], [17 x i32]* %2, i64 0, i64 %61
%63 = load i32, i32* %62
%64 = load i32, i32* %6
%65 = sext i32 %64 to i64
%66 = getelementptr inbounds [17 x i32], [17 x i32]* %2, i64 0, i64 %65
%67 = load i32, i32* %66
%68 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %63, i32 %67)
%69 = load i32, i32* %8
%70 = load i32, i32* %6
%71 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i32 %69, i32 %70)
%72 = load i32, i32* %1
ret i32 %72
}

LLVM IR (-O2):
@.str = [10 x i8] c"arr = %d \00"
@.str.1 = [4 x i8] c"%d \00"
@.str.2 = [24 x i8] c"\0Abuy at %d, sell at %d\0A\00"
@.str.3 = [35 x i8] c"buy at index %d, sell at index %d\0A\00"
define i32 @main() {
%1 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 10)
%2 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 8)
%3 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 4)
%4 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 12)
%5 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 2)
%6 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 10)
%7 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 6)
%8 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 14)
%9 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 1)
%10 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 9)
%11 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 5)
%12 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 13)
%13 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 3)
%14 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 11)
%15 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 7)
%16 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 15)
%17 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 0)
%18 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 1, i32 15)
%19 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i32 8, i32 15)
ret i32 0
}