#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 abs(int);
int /*<<< orphan*/ get_max(int *, int, int *, int *, int *, int *);
int KarmarkarKarp(int array[], int size_int) {
  int x = 0;
  int y = 0;
  int i = 0;
  int j = 99;
  int residue = 0;
  int max, nextmax, maxpos, nextmaxpos;
  while (1) {
    get_max(array, size_int, &max, &nextmax, &maxpos, &nextmaxpos);
    if (nextmax == 0) {
      residue = max;
      break;
    }
    residue = abs(max - nextmax);
    array[nextmaxpos] = 0;
    array[maxpos] = residue;
  }
  return residue;
}


x86 assembly (clang, -O0):
<KarmarkarKarp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x63,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
lea    -0x24(%rbp),%rdx
lea    -0x28(%rbp),%rcx
lea    -0x2c(%rbp),%r8
lea    -0x30(%rbp),%r9
call   1040 <get_max@plt>
cmpl   $0x0,-0x28(%rbp)
jne    1183 <KarmarkarKarp+0x63>
mov    -0x24(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    11b3 <KarmarkarKarp+0x93>
mov    -0x24(%rbp),%edi
sub    -0x28(%rbp),%edi
call   1030 <abs@plt>
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x30(%rbp),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    -0x20(%rbp),%edx
mov    -0x8(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
jmp    1152 <KarmarkarKarp+0x32>
mov    -0x20(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<KarmarkarKarp>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebp
mov    %rdi,%rbx
lea    0xc(%rsp),%rdx
lea    0x8(%rsp),%rcx
lea    0x14(%rsp),%r8
lea    0x10(%rsp),%r9
call   1030 <get_max@plt>
mov    0x8(%rsp),%eax
test   %eax,%eax
je     119f <KarmarkarKarp+0x8f>
lea    0xc(%rsp),%r14
lea    0x8(%rsp),%r15
lea    0x14(%rsp),%r12
lea    0x10(%rsp),%r13
nopl   0x0(%rax,%rax,1)
mov    0xc(%rsp),%ecx
sub    %eax,%ecx
mov    %ecx,%eax
neg    %eax
cmovs  %ecx,%eax
movslq 0x10(%rsp),%rcx
movl   $0x0,(%rbx,%rcx,4)
movslq 0x14(%rsp),%rcx
mov    %eax,(%rbx,%rcx,4)
mov    %rbx,%rdi
mov    %ebp,%esi
mov    %r14,%rdx
mov    %r15,%rcx
mov    %r12,%r8
mov    %r13,%r9
call   1030 <get_max@plt>
mov    0x8(%rsp),%eax
test   %eax,%eax
jne    1160 <KarmarkarKarp+0x50>
mov    0xc(%rsp),%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @KarmarkarKarp(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i32* %0, i32** %3
store i32 %1, i32* %4
store i32 0, i32* %5
store i32 0, i32* %6
store i32 0, i32* %7
store i32 99, i32* %8
store i32 0, i32* %9
br label %14
14: ; preds = %2, %22
%15 = load i32*, i32** %3
%16 = load i32, i32* %4
%17 = call i32 @get_max(i32* %15, i32 %16, i32* %10, i32* %11, i32* %12, i32* %13)
%18 = load i32, i32* %11
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %14
%21 = load i32, i32* %10
store i32 %21, i32* %9
br label %36
22: ; preds = %14
%23 = load i32, i32* %10
%24 = load i32, i32* %11
%25 = sub nsw i32 %23, %24
%26 = call i32 @abs(i32 %25)
store i32 %26, i32* %9
%27 = load i32*, i32** %3
%28 = load i32, i32* %13
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i32, i32* %27, i64 %29
store i32 0, i32* %30
%31 = load i32, i32* %9
%32 = load i32*, i32** %3
%33 = load i32, i32* %12
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i32, i32* %32, i64 %34
store i32 %31, i32* %35
br label %14
36: ; preds = %20
%37 = load i32, i32* %9
ret i32 %37
}

LLVM IR (-O2):
define i32 @KarmarkarKarp(i32* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = bitcast i32* %3 to i8*
%8 = bitcast i32* %4 to i8*
%9 = bitcast i32* %5 to i8*
%10 = bitcast i32* %6 to i8*
%11 = call i32 @get_max(i32* %0, i32 %1, i32* %3, i32* %4, i32* %5, i32* %6)
%12 = load i32, i32* %4
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %16, %2
%15 = load i32, i32* %3
ret i32 %15
16: ; preds = %2, %16
%17 = phi i32 [ %28, %16 ], [ %12, %2 ]
%18 = load i32, i32* %3
%19 = sub nsw i32 %18, %17
%20 = call i32 @llvm.abs.i32(i32 %19, i1 true)
%21 = load i32, i32* %6
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i32, i32* %0, i64 %22
store i32 0, i32* %23
%24 = load i32, i32* %5
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i32, i32* %0, i64 %25
store i32 %20, i32* %26
%27 = call i32 @get_max(i32* %0, i32 %1, i32* %3, i32* %4, i32* %5, i32* %6)
%28 = load i32, i32* %4
%29 = icmp eq i32 %28, 0
br i1 %29, label %14, label %16
}