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

/* Variables and functions */
int /*<<< orphan*/ delinearize(size_t, int, size_t *, size_t *);
int /*<<< orphan*/ dump(char *, int, size_t *);
size_t edgeoffset(int, size_t *, size_t *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
size_t linearize(int, size_t *, size_t *);
int /*<<< orphan*/ memset(size_t *, int /*<<< orphan*/, int);
scalar_t__ more(int, size_t *, size_t *);
int /*<<< orphan*/ next(int, size_t *, size_t *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sprintf(char *, char *, size_t);
int main() {
  int k;
  size_t i, j;
  size_t sizes[3] = {3, 2, 2};
  size_t indices[3];
  size_t xp;
  printf("test linearize:\n");
  dump("sizes", 3, sizes);
  memset(indices, 0, sizeof(indices));
  while (more(3, sizes, indices)) {
    char tmp[64];
    size_t linear = linearize(3, sizes, indices);
    sprintf(tmp, "%02u", linear);
    dump(tmp, 3, indices);
    next(3, sizes, indices);
  }
  printf("\ntest de-linearize:\n");
  for (xp = 1, i = 0; i < 3; i++)
    xp *= sizes[i];
  printf("x-product = %u\n", xp);
  for (k = (int)xp - 1; k >= 0; k--) {
    char tmp[64];
    size_t test;
    size_t linear = (size_t)k;
    delinearize(linear, 3, sizes, indices);
    sprintf(tmp, "%02u", linear);
    dump(tmp, 3, indices);
    test = linearize(3, sizes, indices);
    if (test != linear)
      printf("failure: %d\n", k);
  }
  printf("\ntest endpoint:\n");
  {
    size_t count;
    size_t edges[3];
    size_t size1[3];
    count = edgeoffset(3, sizes, sizes);
    printf("|edge count| = %lu\n", (unsigned long)count);
    goto done;
    count = linearize(3, sizes, sizes);
    printf("|sizes| = %lu\n", (unsigned long)count);
    memset(edges, 0, sizeof(edges));
    delinearize(count, 3, sizes, edges);
    dump("invert", 3, edges);
    for (j = 0; j < 3; j++)
      edges[j] = sizes[j];
    for (j = 0; j < 3; j++)
      size1[j] = sizes[j] - 1;
    size1[3 - 1]++;
  }
done:
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe47(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xe44(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0xe41(%rip),%rax
mov    %rax,-0x20(%rbp)
lea    0xe3e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x30(%rbp),%rdx
lea    0xe3d(%rip),%rdi
mov    $0x3,%esi
call   1060 <dump@plt>
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
lea    -0x30(%rbp),%rsi
lea    -0x50(%rbp),%rdx
mov    $0x3,%edi
call   1080 <more@plt>
cmp    $0x0,%rax
je     1281 <main+0xe1>
lea    -0x30(%rbp),%rsi
lea    -0x50(%rbp),%rdx
mov    $0x3,%edi
call   10c0 <linearize@plt>
mov    %rax,-0xa8(%rbp)
lea    -0xa0(%rbp),%rdi
mov    -0xa8(%rbp),%rdx
lea    0xddf(%rip),%rsi
call   10a0 <sprintf@plt>
lea    -0xa0(%rbp),%rdi
lea    -0x50(%rbp),%rdx
mov    $0x3,%esi
call   1060 <dump@plt>
lea    -0x30(%rbp),%rsi
lea    -0x50(%rbp),%rdx
mov    $0x3,%edi
call   1070 <next@plt>
jmp    1206 <main+0x66>
lea    0xdac(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movq   $0x1,-0x58(%rbp)
movq   $0x0,-0x10(%rbp)
cmpq   $0x3,-0x10(%rbp)
jae    12cd <main+0x12d>
mov    -0x10(%rbp),%rax
mov    -0x30(%rbp,%rax,8),%rax
imul   -0x58(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    129f <main+0xff>
mov    -0x58(%rbp),%rsi
lea    0xd71(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x58(%rbp),%rax
sub    $0x1,%eax
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     1397 <main+0x1f7>
movslq -0x8(%rbp),%rax
mov    %rax,-0x100(%rbp)
mov    -0x100(%rbp),%rdi
lea    -0x30(%rbp),%rdx
lea    -0x50(%rbp),%rcx
mov    $0x3,%esi
call   1090 <delinearize@plt>
lea    -0xf0(%rbp),%rdi
mov    -0x100(%rbp),%rdx
lea    0xd03(%rip),%rsi
call   10a0 <sprintf@plt>
lea    -0xf0(%rbp),%rdi
lea    -0x50(%rbp),%rdx
mov    $0x3,%esi
call   1060 <dump@plt>
lea    -0x30(%rbp),%rsi
lea    -0x50(%rbp),%rdx
mov    $0x3,%edi
call   10c0 <linearize@plt>
mov    %rax,-0xf8(%rbp)
mov    -0xf8(%rbp),%rax
cmp    -0x100(%rbp),%rax
je     1384 <main+0x1e4>
mov    -0x8(%rbp),%esi
lea    0xcdc(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1389 <main+0x1e9>
mov    -0x8(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x8(%rbp)
jmp    12e9 <main+0x149>
lea    0xcc8(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x30(%rbp),%rsi
lea    -0x30(%rbp),%rdx
mov    $0x3,%edi
call   1050 <edgeoffset@plt>
mov    %rax,-0x108(%rbp)
mov    -0x108(%rbp),%rsi
lea    0xcab(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x88,%rsp
mov    0xe48(%rip),%rax
mov    %rax,0x10(%rsp)
movaps 0xe2c(%rip),%xmm0
movaps %xmm0,(%rsp)
lea    0xe75(%rip),%rdi
call   1030 <puts@plt>
lea    0xe2d(%rip),%rdi
mov    %rsp,%rbx
mov    $0x3,%esi
mov    %rbx,%rdx
call   1070 <dump@plt>
lea    0x20(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
mov    $0x3,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1090 <more@plt>
test   %rax,%rax
je     1293 <main+0xe3>
mov    %rsp,%rbx
lea    0x20(%rsp),%rbp
lea    0xdeb(%rip),%r14
lea    0x40(%rsp),%r15
nopl   0x0(%rax,%rax,1)
mov    $0x3,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   10d0 <linearize@plt>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rax,%rdx
call   10b0 <sprintf@plt>
mov    %r15,%rdi
mov    $0x3,%esi
mov    %rbp,%rdx
call   1070 <dump@plt>
mov    $0x3,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1080 <next@plt>
mov    $0x3,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1090 <more@plt>
test   %rax,%rax
jne    1240 <main+0x90>
lea    0xdca(%rip),%rdi
call   1030 <puts@plt>
mov    0x8(%rsp),%rbx
imul   (%rsp),%rbx
imul   0x10(%rsp),%rbx
lea    0xd6d(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
test   %ebx,%ebx
jle    135e <main+0x1ae>
mov    %ebx,%r12d
add    $0x1,%r12
add    $0xffffffff,%ebx
mov    %rsp,%r14
lea    0x20(%rsp),%r15
lea    0x40(%rsp),%r13
jmp    12fd <main+0x14d>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    $0xffffffffffffffff,%r12
add    $0xffffffff,%ebx
cmp    $0x1,%r12
jbe    135e <main+0x1ae>
lea    -0x2(%r12),%ebp
mov    %rbp,%rdi
mov    $0x3,%esi
mov    %r14,%rdx
mov    %r15,%rcx
call   10a0 <delinearize@plt>
mov    %r13,%rdi
lea    0xcff(%rip),%rsi
mov    %rbp,%rdx
call   10b0 <sprintf@plt>
mov    %r13,%rdi
mov    $0x3,%esi
mov    %r15,%rdx
call   1070 <dump@plt>
mov    $0x3,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   10d0 <linearize@plt>
cmp    %rbp,%rax
je     12f0 <main+0x140>
lea    0xce0(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    12f0 <main+0x140>
lea    0xd13(%rip),%rdi
call   1030 <puts@plt>
mov    %rsp,%rdx
mov    $0x3,%edi
mov    %rdx,%rsi
call   1060 <edgeoffset@plt>
lea    0xcbf(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %edi,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@__const.main.sizes = [3 x i64] [i64 3, i64 2, i64 2]
@.str = [17 x i8] c"test linearize:\0A\00"
@.str.1 = [6 x i8] c"sizes\00"
@.str.2 = [5 x i8] c"%02u\00"
@.str.3 = [21 x i8] c"\0Atest de-linearize:\0A\00"
@.str.4 = [16 x i8] c"x-product = %u\0A\00"
@.str.5 = [13 x i8] c"failure: %d\0A\00"
@.str.6 = [17 x i8] c"\0Atest endpoint:\0A\00"
@.str.7 = [20 x i8] c"|edge count| = %lu\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i64
%4 = alloca i64
%5 = alloca [3 x i64]
%6 = alloca [3 x i64]
%7 = alloca i64
%8 = alloca [64 x i8]
%9 = alloca i64
%10 = alloca [64 x i8]
%11 = alloca i64
%12 = alloca i64
%13 = alloca i64
%14 = alloca [3 x i64]
%15 = alloca [3 x i64]
store i32 0, i32* %1
%16 = bitcast [3 x i64]* %5 to i8*
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%18 = getelementptr inbounds [3 x i64], [3 x i64]* %5, i64 0, i64 0
%19 = call i32 @dump(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 3, i64* %18)
%20 = getelementptr inbounds [3 x i64], [3 x i64]* %6, i64 0, i64 0
%21 = call i32 @memset(i64* %20, i32 0, i32 24)
br label %22
22: ; preds = %27, %0
%23 = getelementptr inbounds [3 x i64], [3 x i64]* %5, i64 0, i64 0
%24 = getelementptr inbounds [3 x i64], [3 x i64]* %6, i64 0, i64 0
%25 = call i64 @more(i32 3, i64* %23, i64* %24)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %40
27: ; preds = %22
%28 = getelementptr inbounds [3 x i64], [3 x i64]* %5, i64 0, i64 0
%29 = getelementptr inbounds [3 x i64], [3 x i64]* %6, i64 0, i64 0
%30 = call i64 @linearize(i32 3, i64* %28, i64* %29)
store i64 %30, i64* %9
%31 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%32 = load i64, i64* %9
%33 = call i32 @sprintf(i8* %31, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i64 %32)
%34 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%35 = getelementptr inbounds [3 x i64], [3 x i64]* %6, i64 0, i64 0
%36 = call i32 @dump(i8* %34, i32 3, i64* %35)
%37 = getelementptr inbounds [3 x i64], [3 x i64]* %5, i64 0, i64 0
%38 = getelementptr inbounds [3 x i64], [3 x i64]* %6, i64 0, i64 0
%39 = call i32 @next(i32 3, i64* %37, i64* %38)
br label %22
40: ; preds = %22
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
store i64 1, i64* %7
store i64 0, i64* %3
br label %42
42: ; preds = %51, %40
%43 = load i64, i64* %3
%44 = icmp ult i64 %43, 3
br i1 %44, label %45, label %54
45: ; preds = %42
%46 = load i64, i64* %3
%47 = getelementptr inbounds [3 x i64], [3 x i64]* %5, i64 0, i64 %46
%48 = load i64, i64* %47
%49 = load i64, i64* %7
%50 = mul i64 %49, %48
store i64 %50, i64* %7
br label %51
51: ; preds = %45
%52 = load i64, i64* %3
%53 = add i64 %52, 1
store i64 %53, i64* %3
br label %42
54: ; preds = %42
%55 = load i64, i64* %7
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i64 %55)
%57 = load i64, i64* %7
%58 = trunc i64 %57 to i32
%59 = sub nsw i32 %58, 1
store i32 %59, i32* %2
br label %60
60: ; preds = %86, %54
%61 = load i32, i32* %2
%62 = icmp sge i32 %61, 0
br i1 %62, label %63, label %89
63: ; preds = %60
%64 = load i32, i32* %2
%65 = sext i32 %64 to i64
store i64 %65, i64* %12
%66 = load i64, i64* %12
%67 = getelementptr inbounds [3 x i64], [3 x i64]* %5, i64 0, i64 0
%68 = getelementptr inbounds [3 x i64], [3 x i64]* %6, i64 0, i64 0
%69 = call i32 @delinearize(i64 %66, i32 3, i64* %67, i64* %68)
%70 = getelementptr inbounds [64 x i8], [64 x i8]* %10, i64 0, i64 0
%71 = load i64, i64* %12
%72 = call i32 @sprintf(i8* %70, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i64 %71)
%73 = getelementptr inbounds [64 x i8], [64 x i8]* %10, i64 0, i64 0
%74 = getelementptr inbounds [3 x i64], [3 x i64]* %6, i64 0, i64 0
%75 = call i32 @dump(i8* %73, i32 3, i64* %74)
%76 = getelementptr inbounds [3 x i64], [3 x i64]* %5, i64 0, i64 0
%77 = getelementptr inbounds [3 x i64], [3 x i64]* %6, i64 0, i64 0
%78 = call i64 @linearize(i32 3, i64* %76, i64* %77)
store i64 %78, i64* %11
%79 = load i64, i64* %11
%80 = load i64, i64* %12
%81 = icmp ne i64 %79, %80
br i1 %81, label %82, label %85
82: ; preds = %63
%83 = load i32, i32* %2
%84 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0), i32 %83)
br label %85
85: ; preds = %82, %63
br label %86
86: ; preds = %85
%87 = load i32, i32* %2
%88 = add nsw i32 %87, -1
store i32 %88, i32* %2
br label %60
89: ; preds = %60
%90 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.6, i64 0, i64 0))
%91 = getelementptr inbounds [3 x i64], [3 x i64]* %5, i64 0, i64 0
%92 = getelementptr inbounds [3 x i64], [3 x i64]* %5, i64 0, i64 0
%93 = call i64 @edgeoffset(i32 3, i64* %91, i64* %92)
store i64 %93, i64* %13
%94 = load i64, i64* %13
%95 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.7, i64 0, i64 0), i64 %94)
br label %96
96: ; preds = %89
%97 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@__const.main.sizes = [3 x i64] [i64 3, i64 2, i64 2]
@.str.1 = [6 x i8] c"sizes\00"
@.str.2 = [5 x i8] c"%02u\00"
@.str.4 = [16 x i8] c"x-product = %u\0A\00"
@.str.5 = [13 x i8] c"failure: %d\0A\00"
@.str.7 = [20 x i8] c"|edge count| = %lu\0A\00"
@str = [16 x i8] c"test linearize:\00"
@str.8 = [20 x i8] c"\0Atest de-linearize:\00"
@str.9 = [16 x i8] c"\0Atest endpoint:\00"
define i32 @main() {
%1 = alloca [3 x i64]
%2 = alloca [3 x i64]
%3 = alloca [64 x i8]
%4 = alloca [64 x i8]
%5 = bitcast [3 x i64]* %1 to i8*
%6 = bitcast [3 x i64]* %2 to i8*
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
%8 = getelementptr inbounds [3 x i64], [3 x i64]* %1, i64 0, i64 0
%9 = call i32 @dump(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 3, i64* %8)
%10 = getelementptr inbounds [3 x i64], [3 x i64]* %2, i64 0, i64 0
%11 = call i32 @memset(i64* %10, i32 0, i32 24)
%12 = call i64 @more(i32 3, i64* %8, i64* %10)
%13 = icmp eq i64 %12, 0
br i1 %13, label %23, label %14
14: ; preds = %0
%15 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
br label %16
16: ; preds = %14, %16
%17 = call i64 @linearize(i32 3, i64* %8, i64* %10)
%18 = call i32 @sprintf(i8* %15, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i64 %17)
%19 = call i32 @dump(i8* %15, i32 3, i64* %10)
%20 = call i32 @next(i32 3, i64* %8, i64* %10)
%21 = call i64 @more(i32 3, i64* %8, i64* %10)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %16
23: ; preds = %16, %0
%24 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.8, i64 0, i64 0))
%25 = load i64, i64* %8
%26 = getelementptr inbounds [3 x i64], [3 x i64]* %1, i64 0, i64 1
%27 = load i64, i64* %26
%28 = mul i64 %27, %25
%29 = getelementptr inbounds [3 x i64], [3 x i64]* %1, i64 0, i64 2
%30 = load i64, i64* %29
%31 = mul i64 %30, %28
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i64 %31)
%33 = trunc i64 %31 to i32
%34 = icmp sgt i32 %33, 0
br i1 %34, label %35, label %52
35: ; preds = %23
%36 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%37 = and i64 %31, 4294967295
br label %38
38: ; preds = %35, %50
%39 = phi i64 [ %37, %35 ], [ %40, %50 ]
%40 = add nsw i64 %39, -1
%41 = and i64 %40, 4294967295
%42 = call i32 @delinearize(i64 %41, i32 3, i64* %8, i64* %10)
%43 = call i32 @sprintf(i8* %36, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i64 %41)
%44 = call i32 @dump(i8* %36, i32 3, i64* %10)
%45 = call i64 @linearize(i32 3, i64* %8, i64* %10)
%46 = icmp eq i64 %45, %41
br i1 %46, label %50, label %47
47: ; preds = %38
%48 = trunc i64 %40 to i32
%49 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0), i32 %48)
br label %50
50: ; preds = %47, %38
%51 = icmp ugt i64 %39, 1
br i1 %51, label %38, label %52
52: ; preds = %50, %23
%53 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.9, i64 0, i64 0))
%54 = call i64 @edgeoffset(i32 3, i64* %8, i64* %8)
%55 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.7, i64 0, i64 0), i64 %54)
%56 = call i32 @exit(i32 0)
unreachable
}