#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_in {
  int /*<<< orphan*/ sin_addr;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ addr_text;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SHUT_RDWR;
int /*<<< orphan*/ ab_close(int);
int /*<<< orphan*/ ab_shutdown(int, int /*<<< orphan*/);
int do_echo(int, int);
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int getpeername(int, struct sockaddr *, int *);
int /*<<< orphan*/ inet_ntop(int /*<<< orphan*/, int /*<<< orphan*/ *, char *,
                             int);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
void *thread_tcp_handler(void *v_fd) {
  int fd = (int)(intptr_t)v_fd, rc;
  struct sockaddr_in addr;
  socklen_t addr_len = sizeof(struct sockaddr_in);
  char addr_text[20] = {0};
  rc = getpeername(fd, (struct sockaddr *)&addr, &addr_len);
  inet_ntop(AF_INET, &addr.sin_addr, addr_text, sizeof(addr_text));
  printf("[%s]Connected\n", addr_text);
  if (rc < 0) {
    fprintf(stderr, "Fail to getpeername on %d\n", fd);
    return (void *)(intptr_t)errno;
  }
  rc = do_echo(fd, fd);
  ab_shutdown(fd, SHUT_RDWR);
  ab_close(fd);
  fprintf(stdout, "[%s]Disconnected\n", addr_text);
  return (void *)(intptr_t)rc;
}


x86 assembly (clang, -O0):
<thread_tcp_handler>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %eax,-0x14(%rbp)
movl   $0x4,-0x24(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1090 <memset@plt>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
lea    -0x24(%rbp),%rdx
call   1070 <getpeername@plt>
mov    %eax,-0x18(%rbp)
mov    0x2e1c(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
lea    -0x40(%rbp),%rdx
mov    $0x14,%ecx
call   1040 <inet_ntop@plt>
lea    -0x40(%rbp),%rsi
lea    0xe1d(%rip),%rdi
call   1080 <printf@plt>
cmpl   $0x0,-0x18(%rbp)
jge    121f <thread_tcp_handler+0x9f>
mov    0x2dd7(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
lea    0xe0a(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    0x2db5(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    1269 <thread_tcp_handler+0xe9>
mov    -0x14(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1060 <do_echo@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2db9(%rip),%rax
mov    (%rax),%esi
call   1050 <ab_shutdown@plt>
mov    -0x14(%rbp),%edi
call   1030 <ab_close@plt>
mov    0x2d73(%rip),%rax
mov    (%rax),%edi
lea    -0x40(%rbp),%rdx
lea    0xdd0(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
movslq -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<thread_tcp_handler>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%rbx
movl   $0x4,0x4(%rsp)
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
movl   $0x0,0x20(%rsp)
lea    0x8(%rsp),%r14
lea    0x4(%rsp),%rdx
mov    %r14,%rsi
call   1070 <getpeername@plt>
mov    %eax,%ebp
mov    0x2e30(%rip),%rax
mov    (%rax),%edi
lea    0x10(%rsp),%r15
mov    %r14,%rsi
mov    %r15,%rdx
mov    $0x14,%ecx
call   1040 <inet_ntop@plt>
lea    0xe32(%rip),%rdi
mov    %r15,%rsi
call   1080 <printf@plt>
test   %ebp,%ebp
js     121d <thread_tcp_handler+0xad>
mov    %ebx,%edi
mov    %ebx,%esi
call   1060 <do_echo@plt>
mov    %eax,%ebp
mov    0x2e04(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1050 <ab_shutdown@plt>
mov    %ebx,%edi
call   1030 <ab_close@plt>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
lea    0xe1e(%rip),%rsi
lea    0x10(%rsp),%rdx
xor    %eax,%eax
call   1090 <fprintf@plt>
movslq %ebp,%rax
jmp    1240 <thread_tcp_handler+0xd0>
mov    0x2dac(%rip),%rax
mov    (%rax),%edi
lea    0xde2(%rip),%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    0x2d8b(%rip),%rax
mov    (%rax),%rax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@.str = [15 x i8] c"[%s]Connected\0A\00"
@stderr = global i32 0
@.str.1 = [27 x i8] c"Fail to getpeername on %d\0A\00"
@errno = global i64 0
@SHUT_RDWR = global i32 0
@stdout = global i32 0
@.str.2 = [18 x i8] c"[%s]Disconnected\0A\00"
define i8* @thread_tcp_handler(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
%8 = alloca [20 x i8]
store i8* %0, i8** %3
%9 = load i8*, i8** %3
%10 = ptrtoint i8* %9 to i64
%11 = trunc i64 %10 to i32
store i32 %11, i32* %4
store i32 4, i32* %7
%12 = bitcast [20 x i8]* %8 to i8*
%13 = load i32, i32* %4
%14 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%15 = call i32 @getpeername(i32 %13, %struct.sockaddr* %14, i32* %7)
store i32 %15, i32* %5
%16 = load i32, i32* @AF_INET
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%18 = getelementptr inbounds [20 x i8], [20 x i8]* %8, i64 0, i64 0
%19 = call i32 @inet_ntop(i32 %16, i32* %17, i8* %18, i32 20)
%20 = getelementptr inbounds [20 x i8], [20 x i8]* %8, i64 0, i64 0
%21 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %20)
%22 = load i32, i32* %5
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %30
24: ; preds = %1
%25 = load i32, i32* @stderr
%26 = load i32, i32* %4
%27 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i32 %26)
%28 = load i64, i64* @errno
%29 = inttoptr i64 %28 to i8*
store i8* %29, i8** %2
br label %45
30: ; preds = %1
%31 = load i32, i32* %4
%32 = load i32, i32* %4
%33 = call i32 @do_echo(i32 %31, i32 %32)
store i32 %33, i32* %5
%34 = load i32, i32* %4
%35 = load i32, i32* @SHUT_RDWR
%36 = call i32 @ab_shutdown(i32 %34, i32 %35)
%37 = load i32, i32* %4
%38 = call i32 @ab_close(i32 %37)
%39 = load i32, i32* @stdout
%40 = getelementptr inbounds [20 x i8], [20 x i8]* %8, i64 0, i64 0
%41 = call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %40)
%42 = load i32, i32* %5
%43 = sext i32 %42 to i64
%44 = inttoptr i64 %43 to i8*
store i8* %44, i8** %2
br label %45
45: ; preds = %30, %24
%46 = load i8*, i8** %2
ret i8* %46
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@.str = [15 x i8] c"[%s]Connected\0A\00"
@stderr = global i32 0
@.str.1 = [27 x i8] c"Fail to getpeername on %d\0A\00"
@errno = global i64 0
@SHUT_RDWR = global i32 0
@stdout = global i32 0
@.str.2 = [18 x i8] c"[%s]Disconnected\0A\00"
define i8* @thread_tcp_handler(i8* %0) {
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = alloca [20 x i8]
%5 = ptrtoint i8* %0 to i64
%6 = trunc i64 %5 to i32
%7 = bitcast %struct.sockaddr_in* %2 to i8*
%8 = bitcast i32* %3 to i8*
store i32 4, i32* %3
%9 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%10 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%11 = call i32 @getpeername(i32 %6, %struct.sockaddr* %10, i32* %3)
%12 = load i32, i32* @AF_INET
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
%14 = call i32 @inet_ntop(i32 %12, i32* %13, i8* %9, i32 20)
%15 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %9)
%16 = icmp slt i32 %11, 0
br i1 %16, label %17, label %21
17: ; preds = %1
%18 = load i32, i32* @stderr
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i32 %6)
%20 = load i64, i64* @errno
br label %29
21: ; preds = %1
%22 = call i32 @do_echo(i32 %6, i32 %6)
%23 = load i32, i32* @SHUT_RDWR
%24 = call i32 @ab_shutdown(i32 %6, i32 %23)
%25 = call i32 @ab_close(i32 %6)
%26 = load i32, i32* @stdout
%27 = call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %9)
%28 = sext i32 %22 to i64
br label %29
29: ; preds = %21, %17
%30 = phi i64 [ %20, %17 ], [ %28, %21 ]
%31 = inttoptr i64 %30 to i8*
ret i8* %31
}