#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 */
typedef int /*<<< orphan*/ freq;

/* Variables and functions */
int /*<<< orphan*/ constructBITtree(int *, int *, int);
int getSum(int *, int);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ update(int *, int, int, int);
int main() {
  int freq[] = {2, 1, 1, 3, 2, 3, 4, 5, 6, 7, 8, 9};
  int n = sizeof(freq) / sizeof(freq[0]);
  int bit_tree[n + 1];
  constructBITtree(freq, bit_tree, n);
  printf("Sum of elements in arr[0..5] is %d\n", getSum(bit_tree, 5));
  freq[3] += 6;
  update(bit_tree, n, 3, 6);
  printf("Sum of elements in arr[0..5] after the update is %d\n",
         getSum(bit_tree, 5));
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
movaps 0xeca(%rip),%xmm0
movaps %xmm0,-0x20(%rbp)
movaps 0xeaf(%rip),%xmm0
movaps %xmm0,-0x30(%rbp)
movaps 0xe94(%rip),%xmm0
movaps %xmm0,-0x40(%rbp)
movl   $0xc,-0x44(%rbp)
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %rsp,%rcx
mov    %rcx,-0x50(%rbp)
lea    0xf(,%rax,4),%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rsi
sub    %rcx,%rsi
mov    %rsi,-0x60(%rbp)
mov    %rsi,%rsp
mov    %rax,-0x58(%rbp)
lea    -0x40(%rbp),%rdi
mov    -0x44(%rbp),%edx
call   1060 <constructBITtree@plt>
mov    -0x60(%rbp),%rdi
mov    $0x5,%esi
call   1040 <getSum@plt>
mov    %eax,%esi
lea    0xe6c(%rip),%rdi
call   1050 <printf@plt>
mov    -0x60(%rbp),%rdi
mov    -0x34(%rbp),%eax
add    $0x6,%eax
mov    %eax,-0x34(%rbp)
mov    -0x44(%rbp),%esi
mov    $0x3,%edx
mov    $0x6,%ecx
call   1030 <update@plt>
mov    -0x60(%rbp),%rdi
mov    $0x5,%esi
call   1040 <getSum@plt>
mov    %eax,%esi
lea    0xe55(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x50(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x68,%rsp
mov    %rsp,%r14
movaps 0xecf(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
movaps 0xeb3(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movaps 0xe97(%rip),%xmm0
movaps %xmm0,(%rsp)
mov    %rsp,%rdi
lea    0x30(%rsp),%rbx
mov    %rbx,%rsi
mov    $0xc,%edx
call   1060 <constructBITtree@plt>
mov    %rbx,%rdi
mov    $0x5,%esi
call   1040 <getSum@plt>
lea    0xe9a(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
addl   $0x6,0xc(%rsp)
mov    %rbx,%rdi
mov    $0xc,%esi
mov    $0x3,%edx
mov    $0x6,%ecx
call   1030 <update@plt>
mov    %rbx,%rdi
mov    $0x5,%esi
call   1040 <getSum@plt>
lea    0xe87(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
mov    %r14,%rsp
xor    %eax,%eax
add    $0x68,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@__const.main.freq = [12 x i32] [i32 2, i32 1, i32 1, i32 3, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8, i32 9]
@.str = [36 x i8] c"Sum of elements in arr[0..5] is %d\0A\00"
@.str.1 = [53 x i8] c"Sum of elements in arr[0..5] after the update is %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [12 x i32]
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
store i32 0, i32* %1
%6 = bitcast [12 x i32]* %2 to i8*
store i32 12, i32* %3
%7 = load i32, i32* %3
%8 = add nsw i32 %7, 1
%9 = zext i32 %8 to i64
%10 = call i8* @llvm.stacksave()
store i8* %10, i8** %4
%11 = alloca i32, i64 %9
store i64 %9, i64* %5
%12 = getelementptr inbounds [12 x i32], [12 x i32]* %2, i64 0, i64 0
%13 = load i32, i32* %3
%14 = call i32 @constructBITtree(i32* %12, i32* %11, i32 %13)
%15 = call i32 @getSum(i32* %11, i32 5)
%16 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 %15)
%17 = getelementptr inbounds [12 x i32], [12 x i32]* %2, i64 0, i64 3
%18 = load i32, i32* %17
%19 = add nsw i32 %18, 6
store i32 %19, i32* %17
%20 = load i32, i32* %3
%21 = call i32 @update(i32* %11, i32 %20, i32 3, i32 6)
%22 = call i32 @getSum(i32* %11, i32 5)
%23 = call i32 @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.1, i64 0, i64 0), i32 %22)
store i32 0, i32* %1
%24 = load i8*, i8** %4
%25 = load i32, i32* %1
ret i32 %25
}

LLVM IR (-O2):
@__const.main.freq = [12 x i32] [i32 2, i32 1, i32 1, i32 3, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8, i32 9]
@.str = [36 x i8] c"Sum of elements in arr[0..5] is %d\0A\00"
@.str.1 = [53 x i8] c"Sum of elements in arr[0..5] after the update is %d\0A\00"
define i32 @main() {
%1 = alloca [12 x i32]
%2 = bitcast [12 x i32]* %1 to i8*
%3 = tail call i8* @llvm.stacksave()
%4 = alloca [13 x i32]
%5 = getelementptr inbounds [13 x i32], [13 x i32]* %4, i64 0, i64 0
%6 = getelementptr inbounds [12 x i32], [12 x i32]* %1, i64 0, i64 0
%7 = call i32 @constructBITtree(i32* %6, i32* %5, i32 12)
%8 = call i32 @getSum(i32* %5, i32 5)
%9 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = getelementptr inbounds [12 x i32], [12 x i32]* %1, i64 0, i64 3
%11 = load i32, i32* %10
%12 = add nsw i32 %11, 6
store i32 %12, i32* %10
%13 = call i32 @update(i32* %5, i32 12, i32 3, i32 6)
%14 = call i32 @getSum(i32* %5, i32 5)
%15 = call i32 @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.1, i64 0, i64 0), i32 %14)
ret i32 0
}