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

/* Variables and functions */
int /*<<< orphan*/ exit(int);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ *open_log_out();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ setbuf(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ *stdout;
char *strtok(char *, char *);
int main(int argc, char **argv) {
  FILE *log = fopen("/dev/syslog", "r");
  if (!log) {
    fprintf(stderr, "failed to open /dev/syslog file\n");
    exit(1);
  }
  printf("Started system logger\n");
  FILE *out = open_log_out();
  if (out)
    setbuf(out, NULL);
  setbuf(stdout, NULL);
  char buffer[4096];
  char ident[32];
  int pri;
  while (true) {
    if (fgets(buffer, sizeof(buffer), log) > 0) {
      if (buffer[0] == '<') {
        char *logentry = &buffer[1];
        char *pristr = strtok(logentry, ":");
        char *ident = strtok(NULL, ">");
        char *message = strtok(NULL, "");
        printf("[%s]: %s", ident, message);
        if (out)
          fprintf(out, "[%s]: %s", ident, message);
      } else {
        printf("%s", buffer);
        if (out)
          fprintf(out, "%s", buffer);
      }
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1070,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe60(%rip),%rdi
lea    0xe65(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11dd <main+0x5d>
mov    0x2e1e(%rip),%rax
movslq (%rax),%rdi
lea    0xe42(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xe4b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1070 <open_log_out@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     120e <main+0x8e>
mov    -0x20(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <setbuf@plt>
mov    0x2dc3(%rip),%rax
mov    (%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <setbuf@plt>
lea    -0x1020(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x1000,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
jle    1339 <main+0x1b9>
movsbl -0x1020(%rbp),%eax
cmp    $0x3c,%eax
jne    12f6 <main+0x176>
lea    -0x1020(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x1050(%rbp)
mov    -0x1050(%rbp),%rdi
lea    0xdd6(%rip),%rsi
call   1090 <strtok@plt>
mov    %rax,-0x1058(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xdc1(%rip),%rsi
call   1090 <strtok@plt>
mov    %rax,-0x1060(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xd90(%rip),%rsi
call   1090 <strtok@plt>
mov    %rax,-0x1068(%rbp)
mov    -0x1060(%rbp),%rsi
mov    -0x1068(%rbp),%rdx
lea    0xd8b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpq   $0x0,-0x20(%rbp)
je     12f1 <main+0x171>
mov    -0x20(%rbp),%rdi
mov    -0x1060(%rbp),%rdx
mov    -0x1068(%rbp),%rcx
lea    0xd60(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    1334 <main+0x1b4>
lea    -0x1020(%rbp),%rsi
lea    0xd4c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpq   $0x0,-0x20(%rbp)
je     132f <main+0x1af>
mov    -0x20(%rbp),%rdi
lea    -0x1020(%rbp),%rdx
lea    0xd28(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    1334 <main+0x1b4>
jmp    1339 <main+0x1b9>
jmp    1221 <main+0xa1>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1008,%rsp
lea    0xe38(%rip),%rdi
lea    0xe7f(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     12ca <main+0x11a>
mov    %rax,%r14
lea    0xe53(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1090 <open_log_out@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1205 <main+0x55>
mov    %rbx,%rdi
xor    %esi,%esi
call   1040 <setbuf@plt>
mov    0x2dcc(%rip),%rax
mov    (%rax),%rdi
xor    %esi,%esi
call   1040 <setbuf@plt>
mov    %rsp,%r12
lea    0xe11(%rip),%r13
mov    %r12,%rdi
mov    $0x1000,%esi
mov    %r14,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
jle    1220 <main+0x70>
cmpb   $0x3c,(%rsp)
jne    12a0 <main+0xf0>
lea    0x1(%rsp),%rdi
lea    0xde6(%rip),%rsi
call   10b0 <strtok@plt>
xor    %edi,%edi
lea    0xdda(%rip),%rsi
call   10b0 <strtok@plt>
mov    %rax,%r15
xor    %edi,%edi
lea    0xdc6(%rip),%rsi
call   10b0 <strtok@plt>
mov    %rax,%rbp
mov    %r13,%rdi
mov    %r15,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
test   %rbx,%rbx
je     1220 <main+0x70>
mov    %rbx,%rdi
mov    %r13,%rsi
mov    %r15,%rdx
mov    %rbp,%rcx
xor    %eax,%eax
call   1080 <fprintf@plt>
jmp    1220 <main+0x70>
nopl   0x0(%rax,%rax,1)
lea    0xd90(%rip),%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
test   %rbx,%rbx
je     1220 <main+0x70>
mov    %r12,%rdi
mov    %rbx,%rsi
call   1060 <fputs@plt>
jmp    1220 <main+0x70>
mov    0x2d0f(%rip),%rax
movslq (%rax),%rcx
lea    0xd31(%rip),%rdi
mov    $0x20,%esi
mov    $0x1,%edx
call   10d0 <fwrite@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [12 x i8] c"/dev/syslog\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [33 x i8] c"failed to open /dev/syslog file\0A\00"
@.str.3 = [23 x i8] c"Started system logger\0A\00"
@stdout = global i32* null
@.str.4 = [2 x i8] c":\00"
@.str.5 = [2 x i8] c">\00"
@.str.6 = [1 x i8] zeroinitializer
@.str.7 = [9 x i8] c"[%s]: %s\00"
@.str.8 = [3 x i8] c"%s\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca [4096 x i8]
%9 = alloca [32 x i8]
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%15 = call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %15, i32** %6
%16 = load i32*, i32** %6
%17 = icmp ne i32* %16, null
br i1 %17, label %24, label %18
18: ; preds = %2
%19 = load i32, i32* @stderr
%20 = sext i32 %19 to i64
%21 = inttoptr i64 %20 to i32*
%22 = call i32 (i32*, i8*, ...) @fprintf(i32* %21, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %2
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
%26 = call i32* (...) @open_log_out()
store i32* %26, i32** %7
%27 = load i32*, i32** %7
%28 = icmp ne i32* %27, null
br i1 %28, label %29, label %32
29: ; preds = %24
%30 = load i32*, i32** %7
%31 = call i32 @setbuf(i32* %30, i32* null)
br label %32
32: ; preds = %29, %24
%33 = load i32*, i32** @stdout
%34 = call i32 @setbuf(i32* %33, i32* null)
br label %35
35: ; preds = %32, %73
%36 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%37 = load i32*, i32** %6
%38 = call i64 @fgets(i8* %36, i32 4096, i32* %37)
%39 = icmp sgt i64 %38, 0
br i1 %39, label %40, label %73
40: ; preds = %35
%41 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%42 = load i8, i8* %41
%43 = sext i8 %42 to i32
%44 = icmp eq i32 %43, 60
br i1 %44, label %45, label %62
45: ; preds = %40
%46 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 1
store i8* %46, i8** %11
%47 = load i8*, i8** %11
%48 = call i8* @strtok(i8* %47, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i8* %48, i8** %12
%49 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i8* %49, i8** %13
%50 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.6, i64 0, i64 0))
store i8* %50, i8** %14
%51 = load i8*, i8** %13
%52 = load i8*, i8** %14
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0), i8* %51, i8* %52)
%54 = load i32*, i32** %7
%55 = icmp ne i32* %54, null
br i1 %55, label %56, label %61
56: ; preds = %45
%57 = load i32*, i32** %7
%58 = load i8*, i8** %13
%59 = load i8*, i8** %14
%60 = call i32 (i32*, i8*, ...) @fprintf(i32* %57, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0), i8* %58, i8* %59)
br label %61
61: ; preds = %56, %45
br label %72
62: ; preds = %40
%63 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0), i8* %63)
%65 = load i32*, i32** %7
%66 = icmp ne i32* %65, null
br i1 %66, label %67, label %71
67: ; preds = %62
%68 = load i32*, i32** %7
%69 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%70 = call i32 (i32*, i8*, ...) @fprintf(i32* %68, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0), i8* %69)
br label %71
71: ; preds = %67, %62
br label %72
72: ; preds = %71, %61
br label %73
73: ; preds = %72, %35
br label %35
}

LLVM IR (-O2):
@.str = [12 x i8] c"/dev/syslog\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [33 x i8] c"failed to open /dev/syslog file\0A\00"
@stdout = global i32* null
@.str.4 = [2 x i8] c":\00"
@.str.5 = [2 x i8] c">\00"
@.str.6 = [1 x i8] zeroinitializer
@.str.7 = [9 x i8] c"[%s]: %s\00"
@.str.8 = [3 x i8] c"%s\00"
@str = [22 x i8] c"Started system logger\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [4096 x i8]
%4 = tail call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %12
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = sext i32 %7 to i64
%9 = inttoptr i64 %8 to i32*
%10 = tail call i64 @fwrite(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i64 32, i64 1, i32* %9)
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %2
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
%14 = tail call i32* (...) @open_log_out()
%15 = icmp eq i32* %14, null
br i1 %15, label %18, label %16
16: ; preds = %12
%17 = tail call i32 @setbuf(i32* %14, i32* null)
br label %18
18: ; preds = %16, %12
%19 = load i32*, i32** @stdout
%20 = tail call i32 @setbuf(i32* %19, i32* null)
%21 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%22 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 1
br label %23
23: ; preds = %34, %18
%24 = call i64 @fgets(i8* %21, i32 4096, i32* %4)
%25 = icmp sgt i64 %24, 0
br i1 %25, label %26, label %34
26: ; preds = %23
%27 = load i8, i8* %21
%28 = icmp eq i8 %27, 60
br i1 %28, label %29, label %37
29: ; preds = %26
%30 = call i8* @strtok(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%31 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%32 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.6, i64 0, i64 0))
%33 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0), i8* %31, i8* %32)
br i1 %15, label %34, label %35
34: ; preds = %29, %35, %39, %37, %23
br label %23
35: ; preds = %29
%36 = call i32 (i32*, i8*, ...) @fprintf(i32* %14, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0), i8* %31, i8* %32)
br label %34
37: ; preds = %26
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0), i8* %21)
br i1 %15, label %34, label %39
39: ; preds = %37
%40 = call i32 @fputs(i8* %21, i32* %14)
br label %34
}