#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*/ unixs;
struct sockaddr_un {
  int dummy;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ sockaddr;
typedef int /*<<< orphan*/ inbuf;
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int /*<<< orphan*/ FD_CLR(int, int /*<<< orphan*/ *);
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int O_CREAT;
int O_RDONLY;
int O_WRONLY;
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
scalar_t__ fork();
int /*<<< orphan*/ klogctl(int, int /*<<< orphan*/ *, int);
int logChunk(int, char *, char *);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/ *, int);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ printstr(char *);
int read(int, char *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ sleep(int);
int write(int, char *, int);
void doklog(char *fn) {
  fd_set readset, unixs;
  int in, out, i;
  int log;
  socklen_t s;
  int sock = -1;
  struct sockaddr_un sockaddr;
  char inbuf[1024], outbuf[1024];
  int readfd;
  int ret;
  in = open("/proc/kmsg", O_RDONLY, 0);
  if (in < 0) {
    printstr("open /proc/kmsg");
    return;
  }
  out = open(fn, O_WRONLY, 0);
  if (out < 0)
    printf("couldn't open %s for syslog -- still using /tmp/syslog\n", fn);
  log = open("/tmp/syslog", O_WRONLY | O_CREAT, 0644);
  if (log < 0) {
    printstr("error opening /tmp/syslog");
    sleep(5);
    close(in);
    return;
  }
  if (fork()) {
    close(in);
    close(out);
    close(log);
    return;
  }
  close(0);
  close(1);
  close(2);
  dup2(1, log);
  klogctl(8, NULL, 1);
  FD_ZERO(&unixs);
  while (1) {
    memcpy(&readset, &unixs, sizeof(unixs));
    if (sock >= 0)
      FD_SET(sock, &readset);
    FD_SET(in, &readset);
    i = select(20, &readset, NULL, NULL, NULL);
    if (i <= 0)
      continue;
    if (FD_ISSET(in, &readset)) {
      i = read(in, inbuf, sizeof(inbuf));
      if (i > 0) {
        int loggedLen = logChunk(i, inbuf, outbuf);
        if (out >= 0)
          ret = write(out, outbuf, loggedLen);
        ret = write(log, outbuf, loggedLen);
      }
    }
    for (readfd = 0; readfd < 20; ++readfd) {
      if (FD_ISSET(readfd, &readset) && FD_ISSET(readfd, &unixs)) {
        i = read(readfd, inbuf, sizeof(inbuf));
        if (i > 0) {
          int loggedLen = logChunk(i, inbuf, outbuf);
          if (out >= 0)
            ret = write(out, outbuf, loggedLen);
          ret = write(log, outbuf, loggedLen);
        } else if (i == 0) {
          close(readfd);
          FD_CLR(readfd, &unixs);
        }
      }
    }
    if (sock >= 0 && FD_ISSET(sock, &readset)) {
      s = sizeof(sockaddr);
      readfd = accept(sock, (struct sockaddr *)&sockaddr, &s);
      if (readfd < 0) {
        if (out >= 0)
          ret = write(out, "error in accept\n", 16);
        ret = write(log, "error in accept\n", 16);
        close(sock);
        sock = -1;
      } else {
        FD_SET(readfd, &unixs);
      }
    }
  }
}


x86 assembly (clang, -O0):
<doklog>:
push   %rbp
mov    %rsp,%rbp
sub    $0x840,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0xffffffff,-0x28(%rbp)
mov    0x2da3(%rip),%rax
mov    (%rax),%esi
lea    0xdbf(%rip),%rdi
xor    %edx,%edx
call   10e0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    126b <doklog+0x4b>
lea    0xd9f(%rip),%rdi
call   1090 <printstr@plt>
jmp    1622 <doklog+0x402>
mov    -0x8(%rbp),%rdi
mov    0x2d52(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    129c <doklog+0x7c>
mov    -0x8(%rbp),%rsi
lea    0xd79(%rip),%rdi
call   1060 <printf@plt>
mov    0x2d25(%rip),%rax
mov    (%rax),%esi
mov    0x2d4c(%rip),%rax
or     (%rax),%esi
lea    0xda1(%rip),%rdi
mov    $0x1a4,%edx
call   10e0 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    12ef <doklog+0xcf>
lea    0xd75(%rip),%rdi
call   1090 <printstr@plt>
mov    $0x5,%edi
call   1130 <sleep@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
jmp    1622 <doklog+0x402>
mov    $0x0,%al
call   1140 <fork@plt>
cmp    $0x0,%rax
je     131d <doklog+0xfd>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
mov    -0x20(%rbp),%edi
call   1070 <close@plt>
jmp    1622 <doklog+0x402>
xor    %edi,%edi
call   1070 <close@plt>
mov    $0x1,%edi
call   1070 <close@plt>
mov    $0x2,%edi
call   1070 <close@plt>
mov    -0x20(%rbp),%esi
mov    $0x1,%edi
call   1050 <dup2@plt>
mov    $0x8,%edi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x1,%edx
call   10d0 <klogctl@plt>
lea    -0x10(%rbp),%rdi
call   10b0 <FD_ZERO@plt>
lea    -0xc(%rbp),%rdi
lea    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   10a0 <memcpy@plt>
cmpl   $0x0,-0x28(%rbp)
jl     1389 <doklog+0x169>
mov    -0x28(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1120 <FD_SET@plt>
mov    -0x14(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1120 <FD_SET@plt>
mov    $0x14,%edi
lea    -0xc(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   10c0 <select@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jg     13c0 <doklog+0x1a0>
jmp    1361 <doklog+0x141>
mov    -0x14(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1458 <doklog+0x238>
mov    -0x14(%rbp),%edi
lea    -0x430(%rbp),%rsi
mov    $0x400,%edx
call   1080 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    1453 <doklog+0x233>
mov    -0x1c(%rbp),%edi
lea    -0x430(%rbp),%rsi
lea    -0x830(%rbp),%rdx
call   1110 <logChunk@plt>
mov    %eax,-0x83c(%rbp)
cmpl   $0x0,-0x18(%rbp)
jl     1438 <doklog+0x218>
mov    -0x18(%rbp),%edi
lea    -0x830(%rbp),%rsi
mov    -0x83c(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x838(%rbp)
mov    -0x20(%rbp),%edi
lea    -0x830(%rbp),%rsi
mov    -0x83c(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x838(%rbp)
jmp    1458 <doklog+0x238>
movl   $0x0,-0x834(%rbp)
cmpl   $0x14,-0x834(%rbp)
jge    156d <doklog+0x34d>
mov    -0x834(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1554 <doklog+0x334>
mov    -0x834(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1554 <doklog+0x334>
mov    -0x834(%rbp),%edi
lea    -0x430(%rbp),%rsi
mov    $0x400,%edx
call   1080 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    1526 <doklog+0x306>
mov    -0x1c(%rbp),%edi
lea    -0x430(%rbp),%rsi
lea    -0x830(%rbp),%rdx
call   1110 <logChunk@plt>
mov    %eax,-0x840(%rbp)
cmpl   $0x0,-0x18(%rbp)
jl     1506 <doklog+0x2e6>
mov    -0x18(%rbp),%edi
lea    -0x830(%rbp),%rsi
mov    -0x840(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x838(%rbp)
mov    -0x20(%rbp),%edi
lea    -0x830(%rbp),%rsi
mov    -0x840(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x838(%rbp)
jmp    154f <doklog+0x32f>
cmpl   $0x0,-0x1c(%rbp)
jne    154a <doklog+0x32a>
mov    -0x834(%rbp),%edi
call   1070 <close@plt>
mov    -0x834(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1100 <FD_CLR@plt>
jmp    154f <doklog+0x32f>
jmp    1554 <doklog+0x334>
jmp    1559 <doklog+0x339>
mov    -0x834(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x834(%rbp)
jmp    1462 <doklog+0x242>
cmpl   $0x0,-0x28(%rbp)
jl     161d <doklog+0x3fd>
mov    -0x28(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     161d <doklog+0x3fd>
movl   $0x4,-0x24(%rbp)
mov    -0x28(%rbp),%edi
lea    -0x30(%rbp),%rsi
lea    -0x24(%rbp),%rdx
call   10f0 <accept@plt>
mov    %eax,-0x834(%rbp)
cmpl   $0x0,-0x834(%rbp)
jge    1609 <doklog+0x3e9>
cmpl   $0x0,-0x18(%rbp)
jl     15db <doklog+0x3bb>
mov    -0x18(%rbp),%edi
lea    0xa97(%rip),%rsi
mov    $0x10,%edx
call   1030 <write@plt>
mov    %eax,-0x838(%rbp)
mov    -0x20(%rbp),%edi
lea    0xa7d(%rip),%rsi
mov    $0x10,%edx
call   1030 <write@plt>
mov    %eax,-0x838(%rbp)
mov    -0x28(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x28(%rbp)
jmp    1618 <doklog+0x3f8>
mov    -0x834(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1120 <FD_SET@plt>
jmp    161d <doklog+0x3fd>
jmp    1361 <doklog+0x141>
add    $0x840,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<doklog>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x828,%rsp
mov    %rdi,%rbx
mov    0x2db5(%rip),%rax
mov    (%rax),%esi
lea    0xdd1(%rip),%rdi
xor    %edx,%edx
call   10e0 <open@plt>
mov    %eax,0xc(%rsp)
test   %eax,%eax
js     12b3 <doklog+0xa3>
mov    0x2d7e(%rip),%r14
mov    (%r14),%esi
mov    %rbx,%rdi
xor    %edx,%edx
call   10e0 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    126c <doklog+0x5c>
lea    0xdac(%rip),%rdi
mov    %rbx,%rsi
call   1060 <printf@plt>
mov    0x2d85(%rip),%rax
mov    (%rax),%esi
or     (%r14),%esi
lea    0xdd7(%rip),%rdi
mov    $0x1a4,%edx
call   10e0 <open@plt>
mov    %eax,0x10(%rsp)
test   %eax,%eax
js     12d0 <doklog+0xc0>
xor    %eax,%eax
call   1130 <fork@plt>
test   %rax,%rax
je     1300 <doklog+0xf0>
mov    0xc(%rsp),%edi
call   1070 <close@plt>
mov    %ebp,%edi
call   1070 <close@plt>
mov    0x10(%rsp),%edi
jmp    12ea <doklog+0xda>
lea    0xd46(%rip),%rdi
add    $0x828,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1090 <printstr@plt>
lea    0xd71(%rip),%rdi
call   1090 <printstr@plt>
mov    $0x5,%edi
call   1120 <sleep@plt>
mov    0xc(%rsp),%edi
add    $0x828,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <close@plt>
mov    %ebp,0x14(%rsp)
xor    %edi,%edi
call   1070 <close@plt>
mov    $0x1,%edi
call   1070 <close@plt>
mov    $0x2,%edi
call   1070 <close@plt>
mov    $0x1,%edi
mov    0x10(%rsp),%esi
call   1050 <dup2@plt>
mov    $0x8,%edi
xor    %esi,%esi
mov    $0x1,%edx
call   10d0 <klogctl@plt>
lea    0x18(%rsp),%rbx
mov    %rbx,%rdi
call   10b0 <FD_ZERO@plt>
lea    0x1c(%rsp),%rbp
lea    0x420(%rsp),%r13
lea    0x20(%rsp),%r14
nopl   (%rax)
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    $0x4,%edx
call   10a0 <memcpy@plt>
mov    0xc(%rsp),%edi
mov    %rbp,%rsi
call   1110 <FD_SET@plt>
mov    $0x14,%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   10c0 <select@plt>
test   %eax,%eax
jle    1360 <doklog+0x150>
mov    0xc(%rsp),%edi
mov    %rbp,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     13ec <doklog+0x1dc>
mov    0xc(%rsp),%edi
mov    %r13,%rsi
mov    $0x400,%edx
call   1080 <read@plt>
test   %eax,%eax
jle    13ec <doklog+0x1dc>
mov    %eax,%edi
mov    %r13,%rsi
mov    %r14,%rdx
call   1100 <logChunk@plt>
mov    %eax,%r15d
mov    0x14(%rsp),%edi
test   %edi,%edi
js     13dd <doklog+0x1cd>
mov    %r14,%rsi
mov    %r15d,%edx
call   1030 <write@plt>
mov    0x10(%rsp),%edi
mov    %r14,%rsi
mov    %r15d,%edx
call   1030 <write@plt>
xor    %r15d,%r15d
jmp    141d <doklog+0x20d>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    0x10(%rsp),%edi
mov    %r14,%rsi
mov    %r12d,%edx
call   1030 <write@plt>
add    $0x1,%r15d
cmp    $0x14,%r15d
je     1360 <doklog+0x150>
mov    %r15d,%edi
mov    %rbp,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     140f <doklog+0x1ff>
mov    %r15d,%edi
mov    %rbx,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     140f <doklog+0x1ff>
mov    %r15d,%edi
mov    %r13,%rsi
mov    $0x400,%edx
call   1080 <read@plt>
test   %eax,%eax
jle    1476 <doklog+0x266>
mov    %eax,%edi
mov    %r13,%rsi
mov    %r14,%rdx
call   1100 <logChunk@plt>
mov    %eax,%r12d
mov    0x14(%rsp),%edi
test   %edi,%edi
js     1400 <doklog+0x1f0>
mov    %r14,%rsi
mov    %r12d,%edx
call   1030 <write@plt>
jmp    1400 <doklog+0x1f0>
jne    140f <doklog+0x1ff>
mov    %r15d,%edi
call   1070 <close@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
call   10f0 <FD_CLR@plt>
jmp    140f <doklog+0x1ff>

LLVM IR (-O0):
%struct.sockaddr_un = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [11 x i8] c"/proc/kmsg\00"
@O_RDONLY = global i32 0
@.str.1 = [16 x i8] c"open /proc/kmsg\00"
@O_WRONLY = global i32 0
@.str.2 = [56 x i8] c"couldn't open %s for syslog -- still using /tmp/syslog\0A\00"
@.str.3 = [12 x i8] c"/tmp/syslog\00"
@O_CREAT = global i32 0
@.str.4 = [26 x i8] c"error opening /tmp/syslog\00"
@.str.5 = [17 x i8] c"error in accept\0A\00"
define void @doklog(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca %struct.sockaddr_un
%12 = alloca [1024 x i8]
%13 = alloca [1024 x i8]
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
store i8* %0, i8** %2
store i32 -1, i32* %10
%18 = load i32, i32* @O_RDONLY
%19 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %18, i32 0)
store i32 %19, i32* %5
%20 = load i32, i32* %5
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %1
%23 = call i32 @printstr(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %183
24: ; preds = %1
%25 = load i8*, i8** %2
%26 = load i32, i32* @O_WRONLY
%27 = call i32 @open(i8* %25, i32 %26, i32 0)
store i32 %27, i32* %6
%28 = load i32, i32* %6
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %24
%31 = load i8*, i8** %2
%32 = call i32 @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.2, i64 0, i64 0), i8* %31)
br label %33
33: ; preds = %30, %24
%34 = load i32, i32* @O_WRONLY
%35 = load i32, i32* @O_CREAT
%36 = or i32 %34, %35
%37 = call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %36, i32 420)
store i32 %37, i32* %8
%38 = load i32, i32* %8
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %45
40: ; preds = %33
%41 = call i32 @printstr(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
%42 = call i32 @sleep(i32 5)
%43 = load i32, i32* %5
%44 = call i32 @close(i32 %43)
br label %183
45: ; preds = %33
%46 = call i64 (...) @fork()
%47 = icmp ne i64 %46, 0
br i1 %47, label %48, label %55
48: ; preds = %45
%49 = load i32, i32* %5
%50 = call i32 @close(i32 %49)
%51 = load i32, i32* %6
%52 = call i32 @close(i32 %51)
%53 = load i32, i32* %8
%54 = call i32 @close(i32 %53)
br label %183
55: ; preds = %45
%56 = call i32 @close(i32 0)
%57 = call i32 @close(i32 1)
%58 = call i32 @close(i32 2)
%59 = load i32, i32* %8
%60 = call i32 @dup2(i32 1, i32 %59)
%61 = call i32 @klogctl(i32 8, i32* null, i32 1)
%62 = call i32 @FD_ZERO(i32* %4)
br label %63
63: ; preds = %55, %76, %182
%64 = call i32 @memcpy(i32* %3, i32* %4, i32 4)
%65 = load i32, i32* %10
%66 = icmp sge i32 %65, 0
br i1 %66, label %67, label %70
67: ; preds = %63
%68 = load i32, i32* %10
%69 = call i32 @FD_SET(i32 %68, i32* %3)
br label %70
70: ; preds = %67, %63
%71 = load i32, i32* %5
%72 = call i32 @FD_SET(i32 %71, i32* %3)
%73 = call i32 @select(i32 20, i32* %3, i32* null, i32* null, i32* null)
store i32 %73, i32* %7
%74 = load i32, i32* %7
%75 = icmp sle i32 %74, 0
br i1 %75, label %76, label %77
76: ; preds = %70
br label %63
77: ; preds = %70
%78 = load i32, i32* %5
%79 = call i64 @FD_ISSET(i32 %78, i32* %3)
%80 = icmp ne i64 %79, 0
br i1 %80, label %81, label %105
81: ; preds = %77
%82 = load i32, i32* %5
%83 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%84 = call i32 @read(i32 %82, i8* %83, i32 1024)
store i32 %84, i32* %7
%85 = load i32, i32* %7
%86 = icmp sgt i32 %85, 0
br i1 %86, label %87, label %104
87: ; preds = %81
%88 = load i32, i32* %7
%89 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%90 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%91 = call i32 @logChunk(i32 %88, i8* %89, i8* %90)
store i32 %91, i32* %16
%92 = load i32, i32* %6
%93 = icmp sge i32 %92, 0
br i1 %93, label %94, label %99
94: ; preds = %87
%95 = load i32, i32* %6
%96 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%97 = load i32, i32* %16
%98 = call i32 @write(i32 %95, i8* %96, i32 %97)
store i32 %98, i32* %15
br label %99
99: ; preds = %94, %87
%100 = load i32, i32* %8
%101 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%102 = load i32, i32* %16
%103 = call i32 @write(i32 %100, i8* %101, i32 %102)
store i32 %103, i32* %15
br label %104
104: ; preds = %99, %81
br label %105
105: ; preds = %104, %77
store i32 0, i32* %14
br label %106
106: ; preds = %151, %105
%107 = load i32, i32* %14
%108 = icmp slt i32 %107, 20
br i1 %108, label %109, label %154
109: ; preds = %106
%110 = load i32, i32* %14
%111 = call i64 @FD_ISSET(i32 %110, i32* %3)
%112 = icmp ne i64 %111, 0
br i1 %112, label %113, label %150
113: ; preds = %109
%114 = load i32, i32* %14
%115 = call i64 @FD_ISSET(i32 %114, i32* %4)
%116 = icmp ne i64 %115, 0
br i1 %116, label %117, label %150
117: ; preds = %113
%118 = load i32, i32* %14
%119 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%120 = call i32 @read(i32 %118, i8* %119, i32 1024)
store i32 %120, i32* %7
%121 = load i32, i32* %7
%122 = icmp sgt i32 %121, 0
br i1 %122, label %123, label %140
123: ; preds = %117
%124 = load i32, i32* %7
%125 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%126 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%127 = call i32 @logChunk(i32 %124, i8* %125, i8* %126)
store i32 %127, i32* %17
%128 = load i32, i32* %6
%129 = icmp sge i32 %128, 0
br i1 %129, label %130, label %135
130: ; preds = %123
%131 = load i32, i32* %6
%132 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%133 = load i32, i32* %17
%134 = call i32 @write(i32 %131, i8* %132, i32 %133)
store i32 %134, i32* %15
br label %135
135: ; preds = %130, %123
%136 = load i32, i32* %8
%137 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%138 = load i32, i32* %17
%139 = call i32 @write(i32 %136, i8* %137, i32 %138)
store i32 %139, i32* %15
br label %149
140: ; preds = %117
%141 = load i32, i32* %7
%142 = icmp eq i32 %141, 0
br i1 %142, label %143, label %148
143: ; preds = %140
%144 = load i32, i32* %14
%145 = call i32 @close(i32 %144)
%146 = load i32, i32* %14
%147 = call i32 @FD_CLR(i32 %146, i32* %4)
br label %148
148: ; preds = %143, %140
br label %149
149: ; preds = %148, %135
br label %150
150: ; preds = %149, %113, %109
br label %151
151: ; preds = %150
%152 = load i32, i32* %14
%153 = add nsw i32 %152, 1
store i32 %153, i32* %14
br label %106
154: ; preds = %106
%155 = load i32, i32* %10
%156 = icmp sge i32 %155, 0
br i1 %156, label %157, label %182
157: ; preds = %154
%158 = load i32, i32* %10
%159 = call i64 @FD_ISSET(i32 %158, i32* %3)
%160 = icmp ne i64 %159, 0
br i1 %160, label %161, label %182
161: ; preds = %157
store i32 4, i32* %9
%162 = load i32, i32* %10
%163 = bitcast %struct.sockaddr_un* %11 to %struct.sockaddr*
%164 = call i32 @accept(i32 %162, %struct.sockaddr* %163, i32* %9)
store i32 %164, i32* %14
%165 = load i32, i32* %14
%166 = icmp slt i32 %165, 0
br i1 %166, label %167, label %178
167: ; preds = %161
%168 = load i32, i32* %6
%169 = icmp sge i32 %168, 0
br i1 %169, label %170, label %173
170: ; preds = %167
%171 = load i32, i32* %6
%172 = call i32 @write(i32 %171, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0), i32 16)
store i32 %172, i32* %15
br label %173
173: ; preds = %170, %167
%174 = load i32, i32* %8
%175 = call i32 @write(i32 %174, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0), i32 16)
store i32 %175, i32* %15
%176 = load i32, i32* %10
%177 = call i32 @close(i32 %176)
store i32 -1, i32* %10
br label %181
178: ; preds = %161
%179 = load i32, i32* %14
%180 = call i32 @FD_SET(i32 %179, i32* %4)
br label %181
181: ; preds = %178, %173
br label %182
182: ; preds = %181, %157, %154
br label %63
183: ; preds = %48, %40, %22
ret void
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32 }
@.str = [11 x i8] c"/proc/kmsg\00"
@O_RDONLY = global i32 0
@.str.1 = [16 x i8] c"open /proc/kmsg\00"
@O_WRONLY = global i32 0
@.str.2 = [56 x i8] c"couldn't open %s for syslog -- still using /tmp/syslog\0A\00"
@.str.3 = [12 x i8] c"/tmp/syslog\00"
@O_CREAT = global i32 0
@.str.4 = [26 x i8] c"error opening /tmp/syslog\00"
define void @doklog(i8* %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [1024 x i8]
%5 = alloca [1024 x i8]
%6 = bitcast i32* %2 to i8*
%7 = bitcast i32* %3 to i8*
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%9 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%10 = load i32, i32* @O_RDONLY
%11 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %10, i32 0)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %1
%14 = tail call i32 @printstr(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %89
15: ; preds = %1
%16 = load i32, i32* @O_WRONLY
%17 = tail call i32 @open(i8* %0, i32 %16, i32 0)
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %15
%20 = tail call i32 @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %21
21: ; preds = %19, %15
%22 = load i32, i32* @O_WRONLY
%23 = load i32, i32* @O_CREAT
%24 = or i32 %23, %22
%25 = tail call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %24, i32 420)
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %21
%28 = tail call i32 @printstr(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
%29 = tail call i32 @sleep(i32 5)
%30 = tail call i32 @close(i32 %11)
br label %89
31: ; preds = %21
%32 = tail call i64 (...) @fork()
%33 = icmp eq i64 %32, 0
br i1 %33, label %38, label %34
34: ; preds = %31
%35 = tail call i32 @close(i32 %11)
%36 = tail call i32 @close(i32 %17)
%37 = tail call i32 @close(i32 %25)
br label %89
38: ; preds = %31
%39 = tail call i32 @close(i32 0)
%40 = tail call i32 @close(i32 1)
%41 = tail call i32 @close(i32 2)
%42 = tail call i32 @dup2(i32 1, i32 %25)
%43 = tail call i32 @klogctl(i32 8, i32* null, i32 1)
%44 = call i32 @FD_ZERO(i32* %3)
%45 = icmp sgt i32 %17, -1
br label %46
46: ; preds = %51, %38
%47 = call i32 @memcpy(i32* %2, i32* %3, i32 4)
%48 = call i32 @FD_SET(i32 %11, i32* %2)
%49 = call i32 @select(i32 20, i32* %2, i32* null, i32* null, i32* null)
%50 = icmp slt i32 %49, 1
br i1 %50, label %51, label %52
51: ; preds = %86, %46
br label %46
52: ; preds = %46
%53 = call i64 @FD_ISSET(i32 %11, i32* %2)
%54 = icmp eq i64 %53, 0
br i1 %54, label %64, label %55
55: ; preds = %52
%56 = call i32 @read(i32 %11, i8* %8, i32 1024)
%57 = icmp sgt i32 %56, 0
br i1 %57, label %58, label %64
58: ; preds = %55
%59 = call i32 @logChunk(i32 %56, i8* %8, i8* %9)
br i1 %45, label %60, label %62
60: ; preds = %58
%61 = call i32 @write(i32 %17, i8* %9, i32 %59)
br label %62
62: ; preds = %60, %58
%63 = call i32 @write(i32 %25, i8* %9, i32 %59)
br label %64
64: ; preds = %55, %62, %52
br label %65
65: ; preds = %64, %86
%66 = phi i32 [ %87, %86 ], [ 0, %64 ]
%67 = call i64 @FD_ISSET(i32 %66, i32* %2)
%68 = icmp eq i64 %67, 0
br i1 %68, label %86, label %69
69: ; preds = %65
%70 = call i64 @FD_ISSET(i32 %66, i32* %3)
%71 = icmp eq i64 %70, 0
br i1 %71, label %86, label %72
72: ; preds = %69
%73 = call i32 @read(i32 %66, i8* %8, i32 1024)
%74 = icmp sgt i32 %73, 0
br i1 %74, label %75, label %81
75: ; preds = %72
%76 = call i32 @logChunk(i32 %73, i8* %8, i8* %9)
br i1 %45, label %77, label %79
77: ; preds = %75
%78 = call i32 @write(i32 %17, i8* %9, i32 %76)
br label %79
79: ; preds = %77, %75
%80 = call i32 @write(i32 %25, i8* %9, i32 %76)
br label %86
81: ; preds = %72
%82 = icmp eq i32 %73, 0
br i1 %82, label %83, label %86
83: ; preds = %81
%84 = call i32 @close(i32 %66)
%85 = call i32 @FD_CLR(i32 %66, i32* %3)
br label %86
86: ; preds = %65, %69, %81, %83, %79
%87 = add nuw nsw i32 %66, 1
%88 = icmp eq i32 %87, 20
br i1 %88, label %51, label %65
89: ; preds = %34, %27, %13
ret void
}