#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*/ buffer;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
char *inet_ntoa(TYPE_1__);
int listen(int, int);
int /*<<< orphan*/ memset(char *, char, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *, ...);
int /*<<< orphan*/ puts(char *);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(void) {
  int sockfd, cli_fd;
  char buffer[80];
  struct sockaddr_in server_addr;
  struct sockaddr_in client_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);
  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) {
    int sock_size = sizeof(struct sockaddr);
    if ((cli_fd = accept(sockfd, (struct sockaddr *)&client_addr,
                         &sock_size)) == -1) {
      perror("accept client error");
      return 4;
    }
    printf("server get connection from %s\n", inet_ntoa(client_addr.sin_addr));
    memset(buffer, '\0', sizeof(buffer));
    if (read(cli_fd, buffer, sizeof(buffer)) == -1) {
      perror("read data from client error");
      return 5;
    }
    printf("server receive data from %s is: %s",
           inet_ntoa(client_addr.sin_addr), buffer);
    close(cli_fd);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2def(%rip),%rax
mov    (%rax),%edi
mov    0x2dce(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    122f <main+0x4f>
lea    0xde2(%rip),%rdi
call   10d0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    13bd <main+0x1dd>
lea    0xdde(%rip),%rdi
call   1030 <puts@plt>
lea    -0x70(%rbp),%rdi
mov    $0xc,%esi
call   10e0 <bzero@plt>
mov    0x2d98(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x68(%rbp)
mov    0x2d84(%rip),%rax
mov    (%rax),%edi
call   1070 <htonl@plt>
mov    %eax,-0x70(%rbp)
mov    $0x22b8,%edi
call   1050 <htons@plt>
mov    %eax,-0x6c(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x70(%rbp),%rsi
mov    $0x4,%edx
call   10c0 <bind@plt>
cmp    $0xffffffff,%eax
jne    12a5 <main+0xc5>
lea    0xd94(%rip),%rdi
call   10d0 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    13bd <main+0x1dd>
lea    0xd8e(%rip),%rdi
call   1030 <puts@plt>
mov    -0x8(%rbp),%edi
mov    $0x5,%esi
call   10b0 <listen@plt>
cmp    $0xffffffff,%eax
jne    12df <main+0xff>
lea    0xd7e(%rip),%rdi
call   10d0 <perror@plt>
movl   $0x3,-0x4(%rbp)
jmp    13bd <main+0x1dd>
lea    0xd87(%rip),%rdi
call   1030 <puts@plt>
lea    0xd9c(%rip),%rdi
call   1030 <puts@plt>
movl   $0x4,-0x84(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x80(%rbp),%rsi
lea    -0x84(%rbp),%rdx
call   10f0 <accept@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    1338 <main+0x158>
lea    0xd78(%rip),%rdi
call   10d0 <perror@plt>
movl   $0x4,-0x4(%rbp)
jmp    13bd <main+0x1dd>
mov    -0x80(%rbp),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,%rsi
lea    0xd69(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x60(%rbp),%rdi
xor    %esi,%esi
mov    $0x50,%edx
call   1080 <memset@plt>
mov    -0xc(%rbp),%edi
lea    -0x60(%rbp),%rsi
mov    $0x50,%edx
call   10a0 <read@plt>
cmp    $0xffffffff,%eax
jne    1393 <main+0x1b3>
lea    0xd50(%rip),%rdi
call   10d0 <perror@plt>
movl   $0x5,-0x4(%rbp)
jmp    13bd <main+0x1dd>
mov    -0x80(%rbp),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,%rsi
lea    -0x60(%rbp),%rdx
lea    0xd45(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0xc(%rbp),%edi
call   1090 <close@plt>
jmp    12f7 <main+0x117>
mov    -0x4(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x88,%rsp
mov    0x2df0(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2dce(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
cmp    $0xffffffff,%eax
jne    1226 <main+0x46>
lea    0xde9(%rip),%rdi
call   10d0 <perror@plt>
mov    $0x1,%ebp
jmp    13c9 <main+0x1e9>
mov    %eax,%r13d
lea    0xde4(%rip),%rdi
call   1030 <puts@plt>
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
mov    $0xc,%esi
call   10e0 <bzero@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x28(%rsp)
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
call   1070 <htonl@plt>
mov    %eax,0x20(%rsp)
mov    $0x22b8,%edi
call   1050 <htons@plt>
mov    %eax,0x24(%rsp)
mov    %r13d,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   10c0 <bind@plt>
cmp    $0xffffffff,%eax
jne    1299 <main+0xb9>
lea    0xd9e(%rip),%rdi
call   10d0 <perror@plt>
mov    $0x2,%ebp
jmp    13c9 <main+0x1e9>
lea    0xd9a(%rip),%rdi
call   1030 <puts@plt>
mov    %r13d,%edi
mov    $0x5,%esi
call   10b0 <listen@plt>
cmp    $0xffffffff,%eax
jne    12cd <main+0xed>
lea    0xd8e(%rip),%rdi
call   10d0 <perror@plt>
mov    $0x3,%ebp
jmp    13c9 <main+0x1e9>
lea    0xd99(%rip),%rdi
call   1030 <puts@plt>
lea    0xdae(%rip),%rdi
call   1030 <puts@plt>
movl   $0x4,0xc(%rsp)
lea    0x10(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %r13d,%edi
call   10f0 <accept@plt>
mov    $0x4,%ebp
lea    0xd94(%rip),%rdi
cmp    $0xffffffff,%eax
je     13c4 <main+0x1e4>
mov    %eax,%ebx
lea    0x30(%rsp),%rbp
lea    0xdcc(%rip),%r14
lea    0x10(%rsp),%r15
lea    0xc(%rsp),%r12
nopl   0x0(%rax)
mov    0x10(%rsp),%edi
call   1040 <inet_ntoa@plt>
lea    0xd73(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x50,%edx
call   1080 <memset@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x50,%edx
call   10a0 <read@plt>
cmp    $0xffffffff,%eax
je     13b8 <main+0x1d8>
mov    0x10(%rsp),%edi
call   1040 <inet_ntoa@plt>
mov    %r14,%rdi
mov    %rax,%rsi
mov    %rbp,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebx,%edi
call   1090 <close@plt>
movl   $0x4,0xc(%rsp)
mov    %r13d,%edi
mov    %r15,%rsi
mov    %r12,%rdx
call   10f0 <accept@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    1330 <main+0x150>
lea    0xcee(%rip),%rdi
mov    $0x4,%ebp
jmp    13c4 <main+0x1e4>
mov    $0x5,%ebp
lea    0xd0e(%rip),%rdi
call   10d0 <perror@plt>
mov    %ebp,%eax
add    $0x88,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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 = [20 x i8] c"create socket error\00"
@.str.1 = [20 x i8] c"create socket done!\00"
@INADDR_ANY = 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"
@.str.7 = [20 x i8] c"accept client error\00"
@.str.8 = [31 x i8] c"server get connection from %s\0A\00"
@.str.9 = [28 x i8] c"read data from client error\00"
@.str.10 = [35 x i8] c"server receive data from %s is: %s\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [80 x i8]
%5 = alloca %struct.sockaddr_in
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
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 %14
12: ; preds = %0
%13 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %71
14: ; preds = %0
%15 = call i32 @puts(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%16 = call i32 @bzero(%struct.sockaddr_in* %5, i32 12)
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 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* %5, i32 0, i32 0
%22 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %21, i32 0, i32 0
store i32 %20, i32* %22
%23 = call i32 @htons(i32 8888)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
store i32 %23, i32* %24
%25 = load i32, i32* %2
%26 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%27 = call i32 @bind(i32 %25, %struct.sockaddr* %26, i32 4)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %31
29: ; preds = %14
%30 = 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 %71
31: ; preds = %14
%32 = call i32 @puts(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
%33 = load i32, i32* %2
%34 = call i32 @listen(i32 %33, i32 5)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %38
36: ; preds = %31
%37 = 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 %71
38: ; preds = %31
%39 = call i32 @puts(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0))
%40 = call i32 @puts(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.6, i64 0, i64 0))
br label %41
41: ; preds = %38, %62
store i32 4, i32* %7
%42 = load i32, i32* %2
%43 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%44 = call i32 @accept(i32 %42, %struct.sockaddr* %43, i32* %7)
store i32 %44, i32* %3
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %48
46: ; preds = %41
%47 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.7, i64 0, i64 0))
store i32 4, i32* %1
br label %71
48: ; preds = %41
%49 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%50 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %49, i32 0, i32 0
%51 = load i32, i32* %50
%52 = call i8* @inet_ntoa(i32 %51)
%53 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.8, i64 0, i64 0), i8* %52)
%54 = getelementptr inbounds [80 x i8], [80 x i8]* %4, i64 0, i64 0
%55 = call i32 @memset(i8* %54, i8 signext 0, i32 80)
%56 = load i32, i32* %3
%57 = getelementptr inbounds [80 x i8], [80 x i8]* %4, i64 0, i64 0
%58 = call i32 @read(i32 %56, i8* %57, i32 80)
%59 = icmp eq i32 %58, -1
br i1 %59, label %60, label %62
60: ; preds = %48
%61 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.9, i64 0, i64 0))
store i32 5, i32* %1
br label %71
62: ; preds = %48
%63 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%64 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %63, i32 0, i32 0
%65 = load i32, i32* %64
%66 = call i8* @inet_ntoa(i32 %65)
%67 = getelementptr inbounds [80 x i8], [80 x i8]* %4, i64 0, i64 0
%68 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.10, i64 0, i64 0), i8* %66, i8* %67)
%69 = load i32, i32* %3
%70 = call i32 @close(i32 %69)
br label %41
71: ; preds = %60, %46, %36, %29, %12
%72 = load i32, i32* %1
ret i32 %72
}

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