#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);
scalar_t__ castle_norm_key_data_compare(unsigned char const *, int,
                                        unsigned char const *, int);
size_t castle_norm_key_dim_get(unsigned char const **);
unsigned char *castle_norm_key_dim_next(unsigned char const *);
unsigned char *castle_norm_key_end(struct castle_norm_key const *,
                                   unsigned char const **);
int castle_norm_key_bounds_check(const struct castle_norm_key *key,
                                 const struct castle_norm_key *lower,
                                 const struct castle_norm_key *upper,
                                 unsigned int *offending_dim) {
  const unsigned char *key_curr, *key_end = castle_norm_key_end(key, &key_curr);
  const unsigned char *lower_curr,
      *lower_end = castle_norm_key_end(lower, &lower_curr);
  const unsigned char *upper_curr,
      *upper_end = castle_norm_key_end(upper, &upper_curr);
  unsigned int dim;
  size_t key_dim = castle_norm_key_dim_get(&key_curr);
  size_t lower_dim = castle_norm_key_dim_get(&lower_curr);
  size_t upper_dim = castle_norm_key_dim_get(&upper_curr);
  assert(!(key_dim != lower_dim || key_dim != upper_dim));
  for (dim = 0; dim < key_dim; ++dim) {
    const unsigned char *key_next = castle_norm_key_dim_next(key_curr);
    const unsigned char *lower_next = castle_norm_key_dim_next(lower_curr);
    const unsigned char *upper_next = castle_norm_key_dim_next(upper_curr);
    if (castle_norm_key_data_compare(key_curr, key_next - key_curr, lower_curr,
                                     lower_next - lower_curr) < 0) {
      if (offending_dim)
        *offending_dim = dim;
      return -1;
    }
    if (castle_norm_key_data_compare(key_curr, key_next - key_curr, upper_curr,
                                     upper_next - upper_curr) > 0) {
      if (offending_dim)
        *offending_dim = dim;
      return 1;
    }
    key_curr = key_next;
    lower_curr = lower_next;
    upper_curr = upper_next;
  }
  assert(!(key_curr != key_end || lower_curr != lower_end ||
           upper_curr != upper_end));
  return 0;
}


x86 assembly (clang, -O0):
<castle_norm_key_bounds_check>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1060 <castle_norm_key_end@plt>
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x40(%rbp),%rsi
call   1060 <castle_norm_key_end@plt>
mov    %rax,-0x48(%rbp)
mov    -0x20(%rbp),%rdi
lea    -0x50(%rbp),%rsi
call   1060 <castle_norm_key_end@plt>
mov    %rax,-0x58(%rbp)
lea    -0x30(%rbp),%rdi
call   1070 <castle_norm_key_dim_get@plt>
mov    %rax,-0x68(%rbp)
lea    -0x40(%rbp),%rdi
call   1070 <castle_norm_key_dim_get@plt>
mov    %rax,-0x70(%rbp)
lea    -0x50(%rbp),%rdi
call   1070 <castle_norm_key_dim_get@plt>
mov    %rax,-0x78(%rbp)
mov    -0x68(%rbp),%rcx
mov    $0x1,%al
cmp    -0x70(%rbp),%rcx
mov    %al,-0x91(%rbp)
jne    11ec <castle_norm_key_bounds_check+0x9c>
mov    -0x68(%rbp),%rax
cmp    -0x78(%rbp),%rax
setne  %al
mov    %al,-0x91(%rbp)
mov    -0x91(%rbp),%al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
movl   $0x0,-0x5c(%rbp)
mov    -0x5c(%rbp),%eax
cmp    -0x68(%rbp),%rax
jae    1313 <castle_norm_key_bounds_check+0x1c3>
mov    -0x30(%rbp),%rdi
call   1030 <castle_norm_key_dim_next@plt>
mov    %rax,-0x80(%rbp)
mov    -0x40(%rbp),%rdi
call   1030 <castle_norm_key_dim_next@plt>
mov    %rax,-0x88(%rbp)
mov    -0x50(%rbp),%rdi
call   1030 <castle_norm_key_dim_next@plt>
mov    %rax,-0x90(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x80(%rbp),%rax
mov    -0x30(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%esi
mov    -0x40(%rbp),%rdx
mov    -0x88(%rbp),%rax
mov    -0x40(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%ecx
call   1050 <castle_norm_key_data_compare@plt>
cmp    $0x0,%rax
jge    1293 <castle_norm_key_bounds_check+0x143>
cmpq   $0x0,-0x28(%rbp)
je     1287 <castle_norm_key_bounds_check+0x137>
mov    -0x5c(%rbp),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1369 <castle_norm_key_bounds_check+0x219>
mov    -0x30(%rbp),%rdi
mov    -0x80(%rbp),%rax
mov    -0x30(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%esi
mov    -0x50(%rbp),%rdx
mov    -0x90(%rbp),%rax
mov    -0x50(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%ecx
call   1050 <castle_norm_key_data_compare@plt>
cmp    $0x0,%rax
jle    12e7 <castle_norm_key_bounds_check+0x197>
cmpq   $0x0,-0x28(%rbp)
je     12db <castle_norm_key_bounds_check+0x18b>
mov    -0x5c(%rbp),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    1369 <castle_norm_key_bounds_check+0x219>
mov    -0x80(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x88(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x90(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x5c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x5c(%rbp)
jmp    1205 <castle_norm_key_bounds_check+0xb5>
mov    -0x30(%rbp),%rcx
mov    $0x1,%al
cmp    -0x38(%rbp),%rcx
mov    %al,-0x92(%rbp)
jne    1350 <castle_norm_key_bounds_check+0x200>
mov    -0x40(%rbp),%rcx
mov    $0x1,%al
cmp    -0x48(%rbp),%rcx
mov    %al,-0x92(%rbp)
jne    1350 <castle_norm_key_bounds_check+0x200>
mov    -0x50(%rbp),%rax
cmp    -0x58(%rbp),%rax
setne  %al
mov    %al,-0x92(%rbp)
mov    -0x92(%rbp),%al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<castle_norm_key_bounds_check>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rcx,0x18(%rsp)
mov    %rdx,%rbx
mov    %rsi,%rbp
lea    0x10(%rsp),%r14
mov    %r14,%rsi
call   1060 <castle_norm_key_end@plt>
mov    %rax,0x30(%rsp)
lea    0x8(%rsp),%r15
mov    %rbp,%rdi
mov    %r15,%rsi
call   1060 <castle_norm_key_end@plt>
mov    %rax,0x28(%rsp)
mov    %rsp,%r12
mov    %rbx,%rdi
mov    %r12,%rsi
call   1060 <castle_norm_key_end@plt>
mov    %rax,0x20(%rsp)
mov    %r14,%rdi
call   1070 <castle_norm_key_dim_get@plt>
mov    %rax,%r13
mov    %r15,%rdi
call   1070 <castle_norm_key_dim_get@plt>
mov    %rax,%rbp
mov    %r12,%rdi
call   1070 <castle_norm_key_dim_get@plt>
xor    %r13,%rbp
xor    %r13,%rax
xor    %edi,%edi
or     %rbp,%rax
sete   %dil
call   1040 <assert@plt>
mov    0x10(%rsp),%rbp
test   %r13,%r13
je     1265 <castle_norm_key_bounds_check+0x115>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbp,%rdi
call   1030 <castle_norm_key_dim_next@plt>
mov    %rax,%rbp
mov    0x8(%rsp),%rdi
call   1030 <castle_norm_key_dim_next@plt>
mov    %rax,%r14
mov    (%rsp),%rdi
call   1030 <castle_norm_key_dim_next@plt>
mov    %rax,%r15
mov    0x10(%rsp),%rdi
mov    %ebp,%esi
sub    %edi,%esi
mov    0x8(%rsp),%rdx
mov    %r14d,%ecx
sub    %edx,%ecx
call   1050 <castle_norm_key_data_compare@plt>
test   %rax,%rax
js     1292 <castle_norm_key_bounds_check+0x142>
mov    0x10(%rsp),%rdi
mov    %ebp,%esi
sub    %edi,%esi
mov    (%rsp),%rdx
mov    %r15d,%ecx
sub    %edx,%ecx
call   1050 <castle_norm_key_data_compare@plt>
test   %rax,%rax
jg     1299 <castle_norm_key_bounds_check+0x149>
mov    %rbp,0x10(%rsp)
mov    %r14,0x8(%rsp)
mov    %r15,(%rsp)
add    $0x1,%ebx
cmp    %rbx,%r13
ja     11f0 <castle_norm_key_bounds_check+0xa0>
jmp    126e <castle_norm_key_bounds_check+0x11e>
mov    0x8(%rsp),%r14
mov    (%rsp),%r15
xor    0x30(%rsp),%rbp
xor    0x28(%rsp),%r14
or     %rbp,%r14
xor    0x20(%rsp),%r15
xor    %edi,%edi
or     %r14,%r15
sete   %dil
call   1040 <assert@plt>
xor    %eax,%eax
jmp    12aa <castle_norm_key_bounds_check+0x15a>
mov    $0xffffffff,%eax
jmp    129e <castle_norm_key_bounds_check+0x14e>
mov    $0x1,%eax
mov    0x18(%rsp),%rcx
test   %rcx,%rcx
je     12aa <castle_norm_key_bounds_check+0x15a>
mov    %ebx,(%rcx)
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.castle_norm_key = type { i32 }
define i32 @castle_norm_key_bounds_check(%struct.castle_norm_key* %0, %struct.castle_norm_key* %1, %struct.castle_norm_key* %2, i32* %3) {
%5 = alloca i32
%6 = alloca %struct.castle_norm_key*
%7 = alloca %struct.castle_norm_key*
%8 = alloca %struct.castle_norm_key*
%9 = alloca i32*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca i8*
%15 = alloca i8*
%16 = alloca i32
%17 = alloca i64
%18 = alloca i64
%19 = alloca i64
%20 = alloca i8*
%21 = alloca i8*
%22 = alloca i8*
store %struct.castle_norm_key* %0, %struct.castle_norm_key** %6
store %struct.castle_norm_key* %1, %struct.castle_norm_key** %7
store %struct.castle_norm_key* %2, %struct.castle_norm_key** %8
store i32* %3, i32** %9
%23 = load %struct.castle_norm_key*, %struct.castle_norm_key** %6
%24 = call i8* @castle_norm_key_end(%struct.castle_norm_key* %23, i8** %10)
store i8* %24, i8** %11
%25 = load %struct.castle_norm_key*, %struct.castle_norm_key** %7
%26 = call i8* @castle_norm_key_end(%struct.castle_norm_key* %25, i8** %12)
store i8* %26, i8** %13
%27 = load %struct.castle_norm_key*, %struct.castle_norm_key** %8
%28 = call i8* @castle_norm_key_end(%struct.castle_norm_key* %27, i8** %14)
store i8* %28, i8** %15
%29 = call i64 @castle_norm_key_dim_get(i8** %10)
store i64 %29, i64* %17
%30 = call i64 @castle_norm_key_dim_get(i8** %12)
store i64 %30, i64* %18
%31 = call i64 @castle_norm_key_dim_get(i8** %14)
store i64 %31, i64* %19
%32 = load i64, i64* %17
%33 = load i64, i64* %18
%34 = icmp ne i64 %32, %33
br i1 %34, label %39, label %35
35: ; preds = %4
%36 = load i64, i64* %17
%37 = load i64, i64* %19
%38 = icmp ne i64 %36, %37
br label %39
39: ; preds = %35, %4
%40 = phi i1 [ true, %4 ], [ %38, %35 ]
%41 = xor i1 %40, true
%42 = zext i1 %41 to i32
%43 = call i32 @assert(i32 %42)
store i32 0, i32* %16
br label %44
44: ; preds = %107, %39
%45 = load i32, i32* %16
%46 = zext i32 %45 to i64
%47 = load i64, i64* %17
%48 = icmp ult i64 %46, %47
br i1 %48, label %49, label %110
49: ; preds = %44
%50 = load i8*, i8** %10
%51 = call i8* @castle_norm_key_dim_next(i8* %50)
store i8* %51, i8** %20
%52 = load i8*, i8** %12
%53 = call i8* @castle_norm_key_dim_next(i8* %52)
store i8* %53, i8** %21
%54 = load i8*, i8** %14
%55 = call i8* @castle_norm_key_dim_next(i8* %54)
store i8* %55, i8** %22
%56 = load i8*, i8** %10
%57 = load i8*, i8** %20
%58 = load i8*, i8** %10
%59 = ptrtoint i8* %57 to i64
%60 = ptrtoint i8* %58 to i64
%61 = sub i64 %59, %60
%62 = trunc i64 %61 to i32
%63 = load i8*, i8** %12
%64 = load i8*, i8** %21
%65 = load i8*, i8** %12
%66 = ptrtoint i8* %64 to i64
%67 = ptrtoint i8* %65 to i64
%68 = sub i64 %66, %67
%69 = trunc i64 %68 to i32
%70 = call i64 @castle_norm_key_data_compare(i8* %56, i32 %62, i8* %63, i32 %69)
%71 = icmp slt i64 %70, 0
br i1 %71, label %72, label %79
72: ; preds = %49
%73 = load i32*, i32** %9
%74 = icmp ne i32* %73, null
br i1 %74, label %75, label %78
75: ; preds = %72
%76 = load i32, i32* %16
%77 = load i32*, i32** %9
store i32 %76, i32* %77
br label %78
78: ; preds = %75, %72
store i32 -1, i32* %5
br label %127
79: ; preds = %49
%80 = load i8*, i8** %10
%81 = load i8*, i8** %20
%82 = load i8*, i8** %10
%83 = ptrtoint i8* %81 to i64
%84 = ptrtoint i8* %82 to i64
%85 = sub i64 %83, %84
%86 = trunc i64 %85 to i32
%87 = load i8*, i8** %14
%88 = load i8*, i8** %22
%89 = load i8*, i8** %14
%90 = ptrtoint i8* %88 to i64
%91 = ptrtoint i8* %89 to i64
%92 = sub i64 %90, %91
%93 = trunc i64 %92 to i32
%94 = call i64 @castle_norm_key_data_compare(i8* %80, i32 %86, i8* %87, i32 %93)
%95 = icmp sgt i64 %94, 0
br i1 %95, label %96, label %103
96: ; preds = %79
%97 = load i32*, i32** %9
%98 = icmp ne i32* %97, null
br i1 %98, label %99, label %102
99: ; preds = %96
%100 = load i32, i32* %16
%101 = load i32*, i32** %9
store i32 %100, i32* %101
br label %102
102: ; preds = %99, %96
store i32 1, i32* %5
br label %127
103: ; preds = %79
%104 = load i8*, i8** %20
store i8* %104, i8** %10
%105 = load i8*, i8** %21
store i8* %105, i8** %12
%106 = load i8*, i8** %22
store i8* %106, i8** %14
br label %107
107: ; preds = %103
%108 = load i32, i32* %16
%109 = add i32 %108, 1
store i32 %109, i32* %16
br label %44
110: ; preds = %44
%111 = load i8*, i8** %10
%112 = load i8*, i8** %11
%113 = icmp ne i8* %111, %112
br i1 %113, label %122, label %114
114: ; preds = %110
%115 = load i8*, i8** %12
%116 = load i8*, i8** %13
%117 = icmp ne i8* %115, %116
br i1 %117, label %122, label %118
118: ; preds = %114
%119 = load i8*, i8** %14
%120 = load i8*, i8** %15
%121 = icmp ne i8* %119, %120
br label %122
122: ; preds = %118, %114, %110
%123 = phi i1 [ true, %114 ], [ true, %110 ], [ %121, %118 ]
%124 = xor i1 %123, true
%125 = zext i1 %124 to i32
%126 = call i32 @assert(i32 %125)
store i32 0, i32* %5
br label %127
127: ; preds = %122, %102, %78
%128 = load i32, i32* %5
ret i32 %128
}

LLVM IR (-O2):
%struct.castle_norm_key = type { i32 }
define i32 @castle_norm_key_bounds_check(%struct.castle_norm_key* %0, %struct.castle_norm_key* %1, %struct.castle_norm_key* %2, i32* writeonly %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = bitcast i8** %5 to i8*
%9 = call i8* @castle_norm_key_end(%struct.castle_norm_key* %0, i8** %5)
%10 = bitcast i8** %6 to i8*
%11 = call i8* @castle_norm_key_end(%struct.castle_norm_key* %1, i8** %6)
%12 = bitcast i8** %7 to i8*
%13 = call i8* @castle_norm_key_end(%struct.castle_norm_key* %2, i8** %7)
%14 = call i64 @castle_norm_key_dim_get(i8** %5)
%15 = call i64 @castle_norm_key_dim_get(i8** %6)
%16 = call i64 @castle_norm_key_dim_get(i8** %7)
%17 = icmp eq i64 %14, %15
%18 = icmp eq i64 %14, %16
%19 = select i1 %17, i1 %18, i1 false
%20 = zext i1 %19 to i32
%21 = call i32 @assert(i32 %20)
%22 = icmp eq i64 %14, 0
%23 = load i8*, i8** %5
br i1 %22, label %24, label %27
24: ; preds = %4
%25 = load i8*, i8** %6
%26 = load i8*, i8** %7
br label %69
27: ; preds = %4, %65
%28 = phi i8* [ %30, %65 ], [ %23, %4 ]
%29 = phi i32 [ %66, %65 ], [ 0, %4 ]
%30 = call i8* @castle_norm_key_dim_next(i8* %28)
%31 = load i8*, i8** %6
%32 = call i8* @castle_norm_key_dim_next(i8* %31)
%33 = load i8*, i8** %7
%34 = call i8* @castle_norm_key_dim_next(i8* %33)
%35 = load i8*, i8** %5
%36 = ptrtoint i8* %30 to i64
%37 = ptrtoint i8* %35 to i64
%38 = sub i64 %36, %37
%39 = trunc i64 %38 to i32
%40 = load i8*, i8** %6
%41 = ptrtoint i8* %32 to i64
%42 = ptrtoint i8* %40 to i64
%43 = sub i64 %41, %42
%44 = trunc i64 %43 to i32
%45 = call i64 @castle_norm_key_data_compare(i8* %35, i32 %39, i8* %40, i32 %44)
%46 = icmp slt i64 %45, 0
br i1 %46, label %47, label %50
47: ; preds = %27
%48 = icmp eq i32* %3, null
br i1 %48, label %80, label %49
49: ; preds = %47
store i32 %29, i32* %3
br label %80
50: ; preds = %27
%51 = load i8*, i8** %5
%52 = ptrtoint i8* %51 to i64
%53 = sub i64 %36, %52
%54 = trunc i64 %53 to i32
%55 = load i8*, i8** %7
%56 = ptrtoint i8* %34 to i64
%57 = ptrtoint i8* %55 to i64
%58 = sub i64 %56, %57
%59 = trunc i64 %58 to i32
%60 = call i64 @castle_norm_key_data_compare(i8* %51, i32 %54, i8* %55, i32 %59)
%61 = icmp sgt i64 %60, 0
br i1 %61, label %62, label %65
62: ; preds = %50
%63 = icmp eq i32* %3, null
br i1 %63, label %80, label %64
64: ; preds = %62
store i32 %29, i32* %3
br label %80
65: ; preds = %50
store i8* %30, i8** %5
store i8* %32, i8** %6
store i8* %34, i8** %7
%66 = add i32 %29, 1
%67 = zext i32 %66 to i64
%68 = icmp ugt i64 %14, %67
br i1 %68, label %27, label %69
69: ; preds = %65, %24
%70 = phi i8* [ %26, %24 ], [ %34, %65 ]
%71 = phi i8* [ %25, %24 ], [ %32, %65 ]
%72 = phi i8* [ %23, %24 ], [ %30, %65 ]
%73 = icmp eq i8* %72, %9
%74 = icmp eq i8* %71, %11
%75 = select i1 %73, i1 %74, i1 false
%76 = icmp eq i8* %70, %13
%77 = select i1 %75, i1 %76, i1 false
%78 = zext i1 %77 to i32
%79 = call i32 @assert(i32 %78)
br label %80
80: ; preds = %62, %64, %47, %49, %69
%81 = phi i32 [ 0, %69 ], [ 1, %62 ], [ 1, %64 ], [ -1, %47 ], [ -1, %49 ]
ret i32 %81
}