#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_zero;
  int /*<<< orphan*/ sin_port;
  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 *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(int /*<<< orphan*/ *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fork();
int /*<<< orphan*/ htons(int);
char *inet_ntoa(TYPE_1__);
int listen(int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int send(int, char *, int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
main() {
  int sock_fd, client_fd;
  int sin_size;
  struct sockaddr_in my_addr;
  struct sockaddr_in remote_addr;
  if ((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    perror("socket创建出错！");
    exit(1);
  }
  my_addr.sin_family = AF_INET;
  my_addr.sin_port = htons(3333);
  my_addr.sin_addr.s_addr = INADDR_ANY;
  bzero(&(my_addr.sin_zero), 8);
  if (bind(sock_fd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) ==
      -1) {
    perror("bind出错！");
    exit(1);
  }
  if (listen(sock_fd, 10) == -1) {
    perror("listen出错！");
    exit(1);
  }
  while (1) {
    sin_size = sizeof(struct sockaddr_in);
    if ((client_fd = accept(sock_fd, (struct sockaddr *)&remote_addr,
                            &sin_size)) == -1) {
      perror("accept出错");
      continue;
    }
    printf("received a connection from %s\n", inet_ntoa(remote_addr.sin_addr));
    if (!fork()) {
      if (send(client_fd, "Hello, you are connected!\n", 26, 0) == -1) {
        perror("send出错！");
      }
      close(client_fd);
      exit(0);
    }
    close(client_fd);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
mov    0x2de1(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    121a <main+0x4a>
lea    0xdf5(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    0x2dc7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    $0xd05,%edi
call   1040 <htons@plt>
mov    %eax,-0x18(%rbp)
mov    0x2da6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
lea    -0x20(%rbp),%rdi
add    $0x4,%rdi
mov    $0x8,%esi
call   10b0 <bzero@plt>
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   1090 <bind@plt>
cmp    $0xffffffff,%eax
jne    1281 <main+0xb1>
lea    0xda4(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x8(%rbp),%edi
mov    $0xa,%esi
call   1080 <listen@plt>
cmp    $0xffffffff,%eax
jne    12ad <main+0xdd>
lea    0xd86(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
jmp    12b2 <main+0xe2>
movl   $0x10,-0x10(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
lea    -0x10(%rbp),%rdx
call   10c0 <accept@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    12e6 <main+0x116>
lea    0xd58(%rip),%rdi
call   10a0 <perror@plt>
jmp    12b2 <main+0xe2>
mov    -0x30(%rbp),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,%rsi
lea    0xd49(%rip),%rdi
call   1060 <printf@plt>
mov    $0x0,%al
call   10e0 <fork@plt>
cmp    $0x0,%eax
jne    1347 <main+0x177>
mov    -0xc(%rbp),%edi
lea    0xd49(%rip),%rsi
mov    $0x1a,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
cmp    $0xffffffff,%eax
jne    1338 <main+0x168>
lea    0xd48(%rip),%rdi
call   10a0 <perror@plt>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
xor    %edi,%edi
call   10d0 <exit@plt>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
jmp    12b2 <main+0xe2>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    0x2e03(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2de1(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
cmp    $0xffffffff,%eax
jne    1206 <main+0x36>
lea    0xdfc(%rip),%rdi
jmp    1270 <main+0xa0>
mov    %eax,%ebx
mov    0x0(%rbp),%eax
mov    %eax,0x14(%rsp)
mov    $0xd05,%edi
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
mov    0x2dbc(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
lea    0xc(%rsp),%rdi
mov    $0x8,%esi
call   10b0 <bzero@plt>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   1090 <bind@plt>
cmp    $0xffffffff,%eax
jne    1258 <main+0x88>
lea    0xdc0(%rip),%rdi
jmp    1270 <main+0xa0>
mov    %ebx,%edi
mov    $0xa,%esi
call   1080 <listen@plt>
cmp    $0xffffffff,%eax
jne    127f <main+0xaf>
lea    0xdb4(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    0x18(%rsp),%r14
lea    0x4(%rsp),%r15
lea    0xda4(%rip),%r12
lea    0xdaa(%rip),%r13
jmp    12b0 <main+0xe0>
mov    %r12,%rdi
call   10a0 <perror@plt>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movl   $0x10,0x4(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   10c0 <accept@plt>
cmp    $0xffffffff,%eax
je     1299 <main+0xc9>
mov    %eax,%ebp
mov    0x18(%rsp),%edi
call   1030 <inet_ntoa@plt>
mov    %r13,%rdi
mov    %rax,%rsi
call   1060 <printf@plt>
xor    %eax,%eax
call   10e0 <fork@plt>
test   %eax,%eax
je     12f4 <main+0x124>
mov    %ebp,%edi
call   1070 <close@plt>
jmp    12b0 <main+0xe0>
lea    0xd65(%rip),%rsi
mov    %ebp,%edi
mov    $0x1a,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
cmp    $0xffffffff,%eax
jne    131a <main+0x14a>
lea    0xd66(%rip),%rdi
call   10a0 <perror@plt>
mov    %ebp,%edi
call   1070 <close@plt>
xor    %edi,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [22 x i8] c"socket\E5\88\9B\E5\BB\BA\E5\87\BA\E9\94\99\EF\BC\81\00"
@INADDR_ANY = global i32 0
@.str.1 = [14 x i8] c"bind\E5\87\BA\E9\94\99\EF\BC\81\00"
@.str.2 = [16 x i8] c"listen\E5\87\BA\E9\94\99\EF\BC\81\00"
@.str.3 = [13 x i8] c"accept\E5\87\BA\E9\94\99\00"
@.str.4 = [31 x i8] c"received a connection from %s\0A\00"
@.str.5 = [27 x i8] c"Hello, you are connected!\0A\00"
@.str.6 = [14 x i8] c"send\E5\87\BA\E9\94\99\EF\BC\81\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = alloca %struct.sockaddr_in
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 %14
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %0
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 3
store i32 %15, i32* %16
%17 = call i32 @htons(i32 3333)
%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 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
%21 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %20, i32 0, i32 0
store i32 %19, i32* %21
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
%23 = call i32 @bzero(i32* %22, i32 8)
%24 = load i32, i32* %2
%25 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%26 = call i32 @bind(i32 %24, %struct.sockaddr* %25, i32 4)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %31
28: ; preds = %14
%29 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %14
%32 = load i32, i32* %2
%33 = call i32 @listen(i32 %32, i32 10)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %38
35: ; preds = %31
%36 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %31
br label %39
39: ; preds = %38, %44, %64
store i32 16, i32* %4
%40 = load i32, i32* %2
%41 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%42 = call i32 @accept(i32 %40, %struct.sockaddr* %41, i32* %4)
store i32 %42, i32* %3
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %46
44: ; preds = %39
%45 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
br label %39
46: ; preds = %39
%47 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%48 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %47, i32 0, i32 0
%49 = load i32, i32* %48
%50 = call i8* @inet_ntoa(i32 %49)
%51 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i8* %50)
%52 = call i32 (...) @fork()
%53 = icmp ne i32 %52, 0
br i1 %53, label %64, label %54
54: ; preds = %46
%55 = load i32, i32* %3
%56 = call i32 @send(i32 %55, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0), i32 26, i32 0)
%57 = icmp eq i32 %56, -1
br i1 %57, label %58, label %60
58: ; preds = %54
%59 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0))
br label %60
60: ; preds = %58, %54
%61 = load i32, i32* %3
%62 = call i32 @close(i32 %61)
%63 = call i32 @exit(i32 0)
unreachable
64: ; preds = %46
%65 = load i32, i32* %3
%66 = call i32 @close(i32 %65)
br label %39
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [22 x i8] c"socket\E5\88\9B\E5\BB\BA\E5\87\BA\E9\94\99\EF\BC\81\00"
@INADDR_ANY = global i32 0
@.str.1 = [14 x i8] c"bind\E5\87\BA\E9\94\99\EF\BC\81\00"
@.str.2 = [16 x i8] c"listen\E5\87\BA\E9\94\99\EF\BC\81\00"
@.str.3 = [13 x i8] c"accept\E5\87\BA\E9\94\99\00"
@.str.4 = [31 x i8] c"received a connection from %s\0A\00"
@.str.5 = [27 x i8] c"Hello, you are connected!\0A\00"
@.str.6 = [14 x i8] c"send\E5\87\BA\E9\94\99\EF\BC\81\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_in
%3 = alloca %struct.sockaddr_in
%4 = bitcast i32* %1 to i8*
%5 = bitcast %struct.sockaddr_in* %2 to i8*
%6 = bitcast %struct.sockaddr_in* %3 to i8*
%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 %14
11: ; preds = %0
%12 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %0
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 3
store i32 %15, i32* %16
%17 = tail call i32 @htons(i32 3333)
%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 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0, i32 0
store i32 %19, i32* %20
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
%22 = call i32 @bzero(i32* %21, i32 8)
%23 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%24 = call i32 @bind(i32 %9, %struct.sockaddr* %23, i32 4)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %29
26: ; preds = %14
%27 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %14
%30 = call i32 @listen(i32 %9, i32 10)
%31 = icmp eq i32 %30, -1
br i1 %31, label %35, label %32
32: ; preds = %29
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
%34 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
br label %38
35: ; preds = %29
%36 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %43, %32
store i32 16, i32* %1
%39 = call i32 @accept(i32 %9, %struct.sockaddr* %34, i32* %1)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %44
41: ; preds = %38
%42 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
br label %43
43: ; preds = %41, %58
br label %38
44: ; preds = %38
%45 = load i32, i32* %33
%46 = call i8* @inet_ntoa(i32 %45)
%47 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i8* %46)
%48 = call i32 (...) @fork()
%49 = icmp eq i32 %48, 0
br i1 %49, label %50, label %58
50: ; preds = %44
%51 = call i32 @send(i32 %39, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0), i32 26, i32 0)
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %55
53: ; preds = %50
%54 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0))
br label %55
55: ; preds = %53, %50
%56 = call i32 @close(i32 %39)
%57 = call i32 @exit(i32 0)
unreachable
58: ; preds = %44
%59 = call i32 @close(i32 %39)
br label %43
}