#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 scalar_t__ uint16_t;
typedef int /*<<< orphan*/ int32_t;

/* Variables and functions */
size_t advanceUntil(scalar_t__ const *, int /*<<< orphan*/, int /*<<< orphan*/,
                    scalar_t__);
int32_t intersect_skewed_uint16_cardinality(const uint16_t *small,
                                            size_t size_s,
                                            const uint16_t *large,
                                            size_t size_l) {
  size_t pos = 0, idx_l = 0, idx_s = 0;
  if (0 == size_s) {
    return 0;
  }
  uint16_t val_l = large[idx_l], val_s = small[idx_s];
  while (true) {
    if (val_l < val_s) {
      idx_l = advanceUntil(large, (int32_t)idx_l, (int32_t)size_l, val_s);
      if (idx_l == size_l)
        break;
      val_l = large[idx_l];
    } else if (val_s < val_l) {
      idx_s++;
      if (idx_s == size_s)
        break;
      val_s = small[idx_s];
    } else {
      pos++;
      idx_s++;
      if (idx_s == size_s)
        break;
      val_s = small[idx_s];
      idx_l = advanceUntil(large, (int32_t)idx_l, (int32_t)size_l, val_s);
      if (idx_l == size_l)
        break;
      val_l = large[idx_l];
    }
  }
  return (int32_t)pos;
}


x86 assembly (clang, -O0):
<intersect_skewed_uint16_cardinality>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
xor    %eax,%eax
cmp    -0x18(%rbp),%rax
jne    1158 <intersect_skewed_uint16_cardinality+0x48>
movl   $0x0,-0x4(%rbp)
jmp    1299 <intersect_skewed_uint16_cardinality+0x189>
mov    -0x20(%rbp),%rax
mov    -0x38(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x48(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x40(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x50(%rbp)
mov    -0x48(%rbp),%rax
cmp    -0x50(%rbp),%rax
jge    11cb <intersect_skewed_uint16_cardinality+0xbb>
mov    -0x20(%rbp),%rdi
mov    -0x38(%rbp),%rax
mov    %eax,%esi
mov    -0x28(%rbp),%rax
mov    %eax,%edx
mov    -0x50(%rbp),%rcx
call   1030 <advanceUntil@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x28(%rbp),%rax
jne    11b6 <intersect_skewed_uint16_cardinality+0xa6>
jmp    1292 <intersect_skewed_uint16_cardinality+0x182>
mov    -0x20(%rbp),%rax
mov    -0x38(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x48(%rbp)
jmp    128d <intersect_skewed_uint16_cardinality+0x17d>
mov    -0x50(%rbp),%rax
cmp    -0x48(%rbp),%rax
jge    120d <intersect_skewed_uint16_cardinality+0xfd>
mov    -0x40(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
cmp    -0x18(%rbp),%rax
jne    11f8 <intersect_skewed_uint16_cardinality+0xe8>
jmp    1292 <intersect_skewed_uint16_cardinality+0x182>
mov    -0x10(%rbp),%rax
mov    -0x40(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x50(%rbp)
jmp    1288 <intersect_skewed_uint16_cardinality+0x178>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x40(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
cmp    -0x18(%rbp),%rax
jne    1238 <intersect_skewed_uint16_cardinality+0x128>
jmp    1292 <intersect_skewed_uint16_cardinality+0x182>
mov    -0x10(%rbp),%rax
mov    -0x40(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x50(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x38(%rbp),%rax
mov    %eax,%esi
mov    -0x28(%rbp),%rax
mov    %eax,%edx
mov    -0x50(%rbp),%rcx
call   1030 <advanceUntil@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x28(%rbp),%rax
jne    1278 <intersect_skewed_uint16_cardinality+0x168>
jmp    1292 <intersect_skewed_uint16_cardinality+0x182>
mov    -0x20(%rbp),%rax
mov    -0x38(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x48(%rbp)
jmp    128d <intersect_skewed_uint16_cardinality+0x17d>
jmp    1178 <intersect_skewed_uint16_cardinality+0x68>
mov    -0x30(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<intersect_skewed_uint16_cardinality>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,0x10(%rsp)
test   %rsi,%rsi
je     11d8 <intersect_skewed_uint16_cardinality+0xc8>
mov    %rdx,%r12
mov    %rsi,%rbx
mov    %rdi,%r13
mov    (%rdi),%rbp
xor    %esi,%esi
xor    %eax,%eax
xor    %r14d,%r14d
mov    (%r12,%rax,8),%rcx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    $0x1,%r14
cmp    %rbp,%rcx
jl     1170 <intersect_skewed_uint16_cardinality+0x60>
jle    11a0 <intersect_skewed_uint16_cardinality+0x90>
cmp    %r14,%rbx
je     11da <intersect_skewed_uint16_cardinality+0xca>
mov    0x0(%r13,%r14,8),%rbp
jmp    1150 <intersect_skewed_uint16_cardinality+0x40>
nopw   0x0(%rax,%rax,1)
mov    %r12,%rdi
mov    %rsi,0x8(%rsp)
mov    %eax,%esi
mov    0x10(%rsp),%r15
mov    %r15d,%edx
mov    %rbp,%rcx
call   1030 <advanceUntil@plt>
mov    0x8(%rsp),%rsi
add    $0xffffffffffffffff,%r14
jmp    11cd <intersect_skewed_uint16_cardinality+0xbd>
cs nopw 0x0(%rax,%rax,1)
nop
add    $0x1,%rsi
cmp    %r14,%rbx
je     11da <intersect_skewed_uint16_cardinality+0xca>
mov    0x0(%r13,%r14,8),%rbp
mov    %r12,%rdi
mov    %rsi,0x8(%rsp)
mov    %eax,%esi
mov    0x10(%rsp),%r15
mov    %r15d,%edx
mov    %rbp,%rcx
call   1030 <advanceUntil@plt>
mov    0x8(%rsp),%rsi
cmp    %r15,%rax
jne    113f <intersect_skewed_uint16_cardinality+0x2f>
jmp    11da <intersect_skewed_uint16_cardinality+0xca>
xor    %esi,%esi
mov    %esi,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @intersect_skewed_uint16_cardinality(i64* %0, i64 %1, i64* %2, i64 %3) {
%5 = alloca i32
%6 = alloca i64*
%7 = alloca i64
%8 = alloca i64*
%9 = alloca i64
%10 = alloca i64
%11 = alloca i64
%12 = alloca i64
%13 = alloca i64
%14 = alloca i64
store i64* %0, i64** %6
store i64 %1, i64* %7
store i64* %2, i64** %8
store i64 %3, i64* %9
store i64 0, i64* %10
store i64 0, i64* %11
store i64 0, i64* %12
%15 = load i64, i64* %7
%16 = icmp eq i64 0, %15
br i1 %16, label %17, label %18
17: ; preds = %4
store i32 0, i32* %5
br label %99
18: ; preds = %4
%19 = load i64*, i64** %8
%20 = load i64, i64* %11
%21 = getelementptr inbounds i64, i64* %19, i64 %20
%22 = load i64, i64* %21
store i64 %22, i64* %13
%23 = load i64*, i64** %6
%24 = load i64, i64* %12
%25 = getelementptr inbounds i64, i64* %23, i64 %24
%26 = load i64, i64* %25
store i64 %26, i64* %14
br label %27
27: ; preds = %18, %95
%28 = load i64, i64* %13
%29 = load i64, i64* %14
%30 = icmp slt i64 %28, %29
br i1 %30, label %31, label %48
31: ; preds = %27
%32 = load i64*, i64** %8
%33 = load i64, i64* %11
%34 = trunc i64 %33 to i32
%35 = load i64, i64* %9
%36 = trunc i64 %35 to i32
%37 = load i64, i64* %14
%38 = call i64 @advanceUntil(i64* %32, i32 %34, i32 %36, i64 %37)
store i64 %38, i64* %11
%39 = load i64, i64* %11
%40 = load i64, i64* %9
%41 = icmp eq i64 %39, %40
br i1 %41, label %42, label %43
42: ; preds = %31
br label %96
43: ; preds = %31
%44 = load i64*, i64** %8
%45 = load i64, i64* %11
%46 = getelementptr inbounds i64, i64* %44, i64 %45
%47 = load i64, i64* %46
store i64 %47, i64* %13
br label %95
48: ; preds = %27
%49 = load i64, i64* %14
%50 = load i64, i64* %13
%51 = icmp slt i64 %49, %50
br i1 %51, label %52, label %64
52: ; preds = %48
%53 = load i64, i64* %12
%54 = add i64 %53, 1
store i64 %54, i64* %12
%55 = load i64, i64* %12
%56 = load i64, i64* %7
%57 = icmp eq i64 %55, %56
br i1 %57, label %58, label %59
58: ; preds = %52
br label %96
59: ; preds = %52
%60 = load i64*, i64** %6
%61 = load i64, i64* %12
%62 = getelementptr inbounds i64, i64* %60, i64 %61
%63 = load i64, i64* %62
store i64 %63, i64* %14
br label %94
64: ; preds = %48
%65 = load i64, i64* %10
%66 = add i64 %65, 1
store i64 %66, i64* %10
%67 = load i64, i64* %12
%68 = add i64 %67, 1
store i64 %68, i64* %12
%69 = load i64, i64* %12
%70 = load i64, i64* %7
%71 = icmp eq i64 %69, %70
br i1 %71, label %72, label %73
72: ; preds = %64
br label %96
73: ; preds = %64
%74 = load i64*, i64** %6
%75 = load i64, i64* %12
%76 = getelementptr inbounds i64, i64* %74, i64 %75
%77 = load i64, i64* %76
store i64 %77, i64* %14
%78 = load i64*, i64** %8
%79 = load i64, i64* %11
%80 = trunc i64 %79 to i32
%81 = load i64, i64* %9
%82 = trunc i64 %81 to i32
%83 = load i64, i64* %14
%84 = call i64 @advanceUntil(i64* %78, i32 %80, i32 %82, i64 %83)
store i64 %84, i64* %11
%85 = load i64, i64* %11
%86 = load i64, i64* %9
%87 = icmp eq i64 %85, %86
br i1 %87, label %88, label %89
88: ; preds = %73
br label %96
89: ; preds = %73
%90 = load i64*, i64** %8
%91 = load i64, i64* %11
%92 = getelementptr inbounds i64, i64* %90, i64 %91
%93 = load i64, i64* %92
store i64 %93, i64* %13
br label %94
94: ; preds = %89, %59
br label %95
95: ; preds = %94, %43
br label %27
96: ; preds = %88, %72, %58, %42
%97 = load i64, i64* %10
%98 = trunc i64 %97 to i32
store i32 %98, i32* %5
br label %99
99: ; preds = %96, %17
%100 = load i32, i32* %5
ret i32 %100
}

LLVM IR (-O2):
define i32 @intersect_skewed_uint16_cardinality(i64* nocapture %0, i64 %1, i64* %2, i64 %3) {
%5 = icmp eq i64 %1, 0
br i1 %5, label %54, label %6
6: ; preds = %4
%7 = load i64, i64* %0
%8 = trunc i64 %3 to i32
br label %9
9: ; preds = %49, %6
%10 = phi i64 [ %40, %49 ], [ 0, %6 ]
%11 = phi i64 [ %47, %49 ], [ 0, %6 ]
%12 = phi i64 [ %41, %49 ], [ 0, %6 ]
%13 = phi i64* [ %50, %49 ], [ %2, %6 ]
%14 = phi i64 [ %45, %49 ], [ %7, %6 ]
br label %15
15: ; preds = %9, %29
%16 = phi i64 [ %11, %9 ], [ %27, %29 ]
%17 = phi i64 [ %12, %9 ], [ %22, %29 ]
%18 = phi i64* [ %13, %9 ], [ %30, %29 ]
%19 = phi i64 [ %14, %9 ], [ %23, %29 ]
%20 = load i64, i64* %18
br label %21
21: ; preds = %15, %36
%22 = phi i64 [ %34, %36 ], [ %17, %15 ]
%23 = phi i64 [ %38, %36 ], [ %19, %15 ]
%24 = icmp slt i64 %20, %23
br i1 %24, label %25, label %31
25: ; preds = %21
%26 = trunc i64 %16 to i32
%27 = tail call i64 @advanceUntil(i64* %2, i32 %26, i32 %8, i64 %23)
%28 = icmp eq i64 %27, %3
br i1 %28, label %51, label %29
29: ; preds = %25
%30 = getelementptr inbounds i64, i64* %2, i64 %27
br label %15
31: ; preds = %21
%32 = icmp slt i64 %23, %20
br i1 %32, label %33, label %39
33: ; preds = %31
%34 = add i64 %22, 1
%35 = icmp eq i64 %34, %1
br i1 %35, label %51, label %36
36: ; preds = %33
%37 = getelementptr inbounds i64, i64* %0, i64 %34
%38 = load i64, i64* %37
br label %21
39: ; preds = %31
%40 = add i64 %10, 1
%41 = add i64 %22, 1
%42 = icmp eq i64 %41, %1
br i1 %42, label %51, label %43
43: ; preds = %39
%44 = getelementptr inbounds i64, i64* %0, i64 %41
%45 = load i64, i64* %44
%46 = trunc i64 %16 to i32
%47 = tail call i64 @advanceUntil(i64* %2, i32 %46, i32 %8, i64 %45)
%48 = icmp eq i64 %47, %3
br i1 %48, label %51, label %49
49: ; preds = %43
%50 = getelementptr inbounds i64, i64* %2, i64 %47
br label %9
51: ; preds = %43, %39, %33, %25
%52 = phi i64 [ %10, %25 ], [ %10, %33 ], [ %40, %39 ], [ %40, %43 ]
%53 = trunc i64 %52 to i32
br label %54
54: ; preds = %4, %51
%55 = phi i32 [ %53, %51 ], [ 0, %4 ]
ret i32 %55
}