#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int /*<<< orphan*/ *);
int bind(int, struct sockaddr *, size_t);
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, size_t);
int /*<<< orphan*/ printf(char *, ...);
int recv(int, char **, int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
char *strerror(int);
int main(void) {
  int listen_sockfd, conn_sockfd;
  int n;
  char buf[4096];
  struct sockaddr_in addr;
  size_t addrlen;
  if ((listen_sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    printf("create socket error: %s(errno: %d)\n", strerror(errno), errno);
    exit(1);
  }
  addrlen = sizeof(struct sockaddr_in);
  memset(&addr, 0, addrlen);
  addr.sin_family = AF_INET;
  addr.sin_addr.s_addr = htonl(INADDR_ANY);
  addr.sin_port = htons(9999);
  if (bind(listen_sockfd, (struct sockaddr *)&addr, addrlen) == -1) {
    printf("bind socket error: %s(errno: %d)\n", strerror(errno), errno);
    exit(1);
  }
  if (listen(listen_sockfd, 10) == -1) {
    printf("listen socket error: %s(errno: %d)\n", strerror(errno), errno);
    exit(1);
  }
  while (1) {
    if ((conn_sockfd = accept(listen_sockfd, (struct sockaddr *)NULL, NULL)) ==
        -1) {
      printf("accept socket error: %s(errno: %d)\n", strerror(errno), errno);
      continue;
    } else {
      printf("new socket connect...\nthe client socketfd: %d\n", conn_sockfd);
    }
    n = recv(conn_sockfd, &buf, 4096, 0);
    buf[n] = '\0';
    printf("received msg from client<%d>: %s\n", conn_sockfd, buf);
    close(conn_sockfd);
  }
  close(listen_sockfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e0f(%rip),%rax
mov    (%rax),%edi
mov    0x2dee(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    1229 <main+0x69>
mov    0x2dca(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    %rax,%rsi
mov    0x2db9(%rip),%rax
mov    (%rax),%edx
lea    0xde8(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
movq   $0xc,-0x1028(%rbp)
mov    -0x1028(%rbp),%rdx
lea    -0x1020(%rbp),%rdi
xor    %esi,%esi
call   1070 <memset@plt>
mov    0x2d98(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1018(%rbp)
mov    0x2d81(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,-0x101c(%rbp)
mov    $0x270f,%edi
call   1040 <htons@plt>
mov    %eax,-0x1020(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    -0x1028(%rbp),%rdx
call   10a0 <bind@plt>
cmp    $0xffffffff,%eax
jne    12cd <main+0x10d>
mov    0x2d26(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    %rax,%rsi
mov    0x2d15(%rip),%rax
mov    (%rax),%edx
lea    0xd68(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x8(%rbp),%edi
mov    $0xa,%esi
call   1090 <listen@plt>
cmp    $0xffffffff,%eax
jne    1315 <main+0x155>
mov    0x2cde(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    %rax,%rsi
mov    0x2ccd(%rip),%rax
mov    (%rax),%edx
lea    0xd42(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
jmp    131a <main+0x15a>
mov    -0x8(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   10b0 <accept@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    1362 <main+0x1a2>
mov    0x2c8c(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    %rax,%rsi
mov    0x2c7b(%rip),%rax
mov    (%rax),%edx
lea    0xd14(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    131a <main+0x15a>
mov    -0xc(%rbp),%esi
lea    0xd22(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0xc(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    $0x1000,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x10(%rbp)
movslq -0x10(%rbp),%rax
movb   $0x0,-0x1010(%rbp,%rax,1)
mov    -0xc(%rbp),%esi
lea    -0x1010(%rbp),%rdx
lea    0xd14(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0xc(%rbp),%edi
call   1080 <close@plt>
jmp    131a <main+0x15a>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1018,%rsp
mov    0x2e10(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2dee(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
cmp    $0xffffffff,%eax
je     12eb <main+0x12b>
mov    %eax,%r13d
mov    %rsp,%rbx
mov    $0xc,%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <memset@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x8(%rsp)
mov    0x2dc9(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,0x4(%rsp)
mov    $0x270f,%edi
call   1040 <htons@plt>
mov    %eax,(%rsp)
mov    $0xc,%edx
mov    %r13d,%edi
mov    %rbx,%rsi
call   10a0 <bind@plt>
cmp    $0xffffffff,%eax
je     1304 <main+0x144>
mov    %r13d,%edi
mov    $0xa,%esi
call   1090 <listen@plt>
cmp    $0xffffffff,%eax
je     131d <main+0x15d>
mov    0x2d63(%rip),%r14
lea    0xe22(%rip),%r15
lea    0x10(%rsp),%rbx
lea    0xe45(%rip),%r12
jmp    12bc <main+0xfc>
nopw   0x0(%rax,%rax,1)
mov    %eax,%ebp
mov    %r15,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
cltq
movb   $0x0,0x10(%rsp,%rax,1)
mov    %r12,%rdi
mov    %ebp,%esi
mov    %rbx,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebp,%edi
call   1080 <close@plt>
mov    %r13d,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10b0 <accept@plt>
cmp    $0xffffffff,%eax
jne    1280 <main+0xc0>
mov    (%r14),%edi
call   10d0 <strerror@plt>
mov    (%r14),%edx
lea    0xd8b(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    12bc <main+0xfc>
mov    0x2cd6(%rip),%rbx
mov    (%rbx),%edi
call   10d0 <strerror@plt>
mov    (%rbx),%edx
lea    0xcfe(%rip),%rdi
jmp    1334 <main+0x174>
mov    0x2cbd(%rip),%rbx
mov    (%rbx),%edi
call   10d0 <strerror@plt>
mov    (%rbx),%edx
lea    0xd09(%rip),%rdi
jmp    1334 <main+0x174>
mov    0x2ca4(%rip),%rbx
mov    (%rbx),%edi
call   10d0 <strerror@plt>
mov    (%rbx),%edx
lea    0xd12(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [36 x i8] c"create socket error: %s(errno: %d)\0A\00"
@errno = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [34 x i8] c"bind socket error: %s(errno: %d)\0A\00"
@.str.2 = [36 x i8] c"listen socket error: %s(errno: %d)\0A\00"
@.str.3 = [36 x i8] c"accept socket error: %s(errno: %d)\0A\00"
@.str.4 = [47 x i8] c"new socket connect...\0Athe client socketfd: %d\0A\00"
@.str.5 = [34 x i8] c"received msg from client<%d>: %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [4096 x i8]
%6 = alloca %struct.sockaddr_in
%7 = alloca i64
store i32 0, i32* %1
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %2
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %18
12: ; preds = %0
%13 = load i32, i32* @errno
%14 = call i8* @strerror(i32 %13)
%15 = load i32, i32* @errno
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %14, i32 %15)
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %0
store i64 12, i64* %7
%19 = load i64, i64* %7
%20 = call i32 @memset(%struct.sockaddr_in* %6, i32 0, i64 %19)
%21 = load i32, i32* @AF_INET
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %21, i32* %22
%23 = load i32, i32* @INADDR_ANY
%24 = call i32 @htonl(i32 %23)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
%26 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %25, i32 0, i32 0
store i32 %24, i32* %26
%27 = call i32 @htons(i32 9999)
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 %27, i32* %28
%29 = load i32, i32* %2
%30 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%31 = load i64, i64* %7
%32 = call i32 @bind(i32 %29, %struct.sockaddr* %30, i64 %31)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %40
34: ; preds = %18
%35 = load i32, i32* @errno
%36 = call i8* @strerror(i32 %35)
%37 = load i32, i32* @errno
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* %36, i32 %37)
%39 = call i32 @exit(i32 1)
unreachable
40: ; preds = %18
%41 = load i32, i32* %2
%42 = call i32 @listen(i32 %41, i32 10)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %50
44: ; preds = %40
%45 = load i32, i32* @errno
%46 = call i8* @strerror(i32 %45)
%47 = load i32, i32* @errno
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i8* %46, i32 %47)
%49 = call i32 @exit(i32 1)
unreachable
50: ; preds = %40
br label %51
51: ; preds = %50, %55, %63
%52 = load i32, i32* %2
%53 = call i32 @accept(i32 %52, %struct.sockaddr* null, i32* null)
store i32 %53, i32* %3
%54 = icmp eq i32 %53, -1
br i1 %54, label %55, label %60
55: ; preds = %51
%56 = load i32, i32* @errno
%57 = call i8* @strerror(i32 %56)
%58 = load i32, i32* @errno
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i8* %57, i32 %58)
br label %51
60: ; preds = %51
%61 = load i32, i32* %3
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.4, i64 0, i64 0), i32 %61)
br label %63
63: ; preds = %60
%64 = load i32, i32* %3
%65 = bitcast [4096 x i8]* %5 to i8**
%66 = call i32 @recv(i32 %64, i8** %65, i32 4096, i32 0)
store i32 %66, i32* %4
%67 = load i32, i32* %4
%68 = sext i32 %67 to i64
%69 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 %68
store i8 0, i8* %69
%70 = load i32, i32* %3
%71 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0), i32 %70, i8* %71)
%73 = load i32, i32* %3
%74 = call i32 @close(i32 %73)
br label %51
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [36 x i8] c"create socket error: %s(errno: %d)\0A\00"
@errno = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [34 x i8] c"bind socket error: %s(errno: %d)\0A\00"
@.str.2 = [36 x i8] c"listen socket error: %s(errno: %d)\0A\00"
@.str.3 = [36 x i8] c"accept socket error: %s(errno: %d)\0A\00"
@.str.4 = [47 x i8] c"new socket connect...\0Athe client socketfd: %d\0A\00"
@.str.5 = [34 x i8] c"received msg from client<%d>: %s\0A\00"
define i32 @main() {
%1 = alloca [4096 x i8]
%2 = alloca %struct.sockaddr_in
%3 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 0
%4 = bitcast %struct.sockaddr_in* %2 to i8*
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %15
9: ; preds = %0
%10 = load i32, i32* @errno
%11 = tail call i8* @strerror(i32 %10)
%12 = load i32, i32* @errno
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %11, i32 %12)
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %0
%16 = call i32 @memset(%struct.sockaddr_in* %2, i32 0, i64 12)
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %17, i32* %18
%19 = load i32, i32* @INADDR_ANY
%20 = call i32 @htonl(i32 %19)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1, i32 0
store i32 %20, i32* %21
%22 = call i32 @htons(i32 9999)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i32 %22, i32* %23
%24 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%25 = call i32 @bind(i32 %7, %struct.sockaddr* %24, i64 12)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %33
27: ; preds = %15
%28 = load i32, i32* @errno
%29 = call i8* @strerror(i32 %28)
%30 = load i32, i32* @errno
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* %29, i32 %30)
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %15
%34 = call i32 @listen(i32 %7, i32 10)
%35 = icmp eq i32 %34, -1
br i1 %35, label %38, label %36
36: ; preds = %33
%37 = bitcast [4096 x i8]* %1 to i8**
br label %44
38: ; preds = %33
%39 = load i32, i32* @errno
%40 = call i8* @strerror(i32 %39)
%41 = load i32, i32* @errno
%42 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i8* %40, i32 %41)
%43 = call i32 @exit(i32 1)
unreachable
44: ; preds = %52, %36
%45 = call i32 @accept(i32 %7, %struct.sockaddr* null, i32* null)
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %53
47: ; preds = %44
%48 = load i32, i32* @errno
%49 = call i8* @strerror(i32 %48)
%50 = load i32, i32* @errno
%51 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i8* %49, i32 %50)
br label %52
52: ; preds = %47, %53
br label %44
53: ; preds = %44
%54 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @.str.4, i64 0, i64 0), i32 %45)
%55 = call i32 @recv(i32 %45, i8** %37, i32 4096, i32 0)
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 %56
store i8 0, i8* %57
%58 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0), i32 %45, i8* %3)
%59 = call i32 @close(i32 %45)
br label %52
}