#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 dirent {
  scalar_t__ d_type;
  char *d_name;
};
typedef int /*<<< orphan*/ filename;
typedef int /*<<< orphan*/ fen;
typedef int /*<<< orphan*/ FILE;
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
scalar_t__ DT_DIR;
int engine_score(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ init_engine();
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ printf(char *, char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int strlen(char *);
int /*<<< orphan*/ strncat(char *, char *, int);
int /*<<< orphan*/ strncpy(char *, char *, int);
int main(int argc, char **argv) {
  if (argc < 2) {
    fprintf(stderr, "usage: %s <training dir>\n", argv[0]);
    exit(-1);
  }
  DIR *d = opendir(argv[1]);
  if (d == NULL) {
    fprintf(stderr, "Directory %s does not exist\n", argv[1]);
    exit(-1);
  }
  struct dirent *dir;
  FILE *tset_scores = fopen("scored_examples", "w+");
  init_engine();
  char fen[512] = {0};
  char filename[256] = {0};
  while ((dir = readdir(d)) != NULL) {
    if (dir->d_type == DT_DIR) {
      continue;
    }
    printf("%s\n", dir->d_name);
    strncpy(filename, argv[1], sizeof(filename));
    strncat(filename, "/", sizeof(filename));
    strncat(filename, dir->d_name, sizeof(filename));
    FILE *tset = fopen(filename, "r");
    while (fgets(fen, sizeof(fen), tset) != NULL) {
      if (fen[0] != '#') {
        fen[strlen(fen) - 1] = '\0';
        int score = engine_score(fen);
        fprintf(tset_scores, "%d %s\n", score, fen);
      }
    }
    fclose(tset);
  }
  fclose(tset_scores);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x340,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    122c <main+0x4c>
mov    0x2dd6(%rip),%rax
movslq (%rax),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xde5(%rip),%rsi
mov    $0x0,%al
call   10d0 <fprintf@plt>
mov    $0xffffffff,%edi
call   1100 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1060 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1272 <main+0x92>
mov    0x2d91(%rip),%rax
movslq (%rax),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xdb9(%rip),%rsi
mov    $0x0,%al
call   10d0 <fprintf@plt>
mov    $0xffffffff,%edi
call   1100 <exit@plt>
lea    0xdbe(%rip),%rdi
lea    0xdc7(%rip),%rsi
call   10f0 <fopen@plt>
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   1050 <init_engine@plt>
lea    -0x230(%rbp),%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   10a0 <memset@plt>
lea    -0x330(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   10a0 <memset@plt>
mov    -0x18(%rbp),%rdi
call   10e0 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     13f3 <main+0x213>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    0x2cfd(%rip),%rcx
cmp    (%rcx),%rax
jne    12e9 <main+0x109>
jmp    12b6 <main+0xd6>
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xd59(%rip),%rdi
call   1080 <printf@plt>
lea    -0x330(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
mov    $0x100,%edx
call   1030 <strncpy@plt>
lea    -0x330(%rbp),%rdi
lea    0xd26(%rip),%rsi
mov    $0x100,%edx
call   10b0 <strncat@plt>
lea    -0x330(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rsi
mov    $0x100,%edx
call   10b0 <strncat@plt>
lea    -0x330(%rbp),%rdi
lea    0xcf7(%rip),%rsi
call   10f0 <fopen@plt>
mov    %rax,-0x338(%rbp)
lea    -0x230(%rbp),%rdi
mov    -0x338(%rbp),%rdx
mov    $0x200,%esi
call   10c0 <fgets@plt>
cmp    $0x0,%rax
je     13e2 <main+0x202>
movsbl -0x230(%rbp),%eax
cmp    $0x23,%eax
je     13dd <main+0x1fd>
lea    -0x230(%rbp),%rdi
call   1070 <strlen@plt>
sub    $0x1,%eax
cltq
movb   $0x0,-0x230(%rbp,%rax,1)
lea    -0x230(%rbp),%rdi
call   1090 <engine_score@plt>
mov    %eax,-0x33c(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x33c(%rbp),%edx
lea    -0x230(%rbp),%rcx
lea    0xc78(%rip),%rsi
mov    $0x0,%al
call   10d0 <fprintf@plt>
jmp    1361 <main+0x181>
mov    -0x338(%rbp),%rdi
call   1040 <fclose@plt>
jmp    12b6 <main+0xd6>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x340,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x318,%rsp
mov    %rsi,%r14
cmp    $0x1,%edi
jle    13fa <main+0x21a>
mov    0x8(%r14),%rdi
call   1060 <opendir@plt>
test   %rax,%rax
je     1410 <main+0x230>
mov    %rax,%r15
lea    0xe1e(%rip),%rdi
lea    0xe27(%rip),%rsi
call   10f0 <fopen@plt>
mov    %rax,%r12
xor    %eax,%eax
call   1050 <init_engine@plt>
lea    0x110(%rsp),%rbx
mov    $0x200,%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10a0 <memset@plt>
xorps  %xmm0,%xmm0
movaps %xmm0,0x100(%rsp)
movaps %xmm0,0xf0(%rsp)
movaps %xmm0,0xe0(%rsp)
movaps %xmm0,0xd0(%rsp)
movaps %xmm0,0xc0(%rsp)
movaps %xmm0,0xb0(%rsp)
movaps %xmm0,0xa0(%rsp)
movaps %xmm0,0x90(%rsp)
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
mov    %r15,%rdi
call   10e0 <readdir@plt>
test   %rax,%rax
je     13de <main+0x1fe>
mov    %rax,%rbp
lea    0xd7f(%rip),%r13
mov    %r15,0x8(%rsp)
jmp    1304 <main+0x124>
cs nopw 0x0(%rax,%rax,1)
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    %r15,%r14
mov    0x8(%rsp),%r15
mov    %r15,%rdi
call   10e0 <readdir@plt>
mov    %rax,%rbp
test   %rax,%rax
je     13de <main+0x1fe>
mov    0x0(%rbp),%rax
mov    0x2cc9(%rip),%rcx
cmp    (%rcx),%rax
je     12f0 <main+0x110>
mov    0x8(%rbp),%rsi
lea    0xd32(%rip),%rdi
call   1080 <printf@plt>
mov    %r14,%r15
mov    0x8(%r14),%rsi
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    $0x100,%edx
call   1030 <strncpy@plt>
mov    %r14,%rdi
lea    0xd03(%rip),%rsi
mov    $0x100,%edx
call   10b0 <strncat@plt>
mov    0x8(%rbp),%rsi
mov    %r14,%rdi
mov    $0x100,%edx
call   10b0 <strncat@plt>
mov    %r14,%rdi
lea    0xce0(%rip),%rsi
call   10f0 <fopen@plt>
mov    %rax,%r14
mov    %rbx,%rdi
mov    $0x200,%esi
mov    %rax,%rdx
jmp    139b <main+0x1bb>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    $0x200,%esi
mov    %r14,%rdx
call   10c0 <fgets@plt>
test   %rax,%rax
je     12e0 <main+0x100>
cmpb   $0x23,0x110(%rsp)
23
je     1390 <main+0x1b0>
mov    %rbx,%rdi
call   1070 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
mov    %rbx,%rdi
call   1090 <engine_score@plt>
mov    %r12,%rdi
mov    %r13,%rsi
mov    %eax,%edx
mov    %rbx,%rcx
xor    %eax,%eax
call   10d0 <fprintf@plt>
jmp    1390 <main+0x1b0>
mov    %r12,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x318,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2bdf(%rip),%rax
movslq (%rax),%rdi
mov    (%r14),%rdx
lea    0xbf2(%rip),%rsi
jmp    1425 <main+0x245>
mov    0x2bc9(%rip),%rax
movslq (%rax),%rdi
mov    0x8(%r14),%rdx
lea    0xbf5(%rip),%rsi
xor    %eax,%eax
call   10d0 <fprintf@plt>
mov    $0xffffffff,%edi
call   1100 <exit@plt>

LLVM IR (-O0):
%struct.dirent = type { i64, i8* }
@stderr = global i32 0
@.str = [26 x i8] c"usage: %s <training dir>\0A\00"
@.str.1 = [29 x i8] c"Directory %s does not exist\0A\00"
@.str.2 = [16 x i8] c"scored_examples\00"
@.str.3 = [3 x i8] c"w+\00"
@DT_DIR = global i64 0
@.str.4 = [4 x i8] c"%s\0A\00"
@.str.5 = [2 x i8] c"/\00"
@.str.6 = [2 x i8] c"r\00"
@.str.7 = [7 x i8] c"%d %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca %struct.dirent*
%8 = alloca i32*
%9 = alloca [512 x i8]
%10 = alloca [256 x i8]
%11 = alloca i32*
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 2
br i1 %14, label %15, label %24
15: ; preds = %2
%16 = load i32, i32* @stderr
%17 = sext i32 %16 to i64
%18 = inttoptr i64 %17 to i32*
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 0
%21 = load i8*, i8** %20
%22 = call i32 (i32*, i8*, ...) @fprintf(i32* %18, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %21)
%23 = call i32 @exit(i32 -1)
unreachable
24: ; preds = %2
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 1
%27 = load i8*, i8** %26
%28 = call i32* @opendir(i8* %27)
store i32* %28, i32** %6
%29 = load i32*, i32** %6
%30 = icmp eq i32* %29, null
br i1 %30, label %31, label %40
31: ; preds = %24
%32 = load i32, i32* @stderr
%33 = sext i32 %32 to i64
%34 = inttoptr i64 %33 to i32*
%35 = load i8**, i8*** %5
%36 = getelementptr inbounds i8*, i8** %35, i64 1
%37 = load i8*, i8** %36
%38 = call i32 (i32*, i8*, ...) @fprintf(i32* %34, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %37)
%39 = call i32 @exit(i32 -1)
unreachable
40: ; preds = %24
%41 = call i32* @fopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i32* %41, i32** %8
%42 = call i32 (...) @init_engine()
%43 = bitcast [512 x i8]* %9 to i8*
%44 = bitcast [256 x i8]* %10 to i8*
br label %45
45: ; preds = %98, %55, %40
%46 = load i32*, i32** %6
%47 = call %struct.dirent* @readdir(i32* %46)
store %struct.dirent* %47, %struct.dirent** %7
%48 = icmp ne %struct.dirent* %47, null
br i1 %48, label %49, label %101
49: ; preds = %45
%50 = load %struct.dirent*, %struct.dirent** %7
%51 = getelementptr inbounds %struct.dirent, %struct.dirent* %50, i32 0, i32 0
%52 = load i64, i64* %51
%53 = load i64, i64* @DT_DIR
%54 = icmp eq i64 %52, %53
br i1 %54, label %55, label %56
55: ; preds = %49
br label %45
56: ; preds = %49
%57 = load %struct.dirent*, %struct.dirent** %7
%58 = getelementptr inbounds %struct.dirent, %struct.dirent* %57, i32 0, i32 1
%59 = load i8*, i8** %58
%60 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %59)
%61 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%62 = load i8**, i8*** %5
%63 = getelementptr inbounds i8*, i8** %62, i64 1
%64 = load i8*, i8** %63
%65 = call i32 @strncpy(i8* %61, i8* %64, i32 256)
%66 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%67 = call i32 @strncat(i8* %66, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0), i32 256)
%68 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%69 = load %struct.dirent*, %struct.dirent** %7
%70 = getelementptr inbounds %struct.dirent, %struct.dirent* %69, i32 0, i32 1
%71 = load i8*, i8** %70
%72 = call i32 @strncat(i8* %68, i8* %71, i32 256)
%73 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%74 = call i32* @fopen(i8* %73, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
store i32* %74, i32** %11
br label %75
75: ; preds = %97, %56
%76 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%77 = load i32*, i32** %11
%78 = call i32* @fgets(i8* %76, i32 512, i32* %77)
%79 = icmp ne i32* %78, null
br i1 %79, label %80, label %98
80: ; preds = %75
%81 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%82 = load i8, i8* %81
%83 = sext i8 %82 to i32
%84 = icmp ne i32 %83, 35
br i1 %84, label %85, label %97
85: ; preds = %80
%86 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%87 = call i32 @strlen(i8* %86)
%88 = sub nsw i32 %87, 1
%89 = sext i32 %88 to i64
%90 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 %89
store i8 0, i8* %90
%91 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%92 = call i32 @engine_score(i8* %91)
store i32 %92, i32* %12
%93 = load i32*, i32** %8
%94 = load i32, i32* %12
%95 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%96 = call i32 (i32*, i8*, ...) @fprintf(i32* %93, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0), i32 %94, i8* %95)
br label %97
97: ; preds = %85, %80
br label %75
98: ; preds = %75
%99 = load i32*, i32** %11
%100 = call i32 @fclose(i32* %99)
br label %45
101: ; preds = %45
%102 = load i32*, i32** %8
%103 = call i32 @fclose(i32* %102)
ret i32 0
}

LLVM IR (-O2):
%struct.dirent = type { i64, i8* }
@stderr = global i32 0
@.str = [26 x i8] c"usage: %s <training dir>\0A\00"
@.str.1 = [29 x i8] c"Directory %s does not exist\0A\00"
@.str.2 = [16 x i8] c"scored_examples\00"
@.str.3 = [3 x i8] c"w+\00"
@DT_DIR = global i64 0
@.str.4 = [4 x i8] c"%s\0A\00"
@.str.5 = [2 x i8] c"/\00"
@.str.6 = [2 x i8] c"r\00"
@.str.7 = [7 x i8] c"%d %s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [512 x i8]
%4 = alloca [256 x i8]
%5 = icmp slt i32 %0, 2
br i1 %5, label %6, label %13
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = sext i32 %7 to i64
%9 = inttoptr i64 %8 to i32*
%10 = load i8*, i8** %1
%11 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %9, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = tail call i32 @exit(i32 -1)
unreachable
13: ; preds = %2
%14 = getelementptr inbounds i8*, i8** %1, i64 1
%15 = load i8*, i8** %14
%16 = tail call i32* @opendir(i8* %15)
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %25
18: ; preds = %13
%19 = load i32, i32* @stderr
%20 = sext i32 %19 to i64
%21 = inttoptr i64 %20 to i32*
%22 = load i8*, i8** %14
%23 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %21, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %22)
%24 = tail call i32 @exit(i32 -1)
unreachable
25: ; preds = %13
%26 = tail call i32* @fopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%27 = tail call i32 (...) @init_engine()
%28 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%29 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%30 = call %struct.dirent* @readdir(i32* %16)
%31 = icmp eq %struct.dirent* %30, null
br i1 %31, label %68, label %32
32: ; preds = %25, %65
%33 = phi %struct.dirent* [ %66, %65 ], [ %30, %25 ]
%34 = getelementptr inbounds %struct.dirent, %struct.dirent* %33, i64 0, i32 0
%35 = load i64, i64* %34
%36 = load i64, i64* @DT_DIR
%37 = icmp eq i64 %35, %36
br i1 %37, label %65, label %38
38: ; preds = %32
%39 = getelementptr inbounds %struct.dirent, %struct.dirent* %33, i64 0, i32 1
%40 = load i8*, i8** %39
%41 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %40)
%42 = load i8*, i8** %14
%43 = call i32 @strncpy(i8* %29, i8* %42, i32 256)
%44 = call i32 @strncat(i8* %29, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0), i32 256)
%45 = load i8*, i8** %39
%46 = call i32 @strncat(i8* %29, i8* %45, i32 256)
%47 = call i32* @fopen(i8* %29, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
%48 = call i32* @fgets(i8* %28, i32 512, i32* %47)
%49 = icmp eq i32* %48, null
br i1 %49, label %63, label %50
50: ; preds = %38, %60
%51 = load i8, i8* %28
%52 = icmp eq i8 %51, 35
br i1 %52, label %60, label %53
53: ; preds = %50
%54 = call i32 @strlen(i8* %28)
%55 = add nsw i32 %54, -1
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 %56
store i8 0, i8* %57
%58 = call i32 @engine_score(i8* %28)
%59 = call i32 (i32*, i8*, ...) @fprintf(i32* %26, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0), i32 %58, i8* %28)
br label %60
60: ; preds = %53, %50
%61 = call i32* @fgets(i8* %28, i32 512, i32* %47)
%62 = icmp eq i32* %61, null
br i1 %62, label %63, label %50
63: ; preds = %60, %38
%64 = call i32 @fclose(i32* %47)
br label %65
65: ; preds = %63, %32
%66 = call %struct.dirent* @readdir(i32* %16)
%67 = icmp eq %struct.dirent* %66, null
br i1 %67, label %68, label %32
68: ; preds = %65, %25
%69 = call i32 @fclose(i32* %26)
ret i32 0
}