#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 */

/* Variables and functions */
char *MAP_FAILED;
int MAP_FILE;
int MAP_SHARED;
int O_CREAT;
int O_RDONLY;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ SEEK_END;
char calc_block(unsigned int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(char *);
long lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ malloc(int);
char *mmap(int /*<<< orphan*/ *, long, int /*<<< orphan*/, int, int,
           int /*<<< orphan*/);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ write(int, char *, int);
char *create_lookup_table(char *filename) {
  unsigned int i;
  unsigned int j;
  unsigned int k;
  char *table;
  int fd;
  long int rsz;
  fd = open(filename, O_RDONLY, 0600);
  if (fd < 1) {
    perror("File open failed on lookup table");
    close(fd);
    table = (char *)malloc(sizeof(char) * 16777216);
    if (!table) {
      return (table);
    }
    printf("Populating lookup:\n");
    for (i = 0; i < 16777216; i++) {
      if (i % (16777216 / 10) == 0)
        printf(".\n", i);
      table[i] = calc_block(i);
    }
    fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
    write(fd, table, 16777216);
    close(fd);
    free(table);
    printf("Lookup table written -- trying mmap again\n");
    fd = open(filename, O_RDONLY, 0600);
    if (fd < 1) {
      perror("File open failed again on lookup table, giving up");
      return 0;
    }
  }
  rsz = lseek(fd, 0, SEEK_END);
  if (rsz % 4096) {
    rsz += 4096 - (rsz % 4096);
  }
  table = mmap(NULL, rsz, PROT_READ, MAP_FILE | MAP_SHARED, fd, 0);
  if (table == MAP_FAILED) {
    perror("Couldn't mmap");
    return (NULL);
  }
  close(fd);
  return (table);
}


x86 assembly (clang, -O0):
<create_lookup_table>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
mov    $0x180,%edx
call   10b0 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x1,-0x2c(%rbp)
jge    1312 <create_lookup_table+0x172>
lea    0xe29(%rip),%rdi
call   10c0 <perror@plt>
mov    -0x2c(%rbp),%edi
call   1080 <close@plt>
mov    $0x1000000,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    120a <create_lookup_table+0x6a>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    13cf <create_lookup_table+0x22f>
lea    0xe10(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x14(%rbp)
cmpl   $0x1000000,-0x14(%rbp)
jae    1274 <create_lookup_table+0xd4>
mov    -0x14(%rbp),%eax
mov    $0x199999,%ecx
xor    %edx,%edx
div    %ecx
cmp    $0x0,%edx
jne    1252 <create_lookup_table+0xb2>
mov    -0x14(%rbp),%esi
lea    0xdea(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x14(%rbp),%edi
call   1090 <calc_block@plt>
mov    %al,%dl
mov    -0x28(%rbp),%rax
mov    -0x14(%rbp),%ecx
mov    %dl,(%rax,%rcx,1)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    121f <create_lookup_table+0x7f>
mov    -0x10(%rbp),%rdi
mov    0x2d19(%rip),%rax
mov    (%rax),%esi
mov    0x2d70(%rip),%rax
or     (%rax),%esi
mov    0x2d5f(%rip),%rax
or     (%rax),%esi
mov    $0x180,%edx
call   10b0 <open@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    $0x1000000,%edx
call   1040 <write@plt>
mov    -0x2c(%rbp),%edi
call   1080 <close@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
lea    0xd6f(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    0x2ce5(%rip),%rax
mov    (%rax),%esi
mov    $0x180,%edx
call   10b0 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x1,-0x2c(%rbp)
jge    130d <create_lookup_table+0x16d>
lea    0xd68(%rip),%rdi
call   10c0 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    13cf <create_lookup_table+0x22f>
jmp    1312 <create_lookup_table+0x172>
mov    -0x2c(%rbp),%edi
mov    0x2cac(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <lseek@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    $0x1000,%ecx
cqto
idiv   %rcx
cmp    $0x0,%rdx
je     135f <create_lookup_table+0x1bf>
mov    -0x38(%rbp),%rax
mov    $0x1000,%ecx
cqto
idiv   %rcx
mov    $0x1000,%eax
sub    %rdx,%rax
add    -0x38(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rsi
mov    0x2c7e(%rip),%rax
mov    (%rax),%edx
mov    0x2c45(%rip),%rax
mov    (%rax),%ecx
mov    0x2c5c(%rip),%rax
or     (%rax),%ecx
mov    -0x2c(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2c0b(%rip),%rcx
cmp    (%rcx),%rax
jne    13bf <create_lookup_table+0x21f>
lea    0xce8(%rip),%rdi
call   10c0 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    13cf <create_lookup_table+0x22f>
mov    -0x2c(%rbp),%edi
call   1080 <close@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_lookup_table>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%rbx
mov    0x2e0e(%rip),%r12
mov    (%r12),%esi
mov    $0x180,%edx
call   10b0 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    1257 <create_lookup_table+0xb7>
mov    0x2df7(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1070 <lseek@plt>
lea    0xfff(%rax),%rcx
test   %rax,%rax
cmovns %rax,%rcx
and    $0xfffffffffffff000,%rcx
mov    %rax,%rdx
sub    %rcx,%rdx
mov    $0x1000,%esi
sub    %rdx,%rsi
test   %rdx,%rdx
cmove  %rdx,%rsi
add    %rax,%rsi
mov    0x2dd8(%rip),%rax
mov    (%rax),%edx
mov    0x2d9f(%rip),%rax
mov    0x2db8(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %edi,%edi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1060 <mmap@plt>
mov    %rax,%rbx
mov    0x2d6d(%rip),%rax
cmp    (%rax),%rbx
je     12e4 <create_lookup_table+0x144>
mov    %ebp,%edi
call   1080 <close@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xda2(%rip),%rdi
call   10c0 <perror@plt>
mov    %ebp,%edi
call   1080 <close@plt>
mov    $0x1000000,%edi
call   10a0 <malloc@plt>
test   %rax,%rax
je     1372 <create_lookup_table+0x1d2>
mov    %rax,%r14
lea    0xdda(%rip),%rdi
call   1040 <puts@plt>
xor    %ebp,%ebp
lea    0xe09(%rip),%r15
jmp    12b8 <create_lookup_table+0x118>
nopw   0x0(%rax,%rax,1)
mov    %ebp,%edi
call   1090 <calc_block@plt>
mov    %al,(%r14,%rbp,1)
add    $0x1,%rbp
cmp    $0x1000000,%rbp
je     12f7 <create_lookup_table+0x157>
mov    %ebp,%eax
imul   $0x40000781,%rax,%rax
shr    $0x20,%rax
mov    %ebp,%ecx
sub    %eax,%ecx
shr    %ecx
add    %eax,%ecx
shr    $0x14,%ecx
imul   $0xffe66667,%ecx,%eax
add    %ebp,%eax
jne    12a0 <create_lookup_table+0x100>
mov    %r15,%rdi
call   1040 <puts@plt>
jmp    12a0 <create_lookup_table+0x100>
lea    0xd68(%rip),%rdi
call   10c0 <perror@plt>
xor    %ebx,%ebx
jmp    124b <create_lookup_table+0xab>
mov    0x2c9a(%rip),%rax
mov    0x2cf3(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2ce0(%rip),%rax
or     (%rax),%esi
mov    %rbx,%rdi
mov    $0x180,%edx
call   10b0 <open@plt>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r14,%rsi
mov    $0x1000000,%edx
call   1050 <write@plt>
mov    %ebp,%edi
call   1080 <close@plt>
mov    %r14,%rdi
call   1030 <free@plt>
lea    0xd2e(%rip),%rdi
call   1040 <puts@plt>
mov    (%r12),%esi
mov    %rbx,%rdi
mov    $0x180,%edx
call   10b0 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jg     11ca <create_lookup_table+0x2a>
lea    0xcb4(%rip),%rdi
jmp    12eb <create_lookup_table+0x14b>
xor    %ebx,%ebx
jmp    124b <create_lookup_table+0xab>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [33 x i8] c"File open failed on lookup table\00"
@.str.1 = [20 x i8] c"Populating lookup:\0A\00"
@.str.2 = [3 x i8] c".\0A\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.3 = [43 x i8] c"Lookup table written -- trying mmap again\0A\00"
@.str.4 = [50 x i8] c"File open failed again on lookup table, giving up\00"
@SEEK_END = global i32 0
@PROT_READ = global i32 0
@MAP_FILE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.5 = [14 x i8] c"Couldn't mmap\00"
define i8* @create_lookup_table(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i64
store i8* %0, i8** %3
%10 = load i8*, i8** %3
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* %10, i32 %11, i32 384)
store i32 %12, i32* %8
%13 = load i32, i32* %8
%14 = icmp slt i32 %13, 1
br i1 %14, label %15, label %71
15: ; preds = %1
%16 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
%17 = load i32, i32* %8
%18 = call i32 @close(i32 %17)
%19 = call i64 @malloc(i32 16777216)
%20 = inttoptr i64 %19 to i8*
store i8* %20, i8** %7
%21 = load i8*, i8** %7
%22 = icmp ne i8* %21, null
br i1 %22, label %25, label %23
23: ; preds = %15
%24 = load i8*, i8** %7
store i8* %24, i8** %2
br label %101
25: ; preds = %15
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %4
br label %27
27: ; preds = %44, %25
%28 = load i32, i32* %4
%29 = icmp ult i32 %28, 16777216
br i1 %29, label %30, label %47
30: ; preds = %27
%31 = load i32, i32* %4
%32 = urem i32 %31, 1677721
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %30
%35 = load i32, i32* %4
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %35)
br label %37
37: ; preds = %34, %30
%38 = load i32, i32* %4
%39 = call signext i8 @calc_block(i32 %38)
%40 = load i8*, i8** %7
%41 = load i32, i32* %4
%42 = zext i32 %41 to i64
%43 = getelementptr inbounds i8, i8* %40, i64 %42
store i8 %39, i8* %43
br label %44
44: ; preds = %37
%45 = load i32, i32* %4
%46 = add i32 %45, 1
store i32 %46, i32* %4
br label %27
47: ; preds = %27
%48 = load i8*, i8** %3
%49 = load i32, i32* @O_WRONLY
%50 = load i32, i32* @O_CREAT
%51 = or i32 %49, %50
%52 = load i32, i32* @O_TRUNC
%53 = or i32 %51, %52
%54 = call i32 @open(i8* %48, i32 %53, i32 384)
store i32 %54, i32* %8
%55 = load i32, i32* %8
%56 = load i8*, i8** %7
%57 = call i32 @write(i32 %55, i8* %56, i32 16777216)
%58 = load i32, i32* %8
%59 = call i32 @close(i32 %58)
%60 = load i8*, i8** %7
%61 = call i32 @free(i8* %60)
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.3, i64 0, i64 0))
%63 = load i8*, i8** %3
%64 = load i32, i32* @O_RDONLY
%65 = call i32 @open(i8* %63, i32 %64, i32 384)
store i32 %65, i32* %8
%66 = load i32, i32* %8
%67 = icmp slt i32 %66, 1
br i1 %67, label %68, label %70
68: ; preds = %47
%69 = call i32 @perror(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.4, i64 0, i64 0))
store i8* null, i8** %2
br label %101
70: ; preds = %47
br label %71
71: ; preds = %70, %1
%72 = load i32, i32* %8
%73 = load i32, i32* @SEEK_END
%74 = call i64 @lseek(i32 %72, i32 0, i32 %73)
store i64 %74, i64* %9
%75 = load i64, i64* %9
%76 = srem i64 %75, 4096
%77 = icmp ne i64 %76, 0
br i1 %77, label %78, label %84
78: ; preds = %71
%79 = load i64, i64* %9
%80 = srem i64 %79, 4096
%81 = sub nsw i64 4096, %80
%82 = load i64, i64* %9
%83 = add nsw i64 %82, %81
store i64 %83, i64* %9
br label %84
84: ; preds = %78, %71
%85 = load i64, i64* %9
%86 = load i32, i32* @PROT_READ
%87 = load i32, i32* @MAP_FILE
%88 = load i32, i32* @MAP_SHARED
%89 = or i32 %87, %88
%90 = load i32, i32* %8
%91 = call i8* @mmap(i32* null, i64 %85, i32 %86, i32 %89, i32 %90, i32 0)
store i8* %91, i8** %7
%92 = load i8*, i8** %7
%93 = load i8*, i8** @MAP_FAILED
%94 = icmp eq i8* %92, %93
br i1 %94, label %95, label %97
95: ; preds = %84
%96 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
store i8* null, i8** %2
br label %101
97: ; preds = %84
%98 = load i32, i32* %8
%99 = call i32 @close(i32 %98)
%100 = load i8*, i8** %7
store i8* %100, i8** %2
br label %101
101: ; preds = %97, %95, %68, %23
%102 = load i8*, i8** %2
ret i8* %102
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [33 x i8] c"File open failed on lookup table\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.4 = [50 x i8] c"File open failed again on lookup table, giving up\00"
@SEEK_END = global i32 0
@PROT_READ = global i32 0
@MAP_FILE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.5 = [14 x i8] c"Couldn't mmap\00"
@str = [19 x i8] c"Populating lookup:\00"
@str.6 = [42 x i8] c"Lookup table written -- trying mmap again\00"
@str.7 = [2 x i8] c".\00"
define i8* @create_lookup_table(i8* nocapture %0) {
%2 = load i32, i32* @O_RDONLY
%3 = tail call i32 @open(i8* %0, i32 %2, i32 384)
%4 = icmp slt i32 %3, 1
br i1 %4, label %5, label %41
5: ; preds = %1
%6 = tail call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 @close(i32 %3)
%8 = tail call i64 @malloc(i32 16777216)
%9 = inttoptr i64 %8 to i8*
%10 = icmp eq i64 %8, 0
br i1 %10, label %61, label %11
11: ; preds = %5
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
br label %13
13: ; preds = %11, %20
%14 = phi i64 [ 0, %11 ], [ %23, %20 ]
%15 = trunc i64 %14 to i32
%16 = urem i32 %15, 1677721
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %13
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([2 x i8], [2 x i8]* @str.7, i64 0, i64 0))
br label %20
20: ; preds = %18, %13
%21 = tail call signext i8 @calc_block(i32 %15)
%22 = getelementptr inbounds i8, i8* %9, i64 %14
store i8 %21, i8* %22
%23 = add nuw nsw i64 %14, 1
%24 = icmp eq i64 %23, 16777216
br i1 %24, label %25, label %13
25: ; preds = %20
%26 = load i32, i32* @O_WRONLY
%27 = load i32, i32* @O_CREAT
%28 = or i32 %27, %26
%29 = load i32, i32* @O_TRUNC
%30 = or i32 %28, %29
%31 = tail call i32 @open(i8* %0, i32 %30, i32 384)
%32 = tail call i32 @write(i32 %31, i8* %9, i32 16777216)
%33 = tail call i32 @close(i32 %31)
%34 = tail call i32 @free(i8* %9)
%35 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @str.6, i64 0, i64 0))
%36 = load i32, i32* @O_RDONLY
%37 = tail call i32 @open(i8* %0, i32 %36, i32 384)
%38 = icmp slt i32 %37, 1
br i1 %38, label %39, label %41
39: ; preds = %25
%40 = tail call i32 @perror(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.4, i64 0, i64 0))
br label %61
41: ; preds = %25, %1
%42 = phi i32 [ %37, %25 ], [ %3, %1 ]
%43 = load i32, i32* @SEEK_END
%44 = tail call i64 @lseek(i32 %42, i32 0, i32 %43)
%45 = srem i64 %44, 4096
%46 = icmp eq i64 %45, 0
%47 = sub nsw i64 4096, %45
%48 = select i1 %46, i64 0, i64 %47
%49 = add nsw i64 %48, %44
%50 = load i32, i32* @PROT_READ
%51 = load i32, i32* @MAP_FILE
%52 = load i32, i32* @MAP_SHARED
%53 = or i32 %52, %51
%54 = tail call i8* @mmap(i32* null, i64 %49, i32 %50, i32 %53, i32 %42, i32 0)
%55 = load i8*, i8** @MAP_FAILED
%56 = icmp eq i8* %54, %55
br i1 %56, label %57, label %59
57: ; preds = %41
%58 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
br label %61
59: ; preds = %41
%60 = tail call i32 @close(i32 %42)
br label %61
61: ; preds = %5, %59, %57, %39
%62 = phi i8* [ null, %39 ], [ null, %57 ], [ %54, %59 ], [ null, %5 ]
ret i8* %62
}