#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 socklen_t;
typedef int /*<<< orphan*/ servaddr;
typedef int /*<<< orphan*/ pid_t;
typedef int /*<<< orphan*/ cliaddr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fork();
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ process_connfd(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char **argv) {
  int listenfd, connfd;
  pid_t childpid;
  socklen_t chilen;
  struct sockaddr_in cliaddr, servaddr;
  listenfd = socket(AF_INET, SOCK_STREAM, 0);
  bzero(&servaddr, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  servaddr.sin_port = htons(8888);
  bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
  listen(listenfd, 1024);
  for (;;) {
    chilen = sizeof(cliaddr);
    connfd = accept(listenfd, (struct sockaddr *)&cliaddr, &chilen);
    printf("Three-way handshake finished. Connection is established.\n");
    if ((childpid = fork()) == 0) {
      printf("Child process will close listenfd\n");
      close(listenfd);
      process_connfd(connfd);
      printf("Child process will exit\n");
      exit(0);
    }
    printf("Parent process will close connfd\n");
    close(connfd);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e0b(%rip),%rax
mov    (%rax),%edi
mov    0x2dea(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,-0x14(%rbp)
lea    -0x40(%rbp),%rdi
mov    $0xc,%esi
call   10a0 <bzero@plt>
mov    0x2de1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    0x2dcd(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,-0x3c(%rbp)
mov    $0x22b8,%edi
call   1040 <htons@plt>
mov    %eax,-0x40(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
mov    -0x14(%rbp),%edi
mov    $0x400,%esi
call   1080 <listen@plt>
movl   $0xc,-0x20(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   10b0 <accept@plt>
mov    %eax,-0x18(%rbp)
lea    0xd97(%rip),%rdi
call   1050 <printf@plt>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jne    12b0 <main+0xf0>
lea    0xdb2(%rip),%rdi
call   1050 <printf@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
mov    -0x18(%rbp),%edi
call   1030 <process_connfd@plt>
lea    0xdb9(%rip),%rdi
call   1050 <printf@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
lea    0xdbf(%rip),%rdi
call   1050 <printf@plt>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
jmp    1248 <main+0x88>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    0x2e13(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2df1(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,%r14d
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
mov    $0xc,%esi
call   10a0 <bzero@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x10(%rsp)
mov    0x2dd5(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,0xc(%rsp)
mov    $0x22b8,%edi
call   1050 <htons@plt>
mov    %eax,0x8(%rsp)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
mov    %r14d,%edi
mov    $0x400,%esi
call   1080 <listen@plt>
movl   $0xc,0x4(%rsp)
lea    0x18(%rsp),%rsi
lea    0x4(%rsp),%rdx
mov    %r14d,%edi
call   10b0 <accept@plt>
mov    %eax,%ebp
lea    0xd9c(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   10d0 <fork@plt>
test   %eax,%eax
je     12ca <main+0x10a>
lea    0xdbe(%rip),%r15
lea    0x18(%rsp),%r12
lea    0x4(%rsp),%r13
lea    0xd74(%rip),%rbx
nopl   0x0(%rax)
mov    %r15,%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
call   1070 <close@plt>
movl   $0xc,0x4(%rsp)
mov    %r14d,%edi
mov    %r12,%rsi
mov    %r13,%rdx
call   10b0 <accept@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   10d0 <fork@plt>
test   %eax,%eax
jne    1290 <main+0xd0>
lea    0xd89(%rip),%rdi
call   1030 <puts@plt>
mov    %r14d,%edi
call   1070 <close@plt>
mov    %ebp,%edi
call   1040 <process_connfd@plt>
lea    0xd90(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   10c0 <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
@INADDR_ANY = global i32 0
@.str = [58 x i8] c"Three-way handshake finished. Connection is established.\0A\00"
@.str.1 = [35 x i8] c"Child process will close listenfd\0A\00"
@.str.2 = [25 x i8] c"Child process will exit\0A\00"
@.str.3 = [34 x i8] c"Parent process will close connfd\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 i32
%9 = alloca i32
%10 = alloca %struct.sockaddr_in
%11 = alloca %struct.sockaddr_in
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* @AF_INET
%13 = load i32, i32* @SOCK_STREAM
%14 = call i32 @socket(i32 %12, i32 %13, i32 0)
store i32 %14, i32* %6
%15 = call i32 @bzero(%struct.sockaddr_in* %11, i32 12)
%16 = load i32, i32* @AF_INET
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 2
store i32 %16, i32* %17
%18 = load i32, i32* @INADDR_ANY
%19 = call i32 @htonl(i32 %18)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 1
%21 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %20, i32 0, i32 0
store i32 %19, i32* %21
%22 = call i32 @htons(i32 8888)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
store i32 %22, i32* %23
%24 = load i32, i32* %6
%25 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%26 = call i32 @bind(i32 %24, %struct.sockaddr* %25, i32 12)
%27 = load i32, i32* %6
%28 = call i32 @listen(i32 %27, i32 1024)
br label %29
29: ; preds = %44, %2
store i32 12, i32* %9
%30 = load i32, i32* %6
%31 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%32 = call i32 @accept(i32 %30, %struct.sockaddr* %31, i32* %9)
store i32 %32, i32* %7
%33 = call i32 @printf(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str, i64 0, i64 0))
%34 = call i32 (...) @fork()
store i32 %34, i32* %8
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %44
36: ; preds = %29
%37 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
%38 = load i32, i32* %6
%39 = call i32 @close(i32 %38)
%40 = load i32, i32* %7
%41 = call i32 @process_connfd(i32 %40)
%42 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%43 = call i32 @exit(i32 0)
unreachable
44: ; preds = %29
%45 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0))
%46 = load i32, i32* %7
%47 = call i32 @close(i32 %46)
br label %29
}

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
@INADDR_ANY = global i32 0
@str = [57 x i8] c"Three-way handshake finished. Connection is established.\00"
@str.4 = [33 x i8] c"Parent process will close connfd\00"
@str.5 = [34 x i8] c"Child process will close listenfd\00"
@str.6 = [24 x i8] c"Child process will exit\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca %struct.sockaddr_in
%6 = bitcast i32* %3 to i8*
%7 = bitcast %struct.sockaddr_in* %4 to i8*
%8 = bitcast %struct.sockaddr_in* %5 to i8*
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_STREAM
%11 = tail call i32 @socket(i32 %9, i32 %10, i32 0)
%12 = call i32 @bzero(%struct.sockaddr_in* %5, i32 12)
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 2
store i32 %13, i32* %14
%15 = load i32, i32* @INADDR_ANY
%16 = tail call i32 @htonl(i32 %15)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1, i32 0
store i32 %16, i32* %17
%18 = tail call i32 @htons(i32 8888)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0
store i32 %18, i32* %19
%20 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%21 = call i32 @bind(i32 %11, %struct.sockaddr* %20, i32 12)
%22 = call i32 @listen(i32 %11, i32 1024)
%23 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
store i32 12, i32* %3
%24 = call i32 @accept(i32 %11, %struct.sockaddr* %23, i32* %3)
%25 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([57 x i8], [57 x i8]* @str, i64 0, i64 0))
%26 = call i32 (...) @fork()
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %35
28: ; preds = %35, %2
%29 = phi i32 [ %24, %2 ], [ %39, %35 ]
%30 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str.5, i64 0, i64 0))
%31 = call i32 @close(i32 %11)
%32 = call i32 @process_connfd(i32 %29)
%33 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.6, i64 0, i64 0))
%34 = call i32 @exit(i32 0)
unreachable
35: ; preds = %2, %35
%36 = phi i32 [ %39, %35 ], [ %24, %2 ]
%37 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.4, i64 0, i64 0))
%38 = call i32 @close(i32 %36)
store i32 12, i32* %3
%39 = call i32 @accept(i32 %11, %struct.sockaddr* %23, i32* %3)
%40 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([57 x i8], [57 x i8]* @str, i64 0, i64 0))
%41 = call i32 (...) @fork()
%42 = icmp eq i32 %41, 0
br i1 %42, label %28, label %35
}