#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 */
int dep(int, int);
int geometric_series(int, int);
int reverse_tree_direct_children(int rank, int num_nodes, int width, int depth,
                                 int *children) {
  int current, child_distance;
  int max_depth, sub_depth, max_rank_children;
  int i;
  max_depth = dep(num_nodes, width);
  sub_depth = max_depth - depth;
  if (sub_depth == 0) {
    return 0;
  }
  max_rank_children = geometric_series(width, sub_depth);
  current = rank + 1;
  child_distance = (max_rank_children / width);
  for (i = 0; i < width && current < num_nodes; i++) {
    children[i] = current;
    current += child_distance;
  }
  return i;
}


x86 assembly (clang, -O0):
<reverse_tree_direct_children>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    %r8,-0x20(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x10(%rbp),%esi
call   1040 <dep@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
sub    -0x14(%rbp),%eax
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jne    1165 <reverse_tree_direct_children+0x45>
movl   $0x0,-0x4(%rbp)
jmp    11e5 <reverse_tree_direct_children+0xc5>
mov    -0x10(%rbp),%edi
mov    -0x30(%rbp),%esi
call   1030 <geometric_series@plt>
mov    %eax,-0x34(%rbp)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x34(%rbp),%eax
cltd
idivl  -0x10(%rbp)
mov    %eax,-0x28(%rbp)
movl   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%ecx
xor    %eax,%eax
cmp    -0x10(%rbp),%ecx
mov    %al,-0x39(%rbp)
jge    11aa <reverse_tree_direct_children+0x8a>
mov    -0x24(%rbp),%eax
cmp    -0xc(%rbp),%eax
setl   %al
mov    %al,-0x39(%rbp)
mov    -0x39(%rbp),%al
test   $0x1,%al
jne    11ba <reverse_tree_direct_children+0x9a>
jmp    11df <reverse_tree_direct_children+0xbf>
mov    -0x24(%rbp),%edx
mov    -0x20(%rbp),%rax
movslq -0x38(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x28(%rbp),%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    118d <reverse_tree_direct_children+0x6d>
mov    -0x38(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<reverse_tree_direct_children>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8,%r14
mov    %ecx,%r13d
mov    %edx,%r12d
mov    %esi,%r15d
mov    %edi,%ebp
mov    %esi,%edi
mov    %edx,%esi
call   1040 <dep@plt>
xor    %ebx,%ebx
sub    %r13d,%eax
je     1184 <reverse_tree_direct_children+0x64>
mov    %r12d,%edi
mov    %eax,%esi
call   1030 <geometric_series@plt>
cltd
idiv   %r12d
test   %r12d,%r12d
jle    1184 <reverse_tree_direct_children+0x64>
add    $0x1,%ebp
cmp    %r15d,%ebp
jge    1184 <reverse_tree_direct_children+0x64>
mov    %r12d,%ecx
xor    %ebx,%ebx
nopl   0x0(%rax)
mov    %ebp,(%r14,%rbx,4)
add    $0x1,%rbx
cmp    %rcx,%rbx
jae    1184 <reverse_tree_direct_children+0x64>
add    %eax,%ebp
cmp    %r15d,%ebp
jl     1170 <reverse_tree_direct_children+0x50>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @reverse_tree_direct_children(i32 %0, i32 %1, i32 %2, i32 %3, i32* nocapture writeonly %4) {
%6 = tail call i32 @dep(i32 %1, i32 %2)
%7 = sub nsw i32 %6, %3
%8 = icmp eq i32 %7, 0
br i1 %8, label %29, label %9
9: ; preds = %5
%10 = tail call i32 @geometric_series(i32 %2, i32 %7)
%11 = add nsw i32 %0, 1
%12 = sdiv i32 %10, %2
%13 = icmp sgt i32 %2, 0
%14 = icmp slt i32 %11, %1
%15 = select i1 %13, i1 %14, i1 false
br i1 %15, label %16, label %29
16: ; preds = %9
%17 = zext i32 %2 to i64
br label %18
18: ; preds = %16, %18
%19 = phi i64 [ 0, %16 ], [ %23, %18 ]
%20 = phi i32 [ %11, %16 ], [ %22, %18 ]
%21 = getelementptr inbounds i32, i32* %4, i64 %19
store i32 %20, i32* %21
%22 = add nsw i32 %20, %12
%23 = add nuw nsw i64 %19, 1
%24 = icmp ult i64 %23, %17
%25 = icmp slt i32 %22, %1
%26 = select i1 %24, i1 %25, i1 false
br i1 %26, label %18, label %27
27: ; preds = %18
%28 = trunc i64 %23 to i32
br label %29
29: ; preds = %27, %9, %5
%30 = phi i32 [ 0, %5 ], [ 0, %9 ], [ %28, %27 ]
ret i32 %30
}