#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 */
scalar_t__ amicable(int *, int);
int d(int);
int /*<<< orphan*/ free(int *);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, int, int);
int main() {
  int i, result = 0;
  int *sums = (int *)malloc(10000 * sizeof(int));
  sums[0] = 0;
  sums[1] = 0;
  for (i = 2; i < 10000; i++) {
    sums[i] = d(i);
    if (0) {
      printf("%d: %d\n", i, sums[i]);
    }
  }
  for (i = 2; i < 10000; i++) {
    if (amicable(sums, i)) {
      result += i;
      result += sums[i];
      if (0) {
        printf("%d and %d are amicable\n", i, sums[i]);
      }
      sums[i] = 0;
    }
  }
  free(sums);
  printf("\nThe sum of all amicable numbers between 1 and %d is %d\n\n", 10000,
         result);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0xc(%rbp)
mov    $0x9c40,%edi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x18(%rbp),%rax
movl   $0x0,0x4(%rax)
movl   $0x2,-0x8(%rbp)
cmpl   $0x2710,-0x8(%rbp)
jge    11c0 <main+0x70>
mov    -0x8(%rbp),%edi
call   1040 <d@plt>
mov    %eax,%edx
mov    -0x18(%rbp),%rax
movslq -0x8(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1190 <main+0x40>
movl   $0x2,-0x8(%rbp)
cmpl   $0x2710,-0x8(%rbp)
jge    1226 <main+0xd6>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%esi
call   1060 <amicable@plt>
cmp    $0x0,%rax
je     1213 <main+0xc3>
mov    -0x8(%rbp),%eax
add    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x18(%rbp),%rax
movslq -0x8(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
add    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x18(%rbp),%rax
movslq -0x8(%rbp),%rcx
movl   $0x0,(%rax,%rcx,4)
jmp    1218 <main+0xc8>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    11c7 <main+0x77>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0xc(%rbp),%edx
lea    0xdc7(%rip),%rdi
mov    $0x2710,%esi
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    $0x9c40,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
movq   $0x0,(%rax)
mov    $0x2,%ebp
nopl   (%rax)
mov    %ebp,%edi
call   1040 <d@plt>
mov    %eax,(%rbx,%rbp,4)
add    $0x1,%rbp
cmp    $0x2710,%rbp
jne    1170 <main+0x20>
xor    %r14d,%r14d
mov    $0x2,%ebp
jmp    11ad <main+0x5d>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    $0x1,%rbp
cmp    $0x2710,%rbp
je     11cc <main+0x7c>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1060 <amicable@plt>
test   %rax,%rax
je     11a0 <main+0x50>
add    (%rbx,%rbp,4),%r14d
add    %ebp,%r14d
movl   $0x0,(%rbx,%rbp,4)
jmp    11a0 <main+0x50>
mov    %rbx,%rdi
call   1030 <free@plt>
lea    0xe25(%rip),%rdi
mov    $0x2710,%esi
mov    %r14d,%edx
call   1050 <printf@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [58 x i8] c"\0AThe sum of all amicable numbers between 1 and %d is %d\0A\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32*
store i32 0, i32* %1
store i32 0, i32* %3
%5 = call i64 @malloc(i32 40000)
%6 = inttoptr i64 %5 to i32*
store i32* %6, i32** %4
%7 = load i32*, i32** %4
%8 = getelementptr inbounds i32, i32* %7, i64 0
store i32 0, i32* %8
%9 = load i32*, i32** %4
%10 = getelementptr inbounds i32, i32* %9, i64 1
store i32 0, i32* %10
store i32 2, i32* %2
br label %11
11: ; preds = %21, %0
%12 = load i32, i32* %2
%13 = icmp slt i32 %12, 10000
br i1 %13, label %14, label %24
14: ; preds = %11
%15 = load i32, i32* %2
%16 = call i32 @d(i32 %15)
%17 = load i32*, i32** %4
%18 = load i32, i32* %2
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i32, i32* %17, i64 %19
store i32 %16, i32* %20
br label %21
21: ; preds = %14
%22 = load i32, i32* %2
%23 = add nsw i32 %22, 1
store i32 %23, i32* %2
br label %11
24: ; preds = %11
store i32 2, i32* %2
br label %25
25: ; preds = %49, %24
%26 = load i32, i32* %2
%27 = icmp slt i32 %26, 10000
br i1 %27, label %28, label %52
28: ; preds = %25
%29 = load i32*, i32** %4
%30 = load i32, i32* %2
%31 = call i64 @amicable(i32* %29, i32 %30)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %48
33: ; preds = %28
%34 = load i32, i32* %2
%35 = load i32, i32* %3
%36 = add nsw i32 %35, %34
store i32 %36, i32* %3
%37 = load i32*, i32** %4
%38 = load i32, i32* %2
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i32, i32* %37, i64 %39
%41 = load i32, i32* %40
%42 = load i32, i32* %3
%43 = add nsw i32 %42, %41
store i32 %43, i32* %3
%44 = load i32*, i32** %4
%45 = load i32, i32* %2
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i32, i32* %44, i64 %46
store i32 0, i32* %47
br label %48
48: ; preds = %33, %28
br label %49
49: ; preds = %48
%50 = load i32, i32* %2
%51 = add nsw i32 %50, 1
store i32 %51, i32* %2
br label %25
52: ; preds = %25
%53 = load i32*, i32** %4
%54 = call i32 @free(i32* %53)
%55 = load i32, i32* %3
%56 = call i32 @printf(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str, i64 0, i64 0), i32 10000, i32 %55)
ret i32 0
}

LLVM IR (-O2):
@.str = [58 x i8] c"\0AThe sum of all amicable numbers between 1 and %d is %d\0A\0A\00"
define i32 @main() {
%1 = tail call i64 @malloc(i32 40000)
%2 = inttoptr i64 %1 to i32*
store i32 0, i32* %2
%3 = getelementptr inbounds i32, i32* %2, i64 1
store i32 0, i32* %3
br label %4
4: ; preds = %0, %4
%5 = phi i64 [ 2, %0 ], [ %9, %4 ]
%6 = trunc i64 %5 to i32
%7 = tail call i32 @d(i32 %6)
%8 = getelementptr inbounds i32, i32* %2, i64 %5
store i32 %7, i32* %8
%9 = add nuw nsw i64 %5, 1
%10 = icmp eq i64 %9, 10000
br i1 %10, label %11, label %4
11: ; preds = %4, %22
%12 = phi i64 [ %24, %22 ], [ 2, %4 ]
%13 = phi i32 [ %23, %22 ], [ 0, %4 ]
%14 = trunc i64 %12 to i32
%15 = tail call i64 @amicable(i32* %2, i32 %14)
%16 = icmp eq i64 %15, 0
br i1 %16, label %22, label %17
17: ; preds = %11
%18 = add nsw i32 %13, %14
%19 = getelementptr inbounds i32, i32* %2, i64 %12
%20 = load i32, i32* %19
%21 = add nsw i32 %18, %20
store i32 0, i32* %19
br label %22
22: ; preds = %11, %17
%23 = phi i32 [ %21, %17 ], [ %13, %11 ]
%24 = add nuw nsw i64 %12, 1
%25 = icmp eq i64 %24, 10000
br i1 %25, label %26, label %11
26: ; preds = %22
%27 = tail call i32 @free(i32* %2)
%28 = tail call i32 @printf(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str, i64 0, i64 0), i32 10000, i32 %23)
ret i32 0
}