#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 {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ servaddr;

/* 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 *, int);
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fork();
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int recv(int, char *, int, int /*<<< orphan*/);
int send(int, char *, int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
char *strerror(int);
int main(void) {
  int sockfd, connfd;
  struct sockaddr_in servaddr;
  char buf[10];
  int rcv_n;
  if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    printf("create server socket error!\n");
    exit(1);
  }
  memset(&servaddr, 0, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_port = htons(8000);
  servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  if (-1 == bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr))) {
    printf("bind sockfd error:%s(%d)\n", strerror(errno), errno);
    exit(1);
  }
  if (-1 == listen(sockfd, 10)) {
    printf("listen sockfd error:%s(%d)\n", strerror(errno), errno);
    exit(1);
  }
  printf("=======wait for client's request==========\n");
  while (1) {
    if (-1 == (connfd = accept(sockfd, (struct sockaddr *)NULL, NULL))) {
      printf("accept sockfd error!\n");
      continue;
    }
    rcv_n = recv(connfd, buf, 10, 0);
    if (!fork()) {
      if (-1 == send(connfd, "Hello client!\n", 15, 0)) {
        printf("answer client error!\n");
        exit(1);
      }
      exit(0);
    }
    buf[rcv_n] = '\0';
    printf("recv msg from client: %s\n", buf);
    close(connfd);
    break;
  }
  close(sockfd);
  printf("exit system!\n");
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
mov    0x2dd1(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    122c <main+0x4c>
lea    0xde5(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1080 <memset@plt>
mov    0x2da5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    $0x1f40,%edi
call   1040 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    0x2d84(%rip),%rax
mov    (%rax),%edi
call   1070 <htonl@plt>
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <bind@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    12b8 <main+0xd8>
mov    0x2d3b(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    %rax,%rsi
mov    0x2d2a(%rip),%rax
mov    (%rax),%edx
lea    0xd76(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x8(%rbp),%edi
mov    $0xa,%esi
call   10a0 <listen@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1306 <main+0x126>
mov    0x2ced(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    %rax,%rsi
mov    0x2cdc(%rip),%rax
mov    (%rax),%edx
lea    0xd42(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    0xd46(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x8(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   10c0 <accept@plt>
mov    %eax,%ecx
mov    %ecx,-0xc(%rbp)
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1348 <main+0x168>
lea    0xd43(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1314 <main+0x134>
mov    -0xc(%rbp),%edi
lea    -0x22(%rbp),%rsi
mov    $0xa,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x28(%rbp)
mov    $0x0,%al
call   10f0 <fork@plt>
cmp    $0x0,%eax
jne    13b2 <main+0x1d2>
mov    -0xc(%rbp),%edi
lea    0xd1d(%rip),%rsi
mov    $0xf,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    13ab <main+0x1cb>
lea    0xd0a(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
xor    %edi,%edi
call   10d0 <exit@plt>
movslq -0x28(%rbp),%rax
movb   $0x0,-0x22(%rbp,%rax,1)
lea    -0x22(%rbp),%rsi
lea    0xcf4(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0xc(%rbp),%edi
call   1090 <close@plt>
mov    -0x8(%rbp),%edi
call   1090 <close@plt>
lea    0xcf0(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
xor    %edi,%edi
call   10d0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    0x2de9(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2dc7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
test   %eax,%eax
jns    122c <main+0x3c>
lea    0xea4(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    %eax,%r14d
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1090 <memset@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x10(%rsp)
mov    $0x1f40,%edi
call   1050 <htons@plt>
mov    %eax,0xc(%rsp)
mov    0x2d81(%rip),%rax
mov    (%rax),%edi
call   1080 <htonl@plt>
mov    %eax,0x8(%rsp)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   10c0 <bind@plt>
cmp    $0xffffffff,%eax
jne    1298 <main+0xa8>
mov    0x2d42(%rip),%rbx
mov    (%rbx),%edi
call   10f0 <strerror@plt>
mov    (%rbx),%edx
lea    0xd6a(%rip),%rdi
jmp    12c1 <main+0xd1>
mov    %r14d,%edi
mov    $0xa,%esi
call   10b0 <listen@plt>
cmp    $0xffffffff,%eax
jne    12d5 <main+0xe5>
mov    0x2d17(%rip),%rbx
mov    (%rbx),%edi
call   10f0 <strerror@plt>
mov    (%rbx),%edx
lea    0xd59(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
lea    0xd83(%rip),%rdi
call   1040 <puts@plt>
mov    %r14d,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10d0 <accept@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    131b <main+0x12b>
lea    0xdb1(%rip),%rbx
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1040 <puts@plt>
mov    %r14d,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10d0 <accept@plt>
cmp    $0xffffffff,%eax
je     1300 <main+0x110>
mov    %eax,%ebp
lea    0x16(%rsp),%rsi
mov    %ebp,%edi
mov    $0xa,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1100 <fork@plt>
test   %eax,%eax
jne    1361 <main+0x171>
lea    0xcf4(%rip),%rsi
mov    %ebp,%edi
mov    $0xf,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
cmp    $0xffffffff,%eax
jne    1397 <main+0x1a7>
lea    0xd2e(%rip),%rdi
jmp    121d <main+0x2d>
movslq %ebx,%rax
movb   $0x0,0x16(%rsp,%rax,1)
lea    0xcd5(%rip),%rdi
lea    0x16(%rsp),%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %ebp,%edi
call   10a0 <close@plt>
mov    %r14d,%edi
call   10a0 <close@plt>
lea    0xd0d(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
call   10e0 <exit@plt>

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

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [26 x i8] c"bind sockfd error:%s(%d)\0A\00"
@errno = global i32 0
@.str.2 = [28 x i8] c"listen sockfd error:%s(%d)\0A\00"
@.str.5 = [15 x i8] c"Hello client!\0A\00"
@.str.7 = [26 x i8] c"recv msg from client: %s\0A\00"
@str = [43 x i8] c"=======wait for client's request==========\00"
@str.9 = [21 x i8] c"answer client error!\00"
@str.10 = [13 x i8] c"exit system!\00"
@str.11 = [21 x i8] c"accept sockfd error!\00"
@str.12 = [28 x i8] c"create server socket error!\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca [10 x i8]
%3 = bitcast %struct.sockaddr_in* %1 to i8*
%4 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 0
%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 slt i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.12, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %0
%13 = call i32 @memset(%struct.sockaddr_in* %1, i32 0, i32 12)
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %14, i32* %15
%16 = call i32 @htons(i32 8000)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 %16, i32* %17
%18 = load i32, i32* @INADDR_ANY
%19 = call i32 @htonl(i32 %18)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0, i32 0
store i32 %19, i32* %20
%21 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%22 = call i32 @bind(i32 %7, %struct.sockaddr* %21, i32 12)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %30
24: ; preds = %12
%25 = load i32, i32* @errno
%26 = call i8* @strerror(i32 %25)
%27 = load i32, i32* @errno
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %26, i32 %27)
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %12
%31 = call i32 @listen(i32 %7, i32 10)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %39
33: ; preds = %30
%34 = load i32, i32* @errno
%35 = call i8* @strerror(i32 %34)
%36 = load i32, i32* @errno
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %35, i32 %36)
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %30
%40 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @str, i64 0, i64 0))
%41 = call i32 @accept(i32 %7, %struct.sockaddr* null, i32* null)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %47
43: ; preds = %39, %43
%44 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.11, i64 0, i64 0))
%45 = call i32 @accept(i32 %7, %struct.sockaddr* null, i32* null)
%46 = icmp eq i32 %45, -1
br i1 %46, label %43, label %47
47: ; preds = %43, %39
%48 = phi i32 [ %41, %39 ], [ %45, %43 ]
%49 = call i32 @recv(i32 %48, i8* %4, i32 10, i32 0)
%50 = call i32 (...) @fork()
%51 = icmp eq i32 %50, 0
br i1 %51, label %52, label %60
52: ; preds = %47
%53 = call i32 @send(i32 %48, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i32 15, i32 0)
%54 = icmp eq i32 %53, -1
br i1 %54, label %55, label %58
55: ; preds = %52
%56 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.9, i64 0, i64 0))
%57 = call i32 @exit(i32 1)
unreachable
58: ; preds = %52
%59 = call i32 @exit(i32 0)
unreachable
60: ; preds = %47
%61 = sext i32 %49 to i64
%62 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 %61
store i8 0, i8* %62
%63 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.7, i64 0, i64 0), i8* %4)
%64 = call i32 @close(i32 %48)
%65 = call i32 @close(i32 %7)
%66 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.10, i64 0, i64 0))
%67 = call i32 @exit(i32 0)
unreachable
}