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

/* Variables and functions */
int O_APPEND;
int O_CREAT;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ die(char *, ...);
int open(char *, int, int);
int opensocket(char *);
int recv(int, char *, int, int /*<<< orphan*/);
int strlen(char *);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char **argv) {
  char buf[1024 + 2] = "syslog: ";
  int tag = strlen(buf);
  int len;
  int sockfd;
  int outfd = 1;
  if (argc < 2 || argc > 3)
    die("Usage: slogd socket-name [log-file]\n");
  if (argc > 2)
    if ((outfd = open(argv[2], O_WRONLY | O_CREAT | O_APPEND, 0644)) < 0)
      die("Cannot open %s: %m\n", argv[2]);
  sockfd = opensocket(argv[1]);
  while ((len = recv(sockfd, buf + tag, 1024 - tag, 0)) > 0) {
    buf[tag + len] = '\n';
    write(outfd, buf, tag + len + 1);
  }
  close(sockfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x420(%rbp),%rdi
lea    0xe59(%rip),%rsi
mov    $0x402,%edx
call   1080 <memcpy@plt>
lea    -0x420(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x424(%rbp)
movl   $0x1,-0x430(%rbp)
cmpl   $0x2,-0x8(%rbp)
jl     11e1 <main+0x61>
cmpl   $0x3,-0x8(%rbp)
jle    11ef <main+0x6f>
lea    0x121a(%rip),%rdi
mov    $0x0,%al
call   10a0 <die@plt>
cmpl   $0x2,-0x8(%rbp)
jle    1250 <main+0xd0>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2dc8(%rip),%rax
mov    (%rax),%esi
mov    0x2de7(%rip),%rax
or     (%rax),%esi
mov    0x2dae(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1090 <open@plt>
mov    %eax,-0x430(%rbp)
cmp    $0x0,%eax
jge    124b <main+0xcb>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
lea    0x11e3(%rip),%rdi
mov    $0x0,%al
call   10a0 <die@plt>
jmp    1250 <main+0xd0>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1040 <opensocket@plt>
mov    %eax,-0x42c(%rbp)
mov    -0x42c(%rbp),%edi
lea    -0x420(%rbp),%rsi
movslq -0x424(%rbp),%rax
add    %rax,%rsi
mov    $0x400,%edx
sub    -0x424(%rbp),%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x428(%rbp)
cmp    $0x0,%eax
jle    12d7 <main+0x157>
mov    -0x424(%rbp),%eax
add    -0x428(%rbp),%eax
cltq
movb   $0xa,-0x420(%rbp,%rax,1)
0a
mov    -0x430(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    -0x424(%rbp),%edx
add    -0x428(%rbp),%edx
add    $0x1,%edx
call   1050 <write@plt>
jmp    1263 <main+0xe3>
mov    -0x42c(%rbp),%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
lea    0xe63(%rip),%rsi
mov    %rsp,%rbx
mov    $0x402,%edx
mov    %rbx,%rdi
call   1080 <memcpy@plt>
mov    %rbx,%rdi
call   1060 <strlen@plt>
mov    %eax,%ebx
lea    -0x4(%rbp),%eax
cmp    $0xfffffffd,%eax
ja     11cd <main+0x4d>
lea    0x123c(%rip),%rdi
xor    %eax,%eax
call   10a0 <die@plt>
mov    $0x1,%r15d
cmp    $0x3,%ebp
jl     121a <main+0x9a>
mov    0x2df1(%rip),%rax
mov    0x2e12(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x10(%r14),%rdi
mov    0x2dd3(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1090 <open@plt>
mov    %eax,%r15d
test   %eax,%eax
jns    121a <main+0x9a>
mov    0x10(%r14),%rsi
lea    0x1214(%rip),%rdi
xor    %eax,%eax
call   10a0 <die@plt>
mov    0x8(%r14),%rdi
call   1040 <opensocket@plt>
mov    %eax,%r14d
movslq %ebx,%rax
lea    (%rsp,%rax,1),%r12
mov    $0x400,%ebp
sub    %eax,%ebp
mov    %r14d,%edi
mov    %r12,%rsi
mov    %ebp,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jle    127e <main+0xfe>
mov    %rsp,%r13
nopw   0x0(%rax,%rax,1)
lea    (%rax,%rbx,1),%ecx
movslq %ecx,%rcx
movb   $0xa,(%rsp,%rcx,1)
lea    (%rax,%rbx,1),%edx
add    $0x1,%edx
mov    %r15d,%edi
mov    %r13,%rsi
call   1050 <write@plt>
mov    %r14d,%edi
mov    %r12,%rsi
mov    %ebp,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jg     1250 <main+0xd0>
mov    %r14d,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.main.buf = [1026 x i8] c"syslog: \00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@.str = [37 x i8] c"Usage: slogd socket-name [log-file]\0A\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@.str.1 = [20 x i8] c"Cannot open %s: %m\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [1026 x i8]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = bitcast [1026 x i8]* %6 to i8*
%12 = getelementptr inbounds [1026 x i8], [1026 x i8]* %6, i64 0, i64 0
%13 = call i32 @strlen(i8* %12)
store i32 %13, i32* %7
store i32 1, i32* %10
%14 = load i32, i32* %4
%15 = icmp slt i32 %14, 2
br i1 %15, label %19, label %16
16: ; preds = %2
%17 = load i32, i32* %4
%18 = icmp sgt i32 %17, 3
br i1 %18, label %19, label %21
19: ; preds = %16, %2
%20 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
br label %21
21: ; preds = %19, %16
%22 = load i32, i32* %4
%23 = icmp sgt i32 %22, 2
br i1 %23, label %24, label %41
24: ; preds = %21
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 2
%27 = load i8*, i8** %26
%28 = load i32, i32* @O_WRONLY
%29 = load i32, i32* @O_CREAT
%30 = or i32 %28, %29
%31 = load i32, i32* @O_APPEND
%32 = or i32 %30, %31
%33 = call i32 @open(i8* %27, i32 %32, i32 420)
store i32 %33, i32* %10
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %40
35: ; preds = %24
%36 = load i8**, i8*** %5
%37 = getelementptr inbounds i8*, i8** %36, i64 2
%38 = load i8*, i8** %37
%39 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %38)
br label %40
40: ; preds = %35, %24
br label %41
41: ; preds = %40, %21
%42 = load i8**, i8*** %5
%43 = getelementptr inbounds i8*, i8** %42, i64 1
%44 = load i8*, i8** %43
%45 = call i32 @opensocket(i8* %44)
store i32 %45, i32* %9
br label %46
46: ; preds = %56, %41
%47 = load i32, i32* %9
%48 = getelementptr inbounds [1026 x i8], [1026 x i8]* %6, i64 0, i64 0
%49 = load i32, i32* %7
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i8, i8* %48, i64 %50
%52 = load i32, i32* %7
%53 = sub nsw i32 1024, %52
%54 = call i32 @recv(i32 %47, i8* %51, i32 %53, i32 0)
store i32 %54, i32* %8
%55 = icmp sgt i32 %54, 0
br i1 %55, label %56, label %69
56: ; preds = %46
%57 = load i32, i32* %7
%58 = load i32, i32* %8
%59 = add nsw i32 %57, %58
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds [1026 x i8], [1026 x i8]* %6, i64 0, i64 %60
store i8 10, i8* %61
%62 = load i32, i32* %10
%63 = getelementptr inbounds [1026 x i8], [1026 x i8]* %6, i64 0, i64 0
%64 = load i32, i32* %7
%65 = load i32, i32* %8
%66 = add nsw i32 %64, %65
%67 = add nsw i32 %66, 1
%68 = call i32 @write(i32 %62, i8* %63, i32 %67)
br label %46
69: ; preds = %46
%70 = load i32, i32* %9
%71 = call i32 @close(i32 %70)
ret i32 0
}

LLVM IR (-O2):
@__const.main.buf = [1026 x i8] c"syslog: \00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@.str = [37 x i8] c"Usage: slogd socket-name [log-file]\0A\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@.str.1 = [20 x i8] c"Cannot open %s: %m\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1026 x i8]
%4 = getelementptr inbounds [1026 x i8], [1026 x i8]* %3, i64 0, i64 0
%5 = call i32 @strlen(i8* %4)
%6 = add i32 %0, -4
%7 = icmp ult i32 %6, -2
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %2, %8
%11 = icmp sgt i32 %0, 2
br i1 %11, label %12, label %25
12: ; preds = %10
%13 = getelementptr inbounds i8*, i8** %1, i64 2
%14 = load i8*, i8** %13
%15 = load i32, i32* @O_WRONLY
%16 = load i32, i32* @O_CREAT
%17 = or i32 %16, %15
%18 = load i32, i32* @O_APPEND
%19 = or i32 %17, %18
%20 = call i32 @open(i8* %14, i32 %19, i32 420)
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %12
%23 = load i8*, i8** %13
%24 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %23)
br label %25
25: ; preds = %12, %22, %10
%26 = phi i32 [ %20, %22 ], [ %20, %12 ], [ 1, %10 ]
%27 = getelementptr inbounds i8*, i8** %1, i64 1
%28 = load i8*, i8** %27
%29 = call i32 @opensocket(i8* %28)
%30 = sext i32 %5 to i64
%31 = getelementptr inbounds [1026 x i8], [1026 x i8]* %3, i64 0, i64 %30
%32 = sub nsw i32 1024, %5
%33 = call i32 @recv(i32 %29, i8* %31, i32 %32, i32 0)
%34 = icmp sgt i32 %33, 0
br i1 %34, label %35, label %44
35: ; preds = %25, %35
%36 = phi i32 [ %42, %35 ], [ %33, %25 ]
%37 = add nsw i32 %36, %5
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds [1026 x i8], [1026 x i8]* %3, i64 0, i64 %38
store i8 10, i8* %39
%40 = add nsw i32 %37, 1
%41 = call i32 @write(i32 %26, i8* %4, i32 %40)
%42 = call i32 @recv(i32 %29, i8* %31, i32 %32, i32 0)
%43 = icmp sgt i32 %42, 0
br i1 %43, label %35, label %44
44: ; preds = %35, %25
%45 = call i32 @close(i32 %29)
ret i32 0
}