#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 */
unsigned char GetMAX(char, unsigned char);
int HeaderMax(unsigned char);
int findMAXval(char *imgData, int length) {
  unsigned char max = 0;
  unsigned char c = 0;
  int idx = 0;
  for (idx; idx < length; ++idx) {
    max = GetMAX(imgData[idx], max);
  }
  return HeaderMax(max);
}


x86 assembly (clang, -O0):
<findMAXval>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movb   $0x0,-0xd(%rbp)
movb   $0x0,-0xe(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1172 <findMAXval+0x52>
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,1),%al
movsbl %al,%edi
movzbl -0xd(%rbp),%esi
call   1040 <GetMAX@plt>
mov    %al,-0xd(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    113e <findMAXval+0x1e>
movzbl -0xd(%rbp),%edi
call   1030 <HeaderMax@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<findMAXval>:
push   %r15
push   %r14
push   %rbx
test   %esi,%esi
jle    1158 <findMAXval+0x38>
mov    %rdi,%r14
mov    %esi,%r15d
xor    %ebx,%ebx
xor    %eax,%eax
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movsbl (%r14,%rbx,1),%edi
movzbl %al,%esi
call   1040 <GetMAX@plt>
add    $0x1,%rbx
cmp    %rbx,%r15
jne    1140 <findMAXval+0x20>
jmp    115a <findMAXval+0x3a>
xor    %eax,%eax
movzbl %al,%edi
pop    %rbx
pop    %r14
pop    %r15
jmp    1030 <HeaderMax@plt>

LLVM IR (-O0):
define i32 @findMAXval(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8
%6 = alloca i8
%7 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
store i8 0, i8* %5
store i8 0, i8* %6
store i32 0, i32* %7
%8 = load i32, i32* %7
br label %9
9: ; preds = %21, %2
%10 = load i32, i32* %7
%11 = load i32, i32* %4
%12 = icmp slt i32 %10, %11
br i1 %12, label %13, label %24
13: ; preds = %9
%14 = load i8*, i8** %3
%15 = load i32, i32* %7
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %14, i64 %16
%18 = load i8, i8* %17
%19 = load i8, i8* %5
%20 = call zeroext i8 @GetMAX(i8 signext %18, i8 zeroext %19)
store i8 %20, i8* %5
br label %21
21: ; preds = %13
%22 = load i32, i32* %7
%23 = add nsw i32 %22, 1
store i32 %23, i32* %7
br label %9
24: ; preds = %9
%25 = load i8, i8* %5
%26 = call i32 @HeaderMax(i8 zeroext %25)
ret i32 %26
}

LLVM IR (-O2):
define i32 @findMAXval(i8* nocapture %0, i32 %1) {
%3 = icmp sgt i32 %1, 0
br i1 %3, label %4, label %14
4: ; preds = %2
%5 = zext i32 %1 to i64
br label %6
6: ; preds = %4, %6
%7 = phi i64 [ 0, %4 ], [ %12, %6 ]
%8 = phi i8 [ 0, %4 ], [ %11, %6 ]
%9 = getelementptr inbounds i8, i8* %0, i64 %7
%10 = load i8, i8* %9
%11 = tail call zeroext i8 @GetMAX(i8 signext %10, i8 zeroext %8)
%12 = add nuw nsw i64 %7, 1
%13 = icmp eq i64 %12, %5
br i1 %13, label %14, label %6
14: ; preds = %6, %2
%15 = phi i8 [ 0, %2 ], [ %11, %6 ]
%16 = tail call i32 @HeaderMax(i8 zeroext %15)
ret i32 %16
}