#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 uint32_t;
typedef int uint16_t;

/* Variables and functions */
int /*<<< orphan*/ MAP_PRIVATE;
int /*<<< orphan*/ O_RDONLY;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ longest_match(int *, int, int *, int *);
int *mmap(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/, int,
          int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ output(int);
int /*<<< orphan*/ stderr;
int main() {
  int fd = open("upcase.tab", O_RDONLY);
  if (fd < 0) {
    fprintf(stderr, "ERROR: upcase.tab not found\n");
    return 1;
  }
  uint16_t *tab = mmap(NULL, (65536 * sizeof(uint16_t)), PROT_READ | PROT_WRITE,
                       MAP_PRIVATE, fd, 0);
  uint32_t i;
  for (i = 0; i < 65536; i++)
    tab[i] -= i;
  for (i = 0; i < 65536;) {
    uint16_t len, src_pos;
    longest_match(tab, i, &len, &src_pos);
    if (len <= 1) {
      output(0);
      output(tab[i]);
      i++;
    } else {
      output(len);
      output(src_pos);
      i += len;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e7a(%rip),%rax
mov    (%rax),%esi
lea    0xe91(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11a2 <main+0x52>
mov    0x2e48(%rip),%rax
mov    (%rax),%edi
lea    0xe7a(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1287 <main+0x137>
mov    0x2e4f(%rip),%rax
mov    (%rax),%edx
mov    0x2e16(%rip),%rax
or     (%rax),%edx
mov    0x2e05(%rip),%rax
mov    (%rax),%ecx
mov    -0x8(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x40000,%esi
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x10000,-0x14(%rbp)
jge    120b <main+0xbb>
mov    -0x14(%rbp),%esi
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
sub    %esi,%edx
mov    %edx,(%rax,%rcx,4)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11dd <main+0x8d>
movl   $0x0,-0x14(%rbp)
cmpl   $0x10000,-0x14(%rbp)
jge    1280 <main+0x130>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
lea    -0x18(%rbp),%rdx
lea    -0x1c(%rbp),%rcx
call   1030 <longest_match@plt>
cmpl   $0x1,-0x18(%rbp)
jg     1262 <main+0x112>
xor    %edi,%edi
call   1040 <output@plt>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
call   1040 <output@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    127b <main+0x12b>
mov    -0x18(%rbp),%edi
call   1040 <output@plt>
mov    -0x1c(%rbp),%edi
call   1040 <output@plt>
mov    -0x18(%rbp),%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
jmp    1212 <main+0xc2>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2e82(%rip),%rax
mov    (%rax),%esi
lea    0xee9(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     12a2 <main+0x152>
mov    0x2e7d(%rip),%rcx
mov    0x2e46(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    0x2e33(%rip),%rcx
mov    (%rcx),%ecx
xor    %edi,%edi
mov    $0x40000,%esi
mov    %eax,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,%rbx
movdqa 0xe54(%rip),%xmm0
mov    $0xc,%eax
movdqa 0xe56(%rip),%xmm8
movdqa 0xe5e(%rip),%xmm2
movdqa 0xe66(%rip),%xmm3
movdqa 0xe6e(%rip),%xmm4
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movdqu -0x30(%rbx,%rax,4),%xmm5
movdqu -0x20(%rbx,%rax,4),%xmm6
movdqu -0x10(%rbx,%rax,4),%xmm7
movdqu (%rbx,%rax,4),%xmm1
psubd  %xmm0,%xmm5
psubd  %xmm0,%xmm6
psubd  %xmm8,%xmm6
movdqu %xmm5,-0x30(%rbx,%rax,4)
movdqu %xmm6,-0x20(%rbx,%rax,4)
psubd  %xmm0,%xmm7
psubd  %xmm2,%xmm7
psubd  %xmm0,%xmm1
psubd  %xmm3,%xmm1
movdqu %xmm7,-0x10(%rbx,%rax,4)
movdqu %xmm1,(%rbx,%rax,4)
paddd  %xmm4,%xmm0
add    $0x10,%rax
cmp    $0x1000c,%rax
jne    11e0 <main+0x90>
xor    %ebp,%ebp
mov    %rsp,%r14
lea    0x4(%rsp),%r15
jmp    1269 <main+0x119>
nopw   0x0(%rax,%rax,1)
call   1040 <output@plt>
mov    0x4(%rsp),%edi
call   1040 <output@plt>
add    (%rsp),%ebp
cmp    $0x10000,%ebp
jge    129e <main+0x14e>
mov    %rbx,%rdi
mov    %ebp,%esi
mov    %r14,%rdx
mov    %r15,%rcx
call   1030 <longest_match@plt>
mov    (%rsp),%edi
cmp    $0x1,%edi
jg     1250 <main+0x100>
xor    %edi,%edi
call   1040 <output@plt>
movslq %ebp,%rax
mov    (%rbx,%rax,4),%edi
call   1040 <output@plt>
add    $0x1,%ebp
cmp    $0x10000,%ebp
jl     1269 <main+0x119>
xor    %eax,%eax
jmp    12bc <main+0x16c>
mov    0x2d27(%rip),%rax
mov    (%rax),%edi
lea    0xda9(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [11 x i8] c"upcase.tab\00"
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str.1 = [29 x i8] c"ERROR: upcase.tab not found\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
%7 = load i32, i32* @O_RDONLY
%8 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %7)
store i32 %8, i32* %2
%9 = load i32, i32* %2
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %0
%12 = load i32, i32* @stderr
%13 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %65
14: ; preds = %0
%15 = load i32, i32* @PROT_READ
%16 = load i32, i32* @PROT_WRITE
%17 = or i32 %15, %16
%18 = load i32, i32* @MAP_PRIVATE
%19 = load i32, i32* %2
%20 = call i32* @mmap(i32* null, i32 262144, i32 %17, i32 %18, i32 %19, i32 0)
store i32* %20, i32** %3
store i32 0, i32* %4
br label %21
21: ; preds = %32, %14
%22 = load i32, i32* %4
%23 = icmp slt i32 %22, 65536
br i1 %23, label %24, label %35
24: ; preds = %21
%25 = load i32, i32* %4
%26 = load i32*, i32** %3
%27 = load i32, i32* %4
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i32, i32* %26, i64 %28
%30 = load i32, i32* %29
%31 = sub nsw i32 %30, %25
store i32 %31, i32* %29
br label %32
32: ; preds = %24
%33 = load i32, i32* %4
%34 = add nsw i32 %33, 1
store i32 %34, i32* %4
br label %21
35: ; preds = %21
store i32 0, i32* %4
br label %36
36: ; preds = %63, %35
%37 = load i32, i32* %4
%38 = icmp slt i32 %37, 65536
br i1 %38, label %39, label %64
39: ; preds = %36
%40 = load i32*, i32** %3
%41 = load i32, i32* %4
%42 = call i32 @longest_match(i32* %40, i32 %41, i32* %5, i32* %6)
%43 = load i32, i32* %5
%44 = icmp sle i32 %43, 1
br i1 %44, label %45, label %55
45: ; preds = %39
%46 = call i32 @output(i32 0)
%47 = load i32*, i32** %3
%48 = load i32, i32* %4
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i32, i32* %47, i64 %49
%51 = load i32, i32* %50
%52 = call i32 @output(i32 %51)
%53 = load i32, i32* %4
%54 = add nsw i32 %53, 1
store i32 %54, i32* %4
br label %63
55: ; preds = %39
%56 = load i32, i32* %5
%57 = call i32 @output(i32 %56)
%58 = load i32, i32* %6
%59 = call i32 @output(i32 %58)
%60 = load i32, i32* %5
%61 = load i32, i32* %4
%62 = add nsw i32 %61, %60
store i32 %62, i32* %4
br label %63
63: ; preds = %55, %45
br label %36
64: ; preds = %36
store i32 0, i32* %1
br label %65
65: ; preds = %64, %11
%66 = load i32, i32* %1
ret i32 %66
}

LLVM IR (-O2):
@.str = [11 x i8] c"upcase.tab\00"
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str.1 = [29 x i8] c"ERROR: upcase.tab not found\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
br label %69
9: ; preds = %0
%10 = load i32, i32* @PROT_READ
%11 = load i32, i32* @PROT_WRITE
%12 = or i32 %11, %10
%13 = load i32, i32* @MAP_PRIVATE
%14 = tail call i32* @mmap(i32* null, i32 262144, i32 %12, i32 %13, i32 %4, i32 0)
br label %15
15: ; preds = %15, %9
%16 = phi i64 [ 0, %9 ], [ %42, %15 ]
%17 = phi <4 x i32> [ <i32 0, i32 1, i32 2, i32 3>, %9 ], [ %43, %15 ]
%18 = add <4 x i32> %17, <i32 4, i32 4, i32 4, i32 4>
%19 = getelementptr inbounds i32, i32* %14, i64 %16
%20 = bitcast i32* %19 to <4 x i32>*
%21 = load <4 x i32>, <4 x i32>* %20
%22 = getelementptr inbounds i32, i32* %19, i64 4
%23 = bitcast i32* %22 to <4 x i32>*
%24 = load <4 x i32>, <4 x i32>* %23
%25 = sub nsw <4 x i32> %21, %17
%26 = sub nsw <4 x i32> %24, %18
%27 = bitcast i32* %19 to <4 x i32>*
store <4 x i32> %25, <4 x i32>* %27
%28 = bitcast i32* %22 to <4 x i32>*
store <4 x i32> %26, <4 x i32>* %28
%29 = or i64 %16, 8
%30 = add <4 x i32> %17, <i32 8, i32 8, i32 8, i32 8>
%31 = add <4 x i32> %17, <i32 12, i32 12, i32 12, i32 12>
%32 = getelementptr inbounds i32, i32* %14, i64 %29
%33 = bitcast i32* %32 to <4 x i32>*
%34 = load <4 x i32>, <4 x i32>* %33
%35 = getelementptr inbounds i32, i32* %32, i64 4
%36 = bitcast i32* %35 to <4 x i32>*
%37 = load <4 x i32>, <4 x i32>* %36
%38 = sub nsw <4 x i32> %34, %30
%39 = sub nsw <4 x i32> %37, %31
%40 = bitcast i32* %32 to <4 x i32>*
store <4 x i32> %38, <4 x i32>* %40
%41 = bitcast i32* %35 to <4 x i32>*
store <4 x i32> %39, <4 x i32>* %41
%42 = add nuw nsw i64 %16, 16
%43 = add <4 x i32> %17, <i32 16, i32 16, i32 16, i32 16>
%44 = icmp eq i64 %42, 65536
br i1 %44, label %45, label %15
45: ; preds = %15
%46 = bitcast i32* %1 to i8*
%47 = bitcast i32* %2 to i8*
br label %48
48: ; preds = %45, %66
%49 = phi i32 [ 0, %45 ], [ %67, %66 ]
%50 = call i32 @longest_match(i32* %14, i32 %49, i32* %1, i32* %2)
%51 = load i32, i32* %1
%52 = icmp slt i32 %51, 2
br i1 %52, label %53, label %60
53: ; preds = %48
%54 = call i32 @output(i32 0)
%55 = sext i32 %49 to i64
%56 = getelementptr inbounds i32, i32* %14, i64 %55
%57 = load i32, i32* %56
%58 = call i32 @output(i32 %57)
%59 = add nsw i32 %49, 1
br label %66
60: ; preds = %48
%61 = call i32 @output(i32 %51)
%62 = load i32, i32* %2
%63 = call i32 @output(i32 %62)
%64 = load i32, i32* %1
%65 = add nsw i32 %64, %49
br label %66
66: ; preds = %60, %53
%67 = phi i32 [ %59, %53 ], [ %65, %60 ]
%68 = icmp slt i32 %67, 65536
br i1 %68, label %48, label %69
69: ; preds = %66, %6
%70 = phi i32 [ 1, %6 ], [ 0, %66 ]
ret i32 %70
}