#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;
};
typedef int /*<<< orphan*/ buffer;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ EINTR;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int accept(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ close(int);
scalar_t__ errno;
scalar_t__ fork();
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int listen(int, int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int /*<<< orphan*/ sleep(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(void) {
  int sockfd, cli_fd;
  struct sockaddr_in server_addr;
  if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    perror("create socket error");
    return 1;
  }
  puts("create socket done!");
  bzero(&server_addr, sizeof(struct sockaddr_in));
  server_addr.sin_family = AF_INET;
  server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  server_addr.sin_port = htons(8888);
  int optval = 1;
  setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(int));
  if (bind(sockfd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) ==
      -1) {
    perror("bind socket error");
    return 2;
  }
  puts("bind socket done!");
  if (listen(sockfd, 5) == -1) {
    perror("set the server max connect error");
    return 3;
  }
  puts("set the server max connect done!");
  puts("wait for connect");
  while (1) {
    cli_fd = accept(sockfd, NULL, NULL);
    if ((cli_fd < 0) && (errno == EINTR)) {
      continue;
    } else if (cli_fd < 0) {
      perror("client connect error");
      sleep(1);
      continue;
    }
    if (fork() == 0) {
      char buffer[80];
      memset(buffer, 0, sizeof(buffer));
      read(cli_fd, buffer, sizeof(buffer));
      printf("server receive: %s", buffer);
      close(sockfd);
      close(cli_fd);
      return 0;
    } else {
      close(cli_fd);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2dca(%rip),%rax
mov    (%rax),%edi
mov    0x2da9(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1120 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    124c <main+0x4c>
lea    0xdc5(%rip),%rdi
call   10d0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    140f <main+0x20f>
lea    0xdc1(%rip),%rdi
call   1030 <puts@plt>
lea    -0x18(%rbp),%rdi
mov    $0xc,%esi
call   10e0 <bzero@plt>
mov    0x2d73(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2d5f(%rip),%rax
mov    (%rax),%edi
call   1070 <htonl@plt>
mov    %eax,-0x14(%rbp)
mov    $0x22b8,%edi
call   1050 <htons@plt>
mov    %eax,-0x18(%rbp)
movl   $0x1,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2d47(%rip),%rax
mov    (%rax),%esi
mov    0x2d06(%rip),%rax
mov    (%rax),%edx
lea    -0x1c(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   10c0 <bind@plt>
cmp    $0xffffffff,%eax
jne    12ed <main+0xed>
lea    0xd4c(%rip),%rdi
call   10d0 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    140f <main+0x20f>
lea    0xd46(%rip),%rdi
call   1030 <puts@plt>
mov    -0x8(%rbp),%edi
mov    $0x5,%esi
call   10b0 <listen@plt>
cmp    $0xffffffff,%eax
jne    1327 <main+0x127>
lea    0xd36(%rip),%rdi
call   10d0 <perror@plt>
movl   $0x3,-0x4(%rbp)
jmp    140f <main+0x20f>
lea    0xd3f(%rip),%rdi
call   1030 <puts@plt>
lea    0xd54(%rip),%rdi
call   1030 <puts@plt>
mov    -0x8(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   10f0 <accept@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    137a <main+0x17a>
mov    0x2c5e(%rip),%rax
mov    (%rax),%rax
mov    0x2c4c(%rip),%rcx
cmp    (%rcx),%rax
jne    137a <main+0x17a>
jmp    133f <main+0x13f>
cmpl   $0x0,-0xc(%rbp)
jge    139f <main+0x19f>
lea    0xd14(%rip),%rdi
call   10d0 <perror@plt>
mov    $0x1,%edi
call   1100 <sleep@plt>
jmp    133f <main+0x13f>
jmp    13a4 <main+0x1a4>
mov    $0x0,%al
call   1110 <fork@plt>
cmp    $0x0,%rax
jne    1402 <main+0x202>
lea    -0x70(%rbp),%rdi
xor    %esi,%esi
mov    $0x50,%edx
call   1080 <memset@plt>
mov    -0xc(%rbp),%edi
lea    -0x70(%rbp),%rsi
mov    $0x50,%edx
call   10a0 <read@plt>
lea    -0x70(%rbp),%rsi
lea    0xcd3(%rip),%rdi
call   1060 <printf@plt>
mov    -0x8(%rbp),%edi
call   1090 <close@plt>
mov    -0xc(%rbp),%edi
call   1090 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    140f <main+0x20f>
mov    -0xc(%rbp),%edi
call   1090 <close@plt>
jmp    133f <main+0x13f>
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x70,%rsp
mov    0x2dcd(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2dab(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1120 <socket@plt>
cmp    $0xffffffff,%eax
je     13a6 <main+0x1a6>
mov    %eax,%ebx
lea    0xddc(%rip),%rdi
call   1030 <puts@plt>
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    $0xc,%esi
call   10e0 <bzero@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x18(%rsp)
mov    0x2d7b(%rip),%rax
mov    (%rax),%edi
call   1070 <htonl@plt>
mov    %eax,0x14(%rsp)
mov    $0x22b8,%edi
call   1050 <htons@plt>
mov    %eax,0x10(%rsp)
movl   $0x1,0xc(%rsp)
mov    0x2d63(%rip),%rax
mov    (%rax),%esi
mov    0x2d22(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   10c0 <bind@plt>
cmp    $0xffffffff,%eax
je     13ba <main+0x1ba>
lea    0xd79(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
mov    $0x5,%esi
call   10b0 <listen@plt>
cmp    $0xffffffff,%eax
je     13ce <main+0x1ce>
lea    0xd8b(%rip),%rdi
call   1030 <puts@plt>
lea    0xda0(%rip),%rdi
call   1030 <puts@plt>
mov    0x2cc6(%rip),%r15
mov    0x2cb7(%rip),%r12
lea    0xd97(%rip),%r14
jmp    1319 <main+0x119>
nopw   0x0(%rax,%rax,1)
mov    (%r15),%rax
cmp    (%r12),%rax
jne    133f <main+0x13f>
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10f0 <accept@plt>
test   %eax,%eax
js     1310 <main+0x110>
mov    %eax,%ebp
xor    %eax,%eax
call   1110 <fork@plt>
test   %rax,%rax
je     1353 <main+0x153>
mov    %ebp,%edi
call   1090 <close@plt>
jmp    1319 <main+0x119>
mov    %r14,%rdi
call   10d0 <perror@plt>
mov    $0x1,%edi
call   1100 <sleep@plt>
jmp    1319 <main+0x119>
lea    0x20(%rsp),%r15
xor    %r14d,%r14d
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x50,%edx
call   1080 <memset@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x50,%edx
call   10a0 <read@plt>
lea    0xd34(%rip),%rdi
mov    %r15,%rsi
call   1060 <printf@plt>
mov    %ebx,%edi
call   1090 <close@plt>
mov    %ebp,%edi
call   1090 <close@plt>
mov    %r14d,%eax
add    $0x70,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xc53(%rip),%rdi
call   10d0 <perror@plt>
mov    $0x1,%r14d
jmp    1396 <main+0x196>
lea    0xc67(%rip),%rdi
call   10d0 <perror@plt>
mov    $0x2,%r14d
jmp    1396 <main+0x196>
lea    0xc77(%rip),%rdi
call   10d0 <perror@plt>
mov    $0x3,%r14d
jmp    1396 <main+0x196>

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 = [20 x i8] c"create socket error\00"
@.str.1 = [20 x i8] c"create socket done!\00"
@INADDR_ANY = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.2 = [18 x i8] c"bind socket error\00"
@.str.3 = [18 x i8] c"bind socket done!\00"
@.str.4 = [33 x i8] c"set the server max connect error\00"
@.str.5 = [33 x i8] c"set the server max connect done!\00"
@.str.6 = [17 x i8] c"wait for connect\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.7 = [21 x i8] c"client connect error\00"
@.str.8 = [19 x i8] c"server receive: %s\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca [80 x i8]
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 eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %80
13: ; preds = %0
%14 = call i32 @puts(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%15 = call i32 @bzero(%struct.sockaddr_in* %4, 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 = load i32, i32* @INADDR_ANY
%19 = call i32 @htonl(i32 %18)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
%21 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %20, i32 0, i32 0
store i32 %19, i32* %21
%22 = call i32 @htons(i32 8888)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
store i32 %22, i32* %23
store i32 1, i32* %5
%24 = load i32, i32* %2
%25 = load i32, i32* @SOL_SOCKET
%26 = load i32, i32* @SO_REUSEADDR
%27 = call i32 @setsockopt(i32 %24, i32 %25, i32 %26, i32* %5, i32 4)
%28 = load i32, i32* %2
%29 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%30 = call i32 @bind(i32 %28, %struct.sockaddr* %29, i32 4)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %34
32: ; preds = %13
%33 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
store i32 2, i32* %1
br label %80
34: ; preds = %13
%35 = call i32 @puts(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
%36 = load i32, i32* %2
%37 = call i32 @listen(i32 %36, i32 5)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %41
39: ; preds = %34
%40 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0))
store i32 3, i32* %1
br label %80
41: ; preds = %34
%42 = call i32 @puts(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0))
%43 = call i32 @puts(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.6, i64 0, i64 0))
br label %44
44: ; preds = %41, %53, %57, %79
%45 = load i32, i32* %2
%46 = call i32 @accept(i32 %45, i32* null, i32* null)
store i32 %46, i32* %3
%47 = load i32, i32* %3
%48 = icmp slt i32 %47, 0
br i1 %48, label %49, label %54
49: ; preds = %44
%50 = load i64, i64* @errno
%51 = load i64, i64* @EINTR
%52 = icmp eq i64 %50, %51
br i1 %52, label %53, label %54
53: ; preds = %49
br label %44
54: ; preds = %49, %44
%55 = load i32, i32* %3
%56 = icmp slt i32 %55, 0
br i1 %56, label %57, label %60
57: ; preds = %54
%58 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.7, i64 0, i64 0))
%59 = call i32 @sleep(i32 1)
br label %44
60: ; preds = %54
br label %61
61: ; preds = %60
%62 = call i64 (...) @fork()
%63 = icmp eq i64 %62, 0
br i1 %63, label %64, label %76
64: ; preds = %61
%65 = getelementptr inbounds [80 x i8], [80 x i8]* %6, i64 0, i64 0
%66 = call i32 @memset(i8* %65, i32 0, i32 80)
%67 = load i32, i32* %3
%68 = getelementptr inbounds [80 x i8], [80 x i8]* %6, i64 0, i64 0
%69 = call i32 @read(i32 %67, i8* %68, i32 80)
%70 = getelementptr inbounds [80 x i8], [80 x i8]* %6, i64 0, i64 0
%71 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.8, i64 0, i64 0), i8* %70)
%72 = load i32, i32* %2
%73 = call i32 @close(i32 %72)
%74 = load i32, i32* %3
%75 = call i32 @close(i32 %74)
store i32 0, i32* %1
br label %80
76: ; preds = %61
%77 = load i32, i32* %3
%78 = call i32 @close(i32 %77)
br label %79
79: ; preds = %76
br label %44
80: ; preds = %64, %39, %32, %11
%81 = load i32, i32* %1
ret i32 %81
}

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 = [20 x i8] c"create socket error\00"
@.str.1 = [20 x i8] c"create socket done!\00"
@INADDR_ANY = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.2 = [18 x i8] c"bind socket error\00"
@.str.3 = [18 x i8] c"bind socket done!\00"
@.str.4 = [33 x i8] c"set the server max connect error\00"
@.str.5 = [33 x i8] c"set the server max connect done!\00"
@.str.6 = [17 x i8] c"wait for connect\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.7 = [21 x i8] c"client connect error\00"
@.str.8 = [19 x i8] c"server receive: %s\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca i32
%3 = alloca [80 x i8]
%4 = bitcast %struct.sockaddr_in* %1 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 %11
9: ; preds = %0
%10 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %64
11: ; preds = %0
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%13 = call i32 @bzero(%struct.sockaddr_in* %1, 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 = load i32, i32* @INADDR_ANY
%17 = tail call i32 @htonl(i32 %16)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1, i32 0
store i32 %17, i32* %18
%19 = tail call i32 @htons(i32 8888)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i32 %19, i32* %20
%21 = bitcast i32* %2 to i8*
store i32 1, i32* %2
%22 = load i32, i32* @SOL_SOCKET
%23 = load i32, i32* @SO_REUSEADDR
%24 = call i32 @setsockopt(i32 %7, i32 %22, i32 %23, i32* %2, i32 4)
%25 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%26 = call i32 @bind(i32 %7, %struct.sockaddr* %25, i32 4)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %30
28: ; preds = %11
%29 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
br label %62
30: ; preds = %11
%31 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
%32 = call i32 @listen(i32 %7, i32 5)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %36
34: ; preds = %30
%35 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0))
br label %62
36: ; preds = %30
%37 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0))
%38 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.6, i64 0, i64 0))
br label %39
39: ; preds = %49, %36
%40 = call i32 @accept(i32 %7, i32* null, i32* null)
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %50
42: ; preds = %39
%43 = load i64, i64* @errno
%44 = load i64, i64* @EINTR
%45 = icmp eq i64 %43, %44
br i1 %45, label %49, label %46
46: ; preds = %42
%47 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.7, i64 0, i64 0))
%48 = call i32 @sleep(i32 1)
br label %49
49: ; preds = %46, %60, %42
br label %39
50: ; preds = %39
%51 = call i64 (...) @fork()
%52 = icmp eq i64 %51, 0
br i1 %52, label %53, label %60
53: ; preds = %50
%54 = getelementptr inbounds [80 x i8], [80 x i8]* %3, i64 0, i64 0
%55 = call i32 @memset(i8* %54, i32 0, i32 80)
%56 = call i32 @read(i32 %40, i8* %54, i32 80)
%57 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.8, i64 0, i64 0), i8* %54)
%58 = call i32 @close(i32 %7)
%59 = call i32 @close(i32 %40)
br label %62
60: ; preds = %50
%61 = call i32 @close(i32 %40)
br label %49
62: ; preds = %53, %34, %28
%63 = phi i32 [ 2, %28 ], [ 3, %34 ], [ 0, %53 ]
br label %64
64: ; preds = %62, %9
%65 = phi i32 [ 1, %9 ], [ %63, %62 ]
ret i32 %65
}