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

/* Type definitions */
struct sockaddr_in {
  int /*<<< orphan*/ member_0;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ server;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ echo(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int getpid();
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ setup_signal_handler();
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main() {
  setup_signal_handler();
  struct sockaddr_in server = {0};
  server.sin_family = AF_INET;
  server.sin_port = htons(1234);
  int server_fd = socket(AF_INET, SOCK_STREAM, 0);
  bind(server_fd, (struct sockaddr *)&server, sizeof(server));
  listen(server_fd, 100);
  struct sockaddr_in client = {0};
  int client_fd;
  int client_sockaddr_len = 0;
  char buffer[256];
  while (1) {
    printf("server ready to accept\n");
    client_fd =
        accept(server_fd, (struct sockaddr *)&client, &client_sockaddr_len);
    if (client_fd < 0) {
      printf("server accept error!\n");
      continue;
    }
    if (fork() == 0) {
      close(server_fd);
      printf("child process start pid(%d)\n", getpid());
      echo(client_fd);
      printf("child process exit  pid(%d)\n", getpid());
      exit(0);
    }
    close(client_fd);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   10a0 <setup_signal_handler@plt>
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x2de8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
mov    $0x4d2,%edi
call   1050 <htons@plt>
mov    %eax,-0xc(%rbp)
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
mov    0x2db6(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <bind@plt>
mov    -0x14(%rbp),%edi
mov    $0x64,%esi
call   1090 <listen@plt>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
movl   $0x0,-0x28(%rbp)
lea    0xd96(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
lea    -0x28(%rbp),%rdx
call   10c0 <accept@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    12a1 <main+0xd1>
lea    0xd83(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1263 <main+0x93>
mov    $0x0,%al
call   10e0 <fork@plt>
cmp    $0x0,%rax
jne    12f7 <main+0x127>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xd64(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x24(%rbp),%edi
call   1030 <echo@plt>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xd62(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
xor    %edi,%edi
call   10d0 <exit@plt>
mov    -0x24(%rbp),%edi
call   1080 <close@plt>
jmp    1263 <main+0x93>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
xor    %eax,%eax
call   10a0 <setup_signal_handler@plt>
movl   $0x0,0x8(%rsp)
mov    0x2df4(%rip),%rbx
mov    (%rbx),%eax
mov    %eax,0x10(%rsp)
mov    $0x4d2,%edi
call   1060 <htons@plt>
mov    %eax,0xc(%rsp)
mov    (%rbx),%edi
mov    0x2dc7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    $0xc,%edx
call   10b0 <bind@plt>
mov    %ebx,%edi
mov    $0x64,%esi
call   1090 <listen@plt>
movl   $0x0,0x20(%rsp)
movq   $0x0,0x18(%rsp)
movl   $0x0,0x4(%rsp)
lea    0xde1(%rip),%r14
lea    0x18(%rsp),%r15
lea    0x4(%rsp),%r12
lea    0xde7(%rip),%r13
jmp    1278 <main+0xa8>
nopl   0x0(%rax)
mov    %r13,%rdi
call   1040 <puts@plt>
mov    %r14,%rdi
call   1040 <puts@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    %r12,%rdx
call   10c0 <accept@plt>
test   %eax,%eax
js     1270 <main+0xa0>
mov    %eax,%ebp
xor    %eax,%eax
call   10e0 <fork@plt>
test   %rax,%rax
je     12a8 <main+0xd8>
mov    %ebp,%edi
call   1080 <close@plt>
jmp    1278 <main+0xa8>
mov    %ebx,%edi
call   1080 <close@plt>
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xd43(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %ebp,%edi
call   1030 <echo@plt>
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xd42(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
xor    %edi,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [24 x i8] c"server ready to accept\0A\00"
@.str.1 = [22 x i8] c"server accept error!\0A\00"
@.str.2 = [29 x i8] c"child process start pid(%d)\0A\00"
@.str.3 = [29 x i8] c"child process exit pid(%d)\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca i32
%7 = alloca [256 x i8]
store i32 0, i32* %1
%8 = call i32 (...) @setup_signal_handler()
%9 = bitcast %struct.sockaddr_in* %2 to i8*
%10 = load i32, i32* @AF_INET
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 2
store i32 %10, i32* %11
%12 = call i32 @htons(i32 1234)
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 1
store i32 %12, i32* %13
%14 = load i32, i32* @AF_INET
%15 = load i32, i32* @SOCK_STREAM
%16 = call i32 @socket(i32 %14, i32 %15, i32 0)
store i32 %16, i32* %3
%17 = load i32, i32* %3
%18 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%19 = call i32 @bind(i32 %17, %struct.sockaddr* %18, i32 12)
%20 = load i32, i32* %3
%21 = call i32 @listen(i32 %20, i32 100)
%22 = bitcast %struct.sockaddr_in* %4 to i8*
store i32 0, i32* %6
br label %23
23: ; preds = %0, %30, %45
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%25 = load i32, i32* %3
%26 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%27 = call i32 @accept(i32 %25, %struct.sockaddr* %26, i32* %6)
store i32 %27, i32* %5
%28 = load i32, i32* %5
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %23
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %23
32: ; preds = %23
%33 = call i64 (...) @fork()
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %45
35: ; preds = %32
%36 = load i32, i32* %3
%37 = call i32 @close(i32 %36)
%38 = call i32 (...) @getpid()
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i32 %38)
%40 = load i32, i32* %5
%41 = call i32 @echo(i32 %40)
%42 = call i32 (...) @getpid()
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i32 %42)
%44 = call i32 @exit(i32 0)
unreachable
45: ; preds = %32
%46 = load i32, i32* %5
%47 = call i32 @close(i32 %46)
br label %23
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [29 x i8] c"child process start pid(%d)\0A\00"
@.str.3 = [29 x i8] c"child process exit pid(%d)\0A\00"
@str = [23 x i8] c"server ready to accept\00"
@str.4 = [21 x i8] c"server accept error!\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = tail call i32 (...) @setup_signal_handler()
%5 = bitcast %struct.sockaddr_in* %1 to i8*
%6 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i32 0, i32* %6
%7 = load i32, i32* @AF_INET
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %7, i32* %8
%9 = tail call i32 @htons(i32 1234)
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 %9, i32* %10
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_STREAM
%13 = tail call i32 @socket(i32 %11, i32 %12, i32 0)
%14 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%15 = call i32 @bind(i32 %13, %struct.sockaddr* %14, i32 12)
%16 = call i32 @listen(i32 %13, i32 100)
%17 = bitcast %struct.sockaddr_in* %2 to i8*
%18 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%19 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
br label %20
20: ; preds = %26, %0
%21 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
%22 = call i32 @accept(i32 %13, %struct.sockaddr* %19, i32* %3)
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %20
%25 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.4, i64 0, i64 0))
br label %26
26: ; preds = %24, %38
br label %20
27: ; preds = %20
%28 = call i64 (...) @fork()
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %38
30: ; preds = %27
%31 = call i32 @close(i32 %13)
%32 = call i32 (...) @getpid()
%33 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i32 %32)
%34 = call i32 @echo(i32 %22)
%35 = call i32 (...) @getpid()
%36 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i32 %35)
%37 = call i32 @exit(i32 0)
unreachable
38: ; preds = %27
%39 = call i32 @close(i32 %22)
br label %26
}