#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 */
struct sockaddr_in6 {
  int /*<<< orphan*/ sin6_addr;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ src_ip;
typedef int /*<<< orphan*/ src_addr;
typedef int socklen_t;

/* Variables and functions */
int /*<<< orphan*/ AF_INET6;
int INET6_ADDRSTRLEN;
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ close(int);
int get_tcp_socket(int);
char *get_time();
int /*<<< orphan*/ inet_ntop(int /*<<< orphan*/, int /*<<< orphan*/ *, char *,
                             int);
int /*<<< orphan*/ printf(char *, char *);
int strlen(char *);
int /*<<< orphan*/ write(int, char *, int);
void *tcp_server(void *arg) {
  int fd, cxn, dt_len;
  char *daytime, src_ip[INET6_ADDRSTRLEN];
  struct sockaddr_in6 src_addr;
  socklen_t src_addr_len = sizeof(src_addr);
  (void)arg;
  if ((fd = get_tcp_socket(1300)) == -1) {
    return NULL;
  }
  while (1) {
    if ((cxn = accept(fd, (struct sockaddr *)&src_addr, &src_addr_len)) != -1) {
      inet_ntop(AF_INET6, &src_addr.sin6_addr, src_ip, sizeof(src_ip));
      printf("TCP request from %s\n", src_ip);
      daytime = get_time();
      dt_len = strlen(daytime);
      write(cxn, daytime, dt_len);
      close(cxn);
    }
  }
  return NULL;
}


x86 assembly (clang, -O0):
<tcp_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e55(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x50(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
movl   $0x4,-0x3c(%rbp)
mov    $0x514,%edi
call   1090 <get_tcp_socket@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    11e7 <tcp_server+0x67>
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
xor    %eax,%eax
mov    %rbp,%rsp
pop    %rbp
ret
jmp    11ec <tcp_server+0x6c>
jmp    11f1 <tcp_server+0x71>
mov    -0xc(%rbp),%edi
lea    -0x38(%rbp),%rsi
lea    -0x3c(%rbp),%rdx
call   10a0 <accept@plt>
mov    %eax,-0x10(%rbp)
cmp    $0xffffffff,%eax
je     1267 <tcp_server+0xe7>
mov    -0x48(%rbp),%rdx
mov    -0x50(%rbp),%rax
mov    0x2dc4(%rip),%rcx
mov    (%rcx),%edi
lea    -0x38(%rbp),%rsi
mov    %eax,%ecx
call   1030 <inet_ntop@plt>
mov    -0x48(%rbp),%rsi
lea    0xdcc(%rip),%rdi
call   1060 <printf@plt>
mov    $0x0,%al
call   1080 <get_time@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <write@plt>
mov    -0x10(%rbp),%edi
call   1070 <close@plt>
jmp    11ec <tcp_server+0x6c>

x86 assembly (clang, -O2):
<tcp_server>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    0x2e50(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rbx
mov    %rax,-0x38(%rbp)
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
movl   $0x4,-0x2c(%rbp)
mov    $0x514,%edi
call   1090 <get_tcp_socket@plt>
cmp    $0xffffffff,%eax
je     1237 <tcp_server+0xb7>
mov    %eax,%r15d
lea    -0x40(%rbp),%r12
nopl   0x0(%rax)
mov    %r15d,%edi
mov    %r12,%rsi
lea    -0x2c(%rbp),%rdx
call   10a0 <accept@plt>
cmp    $0xffffffff,%eax
je     11d0 <tcp_server+0x50>
mov    %eax,%r14d
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
mov    %r12,%rsi
mov    %rbx,%rdx
mov    -0x38(%rbp),%rcx
call   1030 <inet_ntop@plt>
lea    0xdfa(%rip),%rdi
mov    %rbx,%rsi
call   1060 <printf@plt>
xor    %eax,%eax
call   1080 <get_time@plt>
mov    %rax,%r13
mov    %rax,%rdi
call   1050 <strlen@plt>
mov    %r14d,%edi
mov    %r13,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %r14d,%edi
call   1070 <close@plt>
jmp    11d0 <tcp_server+0x50>
xor    %eax,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in6 = type { i32 }
%struct.sockaddr = type { i32 }
@INET6_ADDRSTRLEN = global i32 0
@AF_INET6 = global i32 0
@.str = [21 x i8] c"TCP request from %s\0A\00"
define i8* @tcp_server(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca %struct.sockaddr_in6
%10 = alloca i32
store i8* %0, i8** %2
%11 = load i32, i32* @INET6_ADDRSTRLEN
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %7
%14 = alloca i8, i64 %12
store i64 %12, i64* %8
store i32 4, i32* %10
%15 = load i8*, i8** %2
%16 = call i32 @get_tcp_socket(i32 1300)
store i32 %16, i32* %3
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %1
%19 = load i8*, i8** %7
ret i8* null
20: ; preds = %1
br label %21
21: ; preds = %42, %20
br label %22
22: ; preds = %21
%23 = load i32, i32* %3
%24 = bitcast %struct.sockaddr_in6* %9 to %struct.sockaddr*
%25 = call i32 @accept(i32 %23, %struct.sockaddr* %24, i32* %10)
store i32 %25, i32* %4
%26 = icmp ne i32 %25, -1
br i1 %26, label %27, label %42
27: ; preds = %22
%28 = load i32, i32* @AF_INET6
%29 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %9, i32 0, i32 0
%30 = trunc i64 %12 to i32
%31 = call i32 @inet_ntop(i32 %28, i32* %29, i8* %14, i32 %30)
%32 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %14)
%33 = call i8* (...) @get_time()
store i8* %33, i8** %6
%34 = load i8*, i8** %6
%35 = call i32 @strlen(i8* %34)
store i32 %35, i32* %5
%36 = load i32, i32* %4
%37 = load i8*, i8** %6
%38 = load i32, i32* %5
%39 = call i32 @write(i32 %36, i8* %37, i32 %38)
%40 = load i32, i32* %4
%41 = call i32 @close(i32 %40)
br label %42
42: ; preds = %27, %22
br label %21
}

LLVM IR (-O2):
%struct.sockaddr_in6 = type { i32 }
%struct.sockaddr = type { i32 }
@INET6_ADDRSTRLEN = global i32 0
@AF_INET6 = global i32 0
@.str = [21 x i8] c"TCP request from %s\0A\00"
define noalias i8* @tcp_server(i8* nocapture readnone %0) {
%2 = alloca %struct.sockaddr_in6
%3 = alloca i32
%4 = load i32, i32* @INET6_ADDRSTRLEN
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = bitcast %struct.sockaddr_in6* %2 to i8*
%8 = bitcast i32* %3 to i8*
store i32 4, i32* %3
%9 = tail call i32 @get_tcp_socket(i32 1300)
%10 = icmp eq i32 %9, -1
br i1 %10, label %14, label %11
11: ; preds = %1
%12 = bitcast %struct.sockaddr_in6* %2 to %struct.sockaddr*
%13 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %2, i64 0, i32 0
br label %15
14: ; preds = %1
ret i8* null
15: ; preds = %26, %11
%16 = call i32 @accept(i32 %9, %struct.sockaddr* %12, i32* %3)
%17 = icmp eq i32 %16, -1
br i1 %17, label %26, label %18
18: ; preds = %15
%19 = load i32, i32* @AF_INET6
%20 = call i32 @inet_ntop(i32 %19, i32* %13, i8* %6, i32 %4)
%21 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %6)
%22 = call i8* (...) @get_time()
%23 = call i32 @strlen(i8* %22)
%24 = call i32 @write(i32 %16, i8* %22, i32 %23)
%25 = call i32 @close(i32 %16)
br label %26
26: ; preds = %18, %15
br label %15
}