#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 O_CREAT;
int O_RDWR;
int O_SYNC;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ execl(char *, char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fork();
int /*<<< orphan*/ fputc(char, int /*<<< orphan*/ *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ remove(char *);
int /*<<< orphan*/ sprintf(char *, char *, char *);
char *strtok(char *, char *);
int /*<<< orphan*/ wait(int);
int factertofile(char path[200]) {
  char buffer[1024];
  char infile[512], outfile[512];
  FILE *infp, *outfp;
  sprintf(infile, "%s/facts/.common.ft", path);
  sprintf(outfile, "%s/facts/common.ft", path);
  infp = fopen(infile, "w");
  if (infp == NULL) {
    printf("Error: can't write to facts directory\n");
    exit(1);
  }
  outfp = fopen(outfile, "w");
  if (infp == NULL) {
    printf("Error: can't write to facts directory\n");
    exit(1);
  }
  fclose(infp);
  fclose(outfp);
  int fd;
  if ((fd = open(infile, O_RDWR | O_CREAT | O_SYNC, 0600)) == -1) {
    perror("open");
    return 1;
  }
  int pid;
  pid = fork();
  if (pid == 0) {
    dup2(fd, STDOUT_FILENO);
    close(fd);
    execl("/usr/bin/facter", (char *)0);
  } else
    wait(pid);
  infp = fopen(infile, "r");
  outfp = fopen(outfile, "w");
  char *ptr;
  fgets(buffer, 1024, infp);
  do {
    ptr = strtok(buffer, " => ");
    fputs(ptr, outfp);
    fputc('=', outfp);
    ptr = strtok(NULL, " => ");
    fputs(ptr, outfp);
    fgets(buffer, 1024, infp);
  } while (feof(infp) != 1);
  fclose(infp);
  fclose(outfp);
  remove(infile);
}


x86 assembly (clang, -O0):
<factertofile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x830,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x610(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xdbf(%rip),%rsi
call   1100 <sprintf@plt>
lea    -0x810(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xdbc(%rip),%rsi
call   1100 <sprintf@plt>
lea    -0x610(%rbp),%rdi
lea    0xdbc(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x818(%rbp)
cmpq   $0x0,-0x818(%rbp)
jne    129b <factertofile+0x7b>
lea    0xd9d(%rip),%rdi
call   1060 <printf@plt>
mov    $0x1,%edi
call   1110 <exit@plt>
lea    -0x810(%rbp),%rdi
lea    0xd7e(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x820(%rbp)
cmpq   $0x0,-0x818(%rbp)
jne    12d9 <factertofile+0xb9>
lea    0xd5f(%rip),%rdi
call   1060 <printf@plt>
mov    $0x1,%edi
call   1110 <exit@plt>
mov    -0x818(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x820(%rbp),%rdi
call   1040 <fclose@plt>
lea    -0x610(%rbp),%rdi
mov    0x2cd9(%rip),%rax
mov    (%rax),%esi
mov    0x2cf0(%rip),%rax
or     (%rax),%esi
mov    0x2cbf(%rip),%rax
or     (%rax),%esi
mov    $0x180,%edx
call   10c0 <open@plt>
mov    %eax,-0x824(%rbp)
cmp    $0xffffffff,%eax
jne    1344 <factertofile+0x124>
lea    0xd1d(%rip),%rdi
call   10e0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    14a2 <factertofile+0x282>
mov    $0x0,%al
call   1140 <fork@plt>
mov    %eax,-0x828(%rbp)
cmpl   $0x0,-0x828(%rbp)
jne    1392 <factertofile+0x172>
mov    -0x824(%rbp),%edi
mov    0x2c7d(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
mov    -0x824(%rbp),%edi
call   1080 <close@plt>
lea    0xcd1(%rip),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1120 <execl@plt>
jmp    139d <factertofile+0x17d>
mov    -0x828(%rbp),%edi
call   1130 <wait@plt>
lea    -0x610(%rbp),%rdi
lea    0xcb8(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x818(%rbp)
lea    -0x810(%rbp),%rdi
lea    0xc62(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x820(%rbp)
lea    -0x410(%rbp),%rdi
mov    -0x818(%rbp),%rdx
mov    $0x400,%esi
call   10a0 <fgets@plt>
lea    -0x410(%rbp),%rdi
lea    0xc6e(%rip),%rsi
call   10f0 <strtok@plt>
mov    %rax,-0x830(%rbp)
mov    -0x830(%rbp),%rdi
mov    -0x820(%rbp),%rsi
call   1070 <fputs@plt>
mov    -0x820(%rbp),%rsi
mov    $0x3d,%edi
call   1090 <fputc@plt>
xor    %eax,%eax
mov    %eax,%edi
lea    0xc33(%rip),%rsi
call   10f0 <strtok@plt>
mov    %rax,-0x830(%rbp)
mov    -0x830(%rbp),%rdi
mov    -0x820(%rbp),%rsi
call   1070 <fputs@plt>
lea    -0x410(%rbp),%rdi
mov    -0x818(%rbp),%rdx
mov    $0x400,%esi
call   10a0 <fgets@plt>
mov    -0x818(%rbp),%rdi
call   10b0 <feof@plt>
cmp    $0x1,%eax
jne    13e9 <factertofile+0x1c9>
mov    -0x818(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x820(%rbp),%rdi
call   1040 <fclose@plt>
lea    -0x610(%rbp),%rdi
call   1030 <remove@plt>
mov    -0x4(%rbp),%eax
add    $0x830,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<factertofile>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x808,%rsp
mov    %rdi,%rbx
lea    0xdc8(%rip),%rsi
mov    %rsp,%r14
mov    %r14,%rdi
mov    %rbx,%rdx
call   1100 <sprintf@plt>
lea    0xdc7(%rip),%rsi
lea    0x200(%rsp),%rdi
mov    %rbx,%rdx
call   1100 <sprintf@plt>
lea    0xdc3(%rip),%rsi
mov    %r14,%rdi
call   10d0 <fopen@plt>
test   %rax,%rax
je     13ef <factertofile+0x1cf>
mov    %rax,%rbx
lea    0xda8(%rip),%rsi
lea    0x200(%rsp),%rdi
call   10d0 <fopen@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %r14,%rdi
call   1050 <fclose@plt>
mov    0x2d32(%rip),%rax
mov    0x2d4b(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2d18(%rip),%rax
or     (%rax),%esi
mov    %rsp,%rdi
mov    $0x180,%edx
call   10c0 <open@plt>
cmp    $0xffffffff,%eax
je     13e1 <factertofile+0x1c1>
mov    %eax,%ebx
xor    %eax,%eax
call   1140 <fork@plt>
test   %eax,%eax
je     12e6 <factertofile+0xc6>
mov    %eax,%edi
call   1130 <wait@plt>
jmp    130b <factertofile+0xeb>
mov    0x2cfb(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1060 <dup2@plt>
mov    %ebx,%edi
call   1080 <close@plt>
lea    0xd2a(%rip),%rdi
xor    %esi,%esi
call   1120 <execl@plt>
lea    0xd2a(%rip),%rsi
mov    %rsp,%rdi
call   10d0 <fopen@plt>
mov    %rax,%r14
lea    0xd03(%rip),%rsi
lea    0x200(%rsp),%rdi
call   10d0 <fopen@plt>
mov    %rax,%rbx
lea    0x400(%rsp),%r15
mov    %r15,%rdi
mov    $0x400,%esi
mov    %r14,%rdx
call   10a0 <fgets@plt>
lea    0xceb(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r15,%rdi
mov    %r12,%rsi
call   10f0 <strtok@plt>
mov    %rax,%rdi
mov    %rbx,%rsi
call   1070 <fputs@plt>
mov    $0x3d,%edi
mov    %rbx,%rsi
call   1090 <fputc@plt>
xor    %edi,%edi
mov    %r12,%rsi
call   10f0 <strtok@plt>
mov    %rax,%rdi
mov    %rbx,%rsi
call   1070 <fputs@plt>
mov    %r15,%rdi
mov    $0x400,%esi
mov    %r14,%rdx
call   10a0 <fgets@plt>
mov    %r14,%rdi
call   10b0 <feof@plt>
cmp    $0x1,%eax
jne    1360 <factertofile+0x140>
mov    %r14,%rdi
call   1050 <fclose@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %rsp,%rdi
call   1030 <remove@plt>
mov    $0x1,%eax
add    $0x808,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xc41(%rip),%rdi
call   10e0 <perror@plt>
jmp    13cd <factertofile+0x1ad>
lea    0xc4d(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   1110 <exit@plt>

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

LLVM IR (-O2):
@.str = [20 x i8] c"%s/facts/.common.ft\00"
@.str.1 = [19 x i8] c"%s/facts/common.ft\00"
@.str.2 = [2 x i8] c"w\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_SYNC = global i32 0
@.str.4 = [5 x i8] c"open\00"
@STDOUT_FILENO = global i32 0
@.str.5 = [16 x i8] c"/usr/bin/facter\00"
@.str.6 = [2 x i8] c"r\00"
@.str.7 = [5 x i8] c" => \00"
@str = [38 x i8] c"Error: can't write to facts directory\00"
define i32 @factertofile(i8* %0) {
%2 = alloca [1024 x i8]
%3 = alloca [512 x i8]
%4 = alloca [512 x i8]
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%6 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%7 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%8 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %0)
%9 = call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%10 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %15
12: ; preds = %1
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str, i64 0, i64 0))
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %1
%16 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%17 = tail call i32 @fclose(i32* %10)
%18 = tail call i32 @fclose(i32* %16)
%19 = load i32, i32* @O_RDWR
%20 = load i32, i32* @O_CREAT
%21 = or i32 %20, %19
%22 = load i32, i32* @O_SYNC
%23 = or i32 %21, %22
%24 = call i32 @open(i8* %6, i32 %23, i32 384)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %28
26: ; preds = %15
%27 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
br label %55
28: ; preds = %15
%29 = tail call i32 (...) @fork()
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %36
31: ; preds = %28
%32 = load i32, i32* @STDOUT_FILENO
%33 = tail call i32 @dup2(i32 %24, i32 %32)
%34 = tail call i32 @close(i32 %24)
%35 = tail call i32 @execl(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), i8* null)
br label %38
36: ; preds = %28
%37 = tail call i32 @wait(i32 %29)
br label %38
38: ; preds = %36, %31
%39 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
%40 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%41 = call i32 @fgets(i8* %5, i32 1024, i32* %39)
br label %42
42: ; preds = %42, %38
%43 = call i8* @strtok(i8* %5, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0))
%44 = call i32 @fputs(i8* %43, i32* %40)
%45 = call i32 @fputc(i8 signext 61, i32* %40)
%46 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0))
%47 = call i32 @fputs(i8* %46, i32* %40)
%48 = call i32 @fgets(i8* %5, i32 1024, i32* %39)
%49 = call i32 @feof(i32* %39)
%50 = icmp eq i32 %49, 1
br i1 %50, label %51, label %42
51: ; preds = %42
%52 = call i32 @fclose(i32* %39)
%53 = call i32 @fclose(i32* %40)
%54 = call i32 @remove(i8* %6)
br label %55
55: ; preds = %51, %26
ret i32 1
}