#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 socklen_t;
typedef int /*<<< orphan*/ server_addr;
typedef int /*<<< orphan*/ client_addr;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htons(int);
int listen(int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ write(int, char *, int);
int main(void) {
  int sd;
  sd = socket(PF_INET, SOCK_STREAM, 0);
  if (sd < 0) {
    perror("socket");
    exit(1);
  }
  struct sockaddr_in server_addr;
  server_addr.sin_family = PF_INET;
  server_addr.sin_port = htons(1234);
  server_addr.sin_addr.s_addr = INADDR_ANY;
  int ret;
  ret = bind(sd, (struct sockaddr *)&server_addr, sizeof(server_addr));
  if (ret < 0) {
    perror("bind");
    exit(1);
  }
  ret = listen(sd, 20);
  if (ret < 0) {
    perror("listen");
    exit(1);
  }
  int new_sd;
  struct sockaddr_in client_addr;
  socklen_t addr_len = sizeof(client_addr);
  while (1) {
    new_sd = accept(sd, (struct sockaddr *)&client_addr, &addr_len);
    if (new_sd < 0) {
      perror("accept");
      continue;
    }
    char buf[1024];
    read(new_sd, buf, sizeof(buf));
    printf("收到客户端的数据是:%s\n", buf);
    write(new_sd, "ok", 3);
    close(new_sd);
  }
  close(sd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11fe <main+0x4e>
lea    0xe11(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    0x2dcb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    $0x4d2,%edi
call   1040 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    0x2dca(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1257 <main+0xa7>
lea    0xdbf(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x8(%rbp),%edi
mov    $0x14,%esi
call   1080 <listen@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1287 <main+0xd7>
lea    0xd94(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
movl   $0xc,-0x34(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
lea    -0x34(%rbp),%rdx
call   10b0 <accept@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    12bc <main+0x10c>
lea    0xd61(%rip),%rdi
call   10a0 <perror@plt>
jmp    128e <main+0xde>
mov    -0x20(%rbp),%edi
lea    -0x440(%rbp),%rsi
mov    $0x400,%edx
call   1070 <read@plt>
lea    -0x440(%rbp),%rsi
lea    0xd3c(%rip),%rdi
call   1050 <printf@plt>
mov    -0x20(%rbp),%edi
lea    0xd4d(%rip),%rsi
mov    $0x3,%edx
call   1030 <write@plt>
mov    -0x20(%rbp),%edi
call   1060 <close@plt>
jmp    128e <main+0xde>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x438,%rsp
mov    0x2e08(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
test   %eax,%eax
js     12b5 <main+0x105>
mov    %eax,%ebx
mov    0x0(%rbp),%eax
mov    %eax,0x18(%rsp)
mov    $0x4d2,%edi
call   1040 <htons@plt>
mov    %eax,0x14(%rsp)
mov    0x2de7(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1090 <bind@plt>
test   %eax,%eax
js     12be <main+0x10e>
mov    %ebx,%edi
mov    $0x14,%esi
call   1080 <listen@plt>
test   %eax,%eax
js     12c7 <main+0x117>
movl   $0xc,0xc(%rsp)
lea    0x20(%rsp),%r14
lea    0xc(%rsp),%r15
lea    0x30(%rsp),%r13
lea    0xde8(%rip),%r12
jmp    1296 <main+0xe6>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %eax,%ebp
mov    %eax,%edi
mov    %r13,%rsi
mov    $0x400,%edx
call   1070 <read@plt>
lea    0xda2(%rip),%rdi
mov    %r13,%rsi
call   1050 <printf@plt>
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x3,%edx
call   1030 <write@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   10b0 <accept@plt>
test   %eax,%eax
jns    1260 <main+0xb0>
lea    0xd65(%rip),%rdi
call   10a0 <perror@plt>
jmp    1296 <main+0xe6>
lea    0xd44(%rip),%rdi
jmp    12ce <main+0x11e>
lea    0xd42(%rip),%rdi
jmp    12ce <main+0x11e>
lea    0xd3e(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@INADDR_ANY = global i32 0
@.str.1 = [5 x i8] c"bind\00"
@.str.2 = [7 x i8] c"listen\00"
@.str.3 = [7 x i8] c"accept\00"
@.str.4 = [32 x i8] c"\E6\94\B6\E5\88\B0\E5\AE\A2\E6\88\B7\E7\AB\AF\E7\9A\84\E6\95\B0\E6\8D\AE\E6\98\AF:%s\0A\00"
@.str.5 = [3 x i8] c"ok\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
%8 = alloca [1024 x i8]
store i32 0, i32* %1
%9 = load i32, i32* @PF_INET
%10 = load i32, i32* @SOCK_STREAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %2
%12 = load i32, i32* %2
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %0
%15 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %0
%18 = load i32, i32* @PF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 2
store i32 %18, i32* %19
%20 = call i32 @htons(i32 1234)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
store i32 %20, i32* %21
%22 = load i32, i32* @INADDR_ANY
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
%24 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %23, i32 0, i32 0
store i32 %22, i32* %24
%25 = load i32, i32* %2
%26 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%27 = call i32 @bind(i32 %25, %struct.sockaddr* %26, i32 12)
store i32 %27, i32* %4
%28 = load i32, i32* %4
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %17
%31 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %17
%34 = load i32, i32* %2
%35 = call i32 @listen(i32 %34, i32 20)
store i32 %35, i32* %4
%36 = load i32, i32* %4
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %33
%39 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%40 = call i32 @exit(i32 1)
unreachable
41: ; preds = %33
store i32 12, i32* %7
br label %42
42: ; preds = %41, %48, %50
%43 = load i32, i32* %2
%44 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%45 = call i32 @accept(i32 %43, %struct.sockaddr* %44, i32* %7)
store i32 %45, i32* %5
%46 = load i32, i32* %5
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %42
%49 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %42
50: ; preds = %42
%51 = load i32, i32* %5
%52 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%53 = call i32 @read(i32 %51, i8* %52, i32 1024)
%54 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%55 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i8* %54)
%56 = load i32, i32* %5
%57 = call i32 @write(i32 %56, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32 3)
%58 = load i32, i32* %5
%59 = call i32 @close(i32 %58)
br label %42
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@INADDR_ANY = global i32 0
@.str.1 = [5 x i8] c"bind\00"
@.str.2 = [7 x i8] c"listen\00"
@.str.3 = [7 x i8] c"accept\00"
@.str.4 = [32 x i8] c"\E6\94\B6\E5\88\B0\E5\AE\A2\E6\88\B7\E7\AB\AF\E7\9A\84\E6\95\B0\E6\8D\AE\E6\98\AF:%s\0A\00"
@.str.5 = [3 x i8] c"ok\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = alloca [1024 x i8]
%5 = load i32, i32* @PF_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 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %0
%13 = bitcast %struct.sockaddr_in* %1 to i8*
%14 = load i32, i32* @PF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %14, i32* %15
%16 = tail call i32 @htons(i32 1234)
%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 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0, i32 0
store i32 %18, i32* %19
%20 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%21 = call i32 @bind(i32 %7, %struct.sockaddr* %20, i32 12)
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %12
%24 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %12
%27 = call i32 @listen(i32 %7, i32 20)
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %26
%30 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %26
%33 = bitcast %struct.sockaddr_in* %2 to i8*
%34 = bitcast i32* %3 to i8*
store i32 12, i32* %3
%35 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%36 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
br label %37
37: ; preds = %42, %32
%38 = call i32 @accept(i32 %7, %struct.sockaddr* %35, i32* %3)
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %37
%41 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %42
42: ; preds = %40, %43
br label %37
43: ; preds = %37
%44 = call i32 @read(i32 %38, i8* %36, i32 1024)
%45 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i8* %36)
%46 = call i32 @write(i32 %38, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32 3)
%47 = call i32 @close(i32 %38)
br label %42
}