#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 /*<<< orphan*/ txt;
typedef int /*<<< orphan*/ title;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ chdir(char *);
scalar_t__ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ mkdir(char *, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ set_file_name(char *, char *);
scalar_t__ strstr(char *, char *);
int /*<<< orphan*/ trim_title(char *);
int main() {
  static char title[(256 * 16)], txt[(256 * 16)], filename[(256 * 16) + 4];
  FILE *ftitles = fopen("titles.txt", "rt");
  FILE *ftxt = fopen("text.txt", "rt");
  FILE *fout = NULL;
  int lineno = 0;
  mkdir("txts", 0755);
  chdir("txts");
  fgets(title, sizeof(title), ftitles);
  trim_title(title);
  set_file_name(title, filename);
  for (;;) {
    fgets(txt, sizeof(txt), ftxt);
    ++lineno;
    if (lineno % 1000 == 0)
      printf("line %d\n", lineno);
    if (lineno > 64000)
      break;
    if (strstr(txt, title)) {
      printf("found %s\n", txt);
      fout = fopen(filename, "w+t");
      fgets(title, sizeof(title), ftitles);
      trim_title(title);
      set_file_name(title, filename);
      if (title[0] == 0) {
        title[0] = 1;
      } else {
        printf("next seek for %s\n", title);
      }
    }
    if (fout)
      fputs(txt, fout);
    if (feof(ftxt))
      break;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe4a(%rip),%rdi
lea    0xe4e(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x10(%rbp)
lea    0xe41(%rip),%rdi
lea    0xe37(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
lea    0xe24(%rip),%rdi
mov    $0x1ed,%esi
call   1040 <mkdir@plt>
lea    0xe13(%rip),%rdi
call   1050 <chdir@plt>
mov    -0x10(%rbp),%rdx
lea    0x2e6c(%rip),%rdi
mov    $0x1000,%esi
call   1080 <fgets@plt>
lea    0x2e5b(%rip),%rdi
call   10a0 <trim_title@plt>
lea    0x2e4f(%rip),%rdi
lea    0x4e48(%rip),%rsi
call   1030 <set_file_name@plt>
mov    -0x18(%rbp),%rdx
lea    0x3e38(%rip),%rdi
mov    $0x1000,%esi
call   1080 <fgets@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    1280 <main+0xe0>
mov    -0x24(%rbp),%esi
lea    0xda3(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
cmpl   $0xfa00,-0x24(%rbp)
jle    1292 <main+0xf2>
jmp    137d <main+0x1dd>
lea    0x3de7(%rip),%rdi
lea    0x2de0(%rip),%rsi
call   10c0 <strstr@plt>
cmp    $0x0,%rax
je     1345 <main+0x1a5>
lea    0xd6f(%rip),%rdi
lea    0x3dc3(%rip),%rsi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0x4db5(%rip),%rdi
lea    0xd5d(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdx
lea    0x2d9a(%rip),%rdi
mov    $0x1000,%esi
call   1080 <fgets@plt>
lea    0x2d89(%rip),%rdi
call   10a0 <trim_title@plt>
lea    0x2d7d(%rip),%rdi
lea    0x4d76(%rip),%rsi
call   1030 <set_file_name@plt>
movsbl 0x2d6a(%rip),%eax
cmp    $0x0,%eax
jne    132b <main+0x18b>
movb   $0x1,0x2d5a(%rip)
jmp    1340 <main+0x1a0>
lea    0xd01(%rip),%rdi
lea    0x2d47(%rip),%rsi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1345 <main+0x1a5>
cmpq   $0x0,-0x20(%rbp)
je     1360 <main+0x1c0>
mov    -0x20(%rbp),%rsi
lea    0x3d25(%rip),%rdi
call   1070 <fputs@plt>
mov    -0x18(%rbp),%rdi
call   1090 <feof@plt>
cmp    $0x0,%rax
je     1378 <main+0x1d8>
jmp    137d <main+0x1dd>
jmp    123d <main+0x9d>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xe4e(%rip),%rdi
lea    0xe52(%rip),%rbx
mov    %rbx,%rsi
call   10b0 <fopen@plt>
mov    %rax,%rbp
lea    0xe43(%rip),%rdi
mov    %rbx,%rsi
call   10b0 <fopen@plt>
mov    %rax,%r15
lea    0xe3a(%rip),%r14
mov    %r14,%rdi
mov    $0x1ed,%esi
call   1040 <mkdir@plt>
mov    %r14,%rdi
call   1050 <chdir@plt>
lea    0x2e87(%rip),%r12
mov    %r12,%rdi
mov    $0x1000,%esi
mov    %rbp,(%rsp)
mov    %rbp,%rdx
call   1080 <fgets@plt>
mov    %r12,%rdi
call   10a0 <trim_title@plt>
lea    0x4e64(%rip),%rsi
mov    %r12,%rdi
call   1030 <set_file_name@plt>
mov    $0x1,%ebp
lea    0x3e50(%rip),%r13
xor    %r14d,%r14d
jmp    1254 <main+0xb4>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r15,%rdi
call   1090 <feof@plt>
add    $0x1,%ebp
test   %rax,%rax
jne    1341 <main+0x1a1>
mov    %r12,%rbx
mov    %ebp,%eax
imul   $0x10624dd3,%rax,%rax
shr    $0x26,%rax
imul   $0x3e8,%eax,%r12d
mov    %r13,%rdi
mov    $0x1000,%esi
mov    %r15,%rdx
call   1080 <fgets@plt>
cmp    %r12d,%ebp
jne    1290 <main+0xf0>
lea    0xd95(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1060 <printf@plt>
cmp    $0xfa01,%ebp
je     1341 <main+0x1a1>
mov    %rbx,%r12
mov    %r13,%rdi
mov    %rbx,%rsi
call   10c0 <strstr@plt>
test   %rax,%rax
je     1317 <main+0x177>
lea    0xd6f(%rip),%rdi
mov    %r13,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0x4db9(%rip),%rbx
mov    %rbx,%rdi
lea    0xd5e(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,%r14
mov    %r12,%rdi
mov    $0x1000,%esi
mov    (%rsp),%rdx
call   1080 <fgets@plt>
mov    %r12,%rdi
call   10a0 <trim_title@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
call   1030 <set_file_name@plt>
cmpb   $0x0,0x2d7c(%rip)
je     1330 <main+0x190>
lea    0xd26(%rip),%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
test   %r14,%r14
je     1240 <main+0xa0>
mov    %r13,%rdi
mov    %r14,%rsi
call   1070 <fputs@plt>
jmp    1240 <main+0xa0>
movb   $0x1,0x2d49(%rip)
test   %r14,%r14
jne    1320 <main+0x180>
jmp    1240 <main+0xa0>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@main.title = internal global [4096 x i8] zeroinitializer
@main.txt = internal global [4096 x i8] zeroinitializer
@main.filename = internal global [4100 x i8] zeroinitializer
@.str = [11 x i8] c"titles.txt\00"
@.str.1 = [3 x i8] c"rt\00"
@.str.2 = [9 x i8] c"text.txt\00"
@.str.3 = [5 x i8] c"txts\00"
@.str.4 = [9 x i8] c"line %d\0A\00"
@.str.5 = [10 x i8] c"found %s\0A\00"
@.str.6 = [4 x i8] c"w+t\00"
@.str.7 = [18 x i8] c"next seek for %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca i32
store i32 0, i32* %1
%6 = call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %6, i32** %2
%7 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %7, i32** %3
store i32* null, i32** %4
store i32 0, i32* %5
%8 = call i32 @mkdir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 493)
%9 = call i32 @chdir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%10 = load i32*, i32** %2
%11 = call i32 @fgets(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @main.title, i64 0, i64 0), i32 4096, i32* %10)
%12 = call i32 @trim_title(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @main.title, i64 0, i64 0))
%13 = call i32 @set_file_name(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @main.title, i64 0, i64 0), i8* getelementptr inbounds ([4100 x i8], [4100 x i8]* @main.filename, i64 0, i64 0))
br label %14
14: ; preds = %57, %0
%15 = load i32*, i32** %3
%16 = call i32 @fgets(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @main.txt, i64 0, i64 0), i32 4096, i32* %15)
%17 = load i32, i32* %5
%18 = add nsw i32 %17, 1
store i32 %18, i32* %5
%19 = load i32, i32* %5
%20 = srem i32 %19, 1000
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %14
%23 = load i32, i32* %5
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 %23)
br label %25
25: ; preds = %22, %14
%26 = load i32, i32* %5
%27 = icmp sgt i32 %26, 64000
br i1 %27, label %28, label %29
28: ; preds = %25
br label %58
29: ; preds = %25
%30 = call i64 @strstr(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @main.txt, i64 0, i64 0), i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @main.title, i64 0, i64 0))
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %46
32: ; preds = %29
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @main.txt, i64 0, i64 0))
%34 = call i32* @fopen(i8* getelementptr inbounds ([4100 x i8], [4100 x i8]* @main.filename, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0))
store i32* %34, i32** %4
%35 = load i32*, i32** %2
%36 = call i32 @fgets(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @main.title, i64 0, i64 0), i32 4096, i32* %35)
%37 = call i32 @trim_title(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @main.title, i64 0, i64 0))
%38 = call i32 @set_file_name(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @main.title, i64 0, i64 0), i8* getelementptr inbounds ([4100 x i8], [4100 x i8]* @main.filename, i64 0, i64 0))
%39 = load i8, i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @main.title, i64 0, i64 0)
%40 = sext i8 %39 to i32
%41 = icmp eq i32 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %32
store i8 1, i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @main.title, i64 0, i64 0)
br label %45
43: ; preds = %32
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @main.title, i64 0, i64 0))
br label %45
45: ; preds = %43, %42
br label %46
46: ; preds = %45, %29
%47 = load i32*, i32** %4
%48 = icmp ne i32* %47, null
br i1 %48, label %49, label %52
49: ; preds = %46
%50 = load i32*, i32** %4
%51 = call i32 @fputs(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @main.txt, i64 0, i64 0), i32* %50)
br label %52
52: ; preds = %49, %46
%53 = load i32*, i32** %3
%54 = call i64 @feof(i32* %53)
%55 = icmp ne i64 %54, 0
br i1 %55, label %56, label %57
56: ; preds = %52
br label %58
57: ; preds = %52
br label %14
58: ; preds = %56, %28
ret i32 0
}

LLVM IR (-O2):
@main.title = internal global [4096 x i8] zeroinitializer
@main.txt = internal global [4096 x i8] zeroinitializer
@main.filename = internal global [4100 x i8] zeroinitializer
@.str = [11 x i8] c"titles.txt\00"
@.str.1 = [3 x i8] c"rt\00"
@.str.2 = [9 x i8] c"text.txt\00"
@.str.3 = [5 x i8] c"txts\00"
@.str.4 = [9 x i8] c"line %d\0A\00"
@.str.5 = [10 x i8] c"found %s\0A\00"
@.str.6 = [4 x i8] c"w+t\00"
@.str.7 = [18 x i8] c"next seek for %s\0A\00"
define i32 @main() {
%1 = tail call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%2 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%3 = tail call i32 @mkdir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 493)
%4 = tail call i32 @chdir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%5 = tail call i32 @fgets(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @main.title, i64 0, i64 0), i32 4096, i32* %1)
%6 = tail call i32 @trim_title(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @main.title, i64 0, i64 0))
%7 = tail call i32 @set_file_name(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @main.title, i64 0, i64 0), i8* getelementptr inbounds ([4100 x i8], [4100 x i8]* @main.filename, i64 0, i64 0))
br label %8
8: ; preds = %38, %0
%9 = phi i32* [ null, %0 ], [ %34, %38 ]
%10 = phi i32 [ 0, %0 ], [ %12, %38 ]
%11 = tail call i32 @fgets(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @main.txt, i64 0, i64 0), i32 4096, i32* %2)
%12 = add nuw nsw i32 %10, 1
%13 = urem i32 %12, 1000
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %8
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 %12)
br label %17
17: ; preds = %15, %8
%18 = icmp eq i32 %10, 64000
br i1 %18, label %41, label %19
19: ; preds = %17
%20 = tail call i64 @strstr(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @main.txt, i64 0, i64 0), i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @main.title, i64 0, i64 0))
%21 = icmp eq i64 %20, 0
br i1 %21, label %33, label %22
22: ; preds = %19
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @main.txt, i64 0, i64 0))
%24 = tail call i32* @fopen(i8* getelementptr inbounds ([4100 x i8], [4100 x i8]* @main.filename, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0))
%25 = tail call i32 @fgets(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @main.title, i64 0, i64 0), i32 4096, i32* %1)
%26 = tail call i32 @trim_title(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @main.title, i64 0, i64 0))
%27 = tail call i32 @set_file_name(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @main.title, i64 0, i64 0), i8* getelementptr inbounds ([4100 x i8], [4100 x i8]* @main.filename, i64 0, i64 0))
%28 = load i8, i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @main.title, i64 0, i64 0)
%29 = icmp eq i8 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %22
store i8 1, i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @main.title, i64 0, i64 0)
br label %33
31: ; preds = %22
%32 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @main.title, i64 0, i64 0))
br label %33
33: ; preds = %30, %31, %19
%34 = phi i32* [ %24, %30 ], [ %24, %31 ], [ %9, %19 ]
%35 = icmp eq i32* %34, null
br i1 %35, label %38, label %36
36: ; preds = %33
%37 = tail call i32 @fputs(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @main.txt, i64 0, i64 0), i32* %34)
br label %38
38: ; preds = %36, %33
%39 = tail call i64 @feof(i32* %2)
%40 = icmp eq i64 %39, 0
br i1 %40, label %8, label %41
41: ; preds = %38, %17
ret i32 0
}