#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 castle_norm_key {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int castle_norm_key_bounds_check(struct castle_norm_key const *,
                                 struct castle_norm_key const *,
                                 struct castle_norm_key const *, int *);
int /*<<< orphan*/ castle_norm_key_dim_inc(struct castle_norm_key *, int);
struct castle_norm_key *castle_norm_key_meld(struct castle_norm_key const *,
                                             struct castle_norm_key const *,
                                             int);
struct castle_norm_key *
castle_norm_key_hypercube_next(const struct castle_norm_key *key,
                               const struct castle_norm_key *low,
                               const struct castle_norm_key *high) {
  int offending_dim, out_of_range;
  out_of_range = castle_norm_key_bounds_check(key, low, high, &offending_dim);
  if (out_of_range) {
    if (offending_dim > 0) {
      struct castle_norm_key *result =
          castle_norm_key_meld(key, low, offending_dim);
      if (!result)
        return NULL;
      if (out_of_range > 0)
        castle_norm_key_dim_inc(result, offending_dim - 1);
      return result;
    } else {
      assert(!(out_of_range < 0));
      return (struct castle_norm_key *)high;
    }
  } else
    return (struct castle_norm_key *)key;
}


x86 assembly (clang, -O0):
<castle_norm_key_hypercube_next>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
lea    -0x24(%rbp),%rcx
call   1030 <castle_norm_key_bounds_check@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     11f2 <castle_norm_key_hypercube_next+0xb2>
cmpl   $0x0,-0x24(%rbp)
jle    11d2 <castle_norm_key_hypercube_next+0x92>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1040 <castle_norm_key_meld@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11ac <castle_norm_key_hypercube_next+0x6c>
movq   $0x0,-0x8(%rbp)
jmp    11fa <castle_norm_key_hypercube_next+0xba>
cmpl   $0x0,-0x28(%rbp)
jle    11c5 <castle_norm_key_hypercube_next+0x85>
mov    -0x30(%rbp),%rdi
mov    -0x24(%rbp),%esi
sub    $0x1,%esi
call   1050 <castle_norm_key_dim_inc@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11fa <castle_norm_key_hypercube_next+0xba>
cmpl   $0x0,-0x28(%rbp)
setl   %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11fa <castle_norm_key_hypercube_next+0xba>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<castle_norm_key_hypercube_next>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0x4(%rsp),%rcx
call   1030 <castle_norm_key_bounds_check@plt>
test   %eax,%eax
je     1190 <castle_norm_key_hypercube_next+0x50>
mov    %eax,%ebp
mov    0x4(%rsp),%edx
test   %edx,%edx
jle    1195 <castle_norm_key_hypercube_next+0x55>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1040 <castle_norm_key_meld@plt>
test   %rax,%rax
je     11a3 <castle_norm_key_hypercube_next+0x63>
mov    %rax,%r15
test   %ebp,%ebp
jle    11a6 <castle_norm_key_hypercube_next+0x66>
mov    0x4(%rsp),%esi
add    $0xffffffff,%esi
mov    %r15,%rdi
call   1050 <castle_norm_key_dim_inc@plt>
jmp    11a6 <castle_norm_key_hypercube_next+0x66>
mov    %rbx,%r15
jmp    11a6 <castle_norm_key_hypercube_next+0x66>
not    %ebp
shr    $0x1f,%ebp
mov    %ebp,%edi
call   1060 <assert@plt>
jmp    11a6 <castle_norm_key_hypercube_next+0x66>
xor    %r15d,%r15d
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.castle_norm_key = type { i32 }
define %struct.castle_norm_key* @castle_norm_key_hypercube_next(%struct.castle_norm_key* %0, %struct.castle_norm_key* %1, %struct.castle_norm_key* %2) {
%4 = alloca %struct.castle_norm_key*
%5 = alloca %struct.castle_norm_key*
%6 = alloca %struct.castle_norm_key*
%7 = alloca %struct.castle_norm_key*
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.castle_norm_key*
store %struct.castle_norm_key* %0, %struct.castle_norm_key** %5
store %struct.castle_norm_key* %1, %struct.castle_norm_key** %6
store %struct.castle_norm_key* %2, %struct.castle_norm_key** %7
%11 = load %struct.castle_norm_key*, %struct.castle_norm_key** %5
%12 = load %struct.castle_norm_key*, %struct.castle_norm_key** %6
%13 = load %struct.castle_norm_key*, %struct.castle_norm_key** %7
%14 = call i32 @castle_norm_key_bounds_check(%struct.castle_norm_key* %11, %struct.castle_norm_key* %12, %struct.castle_norm_key* %13, i32* %8)
store i32 %14, i32* %9
%15 = load i32, i32* %9
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %45
17: ; preds = %3
%18 = load i32, i32* %8
%19 = icmp sgt i32 %18, 0
br i1 %19, label %20, label %38
20: ; preds = %17
%21 = load %struct.castle_norm_key*, %struct.castle_norm_key** %5
%22 = load %struct.castle_norm_key*, %struct.castle_norm_key** %6
%23 = load i32, i32* %8
%24 = call %struct.castle_norm_key* @castle_norm_key_meld(%struct.castle_norm_key* %21, %struct.castle_norm_key* %22, i32 %23)
store %struct.castle_norm_key* %24, %struct.castle_norm_key** %10
%25 = load %struct.castle_norm_key*, %struct.castle_norm_key** %10
%26 = icmp ne %struct.castle_norm_key* %25, null
br i1 %26, label %28, label %27
27: ; preds = %20
store %struct.castle_norm_key* null, %struct.castle_norm_key** %4
br label %47
28: ; preds = %20
%29 = load i32, i32* %9
%30 = icmp sgt i32 %29, 0
br i1 %30, label %31, label %36
31: ; preds = %28
%32 = load %struct.castle_norm_key*, %struct.castle_norm_key** %10
%33 = load i32, i32* %8
%34 = sub nsw i32 %33, 1
%35 = call i32 @castle_norm_key_dim_inc(%struct.castle_norm_key* %32, i32 %34)
br label %36
36: ; preds = %31, %28
%37 = load %struct.castle_norm_key*, %struct.castle_norm_key** %10
store %struct.castle_norm_key* %37, %struct.castle_norm_key** %4
br label %47
38: ; preds = %17
%39 = load i32, i32* %9
%40 = icmp slt i32 %39, 0
%41 = xor i1 %40, true
%42 = zext i1 %41 to i32
%43 = call i32 @assert(i32 %42)
%44 = load %struct.castle_norm_key*, %struct.castle_norm_key** %7
store %struct.castle_norm_key* %44, %struct.castle_norm_key** %4
br label %47
45: ; preds = %3
%46 = load %struct.castle_norm_key*, %struct.castle_norm_key** %5
store %struct.castle_norm_key* %46, %struct.castle_norm_key** %4
br label %47
47: ; preds = %45, %38, %36, %27
%48 = load %struct.castle_norm_key*, %struct.castle_norm_key** %4
ret %struct.castle_norm_key* %48
}

LLVM IR (-O2):
%struct.castle_norm_key = type { i32 }
define %struct.castle_norm_key* @castle_norm_key_hypercube_next(%struct.castle_norm_key* %0, %struct.castle_norm_key* %1, %struct.castle_norm_key* %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
%6 = call i32 @castle_norm_key_bounds_check(%struct.castle_norm_key* %0, %struct.castle_norm_key* %1, %struct.castle_norm_key* %2, i32* %4)
%7 = icmp eq i32 %6, 0
br i1 %7, label %24, label %8
8: ; preds = %3
%9 = load i32, i32* %4
%10 = icmp sgt i32 %9, 0
br i1 %10, label %11, label %20
11: ; preds = %8
%12 = call %struct.castle_norm_key* @castle_norm_key_meld(%struct.castle_norm_key* %0, %struct.castle_norm_key* %1, i32 %9)
%13 = icmp eq %struct.castle_norm_key* %12, null
br i1 %13, label %24, label %14
14: ; preds = %11
%15 = icmp sgt i32 %6, 0
br i1 %15, label %16, label %24
16: ; preds = %14
%17 = load i32, i32* %4
%18 = add nsw i32 %17, -1
%19 = call i32 @castle_norm_key_dim_inc(%struct.castle_norm_key* %12, i32 %18)
br label %24
20: ; preds = %8
%21 = xor i32 %6, -1
%22 = lshr i32 %21, 31
%23 = call i32 @assert(i32 %22)
br label %24
24: ; preds = %3, %11, %16, %14, %20
%25 = phi %struct.castle_norm_key* [ %2, %20 ], [ null, %11 ], [ %12, %16 ], [ %12, %14 ], [ %0, %3 ]
ret %struct.castle_norm_key* %25
}