#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*/ 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 /*<<< orphan*/);
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*/ perror(char *);
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(int argc, char **argv) {
  int socket_fd, connect_fd;
  struct sockaddr_in servaddr;
  char buff[4096];
  int n;
  if ((socket_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    printf("create socket error: %s(errno: %d)\n", strerror(errno), errno);
    exit(0);
  }
  memset(&servaddr, 0, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  servaddr.sin_port = htons(8000);
  if (bind(socket_fd, (struct sockaddr *)&servaddr, sizeof(servaddr)) == -1) {
    printf("bind socket error: %s(errno: %d)\n", strerror(errno), errno);
    exit(0);
  }
  if (listen(socket_fd, 10) == -1) {
    printf("listen socket error: %s(errno: %d)\n", strerror(errno), errno);
    exit(0);
  }
  printf("======waiting for client's request======\n");
  while (1) {
    if ((connect_fd = accept(socket_fd, (struct sockaddr *)NULL, NULL)) == -1) {
      printf("accept socket error: %s(errno: %d)", strerror(errno), errno);
      continue;
    }
    n = recv(connect_fd, buff, 4096, 0);
    if (!fork()) {
      if (send(connect_fd, "Hello,you are connected!\n", 26, 0) == -1)
        perror("send error");
      close(connect_fd);
      exit(0);
    }
    buff[n] = '\0';
    printf("recv msg from client: %s\n", buff);
    close(connect_fd);
  }
  close(socket_fd);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
mov    0x2db7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    125d <main+0x6d>
mov    0x2d93(%rip),%rax
mov    (%rax),%edi
call   10f0 <strerror@plt>
mov    %rax,%rsi
mov    0x2d82(%rip),%rax
mov    (%rax),%edx
lea    0xdb1(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
xor    %edi,%edi
call   10e0 <exit@plt>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1080 <memset@plt>
mov    0x2d74(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2d60(%rip),%rax
mov    (%rax),%edi
call   1070 <htonl@plt>
mov    %eax,-0x24(%rbp)
mov    $0x1f40,%edi
call   1040 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <bind@plt>
cmp    $0xffffffff,%eax
jne    12e0 <main+0xf0>
mov    0x2d10(%rip),%rax
mov    (%rax),%edi
call   10f0 <strerror@plt>
mov    %rax,%rsi
mov    0x2cff(%rip),%rax
mov    (%rax),%edx
lea    0xd52(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
xor    %edi,%edi
call   10e0 <exit@plt>
mov    -0x14(%rbp),%edi
mov    $0xa,%esi
call   10a0 <listen@plt>
cmp    $0xffffffff,%eax
jne    1325 <main+0x135>
mov    0x2ccb(%rip),%rax
mov    (%rax),%edi
call   10f0 <strerror@plt>
mov    %rax,%rsi
mov    0x2cba(%rip),%rax
mov    (%rax),%edx
lea    0xd2f(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
xor    %edi,%edi
call   10e0 <exit@plt>
lea    0xd3e(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x14(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   10d0 <accept@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    137b <main+0x18b>
mov    0x2c73(%rip),%rax
mov    (%rax),%edi
call   10f0 <strerror@plt>
mov    %rax,%rsi
mov    0x2c62(%rip),%rax
mov    (%rax),%edx
lea    0xd25(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1333 <main+0x143>
mov    -0x18(%rbp),%edi
lea    -0x1030(%rbp),%rsi
mov    $0x1000,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x1034(%rbp)
mov    $0x0,%al
call   1100 <fork@plt>
cmp    $0x0,%eax
jne    13e1 <main+0x1f1>
mov    -0x18(%rbp),%edi
lea    0xd06(%rip),%rsi
mov    $0x1a,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
cmp    $0xffffffff,%eax
jne    13d2 <main+0x1e2>
lea    0xd04(%rip),%rdi
call   10c0 <perror@plt>
mov    -0x18(%rbp),%edi
call   1090 <close@plt>
xor    %edi,%edi
call   10e0 <exit@plt>
movslq -0x1034(%rbp),%rax
movb   $0x0,-0x1030(%rbp,%rax,1)
lea    -0x1030(%rbp),%rsi
lea    0xcde(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x18(%rbp),%edi
call   1090 <close@plt>
jmp    1333 <main+0x143>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1018,%rsp
mov    0x2dd0(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2dae(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1120 <socket@plt>
cmp    $0xffffffff,%eax
jne    124c <main+0x4c>
mov    0x2d91(%rip),%rbx
mov    (%rbx),%edi
call   1100 <strerror@plt>
mov    (%rbx),%edx
lea    0xdb9(%rip),%rdi
jmp    12de <main+0xde>
mov    %eax,%r12d
mov    %rsp,%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1090 <memset@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x8(%rsp)
mov    0x2d71(%rip),%rax
mov    (%rax),%edi
call   1080 <htonl@plt>
mov    %eax,0x4(%rsp)
mov    $0x1f40,%edi
call   1050 <htons@plt>
mov    %eax,(%rsp)
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   10c0 <bind@plt>
cmp    $0xffffffff,%eax
jne    12b5 <main+0xb5>
mov    0x2d25(%rip),%rbx
mov    (%rbx),%edi
call   1100 <strerror@plt>
mov    (%rbx),%edx
lea    0xd71(%rip),%rdi
jmp    12de <main+0xde>
mov    %r12d,%edi
mov    $0xa,%esi
call   10b0 <listen@plt>
cmp    $0xffffffff,%eax
jne    12ef <main+0xef>
mov    0x2cfa(%rip),%rbx
mov    (%rbx),%edi
call   1100 <strerror@plt>
mov    (%rbx),%edx
lea    0xd68(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
xor    %edi,%edi
call   10f0 <exit@plt>
lea    0xdd6(%rip),%rdi
call   1040 <puts@plt>
mov    0x2cc6(%rip),%r14
lea    0x10(%rsp),%r13
lea    0xda4(%rip),%r15
jmp    132c <main+0x12c>
mov    (%r14),%edi
call   1100 <strerror@plt>
mov    (%r14),%edx
lea    0xd48(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r12d,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10e0 <accept@plt>
cmp    $0xffffffff,%eax
je     1310 <main+0x110>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r13,%rsi
mov    $0x1000,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1110 <fork@plt>
test   %eax,%eax
je     137b <main+0x17b>
movslq %ebx,%rax
movb   $0x0,0x10(%rsp,%rax,1)
mov    %r15,%rdi
mov    %r13,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %ebp,%edi
call   10a0 <close@plt>
jmp    132c <main+0x12c>
lea    0xd0b(%rip),%rsi
mov    %ebp,%edi
mov    $0x1a,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
cmp    $0xffffffff,%eax
jne    13a1 <main+0x1a1>
lea    0xd0b(%rip),%rdi
call   10d0 <perror@plt>
mov    %ebp,%edi
call   10a0 <close@plt>
xor    %edi,%edi
call   10f0 <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 = [42 x i8] c"======waiting for client's request======\0A\00"
@.str.4 = [35 x i8] c"accept socket error: %s(errno: %d)\00"
@.str.5 = [26 x i8] c"Hello,you are connected!\0A\00"
@.str.6 = [11 x i8] c"send error\00"
@.str.7 = [26 x i8] c"recv msg from client: %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.sockaddr_in
%9 = alloca [4096 x i8]
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_STREAM
%13 = call i32 @socket(i32 %11, i32 %12, i32 0)
store i32 %13, i32* %6
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %21
15: ; preds = %2
%16 = load i32, i32* @errno
%17 = call i8* @strerror(i32 %16)
%18 = load i32, i32* @errno
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %17, i32 %18)
%20 = call i32 @exit(i32 0)
unreachable
21: ; preds = %2
%22 = call i32 @memset(%struct.sockaddr_in* %8, i32 0, i32 12)
%23 = load i32, i32* @AF_INET
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %23, i32* %24
%25 = load i32, i32* @INADDR_ANY
%26 = call i32 @htonl(i32 %25)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%28 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %27, i32 0, i32 0
store i32 %26, i32* %28
%29 = call i32 @htons(i32 8000)
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
store i32 %29, i32* %30
%31 = load i32, i32* %6
%32 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%33 = call i32 @bind(i32 %31, %struct.sockaddr* %32, i32 12)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %41
35: ; preds = %21
%36 = load i32, i32* @errno
%37 = call i8* @strerror(i32 %36)
%38 = load i32, i32* @errno
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* %37, i32 %38)
%40 = call i32 @exit(i32 0)
unreachable
41: ; preds = %21
%42 = load i32, i32* %6
%43 = call i32 @listen(i32 %42, i32 10)
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %51
45: ; preds = %41
%46 = load i32, i32* @errno
%47 = call i8* @strerror(i32 %46)
%48 = load i32, i32* @errno
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i8* %47, i32 %48)
%50 = call i32 @exit(i32 0)
unreachable
51: ; preds = %41
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0))
br label %53
53: ; preds = %51, %57, %78
%54 = load i32, i32* %6
%55 = call i32 @accept(i32 %54, %struct.sockaddr* null, i32* null)
store i32 %55, i32* %7
%56 = icmp eq i32 %55, -1
br i1 %56, label %57, label %62
57: ; preds = %53
%58 = load i32, i32* @errno
%59 = call i8* @strerror(i32 %58)
%60 = load i32, i32* @errno
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), i8* %59, i32 %60)
br label %53
62: ; preds = %53
%63 = load i32, i32* %7
%64 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%65 = call i32 @recv(i32 %63, i8* %64, i32 4096, i32 0)
store i32 %65, i32* %10
%66 = call i32 (...) @fork()
%67 = icmp ne i32 %66, 0
br i1 %67, label %78, label %68
68: ; preds = %62
%69 = load i32, i32* %7
%70 = call i32 @send(i32 %69, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0), i32 26, i32 0)
%71 = icmp eq i32 %70, -1
br i1 %71, label %72, label %74
72: ; preds = %68
%73 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0))
br label %74
74: ; preds = %72, %68
%75 = load i32, i32* %7
%76 = call i32 @close(i32 %75)
%77 = call i32 @exit(i32 0)
unreachable
78: ; preds = %62
%79 = load i32, i32* %10
%80 = sext i32 %79 to i64
%81 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 %80
store i8 0, i8* %81
%82 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%83 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.7, i64 0, i64 0), i8* %82)
%84 = load i32, i32* %7
%85 = call i32 @close(i32 %84)
br label %53
}

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