#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 atoi(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int *malloc(int);
int maxProfit(int, int *, int);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  if (argc < 2) {
    fprintf(stderr, "Usage: ./test k n1 n2...\n");
    exit(-1);
  }
  int i, count = argc - 2;
  int *nums = malloc(count * sizeof(int));
  for (i = 0; i < count; i++) {
    nums[i] = atoi(argv[i + 2]);
  }
  printf("%d\n", maxProfit(atoi(argv[1]), nums, count));
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    119f <main+0x3f>
mov    0x2e59(%rip),%rax
mov    (%rax),%edi
lea    0xe70(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x8(%rbp),%eax
sub    $0x2,%eax
mov    %eax,-0x18(%rbp)
movslq -0x18(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    11ff <main+0x9f>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
add    $0x2,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rdi
call   1070 <atoi@plt>
mov    %eax,%edx
mov    -0x20(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11c2 <main+0x62>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1070 <atoi@plt>
mov    %eax,%edi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1060 <maxProfit@plt>
mov    %eax,%esi
lea    0xdf7(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
cmp    $0x1,%edi
jle    11ee <main+0x8e>
mov    %rsi,%r12
mov    %edi,%r15d
lea    -0x2(%r15),%r14d
lea    -0x8(,%r15,4),%edi
ff
call   1050 <malloc@plt>
mov    %rax,%r13
cmp    $0x2,%r15d
je     11b8 <main+0x58>
mov    %r14d,%ebx
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x10(%r12,%rbp,8),%rdi
call   1070 <atoi@plt>
mov    %eax,0x0(%r13,%rbp,4)
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    11a0 <main+0x40>
mov    0x8(%r12),%rdi
call   1070 <atoi@plt>
mov    %eax,%edi
mov    %r13,%rsi
mov    %r14d,%edx
call   1060 <maxProfit@plt>
lea    0xe44(%rip),%rdi
mov    %eax,%esi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
lea    0xe02(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [26 x i8] c"Usage: ./test k n1 n2...\0A\00"
@.str.1 = [4 x i8] c"%d\0A\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 i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 2
br i1 %10, label %11, label %15
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 -1)
unreachable
15: ; preds = %2
%16 = load i32, i32* %4
%17 = sub nsw i32 %16, 2
store i32 %17, i32* %7
%18 = load i32, i32* %7
%19 = sext i32 %18 to i64
%20 = mul i64 %19, 4
%21 = trunc i64 %20 to i32
%22 = call i32* @malloc(i32 %21)
store i32* %22, i32** %8
store i32 0, i32* %6
br label %23
23: ; preds = %39, %15
%24 = load i32, i32* %6
%25 = load i32, i32* %7
%26 = icmp slt i32 %24, %25
br i1 %26, label %27, label %42
27: ; preds = %23
%28 = load i8**, i8*** %5
%29 = load i32, i32* %6
%30 = add nsw i32 %29, 2
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8*, i8** %28, i64 %31
%33 = load i8*, i8** %32
%34 = call i32 @atoi(i8* %33)
%35 = load i32*, i32** %8
%36 = load i32, i32* %6
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i32, i32* %35, i64 %37
store i32 %34, i32* %38
br label %39
39: ; preds = %27
%40 = load i32, i32* %6
%41 = add nsw i32 %40, 1
store i32 %41, i32* %6
br label %23
42: ; preds = %23
%43 = load i8**, i8*** %5
%44 = getelementptr inbounds i8*, i8** %43, i64 1
%45 = load i8*, i8** %44
%46 = call i32 @atoi(i8* %45)
%47 = load i32*, i32** %8
%48 = load i32, i32* %7
%49 = call i32 @maxProfit(i32 %46, i32* %47, i32 %48)
%50 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %49)
ret i32 0
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [26 x i8] c"Usage: ./test k n1 n2...\0A\00"
@.str.1 = [4 x i8] c"%d\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %8
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 @exit(i32 -1)
unreachable
8: ; preds = %2
%9 = add nsw i32 %0, -2
%10 = shl i32 %9, 2
%11 = tail call i32* @malloc(i32 %10)
%12 = icmp eq i32 %0, 2
br i1 %12, label %24, label %13
13: ; preds = %8
%14 = zext i32 %9 to i64
br label %15
15: ; preds = %13, %15
%16 = phi i64 [ 0, %13 ], [ %22, %15 ]
%17 = add nuw nsw i64 %16, 2
%18 = getelementptr inbounds i8*, i8** %1, i64 %17
%19 = load i8*, i8** %18
%20 = tail call i32 @atoi(i8* %19)
%21 = getelementptr inbounds i32, i32* %11, i64 %16
store i32 %20, i32* %21
%22 = add nuw nsw i64 %16, 1
%23 = icmp eq i64 %22, %14
br i1 %23, label %24, label %15
24: ; preds = %15, %8
%25 = getelementptr inbounds i8*, i8** %1, i64 1
%26 = load i8*, i8** %25
%27 = tail call i32 @atoi(i8* %26)
%28 = tail call i32 @maxProfit(i32 %27, i32* %11, i32 %9)
%29 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %28)
ret i32 0
}