#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 size_t uint16_t;

/* Variables and functions */
size_t match_length(size_t const *, size_t const *, size_t);
void longest_match(const uint16_t *tab, uint16_t cur_pos, uint16_t *len_ret,
                   uint16_t *src_pos_ret) {
  uint16_t max_len = 65536 - cur_pos;
  uint16_t src_pos;
  *len_ret = 0;
  *src_pos_ret = 0;
  for (src_pos = 0; src_pos < cur_pos; src_pos++) {
    uint16_t len = match_length(&tab[cur_pos], &tab[src_pos], max_len);
    if (len > *len_ret) {
      *len_ret = len;
      *src_pos_ret = src_pos;
    }
  }
}


x86 assembly (clang, -O0):
<longest_match>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    $0x10000,%eax
sub    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x20(%rbp),%rax
movq   $0x0,(%rax)
movq   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    11c9 <longest_match+0xb9>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rax
shl    $0x3,%rax
add    %rax,%rdi
mov    -0x8(%rbp),%rsi
mov    -0x30(%rbp),%rax
shl    $0x3,%rax
add    %rax,%rsi
mov    -0x28(%rbp),%rdx
call   1030 <match_length@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    -0x18(%rbp),%rcx
cmp    (%rcx),%rax
jbe    11b3 <longest_match+0xa3>
mov    -0x38(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
jmp    11b8 <longest_match+0xa8>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    1153 <longest_match+0x43>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<longest_match>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
movq   $0x0,(%rdx)
mov    %rcx,(%rsp)
movq   $0x0,(%rcx)
test   %rsi,%rsi
je     117e <longest_match+0x6e>
mov    %rdx,%r12
mov    %rsi,%r15
mov    %rdi,%rbx
mov    $0x10000,%r13d
sub    %rsi,%r13
lea    (%rdi,%rsi,8),%rbp
xor    %r14d,%r14d
jmp    115d <longest_match+0x4d>
nopl   (%rax)
add    $0x1,%r14
add    $0x8,%rbx
cmp    %r14,%r15
je     117e <longest_match+0x6e>
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %r13,%rdx
call   1030 <match_length@plt>
cmp    (%r12),%rax
jbe    1150 <longest_match+0x40>
mov    %rax,(%r12)
mov    (%rsp),%rax
mov    %r14,(%rax)
jmp    1150 <longest_match+0x40>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @longest_match(i64* %0, i64 %1, i64* %2, i64* %3) {
%5 = alloca i64*
%6 = alloca i64
%7 = alloca i64*
%8 = alloca i64*
%9 = alloca i64
%10 = alloca i64
%11 = alloca i64
store i64* %0, i64** %5
store i64 %1, i64* %6
store i64* %2, i64** %7
store i64* %3, i64** %8
%12 = load i64, i64* %6
%13 = sub i64 65536, %12
store i64 %13, i64* %9
%14 = load i64*, i64** %7
store i64 0, i64* %14
%15 = load i64*, i64** %8
store i64 0, i64* %15
store i64 0, i64* %10
br label %16
16: ; preds = %39, %4
%17 = load i64, i64* %10
%18 = load i64, i64* %6
%19 = icmp ult i64 %17, %18
br i1 %19, label %20, label %42
20: ; preds = %16
%21 = load i64*, i64** %5
%22 = load i64, i64* %6
%23 = getelementptr inbounds i64, i64* %21, i64 %22
%24 = load i64*, i64** %5
%25 = load i64, i64* %10
%26 = getelementptr inbounds i64, i64* %24, i64 %25
%27 = load i64, i64* %9
%28 = call i64 @match_length(i64* %23, i64* %26, i64 %27)
store i64 %28, i64* %11
%29 = load i64, i64* %11
%30 = load i64*, i64** %7
%31 = load i64, i64* %30
%32 = icmp ugt i64 %29, %31
br i1 %32, label %33, label %38
33: ; preds = %20
%34 = load i64, i64* %11
%35 = load i64*, i64** %7
store i64 %34, i64* %35
%36 = load i64, i64* %10
%37 = load i64*, i64** %8
store i64 %36, i64* %37
br label %38
38: ; preds = %33, %20
br label %39
39: ; preds = %38
%40 = load i64, i64* %10
%41 = add i64 %40, 1
store i64 %41, i64* %10
br label %16
42: ; preds = %16
ret void
}

LLVM IR (-O2):
define void @longest_match(i64* %0, i64 %1, i64* nocapture %2, i64* nocapture writeonly %3) {
%5 = sub i64 65536, %1
store i64 0, i64* %2
store i64 0, i64* %3
%6 = icmp eq i64 %1, 0
br i1 %6, label %19, label %7
7: ; preds = %4
%8 = getelementptr inbounds i64, i64* %0, i64 %1
br label %9
9: ; preds = %7, %16
%10 = phi i64 [ 0, %7 ], [ %17, %16 ]
%11 = getelementptr inbounds i64, i64* %0, i64 %10
%12 = tail call i64 @match_length(i64* %8, i64* %11, i64 %5)
%13 = load i64, i64* %2
%14 = icmp ugt i64 %12, %13
br i1 %14, label %15, label %16
15: ; preds = %9
store i64 %12, i64* %2
store i64 %10, i64* %3
br label %16
16: ; preds = %15, %9
%17 = add nuw i64 %10, 1
%18 = icmp eq i64 %17, %1
br i1 %18, label %19, label %9
19: ; preds = %16, %4
ret void
}