void build_sum_segment_tree(int *segment_tree, int *input, int low, int high,
                            int pos) {
  int mid;
  if (low == high) {
    segment_tree[pos] = input[low];
    return;
  }
  mid = (low + high) / 2;
  build_sum_segment_tree(segment_tree, input, low, mid, (2 * pos + 1));
  build_sum_segment_tree(segment_tree, input, mid + 1, high, (2 * pos + 2));
  segment_tree[pos] = (segment_tree[2 * pos + 1] + segment_tree[2 * pos + 2]);
}


x86 assembly (clang, -O0):
<build_sum_segment_tree>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
jne    1151 <build_sum_segment_tree+0x41>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
jmp    11d4 <build_sum_segment_tree+0xc4>
mov    -0x14(%rbp),%eax
add    -0x18(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    -0x20(%rbp),%ecx
mov    -0x1c(%rbp),%r8d
shl    $0x1,%r8d
add    $0x1,%r8d
call   1030 <build_sum_segment_tree@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%edx
add    $0x1,%edx
mov    -0x18(%rbp),%ecx
mov    -0x1c(%rbp),%r8d
shl    $0x1,%r8d
add    $0x2,%r8d
call   1030 <build_sum_segment_tree@plt>
mov    -0x8(%rbp),%rax
mov    -0x1c(%rbp),%ecx
shl    $0x1,%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%edx
mov    -0x8(%rbp),%rax
mov    -0x1c(%rbp),%ecx
shl    $0x1,%ecx
add    $0x2,%ecx
movslq %ecx,%rcx
add    (%rax,%rcx,4),%edx
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<build_sum_segment_tree>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8d,%r14d
mov    %rsi,%r12
mov    %rdi,%rbx
cmp    %ecx,%edx
jne    1131 <build_sum_segment_tree+0x21>
movslq %edx,%rax
mov    (%r12,%rax,4),%eax
jmp    118b <build_sum_segment_tree+0x7b>
mov    %ecx,%r15d
lea    (%r15,%rdx,1),%eax
mov    %eax,%ebp
shr    $0x1f,%ebp
add    %eax,%ebp
sar    %ebp
lea    (%r14,%r14,1),%eax
mov    %eax,0x4(%rsp)
lea    (%r14,%r14,1),%r13d
add    $0x1,%r13d
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %ebp,%ecx
mov    %r13d,%r8d
call   1030 <build_sum_segment_tree@plt>
add    $0x1,%ebp
lea    (%r14,%r14,1),%r8d
add    $0x2,%r8d
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %ebp,%edx
mov    %r15d,%ecx
call   1030 <build_sum_segment_tree@plt>
movslq %r13d,%rcx
movslq 0x4(%rsp),%rax
mov    0x8(%rbx,%rax,4),%eax
add    (%rbx,%rcx,4),%eax
movslq %r14d,%rcx
mov    %eax,(%rbx,%rcx,4)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @build_sum_segment_tree(i32* %0, i32* %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32* %0, i32** %6
store i32* %1, i32** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i32 %4, i32* %10
%12 = load i32, i32* %8
%13 = load i32, i32* %9
%14 = icmp eq i32 %12, %13
br i1 %14, label %15, label %25
15: ; preds = %5
%16 = load i32*, i32** %7
%17 = load i32, i32* %8
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i32, i32* %16, i64 %18
%20 = load i32, i32* %19
%21 = load i32*, i32** %6
%22 = load i32, i32* %10
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i32, i32* %21, i64 %23
store i32 %20, i32* %24
br label %64
25: ; preds = %5
%26 = load i32, i32* %8
%27 = load i32, i32* %9
%28 = add nsw i32 %26, %27
%29 = sdiv i32 %28, 2
store i32 %29, i32* %11
%30 = load i32*, i32** %6
%31 = load i32*, i32** %7
%32 = load i32, i32* %8
%33 = load i32, i32* %11
%34 = load i32, i32* %10
%35 = mul nsw i32 2, %34
%36 = add nsw i32 %35, 1
%37 = load i32*, i32** %6
%38 = load i32*, i32** %7
%39 = load i32, i32* %11
%40 = add nsw i32 %39, 1
%41 = load i32, i32* %9
%42 = load i32, i32* %10
%43 = mul nsw i32 2, %42
%44 = add nsw i32 %43, 2
%45 = load i32*, i32** %6
%46 = load i32, i32* %10
%47 = mul nsw i32 2, %46
%48 = add nsw i32 %47, 1
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i32, i32* %45, i64 %49
%51 = load i32, i32* %50
%52 = load i32*, i32** %6
%53 = load i32, i32* %10
%54 = mul nsw i32 2, %53
%55 = add nsw i32 %54, 2
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds i32, i32* %52, i64 %56
%58 = load i32, i32* %57
%59 = add nsw i32 %51, %58
%60 = load i32*, i32** %6
%61 = load i32, i32* %10
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds i32, i32* %60, i64 %62
store i32 %59, i32* %63
br label %64
64: ; preds = %25, %15
ret void
}

LLVM IR (-O2):
define void @build_sum_segment_tree(i32* %0, i32* %1, i32 %2, i32 %3, i32 %4) {
%6 = icmp eq i32 %2, %3
br i1 %6, label %7, label %15
7: ; preds = %5
%8 = sext i32 %2 to i64
%9 = getelementptr inbounds i32, i32* %1, i64 %8
%10 = load i32, i32* %9
br label %11
11: ; preds = %7, %15
%12 = phi i32 [ %10, %7 ], [ %28, %15 ]
%13 = sext i32 %4 to i64
%14 = getelementptr inbounds i32, i32* %0, i64 %13
store i32 %12, i32* %14
ret void
15: ; preds = %5
%16 = add nsw i32 %3, %2
%17 = sdiv i32 %16, 2
%18 = shl nsw i32 %4, 1
%19 = or i32 %18, 1
tail call void @build_sum_segment_tree(i32* %0, i32* %1, i32 %2, i32 %17, i32 %19)
%20 = add nsw i32 %17, 1
%21 = add nsw i32 %18, 2
tail call void @build_sum_segment_tree(i32* %0, i32* %1, i32 %20, i32 %3, i32 %21)
%22 = sext i32 %19 to i64
%23 = getelementptr inbounds i32, i32* %0, i64 %22
%24 = load i32, i32* %23
%25 = sext i32 %21 to i64
%26 = getelementptr inbounds i32, i32* %0, i64 %25
%27 = load i32, i32* %26
%28 = add nsw i32 %27, %24
br label %11
}