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

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fill_var(int, char *, int /*<<< orphan*/ *,
                            int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sprintf(char *, char *, char *, ...);
scalar_t__ strstr(char *, char *);
int parsetemplate(int debug, char *path, char filename[200],
                  char filepath[200]) {
  FILE *fact, *commonfact, *templ, *out;
  char infact[512];
  char incommonfact[512];
  char intempl[512];
  sprintf(infact, "%s/facts/%s.ft", path, filename);
  sprintf(incommonfact, "%s/facts/common.ft", path);
  sprintf(intempl, "%s/template/%s", path, filename);
  templ = fopen(intempl, "r");
  if (templ == NULL) {
    printf("Error: can't open template file\n");
    exit(1);
  }
  fact = fopen(infact, "r");
  if (fact == NULL) {
    printf("Error: can't open facts file\n");
    exit(1);
  }
  commonfact = fopen(incommonfact, "r");
  if (commonfact == NULL) {
    printf("Error: can't open common facts file\n");
    exit(1);
  }
  out = fopen(filepath, "w");
  if (commonfact == NULL) {
    printf("Error: can't open target file: %s\n", filepath);
    exit(1);
  }
  char buffer[512];
  fgets(buffer, 512, templ);
  do {
    if (strstr(buffer, "<%$") != 0) {
      fill_var(debug, buffer, fact, commonfact);
      fputs(buffer, out);
    } else {
      fputs(buffer, out);
    }
    fgets(buffer, 512, templ);
  } while (feof(templ) != 1);
  fclose(out);
  fclose(templ);
  fclose(fact);
  fclose(commonfact);
  return 0;
}


x86 assembly (clang, -O0):
<parsetemplate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x840,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
lea    -0x240(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    0xe30(%rip),%rsi
mov    $0x0,%al
call   10a0 <sprintf@plt>
lea    -0x440(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe26(%rip),%rsi
mov    $0x0,%al
call   10a0 <sprintf@plt>
lea    -0x640(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    0xe1c(%rip),%rsi
mov    $0x0,%al
call   10a0 <sprintf@plt>
lea    -0x640(%rbp),%rdi
lea    0xe16(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1247 <parsetemplate+0xa7>
lea    0xdfd(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    -0x240(%rbp),%rdi
lea    0xddc(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1281 <parsetemplate+0xe1>
lea    0xde4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    -0x440(%rbp),%rdi
lea    0xda2(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    12bb <parsetemplate+0x11b>
lea    0xdc8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x20(%rbp),%rdi
lea    0xdd1(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    12f6 <parsetemplate+0x156>
mov    -0x20(%rbp),%rsi
lea    0xdb4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    -0x840(%rbp),%rdi
mov    -0x38(%rbp),%rdx
mov    $0x200,%esi
call   1060 <fgets@plt>
lea    -0x840(%rbp),%rdi
lea    0xda3(%rip),%rsi
call   10c0 <strstr@plt>
cmp    $0x0,%rax
je     1354 <parsetemplate+0x1b4>
mov    -0x4(%rbp),%edi
lea    -0x840(%rbp),%rsi
mov    -0x28(%rbp),%rdx
mov    -0x30(%rbp),%rcx
call   1090 <fill_var@plt>
lea    -0x840(%rbp),%rdi
mov    -0x40(%rbp),%rsi
call   1050 <fputs@plt>
jmp    1364 <parsetemplate+0x1c4>
lea    -0x840(%rbp),%rdi
mov    -0x40(%rbp),%rsi
call   1050 <fputs@plt>
lea    -0x840(%rbp),%rdi
mov    -0x38(%rbp),%rdx
mov    $0x200,%esi
call   1060 <fgets@plt>
mov    -0x38(%rbp),%rdi
call   1070 <feof@plt>
cmp    $0x1,%eax
jne    130b <parsetemplate+0x16b>
mov    -0x40(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x38(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x840,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parsetemplate>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x818,%rsp
mov    %rcx,%r12
mov    %rdx,%rbx
mov    %rsi,%rbp
mov    %edi,0xc(%rsp)
lea    0xe3b(%rip),%rsi
lea    0x210(%rsp),%rdi
mov    %rbp,%rdx
mov    %rbx,%rcx
xor    %eax,%eax
call   10a0 <sprintf@plt>
lea    0xe2e(%rip),%rsi
lea    0x10(%rsp),%rdi
mov    %rbp,%rdx
xor    %eax,%eax
call   10a0 <sprintf@plt>
lea    0xe2b(%rip),%rsi
lea    0x610(%rsp),%r14
mov    %r14,%rdi
mov    %rbp,%rdx
mov    %rbx,%rcx
xor    %eax,%eax
call   10a0 <sprintf@plt>
lea    0xe1b(%rip),%rsi
mov    %r14,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     1320 <parsetemplate+0x180>
mov    %rax,%rbx
lea    0xe00(%rip),%rsi
lea    0x210(%rsp),%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     1329 <parsetemplate+0x189>
mov    %rax,%r15
lea    0xde0(%rip),%rsi
lea    0x10(%rsp),%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     1332 <parsetemplate+0x192>
mov    %rax,%r13
lea    0xdc5(%rip),%rsi
mov    %r12,%rdi
call   1080 <fopen@plt>
mov    %rax,%r12
lea    0x410(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x200,%esi
mov    %rbx,%rdx
call   1060 <fgets@plt>
lea    0xd9d(%rip),%r14
jmp    12c8 <parsetemplate+0x128>
nopw   0x0(%rax,%rax,1)
mov    %rbp,%rdi
mov    %r12,%rsi
call   1050 <fputs@plt>
mov    %rbp,%rdi
mov    $0x200,%esi
mov    %rbx,%rdx
call   1060 <fgets@plt>
mov    %rbx,%rdi
call   1070 <feof@plt>
cmp    $0x1,%eax
je     12ec <parsetemplate+0x14c>
mov    %rbp,%rdi
mov    %r14,%rsi
call   10c0 <strstr@plt>
test   %rax,%rax
je     12a0 <parsetemplate+0x100>
mov    0xc(%rsp),%edi
mov    %rbp,%rsi
mov    %r15,%rdx
mov    %r13,%rcx
call   1090 <fill_var@plt>
jmp    12a0 <parsetemplate+0x100>
mov    %r12,%rdi
call   1040 <fclose@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    %r13,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x818,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd53(%rip),%rdi
jmp    1339 <parsetemplate+0x199>
lea    0xd2d(%rip),%rdi
jmp    1339 <parsetemplate+0x199>
lea    0xd00(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [15 x i8] c"%s/facts/%s.ft\00"
@.str.1 = [19 x i8] c"%s/facts/common.ft\00"
@.str.2 = [15 x i8] c"%s/template/%s\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [33 x i8] c"Error: can't open template file\0A\00"
@.str.5 = [30 x i8] c"Error: can't open facts file\0A\00"
@.str.6 = [37 x i8] c"Error: can't open common facts file\0A\00"
@.str.7 = [2 x i8] c"w\00"
@.str.8 = [35 x i8] c"Error: can't open target file: %s\0A\00"
@.str.9 = [4 x i8] c"<%$\00"
define i32 @parsetemplate(i32 %0, i8* %1, i8* %2, i8* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca i32*
%12 = alloca i32*
%13 = alloca [512 x i8]
%14 = alloca [512 x i8]
%15 = alloca [512 x i8]
%16 = alloca [512 x i8]
store i32 %0, i32* %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i8* %3, i8** %8
%17 = getelementptr inbounds [512 x i8], [512 x i8]* %13, i64 0, i64 0
%18 = load i8*, i8** %6
%19 = load i8*, i8** %7
%20 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %17, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %18, i8* %19)
%21 = getelementptr inbounds [512 x i8], [512 x i8]* %14, i64 0, i64 0
%22 = load i8*, i8** %6
%23 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %21, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %22)
%24 = getelementptr inbounds [512 x i8], [512 x i8]* %15, i64 0, i64 0
%25 = load i8*, i8** %6
%26 = load i8*, i8** %7
%27 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %24, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %25, i8* %26)
%28 = getelementptr inbounds [512 x i8], [512 x i8]* %15, i64 0, i64 0
%29 = call i32* @fopen(i8* %28, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %29, i32** %11
%30 = load i32*, i32** %11
%31 = icmp eq i32* %30, null
br i1 %31, label %32, label %35
32: ; preds = %4
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0))
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %4
%36 = getelementptr inbounds [512 x i8], [512 x i8]* %13, i64 0, i64 0
%37 = call i32* @fopen(i8* %36, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %37, i32** %9
%38 = load i32*, i32** %9
%39 = icmp eq i32* %38, null
br i1 %39, label %40, label %43
40: ; preds = %35
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0))
%42 = call i32 @exit(i32 1)
unreachable
43: ; preds = %35
%44 = getelementptr inbounds [512 x i8], [512 x i8]* %14, i64 0, i64 0
%45 = call i32* @fopen(i8* %44, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %45, i32** %10
%46 = load i32*, i32** %10
%47 = icmp eq i32* %46, null
br i1 %47, label %48, label %51
48: ; preds = %43
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.6, i64 0, i64 0))
%50 = call i32 @exit(i32 1)
unreachable
51: ; preds = %43
%52 = load i8*, i8** %8
%53 = call i32* @fopen(i8* %52, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0))
store i32* %53, i32** %12
%54 = load i32*, i32** %10
%55 = icmp eq i32* %54, null
br i1 %55, label %56, label %60
56: ; preds = %51
%57 = load i8*, i8** %8
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.8, i64 0, i64 0), i8* %57)
%59 = call i32 @exit(i32 1)
unreachable
60: ; preds = %51
%61 = getelementptr inbounds [512 x i8], [512 x i8]* %16, i64 0, i64 0
%62 = load i32*, i32** %11
%63 = call i32 @fgets(i8* %61, i32 512, i32* %62)
br label %64
64: ; preds = %85, %60
%65 = getelementptr inbounds [512 x i8], [512 x i8]* %16, i64 0, i64 0
%66 = call i64 @strstr(i8* %65, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.9, i64 0, i64 0))
%67 = icmp ne i64 %66, 0
br i1 %67, label %68, label %77
68: ; preds = %64
%69 = load i32, i32* %5
%70 = getelementptr inbounds [512 x i8], [512 x i8]* %16, i64 0, i64 0
%71 = load i32*, i32** %9
%72 = load i32*, i32** %10
%73 = call i32 @fill_var(i32 %69, i8* %70, i32* %71, i32* %72)
%74 = getelementptr inbounds [512 x i8], [512 x i8]* %16, i64 0, i64 0
%75 = load i32*, i32** %12
%76 = call i32 @fputs(i8* %74, i32* %75)
br label %81
77: ; preds = %64
%78 = getelementptr inbounds [512 x i8], [512 x i8]* %16, i64 0, i64 0
%79 = load i32*, i32** %12
%80 = call i32 @fputs(i8* %78, i32* %79)
br label %81
81: ; preds = %77, %68
%82 = getelementptr inbounds [512 x i8], [512 x i8]* %16, i64 0, i64 0
%83 = load i32*, i32** %11
%84 = call i32 @fgets(i8* %82, i32 512, i32* %83)
br label %85
85: ; preds = %81
%86 = load i32*, i32** %11
%87 = call i32 @feof(i32* %86)
%88 = icmp ne i32 %87, 1
br i1 %88, label %64, label %89
89: ; preds = %85
%90 = load i32*, i32** %12
%91 = call i32 @fclose(i32* %90)
%92 = load i32*, i32** %11
%93 = call i32 @fclose(i32* %92)
%94 = load i32*, i32** %9
%95 = call i32 @fclose(i32* %94)
%96 = load i32*, i32** %10
%97 = call i32 @fclose(i32* %96)
ret i32 0
}

LLVM IR (-O2):
@.str = [15 x i8] c"%s/facts/%s.ft\00"
@.str.1 = [19 x i8] c"%s/facts/common.ft\00"
@.str.2 = [15 x i8] c"%s/template/%s\00"
@.str.3 = [2 x i8] c"r\00"
@.str.7 = [2 x i8] c"w\00"
@.str.9 = [4 x i8] c"<%$\00"
@str = [36 x i8] c"Error: can't open common facts file\00"
@str.10 = [29 x i8] c"Error: can't open facts file\00"
@str.11 = [32 x i8] c"Error: can't open template file\00"
define i32 @parsetemplate(i32 %0, i8* %1, i8* %2, i8* nocapture %3) {
%5 = alloca [512 x i8]
%6 = alloca [512 x i8]
%7 = alloca [512 x i8]
%8 = alloca [512 x i8]
%9 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%10 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%11 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%12 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %9, i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %1, i8* %2)
%13 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %10, i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %1)
%14 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %11, i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %1, i8* %2)
%15 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %20
17: ; preds = %4
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str.11, i64 0, i64 0))
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %4
%21 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %26
23: ; preds = %20
%24 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str.10, i64 0, i64 0))
%25 = tail call i32 @exit(i32 1)
unreachable
26: ; preds = %20
%27 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%28 = icmp eq i32* %27, null
br i1 %28, label %29, label %32
29: ; preds = %26
%30 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str, i64 0, i64 0))
%31 = tail call i32 @exit(i32 1)
unreachable
32: ; preds = %26
%33 = tail call i32* @fopen(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0))
%34 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%35 = call i32 @fgets(i8* %34, i32 512, i32* %15)
br label %36
36: ; preds = %41, %32
%37 = call i64 @strstr(i8* %34, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.9, i64 0, i64 0))
%38 = icmp eq i64 %37, 0
br i1 %38, label %41, label %39
39: ; preds = %36
%40 = call i32 @fill_var(i32 %0, i8* %34, i32* %21, i32* %27)
br label %41
41: ; preds = %36, %39
%42 = call i32 @fputs(i8* %34, i32* %33)
%43 = call i32 @fgets(i8* %34, i32 512, i32* %15)
%44 = call i32 @feof(i32* %15)
%45 = icmp eq i32 %44, 1
br i1 %45, label %46, label %36
46: ; preds = %41
%47 = call i32 @fclose(i32* %33)
%48 = call i32 @fclose(i32* %15)
%49 = call i32 @fclose(i32* %21)
%50 = call i32 @fclose(i32* %27)
ret i32 0
}