#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 {
  int dummy;
};
typedef int /*<<< orphan*/ socklen_t;
typedef int /*<<< orphan*/ buf;
typedef int /*<<< orphan*/ addr_str;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ LOG_INFO;
scalar_t__ NI_MAXHOST;
scalar_t__ NI_MAXSERV;
int accept(int, struct sockaddr *, int /*<<< orphan*/ *);
int /*<<< orphan*/ close(int);
char *ctime(int /*<<< orphan*/ *);
int /*<<< orphan*/ daemon_init(char *, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ inet_addrstr(struct sockaddr *, int /*<<< orphan*/, char *,
                                int);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int tcp_listen(char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int listenfd, connfd;
  socklen_t addrlen, len;
  struct sockaddr cliaddr;
  char buf[1024];
  time_t ticks;
  if (argc < 2 || argc > 3) {
    fprintf(stderr, "Usage: %s [<host>] <port>\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  daemon_init(argv[0], 0);
  if (argc == 2)
    listenfd = tcp_listen(NULL, argv[1], &addrlen);
  else
    listenfd = tcp_listen(argv[1], argv[2], &addrlen);
  while (1) {
    len = addrlen;
    connfd = accept(listenfd, &cliaddr, &len);
    char addr_str[(NI_MAXHOST + NI_MAXSERV + 10)];
    syslog(LOG_INFO, "connection from client %s\n",
           inet_addrstr(&cliaddr, len, addr_str, sizeof(addr_str)));
    ticks = time(NULL);
    snprintf(buf, sizeof(buf), "%.24s\r\n", ctime(&ticks));
    write(connfd, buf, strlen(buf));
    close(connfd);
  }
  close(listenfd);
  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)
cmpl   $0x2,-0x8(%rbp)
jl     11fd <main+0x2d>
cmpl   $0x3,-0x8(%rbp)
jle    1227 <main+0x57>
mov    0x2dd4(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xdec(%rip),%rsi
call   1080 <fprintf@plt>
mov    0x2db0(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
xor    %esi,%esi
call   1090 <daemon_init@plt>
cmpl   $0x2,-0x8(%rbp)
jne    125c <main+0x8c>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
xor    %eax,%eax
mov    %eax,%edi
lea    -0x1c(%rbp),%rdx
call   10b0 <tcp_listen@plt>
mov    %eax,-0x14(%rbp)
jmp    1278 <main+0xa8>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
lea    -0x1c(%rbp),%rdx
call   10b0 <tcp_listen@plt>
mov    %eax,-0x14(%rbp)
jmp    127d <main+0xad>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   10d0 <accept@plt>
mov    %eax,-0x18(%rbp)
mov    0x2d2b(%rip),%rax
mov    (%rax),%rcx
mov    0x2d41(%rip),%rax
mov    (%rax),%rdx
lea    0xa(%rcx,%rdx,1),%rax
mov    %rsp,%rsi
mov    %rsi,-0x440(%rbp)
lea    0x19(%rcx,%rdx,1),%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdx
sub    %rcx,%rdx
mov    %rdx,-0x470(%rbp)
mov    %rdx,%rsp
mov    %rax,-0x448(%rbp)
mov    0x2ce0(%rip),%rcx
mov    (%rcx),%ecx
mov    %ecx,-0x464(%rbp)
mov    -0x20(%rbp),%esi
mov    %eax,%ecx
lea    -0x28(%rbp),%rdi
call   10f0 <inet_addrstr@plt>
mov    -0x464(%rbp),%edi
mov    %eax,%edx
lea    0xd16(%rip),%rsi
call   10c0 <syslog@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10a0 <time@plt>
mov    %eax,-0x434(%rbp)
lea    -0x430(%rbp),%rax
mov    %rax,-0x460(%rbp)
lea    -0x434(%rbp),%rdi
call   1040 <ctime@plt>
mov    -0x460(%rbp),%rdi
mov    %rax,%rcx
mov    $0x400,%esi
lea    0xced(%rip),%rdx
call   1060 <snprintf@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x454(%rbp)
lea    -0x430(%rbp),%rax
mov    %rax,-0x450(%rbp)
lea    -0x430(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x454(%rbp),%edi
mov    -0x450(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
mov    -0x440(%rbp),%rax
mov    %rax,%rsp
jmp    127d <main+0xad>

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x428,%rsp
mov    %rsi,%r14
mov    %edi,%ebx
lea    -0x4(%rbx),%eax
cmp    $0xfffffffd,%eax
jbe    12fc <main+0x12c>
mov    (%r14),%rdi
xor    %esi,%esi
call   1090 <daemon_init@plt>
mov    0x8(%r14),%rax
cmp    $0x2,%ebx
jne    1213 <main+0x43>
lea    -0x30(%rbp),%rdx
xor    %edi,%edi
mov    %rax,%rsi
jmp    121e <main+0x4e>
mov    0x10(%r14),%rsi
lea    -0x30(%rbp),%rdx
mov    %rax,%rdi
call   10b0 <tcp_listen@plt>
mov    %eax,%r14d
cs nopw 0x0(%rax,%rax,1)
mov    -0x30(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    %r14d,%edi
lea    -0x48(%rbp),%r12
mov    %r12,%rsi
lea    -0x2c(%rbp),%rdx
call   10d0 <accept@plt>
mov    %eax,%r13d
mov    %rsp,-0x40(%rbp)
mov    0x2d71(%rip),%rax
mov    (%rax),%rax
mov    0x2d87(%rip),%rcx
mov    (%rcx),%rdx
lea    (%rax,%rdx,1),%rcx
add    $0xa,%ecx
add    %rdx,%rax
add    $0x19,%rax
and    $0xfffffffffffffff0,%rax
mov    %rsp,%rdx
sub    %rax,%rdx
mov    %rdx,%rsp
mov    0x2d3a(%rip),%rax
mov    (%rax),%r15d
mov    -0x2c(%rbp),%esi
mov    %r12,%rdi
call   10f0 <inet_addrstr@plt>
mov    %r15d,%edi
lea    0xd7d(%rip),%rsi
mov    %eax,%edx
call   10c0 <syslog@plt>
xor    %edi,%edi
call   10a0 <time@plt>
mov    %eax,-0x34(%rbp)
lea    -0x34(%rbp),%rdi
call   1040 <ctime@plt>
lea    -0x450(%rbp),%rbx
mov    %rbx,%rdi
mov    $0x400,%esi
lea    0xd68(%rip),%rdx
mov    %rax,%rcx
call   1060 <snprintf@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %r13d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %r13d,%edi
call   1070 <close@plt>
mov    -0x40(%rbp),%rsp
jmp    1230 <main+0x60>
mov    0x2cd5(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
lea    0xcf1(%rip),%rsi
call   1080 <fprintf@plt>
mov    0x2cb5(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [27 x i8] c"Usage: %s [<host>] <port>\0A\00"
@EXIT_FAILURE = global i32 0
@NI_MAXHOST = global i64 0
@NI_MAXSERV = global i64 0
@LOG_INFO = global i32 0
@.str.1 = [27 x i8] c"connection from client %s\0A\00"
@.str.2 = [8 x i8] c"%.24s\0D\0A\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 i32
%9 = alloca i32
%10 = alloca %struct.sockaddr
%11 = alloca [1024 x i8]
%12 = alloca i32
%13 = alloca i8*
%14 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%15 = load i32, i32* %4
%16 = icmp slt i32 %15, 2
br i1 %16, label %20, label %17
17: ; preds = %2
%18 = load i32, i32* %4
%19 = icmp sgt i32 %18, 3
br i1 %19, label %20, label %28
20: ; preds = %17, %2
%21 = load i32, i32* @stderr
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 0
%24 = load i8*, i8** %23
%25 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %24)
%26 = load i32, i32* @EXIT_FAILURE
%27 = call i32 @exit(i32 %26)
unreachable
28: ; preds = %17
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 0
%31 = load i8*, i8** %30
%32 = call i32 @daemon_init(i8* %31, i32 0)
%33 = load i32, i32* %4
%34 = icmp eq i32 %33, 2
br i1 %34, label %35, label %40
35: ; preds = %28
%36 = load i8**, i8*** %5
%37 = getelementptr inbounds i8*, i8** %36, i64 1
%38 = load i8*, i8** %37
%39 = call i32 @tcp_listen(i8* null, i8* %38, i32* %8)
store i32 %39, i32* %6
br label %48
40: ; preds = %28
%41 = load i8**, i8*** %5
%42 = getelementptr inbounds i8*, i8** %41, i64 1
%43 = load i8*, i8** %42
%44 = load i8**, i8*** %5
%45 = getelementptr inbounds i8*, i8** %44, i64 2
%46 = load i8*, i8** %45
%47 = call i32 @tcp_listen(i8* %43, i8* %46, i32* %8)
store i32 %47, i32* %6
br label %48
48: ; preds = %40, %35
br label %49
49: ; preds = %48, %49
%50 = load i32, i32* %8
store i32 %50, i32* %9
%51 = load i32, i32* %6
%52 = call i32 @accept(i32 %51, %struct.sockaddr* %10, i32* %9)
store i32 %52, i32* %7
%53 = load i64, i64* @NI_MAXHOST
%54 = load i64, i64* @NI_MAXSERV
%55 = add nsw i64 %53, %54
%56 = add nsw i64 %55, 10
%57 = call i8* @llvm.stacksave()
store i8* %57, i8** %13
%58 = alloca i8, i64 %56
store i64 %56, i64* %14
%59 = load i32, i32* @LOG_INFO
%60 = load i32, i32* %9
%61 = trunc i64 %56 to i32
%62 = call i32 @inet_addrstr(%struct.sockaddr* %10, i32 %60, i8* %58, i32 %61)
%63 = call i32 @syslog(i32 %59, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i32 %62)
%64 = call i32 @time(i32* null)
store i32 %64, i32* %12
%65 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%66 = call i8* @ctime(i32* %12)
%67 = call i32 @snprintf(i8* %65, i32 1024, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* %66)
%68 = load i32, i32* %7
%69 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%70 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%71 = call i32 @strlen(i8* %70)
%72 = call i32 @write(i32 %68, i8* %69, i32 %71)
%73 = load i32, i32* %7
%74 = call i32 @close(i32 %73)
%75 = load i8*, i8** %13
br label %49
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [27 x i8] c"Usage: %s [<host>] <port>\0A\00"
@EXIT_FAILURE = global i32 0
@NI_MAXHOST = global i64 0
@NI_MAXSERV = global i64 0
@LOG_INFO = global i32 0
@.str.1 = [27 x i8] c"connection from client %s\0A\00"
@.str.2 = [8 x i8] c"%.24s\0D\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr
%6 = alloca [1024 x i8]
%7 = alloca i32
%8 = bitcast i32* %3 to i8*
%9 = bitcast i32* %4 to i8*
%10 = bitcast %struct.sockaddr* %5 to i8*
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%12 = bitcast i32* %7 to i8*
%13 = add i32 %0, -4
%14 = icmp ult i32 %13, -2
br i1 %14, label %15, label %21
15: ; preds = %2
%16 = load i32, i32* @stderr
%17 = load i8*, i8** %1
%18 = tail call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = load i32, i32* @EXIT_FAILURE
%20 = tail call i32 @exit(i32 %19)
unreachable
21: ; preds = %2
%22 = load i8*, i8** %1
%23 = tail call i32 @daemon_init(i8* %22, i32 0)
%24 = icmp eq i32 %0, 2
%25 = getelementptr inbounds i8*, i8** %1, i64 1
%26 = load i8*, i8** %25
br i1 %24, label %27, label %29
27: ; preds = %21
%28 = call i32 @tcp_listen(i8* null, i8* %26, i32* %3)
br label %33
29: ; preds = %21
%30 = getelementptr inbounds i8*, i8** %1, i64 2
%31 = load i8*, i8** %30
%32 = call i32 @tcp_listen(i8* %26, i8* %31, i32* %3)
br label %33
33: ; preds = %29, %27
%34 = phi i32 [ %28, %27 ], [ %32, %29 ]
br label %35
35: ; preds = %35, %33
%36 = load i32, i32* %3
store i32 %36, i32* %4
%37 = call i32 @accept(i32 %34, %struct.sockaddr* %5, i32* %4)
%38 = load i64, i64* @NI_MAXHOST
%39 = load i64, i64* @NI_MAXSERV
%40 = add i64 %38, 10
%41 = add i64 %40, %39
%42 = call i8* @llvm.stacksave()
%43 = alloca i8, i64 %41
%44 = load i32, i32* @LOG_INFO
%45 = load i32, i32* %4
%46 = trunc i64 %41 to i32
%47 = call i32 @inet_addrstr(%struct.sockaddr* %5, i32 %45, i8* %43, i32 %46)
%48 = call i32 @syslog(i32 %44, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i32 %47)
%49 = call i32 @time(i32* null)
store i32 %49, i32* %7
%50 = call i8* @ctime(i32* %7)
%51 = call i32 @snprintf(i8* %11, i32 1024, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* %50)
%52 = call i32 @strlen(i8* %11)
%53 = call i32 @write(i32 %37, i8* %11, i32 %52)
%54 = call i32 @close(i32 %37)
br label %35
}