#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 */
double *calloc(size_t, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(double *);
double *malloc(size_t);
int /*<<< orphan*/ printf(char *, double);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ scanf(char *, ...);
int /*<<< orphan*/ stderr;
int main() {
  size_t n, i;
  double *a;
  int cal;
  puts("How many numbers?");
  scanf(" %zu%*c", &n);
  puts("Which allocation routine?\n(0) malloc\n(1) calloc");
  scanf(" %d%*c", &cal);
  if (!(a = cal ? calloc(n, sizeof(*a)) : malloc(n * sizeof(*a)))) {
    fprintf(stderr, "Cannot allocate memory!\n");
    exit(12);
  }
  printf("Enter %zu numbers:\n", n);
  for (i = 0; i < n; ++i)
    scanf(" %lf%*c", a + i);
  for (i = 1; i < n; ++i)
    a[0] += a[i];
  printf("Sum is %lf.\n", a[0]);
  free(a);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe8a(%rip),%rdi
call   1040 <puts@plt>
lea    0xe90(%rip),%rdi
lea    -0x10(%rbp),%rsi
mov    $0x0,%al
call   1090 <scanf@plt>
lea    0xe86(%rip),%rdi
call   1040 <puts@plt>
lea    0xeaa(%rip),%rdi
lea    -0x24(%rbp),%rsi
mov    $0x0,%al
call   1090 <scanf@plt>
cmpl   $0x0,-0x24(%rbp)
je     11ec <main+0x6c>
mov    -0x10(%rbp),%rdi
mov    $0x8,%esi
call   1060 <calloc@plt>
mov    %rax,-0x30(%rbp)
jmp    11fd <main+0x7d>
mov    -0x10(%rbp),%rdi
shl    $0x3,%rdi
call   1080 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    122e <main+0xae>
mov    0x2dca(%rip),%rax
mov    (%rax),%edi
lea    0xe52(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0xc,%edi
call   10a0 <exit@plt>
movaps 0xdcb(%rip),%xmm1
movq   -0x10(%rbp),%xmm0
punpckldq %xmm1,%xmm0
movapd 0xdca(%rip),%xmm1
subpd  %xmm1,%xmm0
movaps %xmm0,%xmm1
unpckhpd %xmm0,%xmm0
addsd  %xmm1,%xmm0
lea    0xe2e(%rip),%rdi
call   1050 <printf@plt>
movq   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    12a5 <main+0x125>
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rax
shl    $0x3,%rax
add    %rax,%rsi
lea    0xe11(%rip),%rdi
mov    $0x0,%al
call   1090 <scanf@plt>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1269 <main+0xe9>
movq   $0x1,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    12e5 <main+0x165>
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x20(%rbp),%rax
addsd  (%rax),%xmm0
movsd  %xmm0,(%rax)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    12ad <main+0x12d>
mov    -0x20(%rbp),%rax
movsd  (%rax),%xmm0
lea    0xdb2(%rip),%rdi
call   1050 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
lea    0xe8e(%rip),%rdi
call   1040 <puts@plt>
lea    0xe94(%rip),%rdi
lea    0x8(%rsp),%rsi
xor    %eax,%eax
call   1090 <scanf@plt>
lea    0xe89(%rip),%rdi
call   1040 <puts@plt>
lea    0xead(%rip),%rdi
lea    0x14(%rsp),%rsi
xor    %eax,%eax
call   1090 <scanf@plt>
cmpl   $0x0,0x14(%rsp)
mov    0x8(%rsp),%rdi
je     11e1 <main+0x61>
mov    $0x8,%esi
call   1060 <calloc@plt>
jmp    11ea <main+0x6a>
shl    $0x3,%rdi
call   1080 <malloc@plt>
mov    %rax,%r15
test   %rax,%rax
je     1325 <main+0x1a5>
movsd  0x8(%rsp),%xmm1
unpcklps 0xdfd(%rip),%xmm1
subpd  0xe05(%rip),%xmm1
movapd %xmm1,%xmm0
unpckhpd %xmm1,%xmm0
addsd  %xmm1,%xmm0
lea    0xe6c(%rip),%rdi
call   1050 <printf@plt>
cmpq   $0x0,0x8(%rsp)
je     1289 <main+0x109>
lea    0xe6c(%rip),%r14
mov    %r15,%r12
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1090 <scanf@plt>
add    $0x1,%rbx
mov    0x8(%rsp),%rcx
add    $0x8,%r12
cmp    %rcx,%rbx
jb     1240 <main+0xc0>
cmp    $0x1,%rcx
jbe    1289 <main+0x109>
movsd  (%r15),%xmm0
lea    -0x1(%rcx),%rdx
add    $0xfffffffffffffffe,%rcx
mov    %edx,%eax
and    $0x3,%eax
cmp    $0x3,%rcx
jae    1290 <main+0x110>
mov    $0x1,%ecx
test   %rax,%rax
jne    12e2 <main+0x162>
jmp    1303 <main+0x183>
movsd  (%r15),%xmm0
jmp    1303 <main+0x183>
and    $0xfffffffffffffffc,%rdx
xor    %ecx,%ecx
cs nopw 0x0(%rax,%rax,1)
addsd  0x8(%r15,%rcx,8),%xmm0
movsd  %xmm0,(%r15)
addsd  0x10(%r15,%rcx,8),%xmm0
movsd  %xmm0,(%r15)
addsd  0x18(%r15,%rcx,8),%xmm0
movsd  %xmm0,(%r15)
addsd  0x20(%r15,%rcx,8),%xmm0
movsd  %xmm0,(%r15)
add    $0x4,%rcx
cmp    %rcx,%rdx
jne    12a0 <main+0x120>
add    $0x1,%rcx
test   %rax,%rax
je     1303 <main+0x183>
lea    (%r15,%rcx,8),%rcx
xor    %edx,%edx
nopl   0x0(%rax,%rax,1)
addsd  (%rcx,%rdx,8),%xmm0
movsd  %xmm0,(%r15)
add    $0x1,%rdx
cmp    %rdx,%rax
jne    12f0 <main+0x170>
lea    0xd9c(%rip),%rdi
call   1050 <printf@plt>
mov    %r15,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    0x2cb4(%rip),%rax
mov    (%rax),%edi
lea    0xd3c(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0xc,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [18 x i8] c"How many numbers?\00"
@.str.1 = [8 x i8] c" %zu%*c\00"
@.str.2 = [48 x i8] c"Which allocation routine?\0A(0) malloc\0A(1) calloc\00"
@.str.3 = [7 x i8] c" %d%*c\00"
@stderr = global i32 0
@.str.4 = [25 x i8] c"Cannot allocate memory!\0A\00"
@.str.5 = [20 x i8] c"Enter %zu numbers:\0A\00"
@.str.6 = [8 x i8] c" %lf%*c\00"
@.str.7 = [13 x i8] c"Sum is %lf.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i64
%4 = alloca double*
%5 = alloca i32
store i32 0, i32* %1
%6 = call i32 @puts(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%7 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i64* %2)
%8 = call i32 @puts(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0))
%9 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32* %5)
%10 = load i32, i32* %5
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %0
%13 = load i64, i64* %2
%14 = call double* @calloc(i64 %13, i32 8)
br label %19
15: ; preds = %0
%16 = load i64, i64* %2
%17 = mul i64 %16, 8
%18 = call double* @malloc(i64 %17)
br label %19
19: ; preds = %15, %12
%20 = phi double* [ %14, %12 ], [ %18, %15 ]
store double* %20, double** %4
%21 = icmp ne double* %20, null
br i1 %21, label %26, label %22
22: ; preds = %19
%23 = load i32, i32* @stderr
%24 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
%25 = call i32 @exit(i32 12)
unreachable
26: ; preds = %19
%27 = load i64, i64* %2
%28 = uitofp i64 %27 to double
%29 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0), double %28)
store i64 0, i64* %3
br label %30
30: ; preds = %39, %26
%31 = load i64, i64* %3
%32 = load i64, i64* %2
%33 = icmp ult i64 %31, %32
br i1 %33, label %34, label %42
34: ; preds = %30
%35 = load double*, double** %4
%36 = load i64, i64* %3
%37 = getelementptr inbounds double, double* %35, i64 %36
%38 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0), double* %37)
br label %39
39: ; preds = %34
%40 = load i64, i64* %3
%41 = add i64 %40, 1
store i64 %41, i64* %3
br label %30
42: ; preds = %30
store i64 1, i64* %3
br label %43
43: ; preds = %56, %42
%44 = load i64, i64* %3
%45 = load i64, i64* %2
%46 = icmp ult i64 %44, %45
br i1 %46, label %47, label %59
47: ; preds = %43
%48 = load double*, double** %4
%49 = load i64, i64* %3
%50 = getelementptr inbounds double, double* %48, i64 %49
%51 = load double, double* %50
%52 = load double*, double** %4
%53 = getelementptr inbounds double, double* %52, i64 0
%54 = load double, double* %53
%55 = fadd double %54, %51
store double %55, double* %53
br label %56
56: ; preds = %47
%57 = load i64, i64* %3
%58 = add i64 %57, 1
store i64 %58, i64* %3
br label %43
59: ; preds = %43
%60 = load double*, double** %4
%61 = getelementptr inbounds double, double* %60, i64 0
%62 = load double, double* %61
%63 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0), double %62)
%64 = load double*, double** %4
%65 = call i32 @free(double* %64)
ret i32 0
}

LLVM IR (-O2):
@.str = [18 x i8] c"How many numbers?\00"
@.str.1 = [8 x i8] c" %zu%*c\00"
@.str.2 = [48 x i8] c"Which allocation routine?\0A(0) malloc\0A(1) calloc\00"
@.str.3 = [7 x i8] c" %d%*c\00"
@stderr = global i32 0
@.str.4 = [25 x i8] c"Cannot allocate memory!\0A\00"
@.str.5 = [20 x i8] c"Enter %zu numbers:\0A\00"
@.str.6 = [8 x i8] c" %lf%*c\00"
@.str.7 = [13 x i8] c"Sum is %lf.\0A\00"
define i32 @main() {
%1 = alloca i64
%2 = alloca i32
%3 = bitcast i64* %1 to i8*
%4 = bitcast i32* %2 to i8*
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%6 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i64* %1)
%7 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0))
%8 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32* %2)
%9 = load i32, i32* %2
%10 = icmp eq i32 %9, 0
%11 = load i64, i64* %1
br i1 %10, label %14, label %12
12: ; preds = %0
%13 = call double* @calloc(i64 %11, i32 8)
br label %17
14: ; preds = %0
%15 = shl i64 %11, 3
%16 = call double* @malloc(i64 %15)
br label %17
17: ; preds = %14, %12
%18 = phi double* [ %13, %12 ], [ %16, %14 ]
%19 = icmp eq double* %18, null
br i1 %19, label %20, label %24
20: ; preds = %17
%21 = load i32, i32* @stderr
%22 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
%23 = call i32 @exit(i32 12)
unreachable
24: ; preds = %17
%25 = load i64, i64* %1
%26 = uitofp i64 %25 to double
%27 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0), double %26)
%28 = load i64, i64* %1
%29 = icmp eq i64 %28, 0
br i1 %29, label %32, label %42
30: ; preds = %42
%31 = icmp ugt i64 %47, 1
br i1 %31, label %34, label %32
32: ; preds = %24, %30
%33 = load double, double* %18
br label %86
34: ; preds = %30
%35 = load double, double* %18
%36 = add i64 %47, -1
%37 = add i64 %47, -2
%38 = and i64 %36, 3
%39 = icmp ult i64 %37, 3
br i1 %39, label %71, label %40
40: ; preds = %34
%41 = and i64 %36, -4
br label %49
42: ; preds = %24, %42
%43 = phi i64 [ %46, %42 ], [ 0, %24 ]
%44 = getelementptr inbounds double, double* %18, i64 %43
%45 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0), double* %44)
%46 = add nuw i64 %43, 1
%47 = load i64, i64* %1
%48 = icmp ult i64 %46, %47
br i1 %48, label %42, label %30
49: ; preds = %49, %40
%50 = phi double [ %35, %40 ], [ %67, %49 ]
%51 = phi i64 [ 1, %40 ], [ %68, %49 ]
%52 = phi i64 [ 0, %40 ], [ %69, %49 ]
%53 = getelementptr inbounds double, double* %18, i64 %51
%54 = load double, double* %53
%55 = fadd double %54, %50
store double %55, double* %18
%56 = add nuw nsw i64 %51, 1
%57 = getelementptr inbounds double, double* %18, i64 %56
%58 = load double, double* %57
%59 = fadd double %58, %55
store double %59, double* %18
%60 = add nuw nsw i64 %51, 2
%61 = getelementptr inbounds double, double* %18, i64 %60
%62 = load double, double* %61
%63 = fadd double %62, %59
store double %63, double* %18
%64 = add nuw i64 %51, 3
%65 = getelementptr inbounds double, double* %18, i64 %64
%66 = load double, double* %65
%67 = fadd double %66, %63
store double %67, double* %18
%68 = add nuw i64 %51, 4
%69 = add i64 %52, 4
%70 = icmp eq i64 %69, %41
br i1 %70, label %71, label %49
71: ; preds = %49, %34
%72 = phi double [ undef, %34 ], [ %67, %49 ]
%73 = phi double [ %35, %34 ], [ %67, %49 ]
%74 = phi i64 [ 1, %34 ], [ %68, %49 ]
%75 = icmp eq i64 %38, 0
br i1 %75, label %86, label %76
76: ; preds = %71, %76
%77 = phi double [ %82, %76 ], [ %73, %71 ]
%78 = phi i64 [ %83, %76 ], [ %74, %71 ]
%79 = phi i64 [ %84, %76 ], [ 0, %71 ]
%80 = getelementptr inbounds double, double* %18, i64 %78
%81 = load double, double* %80
%82 = fadd double %81, %77
store double %82, double* %18
%83 = add nuw i64 %78, 1
%84 = add i64 %79, 1
%85 = icmp eq i64 %84, %38
br i1 %85, label %86, label %76
86: ; preds = %71, %76, %32
%87 = phi double [ %33, %32 ], [ %72, %71 ], [ %82, %76 ]
%88 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0), double %87)
%89 = call i32 @free(double* %18)
ret i32 0
}