#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 */
struct xylo_tnode {
  int dummy;
};
struct xylo_tgraph {
  int dummy;
};
typedef int /*<<< orphan*/ nodes;

/* Variables and functions */
int /*<<< orphan*/ fail_test(char *, size_t, int);
int /*<<< orphan*/ get_parent(size_t, struct xylo_tnode **);
int /*<<< orphan*/ init_id(char *, size_t);
int next_sibling(size_t);
int /*<<< orphan*/ xylo_free_tgraph(struct xylo_tgraph *);
struct xylo_tgraph *xylo_make_tgraph(int);
struct xylo_tnode *xylo_make_tnode(struct xylo_tgraph *, int /*<<< orphan*/,
                                   char *);
int /*<<< orphan*/ xylo_tgraph_compact(struct xylo_tgraph *);
char *xylo_tnode_global(struct xylo_tnode *);
char *xylo_tnode_local(struct xylo_tnode *);
int test_check_transformation_locality_after_compaction(void) {
  struct xylo_tgraph *graph;
  struct xylo_tnode *nodes[2 + 4 + 16 + 1024];
  size_t i;
  int j;
  char buf[32];
  char const *p, *q;
  graph = xylo_make_tgraph(sizeof buf);
  for (i = 0; i < (sizeof(nodes) / sizeof 0 [nodes]); i++) {
    init_id(buf, i);
    nodes[i] = xylo_make_tnode(graph, get_parent(i, nodes), buf);
  }
  xylo_tgraph_compact(graph);
  for (i = 0; i < (sizeof(nodes) / sizeof 0 [nodes]); i++) {
    j = next_sibling(i);
    if (j >= 0) {
      p = xylo_tnode_local(nodes[i]);
      q = xylo_tnode_local(nodes[j]);
      if (p + sizeof buf != q) {
        fail_test("nonlocal: %d and %d\n", i, j);
      }
      p = xylo_tnode_global(nodes[i]);
      q = xylo_tnode_global(nodes[j]);
      if (p + sizeof buf != q) {
        fail_test("nonlocal: %d and %d\n", i, j);
      }
    }
  }
  xylo_free_tgraph(graph);
  return 0;
}


x86 assembly (clang, -O0):
<test_check_transformation_locality_after_compaction>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2110,%rsp
mov    $0x20,%edi
call   10b0 <xylo_make_tgraph@plt>
mov    %rax,-0x8(%rbp)
movq   $0x0,-0x20c8(%rbp)
cmpq   $0x416,-0x20c8(%rbp)
jae    1244 <test_check_transformation_locality_after_compaction+0xa4>
lea    -0x20f0(%rbp),%rdi
mov    -0x20c8(%rbp),%rsi
call   1060 <init_id@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x2108(%rbp)
mov    -0x20c8(%rbp),%rdi
lea    -0x20c0(%rbp),%rsi
call   10a0 <get_parent@plt>
mov    -0x2108(%rbp),%rdi
mov    %eax,%esi
lea    -0x20f0(%rbp),%rdx
call   1090 <xylo_make_tnode@plt>
mov    %rax,%rcx
mov    -0x20c8(%rbp),%rax
mov    %rcx,-0x20c0(%rbp,%rax,8)
ff
mov    -0x20c8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20c8(%rbp)
jmp    11c4 <test_check_transformation_locality_after_compaction+0x24>
mov    -0x8(%rbp),%rdi
call   1040 <xylo_tgraph_compact@plt>
movq   $0x0,-0x20c8(%rbp)
cmpq   $0x416,-0x20c8(%rbp)
jae    1377 <test_check_transformation_locality_after_compaction+0x1d7>
mov    -0x20c8(%rbp),%rdi
call   1050 <next_sibling@plt>
mov    %eax,-0x20cc(%rbp)
cmpl   $0x0,-0x20cc(%rbp)
jl     135b <test_check_transformation_locality_after_compaction+0x1bb>
mov    -0x20c8(%rbp),%rax
mov    -0x20c0(%rbp,%rax,8),%rdi
ff
call   1080 <xylo_tnode_local@plt>
mov    %rax,-0x20f8(%rbp)
movslq -0x20cc(%rbp),%rax
mov    -0x20c0(%rbp,%rax,8),%rdi
ff
call   1080 <xylo_tnode_local@plt>
mov    %rax,-0x2100(%rbp)
mov    -0x20f8(%rbp),%rax
add    $0x20,%rax
cmp    -0x2100(%rbp),%rax
je     12ef <test_check_transformation_locality_after_compaction+0x14f>
mov    -0x20c8(%rbp),%rsi
mov    -0x20cc(%rbp),%edx
lea    0xd16(%rip),%rdi
call   1030 <fail_test@plt>
mov    -0x20c8(%rbp),%rax
mov    -0x20c0(%rbp,%rax,8),%rdi
ff
call   1070 <xylo_tnode_global@plt>
mov    %rax,-0x20f8(%rbp)
movslq -0x20cc(%rbp),%rax
mov    -0x20c0(%rbp,%rax,8),%rdi
ff
call   1070 <xylo_tnode_global@plt>
mov    %rax,-0x2100(%rbp)
mov    -0x20f8(%rbp),%rax
add    $0x20,%rax
cmp    -0x2100(%rbp),%rax
je     1356 <test_check_transformation_locality_after_compaction+0x1b6>
mov    -0x20c8(%rbp),%rsi
mov    -0x20cc(%rbp),%edx
lea    0xcaf(%rip),%rdi
call   1030 <fail_test@plt>
jmp    135b <test_check_transformation_locality_after_compaction+0x1bb>
jmp    1360 <test_check_transformation_locality_after_compaction+0x1c0>
mov    -0x20c8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20c8(%rbp)
jmp    1258 <test_check_transformation_locality_after_compaction+0xb8>
mov    -0x8(%rbp),%rdi
call   10c0 <xylo_free_tgraph@plt>
xor    %eax,%eax
add    $0x2110,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_check_transformation_locality_after_compaction>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x20d8,%rsp
mov    $0x20,%edi
call   10b0 <xylo_make_tgraph@plt>
mov    %rax,%r14
xor    %ebx,%ebx
mov    %rsp,%rbp
lea    0x20(%rsp),%r15
nopl   0x0(%rax,%rax,1)
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1060 <init_id@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   10a0 <get_parent@plt>
mov    %r14,%rdi
mov    %eax,%esi
mov    %rbp,%rdx
call   1090 <xylo_make_tnode@plt>
mov    %rax,0x20(%rsp,%rbx,8)
add    $0x1,%rbx
cmp    $0x416,%rbx
jne    11d0 <test_check_transformation_locality_after_compaction+0x30>
mov    %r14,%rdi
call   1040 <xylo_tgraph_compact@plt>
xor    %ebx,%ebx
lea    0xdea(%rip),%r15
jmp    122d <test_check_transformation_locality_after_compaction+0x8d>
nopl   0x0(%rax,%rax,1)
add    $0x1,%rbx
cmp    $0x416,%rbx
je     129d <test_check_transformation_locality_after_compaction+0xfd>
mov    %rbx,%rdi
call   1050 <next_sibling@plt>
test   %eax,%eax
js     1220 <test_check_transformation_locality_after_compaction+0x80>
mov    %eax,%r12d
mov    0x20(%rsp,%rbx,8),%rdi
call   1080 <xylo_tnode_local@plt>
mov    %rax,%rbp
mov    %r12d,%r13d
mov    0x20(%rsp,%r13,8),%rdi
call   1080 <xylo_tnode_local@plt>
add    $0x20,%rbp
cmp    %rax,%rbp
je     126d <test_check_transformation_locality_after_compaction+0xcd>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %r12d,%edx
call   1030 <fail_test@plt>
mov    0x20(%rsp,%rbx,8),%rdi
call   1070 <xylo_tnode_global@plt>
mov    %rax,%rbp
mov    0x20(%rsp,%r13,8),%rdi
call   1070 <xylo_tnode_global@plt>
add    $0x20,%rbp
cmp    %rax,%rbp
je     1220 <test_check_transformation_locality_after_compaction+0x80>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %r12d,%edx
call   1030 <fail_test@plt>
jmp    1220 <test_check_transformation_locality_after_compaction+0x80>
mov    %r14,%rdi
call   10c0 <xylo_free_tgraph@plt>
xor    %eax,%eax
add    $0x20d8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.xylo_tgraph = type { i32 }
%struct.xylo_tnode = type { i32 }
@.str = [21 x i8] c"nonlocal: %d and %d\0A\00"
define i32 @test_check_transformation_locality_after_compaction() {
%1 = alloca %struct.xylo_tgraph*
%2 = alloca [1046 x %struct.xylo_tnode*]
%3 = alloca i64
%4 = alloca i32
%5 = alloca [32 x i8]
%6 = alloca i8*
%7 = alloca i8*
%8 = call %struct.xylo_tgraph* @xylo_make_tgraph(i32 32)
store %struct.xylo_tgraph* %8, %struct.xylo_tgraph** %1
store i64 0, i64* %3
br label %9
9: ; preds = %24, %0
%10 = load i64, i64* %3
%11 = icmp ult i64 %10, 1046
br i1 %11, label %12, label %27
12: ; preds = %9
%13 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%14 = load i64, i64* %3
%15 = call i32 @init_id(i8* %13, i64 %14)
%16 = load %struct.xylo_tgraph*, %struct.xylo_tgraph** %1
%17 = load i64, i64* %3
%18 = getelementptr inbounds [1046 x %struct.xylo_tnode*], [1046 x %struct.xylo_tnode*]* %2, i64 0, i64 0
%19 = call i32 @get_parent(i64 %17, %struct.xylo_tnode** %18)
%20 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%21 = call %struct.xylo_tnode* @xylo_make_tnode(%struct.xylo_tgraph* %16, i32 %19, i8* %20)
%22 = load i64, i64* %3
%23 = getelementptr inbounds [1046 x %struct.xylo_tnode*], [1046 x %struct.xylo_tnode*]* %2, i64 0, i64 %22
store %struct.xylo_tnode* %21, %struct.xylo_tnode** %23
br label %24
24: ; preds = %12
%25 = load i64, i64* %3
%26 = add i64 %25, 1
store i64 %26, i64* %3
br label %9
27: ; preds = %9
%28 = load %struct.xylo_tgraph*, %struct.xylo_tgraph** %1
%29 = call i32 @xylo_tgraph_compact(%struct.xylo_tgraph* %28)
store i64 0, i64* %3
br label %30
30: ; preds = %76, %27
%31 = load i64, i64* %3
%32 = icmp ult i64 %31, 1046
br i1 %32, label %33, label %79
33: ; preds = %30
%34 = load i64, i64* %3
%35 = call i32 @next_sibling(i64 %34)
store i32 %35, i32* %4
%36 = load i32, i32* %4
%37 = icmp sge i32 %36, 0
br i1 %37, label %38, label %75
38: ; preds = %33
%39 = load i64, i64* %3
%40 = getelementptr inbounds [1046 x %struct.xylo_tnode*], [1046 x %struct.xylo_tnode*]* %2, i64 0, i64 %39
%41 = load %struct.xylo_tnode*, %struct.xylo_tnode** %40
%42 = call i8* @xylo_tnode_local(%struct.xylo_tnode* %41)
store i8* %42, i8** %6
%43 = load i32, i32* %4
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds [1046 x %struct.xylo_tnode*], [1046 x %struct.xylo_tnode*]* %2, i64 0, i64 %44
%46 = load %struct.xylo_tnode*, %struct.xylo_tnode** %45
%47 = call i8* @xylo_tnode_local(%struct.xylo_tnode* %46)
store i8* %47, i8** %7
%48 = load i8*, i8** %6
%49 = getelementptr inbounds i8, i8* %48, i64 32
%50 = load i8*, i8** %7
%51 = icmp ne i8* %49, %50
br i1 %51, label %52, label %56
52: ; preds = %38
%53 = load i64, i64* %3
%54 = load i32, i32* %4
%55 = call i32 @fail_test(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i64 %53, i32 %54)
br label %56
56: ; preds = %52, %38
%57 = load i64, i64* %3
%58 = getelementptr inbounds [1046 x %struct.xylo_tnode*], [1046 x %struct.xylo_tnode*]* %2, i64 0, i64 %57
%59 = load %struct.xylo_tnode*, %struct.xylo_tnode** %58
%60 = call i8* @xylo_tnode_global(%struct.xylo_tnode* %59)
store i8* %60, i8** %6
%61 = load i32, i32* %4
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds [1046 x %struct.xylo_tnode*], [1046 x %struct.xylo_tnode*]* %2, i64 0, i64 %62
%64 = load %struct.xylo_tnode*, %struct.xylo_tnode** %63
%65 = call i8* @xylo_tnode_global(%struct.xylo_tnode* %64)
store i8* %65, i8** %7
%66 = load i8*, i8** %6
%67 = getelementptr inbounds i8, i8* %66, i64 32
%68 = load i8*, i8** %7
%69 = icmp ne i8* %67, %68
br i1 %69, label %70, label %74
70: ; preds = %56
%71 = load i64, i64* %3
%72 = load i32, i32* %4
%73 = call i32 @fail_test(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i64 %71, i32 %72)
br label %74
74: ; preds = %70, %56
br label %75
75: ; preds = %74, %33
br label %76
76: ; preds = %75
%77 = load i64, i64* %3
%78 = add i64 %77, 1
store i64 %78, i64* %3
br label %30
79: ; preds = %30
%80 = load %struct.xylo_tgraph*, %struct.xylo_tgraph** %1
%81 = call i32 @xylo_free_tgraph(%struct.xylo_tgraph* %80)
ret i32 0
}

LLVM IR (-O2):
%struct.xylo_tnode = type { i32 }
%struct.xylo_tgraph = type { i32 }
@.str = [21 x i8] c"nonlocal: %d and %d\0A\00"
define i32 @test_check_transformation_locality_after_compaction() {
%1 = alloca [1046 x %struct.xylo_tnode*]
%2 = alloca [32 x i8]
%3 = bitcast [1046 x %struct.xylo_tnode*]* %1 to i8*
%4 = getelementptr inbounds [32 x i8], [32 x i8]* %2, i64 0, i64 0
%5 = tail call %struct.xylo_tgraph* @xylo_make_tgraph(i32 32)
%6 = getelementptr inbounds [1046 x %struct.xylo_tnode*], [1046 x %struct.xylo_tnode*]* %1, i64 0, i64 0
br label %7
7: ; preds = %0, %7
%8 = phi i64 [ 0, %0 ], [ %13, %7 ]
%9 = call i32 @init_id(i8* %4, i64 %8)
%10 = call i32 @get_parent(i64 %8, %struct.xylo_tnode** %6)
%11 = call %struct.xylo_tnode* @xylo_make_tnode(%struct.xylo_tgraph* %5, i32 %10, i8* %4)
%12 = getelementptr inbounds [1046 x %struct.xylo_tnode*], [1046 x %struct.xylo_tnode*]* %1, i64 0, i64 %8
store %struct.xylo_tnode* %11, %struct.xylo_tnode** %12
%13 = add nuw nsw i64 %8, 1
%14 = icmp eq i64 %13, 1046
br i1 %14, label %15, label %7
15: ; preds = %7
%16 = call i32 @xylo_tgraph_compact(%struct.xylo_tgraph* %5)
br label %17
17: ; preds = %15, %42
%18 = phi i64 [ 0, %15 ], [ %43, %42 ]
%19 = call i32 @next_sibling(i64 %18)
%20 = icmp sgt i32 %19, -1
br i1 %20, label %21, label %42
21: ; preds = %17
%22 = getelementptr inbounds [1046 x %struct.xylo_tnode*], [1046 x %struct.xylo_tnode*]* %1, i64 0, i64 %18
%23 = load %struct.xylo_tnode*, %struct.xylo_tnode** %22
%24 = call i8* @xylo_tnode_local(%struct.xylo_tnode* %23)
%25 = zext i32 %19 to i64
%26 = getelementptr inbounds [1046 x %struct.xylo_tnode*], [1046 x %struct.xylo_tnode*]* %1, i64 0, i64 %25
%27 = load %struct.xylo_tnode*, %struct.xylo_tnode** %26
%28 = call i8* @xylo_tnode_local(%struct.xylo_tnode* %27)
%29 = getelementptr inbounds i8, i8* %24, i64 32
%30 = icmp eq i8* %29, %28
br i1 %30, label %33, label %31
31: ; preds = %21
%32 = call i32 @fail_test(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i64 %18, i32 %19)
br label %33
33: ; preds = %31, %21
%34 = load %struct.xylo_tnode*, %struct.xylo_tnode** %22
%35 = call i8* @xylo_tnode_global(%struct.xylo_tnode* %34)
%36 = load %struct.xylo_tnode*, %struct.xylo_tnode** %26
%37 = call i8* @xylo_tnode_global(%struct.xylo_tnode* %36)
%38 = getelementptr inbounds i8, i8* %35, i64 32
%39 = icmp eq i8* %38, %37
br i1 %39, label %42, label %40
40: ; preds = %33
%41 = call i32 @fail_test(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i64 %18, i32 %19)
br label %42
42: ; preds = %17, %40, %33
%43 = add nuw nsw i64 %18, 1
%44 = icmp eq i64 %43, 1046
br i1 %44, label %45, label %17
45: ; preds = %42
%46 = call i32 @xylo_free_tgraph(%struct.xylo_tgraph* %5)
ret i32 0
}