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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SIGCHLD;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ sleep(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  int server_sockfd = -1;
  int client_sockfd = -1;
  socklen_t client_len = 0;
  struct sockaddr_in server_addr;
  struct sockaddr_in client_addr;
  server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
  server_addr.sin_family = AF_INET;
  server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  server_addr.sin_port = htons(9736);
  bind(server_sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr));
  listen(server_sockfd, 5);
  signal(SIGCHLD, SIG_IGN);
  while (1) {
    char ch = '\0';
    client_len = sizeof(client_addr);
    printf("Server waiting...\n");
    client_sockfd =
        accept(server_sockfd, (struct sockaddr *)&client_addr, &client_len);
    if (fork() == 0) {
      read(client_sockfd, &ch, 1);
      sleep(5);
      ch += 2;
      write(client_sockfd, &ch, 1);
      close(client_sockfd);
      exit(0);
    } else {
      close(client_sockfd);
    }
  }
  return 0;
}


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)
movl   $0xffffffff,-0x14(%rbp)
movl   $0xffffffff,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
mov    0x2da5(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,-0x14(%rbp)
mov    0x2dba(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2da6(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,-0x24(%rbp)
mov    $0x2608,%edi
call   1040 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <bind@plt>
mov    -0x14(%rbp),%edi
mov    $0x5,%esi
call   10a0 <listen@plt>
mov    0x2d62(%rip),%rax
mov    (%rax),%edi
mov    0x2d51(%rip),%rax
mov    (%rax),%esi
call   1090 <signal@plt>
movb   $0x0,-0x39(%rbp)
movl   $0xc,-0x1c(%rbp)
lea    0xd68(%rip),%rdi
call   1050 <printf@plt>
mov    -0x14(%rbp),%edi
lea    -0x38(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
call   10c0 <accept@plt>
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   10f0 <fork@plt>
cmp    $0x0,%rax
jne    1306 <main+0x126>
mov    -0x18(%rbp),%edi
lea    -0x39(%rbp),%rsi
mov    $0x1,%edx
call   1080 <read@plt>
mov    $0x5,%edi
call   10e0 <sleep@plt>
movsbl -0x39(%rbp),%eax
add    $0x2,%eax
mov    %al,-0x39(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x39(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
xor    %edi,%edi
call   10d0 <exit@plt>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
jmp    1286 <main+0xa6>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    0x2df5(%rip),%rbx
mov    (%rbx),%edi
mov    0x2dc4(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,%r14d
mov    (%rbx),%eax
mov    %eax,0x18(%rsp)
mov    0x2dcb(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,0x14(%rsp)
mov    $0x2608,%edi
call   1050 <htons@plt>
mov    %eax,0x10(%rsp)
lea    0x10(%rsp),%rsi
mov    %r14d,%edi
mov    $0xc,%edx
call   10b0 <bind@plt>
mov    %r14d,%edi
mov    $0x5,%esi
call   10a0 <listen@plt>
mov    0x2d84(%rip),%rax
mov    (%rax),%edi
mov    0x2d73(%rip),%rax
mov    (%rax),%esi
call   1090 <signal@plt>
movb   $0x0,0xb(%rsp)
movl   $0xc,0xc(%rsp)
lea    0xd88(%rip),%rdi
call   1030 <puts@plt>
lea    0x20(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %r14d,%edi
call   10c0 <accept@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   10f0 <fork@plt>
test   %rax,%rax
je     12e8 <main+0x108>
lea    0xd5c(%rip),%r15
lea    0x20(%rsp),%r12
lea    0xc(%rsp),%rbx
xchg   %ax,%ax
mov    %ebp,%edi
call   1070 <close@plt>
movb   $0x0,0xb(%rsp)
movl   $0xc,0xc(%rsp)
mov    %r15,%rdi
call   1030 <puts@plt>
mov    %r14d,%edi
mov    %r12,%rsi
mov    %rbx,%rdx
call   10c0 <accept@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   10f0 <fork@plt>
test   %rax,%rax
jne    12b0 <main+0xd0>
lea    0xb(%rsp),%rbx
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1080 <read@plt>
mov    $0x5,%edi
call   10e0 <sleep@plt>
addb   $0x2,0xb(%rsp)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    %ebp,%edi
call   1070 <close@plt>
xor    %edi,%edi
call   10d0 <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
@SIGCHLD = global i32 0
@SIG_IGN = global i32 0
@.str = [19 x i8] c"Server waiting...\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 %struct.sockaddr_in
%10 = alloca %struct.sockaddr_in
%11 = alloca i8
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 -1, i32* %6
store i32 -1, i32* %7
store i32 0, i32* %8
%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 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i32 %15, i32* %16
%17 = load i32, i32* @INADDR_ANY
%18 = call i32 @htonl(i32 %17)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
%20 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %19, i32 0, i32 0
store i32 %18, i32* %20
%21 = call i32 @htons(i32 9736)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
store i32 %21, i32* %22
%23 = load i32, i32* %6
%24 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%25 = call i32 @bind(i32 %23, %struct.sockaddr* %24, i32 12)
%26 = load i32, i32* %6
%27 = call i32 @listen(i32 %26, i32 5)
%28 = load i32, i32* @SIGCHLD
%29 = load i32, i32* @SIG_IGN
%30 = call i32 @signal(i32 %28, i32 %29)
br label %31
31: ; preds = %2, %54
store i8 0, i8* %11
store i32 12, i32* %8
%32 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%33 = load i32, i32* %6
%34 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%35 = call i32 @accept(i32 %33, %struct.sockaddr* %34, i32* %8)
store i32 %35, i32* %7
%36 = call i64 (...) @fork()
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %51
38: ; preds = %31
%39 = load i32, i32* %7
%40 = call i32 @read(i32 %39, i8* %11, i32 1)
%41 = call i32 @sleep(i32 5)
%42 = load i8, i8* %11
%43 = sext i8 %42 to i32
%44 = add nsw i32 %43, 2
%45 = trunc i32 %44 to i8
store i8 %45, i8* %11
%46 = load i32, i32* %7
%47 = call i32 @write(i32 %46, i8* %11, i32 1)
%48 = load i32, i32* %7
%49 = call i32 @close(i32 %48)
%50 = call i32 @exit(i32 0)
unreachable
51: ; preds = %31
%52 = load i32, i32* %7
%53 = call i32 @close(i32 %52)
br label %54
54: ; preds = %51
br label %31
}

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
@SIGCHLD = global i32 0
@SIG_IGN = global i32 0
@str = [18 x i8] c"Server waiting...\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca %struct.sockaddr_in
%6 = alloca i8
%7 = bitcast i32* %3 to i8*
%8 = bitcast %struct.sockaddr_in* %4 to i8*
%9 = bitcast %struct.sockaddr_in* %5 to i8*
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_STREAM
%12 = tail call i32 @socket(i32 %10, i32 %11, i32 0)
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, 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* %4, i64 0, i32 1, i32 0
store i32 %16, i32* %17
%18 = tail call i32 @htons(i32 9736)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
store i32 %18, i32* %19
%20 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%21 = call i32 @bind(i32 %12, %struct.sockaddr* %20, i32 12)
%22 = call i32 @listen(i32 %12, i32 5)
%23 = load i32, i32* @SIGCHLD
%24 = load i32, i32* @SIG_IGN
%25 = call i32 @signal(i32 %23, i32 %24)
store i8 0, i8* %6
store i32 12, i32* %3
%26 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
%27 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%28 = call i32 @accept(i32 %12, %struct.sockaddr* %27, i32* %3)
%29 = call i64 (...) @fork()
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %40
31: ; preds = %40, %2
%32 = phi i32 [ %28, %2 ], [ %44, %40 ]
%33 = call i32 @read(i32 %32, i8* %6, i32 1)
%34 = call i32 @sleep(i32 5)
%35 = load i8, i8* %6
%36 = add i8 %35, 2
store i8 %36, i8* %6
%37 = call i32 @write(i32 %32, i8* %6, i32 1)
%38 = call i32 @close(i32 %32)
%39 = call i32 @exit(i32 0)
unreachable
40: ; preds = %2, %40
%41 = phi i32 [ %44, %40 ], [ %28, %2 ]
%42 = call i32 @close(i32 %41)
store i8 0, i8* %6
store i32 12, i32* %3
%43 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
%44 = call i32 @accept(i32 %12, %struct.sockaddr* %27, i32* %3)
%45 = call i64 (...) @fork()
%46 = icmp eq i64 %45, 0
br i1 %46, label %31, label %40
}