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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int accept(int, struct sockaddr *, int /*<<< orphan*/ *);
int /*<<< orphan*/ close(int);
char *ctime(int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strlen(char *);
int tcp_listen(int /*<<< orphan*/ *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
int main(int argc, char **argv) {
  int listenfd, connfd;
  char buff[4096];
  time_t ticks;
  if (argc != 2) {
    fprintf(stderr, "usage: %s <service or port#>\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  if ((listenfd = tcp_listen(NULL, argv[1], NULL)) == -1) {
    fprintf(stderr, "tcp_listen() error\n");
    exit(EXIT_FAILURE);
  }
  for (;;) {
    if ((connfd = accept(listenfd, (struct sockaddr *)NULL, NULL)) < 0) {
      perror("accept() error");
      exit(EXIT_FAILURE);
    }
    ticks = time(NULL);
    snprintf(buff, sizeof(buff), "%.24s\r\n", ctime(&ticks));
    write(connfd, buff, strlen(buff));
    close(connfd);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11ff <main+0x4f>
mov    0x2e06(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe16(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    0x2de0(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rdi
call   10a0 <tcp_listen@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    1244 <main+0x94>
mov    0x2dba(%rip),%rax
mov    (%rax),%edi
lea    0xdef(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    0x2d9b(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
jmp    1249 <main+0x99>
mov    -0x14(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   10c0 <accept@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    127e <main+0xce>
lea    0xdc7(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2d61(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1090 <time@plt>
mov    %eax,-0x1024(%rbp)
lea    -0x1020(%rbp),%rax
mov    %rax,-0x1040(%rbp)
lea    -0x1024(%rbp),%rdi
call   1040 <ctime@plt>
mov    -0x1040(%rbp),%rdi
mov    %rax,%rcx
mov    $0x1000,%esi
lea    0xd84(%rip),%rdx
call   1060 <snprintf@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x1034(%rbp)
lea    -0x1020(%rbp),%rax
mov    %rax,-0x1030(%rbp)
lea    -0x1020(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x1034(%rbp),%edi
mov    -0x1030(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
jmp    1249 <main+0x99>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1010,%rsp
cmp    $0x2,%edi
jne    11f2 <main+0x42>
mov    0x8(%rsi),%rsi
xor    %edi,%edi
xor    %edx,%edx
call   10a0 <tcp_listen@plt>
cmp    $0xffffffff,%eax
jne    1211 <main+0x61>
mov    0x2e03(%rip),%rax
mov    (%rax),%edi
lea    0xe38(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
jmp    129f <main+0xef>
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xdfb(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
jmp    129f <main+0xef>
mov    %eax,%r12d
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10c0 <accept@plt>
test   %eax,%eax
js     1293 <main+0xe3>
mov    %eax,%ebp
lea    0xc(%rsp),%r14
lea    0xe10(%rip),%r15
lea    0x10(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
xor    %edi,%edi
call   1090 <time@plt>
mov    %eax,0xc(%rsp)
mov    %r14,%rdi
call   1040 <ctime@plt>
mov    %rbx,%rdi
mov    $0x1000,%esi
mov    %r15,%rdx
mov    %rax,%rcx
call   1060 <snprintf@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %ebp,%edi
call   1070 <close@plt>
mov    %r12d,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10c0 <accept@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1240 <main+0x90>
lea    0xd98(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2d32(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [30 x i8] c"usage: %s <service or port#>\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [20 x i8] c"tcp_listen() error\0A\00"
@.str.2 = [15 x i8] c"accept() error\00"
@.str.3 = [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 [4096 x i8]
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 2
br i1 %11, label %12, label %20
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 0
%16 = load i8*, i8** %15
%17 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = load i32, i32* @EXIT_FAILURE
%19 = call i32 @exit(i32 %18)
unreachable
20: ; preds = %2
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = call i32 @tcp_listen(i32* null, i8* %23, i32* null)
store i32 %24, i32* %6
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %31
26: ; preds = %20
%27 = load i32, i32* @stderr
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%29 = load i32, i32* @EXIT_FAILURE
%30 = call i32 @exit(i32 %29)
unreachable
31: ; preds = %20
br label %32
32: ; preds = %40, %31
%33 = load i32, i32* %6
%34 = call i32 @accept(i32 %33, %struct.sockaddr* null, i32* null)
store i32 %34, i32* %7
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %40
36: ; preds = %32
%37 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%38 = load i32, i32* @EXIT_FAILURE
%39 = call i32 @exit(i32 %38)
unreachable
40: ; preds = %32
%41 = call i32 @time(i32* null)
store i32 %41, i32* %9
%42 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%43 = call i8* @ctime(i32* %9)
%44 = call i32 @snprintf(i8* %42, i32 4096, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* %43)
%45 = load i32, i32* %7
%46 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%47 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%48 = call i32 @strlen(i8* %47)
%49 = call i32 @write(i32 %45, i8* %46, i32 %48)
%50 = load i32, i32* %7
%51 = call i32 @close(i32 %50)
br label %32
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [30 x i8] c"usage: %s <service or port#>\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [20 x i8] c"tcp_listen() error\0A\00"
@.str.2 = [15 x i8] c"accept() error\00"
@.str.3 = [8 x i8] c"%.24s\0D\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [4096 x i8]
%4 = alloca i32
%5 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%6 = bitcast i32* %4 to i8*
%7 = icmp eq i32 %0, 2
br i1 %7, label %14, label %8
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = load i8*, i8** %1
%11 = tail call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = load i32, i32* @EXIT_FAILURE
%13 = tail call i32 @exit(i32 %12)
unreachable
14: ; preds = %2
%15 = getelementptr inbounds i8*, i8** %1, i64 1
%16 = load i8*, i8** %15
%17 = tail call i32 @tcp_listen(i32* null, i8* %16, i32* null)
%18 = icmp eq i32 %17, -1
br i1 %18, label %22, label %19
19: ; preds = %14
%20 = call i32 @accept(i32 %17, %struct.sockaddr* null, i32* null)
%21 = icmp slt i32 %20, 0
br i1 %21, label %27, label %31
22: ; preds = %14
%23 = load i32, i32* @stderr
%24 = tail call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%25 = load i32, i32* @EXIT_FAILURE
%26 = tail call i32 @exit(i32 %25)
unreachable
27: ; preds = %31, %19
%28 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%29 = load i32, i32* @EXIT_FAILURE
%30 = call i32 @exit(i32 %29)
unreachable
31: ; preds = %19, %31
%32 = phi i32 [ %39, %31 ], [ %20, %19 ]
%33 = call i32 @time(i32* null)
store i32 %33, i32* %4
%34 = call i8* @ctime(i32* %4)
%35 = call i32 @snprintf(i8* %5, i32 4096, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* %34)
%36 = call i32 @strlen(i8* %5)
%37 = call i32 @write(i32 %32, i8* %5, i32 %36)
%38 = call i32 @close(i32 %32)
%39 = call i32 @accept(i32 %17, %struct.sockaddr* null, i32* null)
%40 = icmp slt i32 %39, 0
br i1 %40, label %27, label %31
}