void build_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_segment_tree(segment_tree, input, low, mid, (2 * pos + 1));
  build_segment_tree(segment_tree, input, mid + 1, high, (2 * pos + 2));
  segment_tree[pos] = ((segment_tree[2 * pos + 1]) > (segment_tree[2 * pos + 2])
                           ? (segment_tree[2 * pos + 1])
                           : (segment_tree[2 * pos + 2]));
}


x86 assembly (clang, -O0):
<build_segment_tree>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%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_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    120e <build_segment_tree+0xfe>
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_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_segment_tree@plt>
mov    -0x8(%rbp),%rax
mov    -0x1c(%rbp),%ecx
shl    $0x1,%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%eax
mov    -0x8(%rbp),%rcx
mov    -0x1c(%rbp),%edx
shl    $0x1,%edx
add    $0x2,%edx
movslq %edx,%rdx
cmp    (%rcx,%rdx,4),%eax
jle    11ea <build_segment_tree+0xda>
mov    -0x8(%rbp),%rax
mov    -0x1c(%rbp),%ecx
shl    $0x1,%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x24(%rbp)
jmp    1200 <build_segment_tree+0xf0>
mov    -0x8(%rbp),%rax
mov    -0x1c(%rbp),%ecx
shl    $0x1,%ecx
add    $0x2,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edx
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<build_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_segment_tree+0x21>
movslq %edx,%rax
mov    (%r12,%rax,4),%eax
jmp    1190 <build_segment_tree+0x80>
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_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_segment_tree@plt>
movslq %r13d,%rax
mov    (%rbx,%rax,4),%ecx
movslq 0x4(%rsp),%rax
mov    0x8(%rbx,%rax,4),%eax
cmp    %eax,%ecx
cmovg  %ecx,%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_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 %82
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 = icmp sgt i32 %51, %58
br i1 %59, label %60, label %68
60: ; preds = %25
%61 = load i32*, i32** %6
%62 = load i32, i32* %10
%63 = mul nsw i32 2, %62
%64 = add nsw i32 %63, 1
%65 = sext i32 %64 to i64
%66 = getelementptr inbounds i32, i32* %61, i64 %65
%67 = load i32, i32* %66
br label %76
68: ; preds = %25
%69 = load i32*, i32** %6
%70 = load i32, i32* %10
%71 = mul nsw i32 2, %70
%72 = add nsw i32 %71, 2
%73 = sext i32 %72 to i64
%74 = getelementptr inbounds i32, i32* %69, i64 %73
%75 = load i32, i32* %74
br label %76
76: ; preds = %68, %60
%77 = phi i32 [ %67, %60 ], [ %75, %68 ]
%78 = load i32*, i32** %6
%79 = load i32, i32* %10
%80 = sext i32 %79 to i64
%81 = getelementptr inbounds i32, i32* %78, i64 %80
store i32 %77, i32* %81
br label %82
82: ; preds = %76, %15
ret void
}

LLVM IR (-O2):
define void @build_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 ], [ %29, %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_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_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 = icmp sgt i32 %24, %27
%29 = select i1 %28, i32 %24, i32 %27
br label %11
}