#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*/ time_t;
struct sockaddr_un {
  int /*<<< orphan*/ sun_path;
  int /*<<< orphan*/ sun_family;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_LOCAL;
int /*<<< orphan*/ SOCK_DGRAM;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
char *ctime(int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int /*<<< orphan*/, char *, char *);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
int strlen(char *);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int main(int argc, char *argv[]) {
  struct sockaddr_un addr;
  char buf[1024];
  char sockname[] = "/tmp/logfilesock";
  char *time_str;
  int num, n, sock;
  socklen_t addrlen;
  time_t now;
  num = 0;
  memset(&addr, 0, sizeof(addr));
  addr.sun_family = AF_LOCAL;
  strcpy(addr.sun_path, sockname);
  addrlen = strlen(sockname) + sizeof(addr.sun_family);
  sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
  if (sock == -1) {
    perror("socket() failed");
    exit(1);
  };
  if (bind(sock, (struct sockaddr *)&addr, addrlen) == -1) {
    perror("bind() failed");
    exit(1);
  };
  while (1) {
    n = read(sock, buf, 1024);
    buf[n] = '\0';
    time(&now);
    time_str = ctime(&now);
    time_str[strlen(time_str) - 1] = '\0';
    printf("[%5d] %s %s\n", num++, time_str, buf);
    fflush(stdout);
  }
  close(sock);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x470,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0xe20(%rip),%rax
mov    %rax,-0x440(%rbp)
mov    0xe1a(%rip),%rax
mov    %rax,-0x438(%rbp)
mov    0xe15(%rip),%al
mov    %al,-0x430(%rbp)
movl   $0x0,-0x44c(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1070 <memset@plt>
mov    0x2dc6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x440(%rbp),%rsi
call   1030 <strcpy@plt>
lea    -0x440(%rbp),%rdi
call   1050 <strlen@plt>
cltq
add    $0x4,%rax
mov    %eax,-0x458(%rbp)
mov    0x2d93(%rip),%rax
mov    (%rax),%edi
mov    0x2d82(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,-0x454(%rbp)
cmpl   $0xffffffff,-0x454(%rbp)
jne    1290 <main+0xd0>
lea    0xd90(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x454(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    -0x458(%rbp),%edx
call   10b0 <bind@plt>
cmp    $0xffffffff,%eax
jne    12c4 <main+0x104>
lea    0xd6c(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
jmp    12c9 <main+0x109>
mov    -0x454(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1080 <read@plt>
mov    %eax,-0x450(%rbp)
movslq -0x450(%rbp),%rax
movb   $0x0,-0x420(%rbp,%rax,1)
lea    -0x45c(%rbp),%rdi
call   1090 <time@plt>
lea    -0x45c(%rbp),%rdi
call   1040 <ctime@plt>
mov    %rax,-0x448(%rbp)
mov    -0x448(%rbp),%rax
mov    %rax,-0x470(%rbp)
mov    -0x448(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x470(%rbp),%rax
sub    $0x1,%ecx
mov    %ecx,-0x464(%rbp)
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x44c(%rbp),%esi
mov    %esi,%eax
add    $0x1,%eax
mov    %eax,-0x460(%rbp)
mov    %eax,-0x44c(%rbp)
mov    -0x448(%rbp),%rdx
lea    -0x420(%rbp),%rcx
lea    0xcbc(%rip),%rdi
call   1060 <printf@plt>
mov    0x2c51(%rip),%rax
mov    (%rax),%edi
call   10a0 <fflush@plt>
jmp    12c9 <main+0x109>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x438,%rsp
movaps 0xe28(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movb   $0x0,0x20(%rsp)
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1070 <memset@plt>
mov    0x2df0(%rip),%rbp
mov    0x0(%rbp),%eax
mov    %eax,0x4(%rsp)
mov    (%rsp),%edi
lea    0x10(%rsp),%rbx
mov    %rbx,%rsi
call   1030 <strcpy@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %eax,%ebx
mov    0x0(%rbp),%edi
mov    0x2dbd(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
cmp    $0xffffffff,%eax
je     12c4 <main+0x104>
mov    %eax,%r14d
add    $0x4,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    %ebx,%edx
call   10b0 <bind@plt>
cmp    $0xffffffff,%eax
je     12cd <main+0x10d>
xor    %ebx,%ebx
lea    0x30(%rsp),%r12
lea    0xc(%rsp),%r13
nopl   0x0(%rax)
mov    %r14d,%edi
mov    %r12,%rsi
mov    $0x400,%edx
call   1080 <read@plt>
cltq
movb   $0x0,0x30(%rsp,%rax,1)
mov    %r13,%rdi
call   1090 <time@plt>
mov    %r13,%rdi
call   1040 <ctime@plt>
mov    %rax,%rbp
mov    %rax,%rdi
call   1050 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbp,1)
lea    0x1(%rbx),%r15d
lea    0xd8b(%rip),%rdi
mov    %ebx,%esi
mov    %rbp,%rdx
mov    %r12,%rcx
call   1060 <printf@plt>
mov    0x2d18(%rip),%rax
mov    (%rax),%edi
call   10a0 <fflush@plt>
mov    %r15d,%ebx
jmp    1260 <main+0xa0>
lea    0xd46(%rip),%rdi
jmp    12d4 <main+0x114>
lea    0xd4d(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@__const.main.sockname = [17 x i8] c"/tmp/logfilesock\00"
@AF_LOCAL = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [16 x i8] c"socket() failed\00"
@.str.1 = [14 x i8] c"bind() failed\00"
@.str.2 = [13 x i8] c"[%5d] %s %s\0A\00"
@stdout = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.sockaddr_un
%7 = alloca [1024 x i8]
%8 = alloca [17 x i8]
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%15 = bitcast [17 x i8]* %8 to i8*
store i32 0, i32* %10
%16 = call i32 @memset(%struct.sockaddr_un* %6, i32 0, i32 8)
%17 = load i32, i32* @AF_LOCAL
%18 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 1
store i32 %17, i32* %18
%19 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 0
%20 = load i32, i32* %19
%21 = getelementptr inbounds [17 x i8], [17 x i8]* %8, i64 0, i64 0
%22 = call i32 @strcpy(i32 %20, i8* %21)
%23 = getelementptr inbounds [17 x i8], [17 x i8]* %8, i64 0, i64 0
%24 = call i32 @strlen(i8* %23)
%25 = sext i32 %24 to i64
%26 = add i64 %25, 4
%27 = trunc i64 %26 to i32
store i32 %27, i32* %13
%28 = load i32, i32* @AF_LOCAL
%29 = load i32, i32* @SOCK_DGRAM
%30 = call i32 @socket(i32 %28, i32 %29, i32 0)
store i32 %30, i32* %12
%31 = load i32, i32* %12
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %36
33: ; preds = %2
%34 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %2
%37 = load i32, i32* %12
%38 = bitcast %struct.sockaddr_un* %6 to %struct.sockaddr*
%39 = load i32, i32* %13
%40 = call i32 @bind(i32 %37, %struct.sockaddr* %38, i32 %39)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %45
42: ; preds = %36
%43 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%44 = call i32 @exit(i32 1)
unreachable
45: ; preds = %36
br label %46
46: ; preds = %45, %46
%47 = load i32, i32* %12
%48 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%49 = call i32 @read(i32 %47, i8* %48, i32 1024)
store i32 %49, i32* %11
%50 = load i32, i32* %11
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 %51
store i8 0, i8* %52
%53 = call i32 @time(i32* %14)
%54 = call i8* @ctime(i32* %14)
store i8* %54, i8** %9
%55 = load i8*, i8** %9
%56 = load i8*, i8** %9
%57 = call i32 @strlen(i8* %56)
%58 = sub nsw i32 %57, 1
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds i8, i8* %55, i64 %59
store i8 0, i8* %60
%61 = load i32, i32* %10
%62 = add nsw i32 %61, 1
store i32 %62, i32* %10
%63 = load i8*, i8** %9
%64 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%65 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 %61, i8* %63, i8* %64)
%66 = load i32, i32* @stdout
%67 = call i32 @fflush(i32 %66)
br label %46
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@__const.main.sockname = [17 x i8] c"/tmp/logfilesock\00"
@AF_LOCAL = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [16 x i8] c"socket() failed\00"
@.str.1 = [14 x i8] c"bind() failed\00"
@.str.2 = [13 x i8] c"[%5d] %s %s\0A\00"
@stdout = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.sockaddr_un
%4 = alloca [1024 x i8]
%5 = alloca [17 x i8]
%6 = alloca i32
%7 = bitcast %struct.sockaddr_un* %3 to i8*
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%9 = getelementptr inbounds [17 x i8], [17 x i8]* %5, i64 0, i64 0
%10 = bitcast i32* %6 to i8*
%11 = call i32 @memset(%struct.sockaddr_un* %3, i32 0, i32 8)
%12 = load i32, i32* @AF_LOCAL
%13 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i64 0, i32 1
store i32 %12, i32* %13
%14 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i64 0, i32 0
%15 = load i32, i32* %14
%16 = call i32 @strcpy(i32 %15, i8* %9)
%17 = call i32 @strlen(i8* %9)
%18 = load i32, i32* @AF_LOCAL
%19 = load i32, i32* @SOCK_DGRAM
%20 = call i32 @socket(i32 %18, i32 %19, i32 0)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %25
22: ; preds = %2
%23 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %2
%26 = add i32 %17, 4
%27 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%28 = call i32 @bind(i32 %20, %struct.sockaddr* %27, i32 %26)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %33
30: ; preds = %25
%31 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %25, %33
%34 = phi i32 [ %44, %33 ], [ 0, %25 ]
%35 = call i32 @read(i32 %20, i8* %8, i32 1024)
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %36
store i8 0, i8* %37
%38 = call i32 @time(i32* %6)
%39 = call i8* @ctime(i32* %6)
%40 = call i32 @strlen(i8* %39)
%41 = add nsw i32 %40, -1
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8, i8* %39, i64 %42
store i8 0, i8* %43
%44 = add nuw nsw i32 %34, 1
%45 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 %34, i8* %39, i8* %8)
%46 = load i32, i32* @stdout
%47 = call i32 @fflush(i32 %46)
br label %33
}