#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 /*<<< orphan*/ server_address;
typedef int /*<<< orphan*/ client_address;

/* 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 server_sockfd, client_sockfd;
  int server_len, client_len;
  struct sockaddr_in server_address;
  struct sockaddr_in client_address;
  server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
  server_address.sin_family = AF_INET;
  server_address.sin_addr.s_addr = htonl(INADDR_ANY);
  server_address.sin_port = htons(9734);
  server_len = sizeof(server_address);
  bind(server_sockfd, (struct sockaddr *)&server_address, server_len);
  listen(server_sockfd, 5);
  signal(SIGCHLD, SIG_IGN);
  while (1) {
    char ch;
    printf("server waiting\n");
    client_len = sizeof(client_address);
    client_sockfd =
        accept(server_sockfd, (struct sockaddr *)&client_address, &client_len);
    if (fork() == 0) {
      read(client_sockfd, &ch, 1);
      sleep(5);
      ch++;
      write(client_sockfd, &ch, 1);
      close(client_sockfd);
      exit(0);
    } else {
      close(client_sockfd);
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
mov    0x2dc1(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,-0x8(%rbp)
mov    0x2dd6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2dc2(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x2606,%edi
call   1040 <htons@plt>
mov    %eax,-0x20(%rbp)
movl   $0xc,-0x10(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%edx
call   10b0 <bind@plt>
mov    -0x8(%rbp),%edi
mov    $0x5,%esi
call   10a0 <listen@plt>
mov    0x2d79(%rip),%rax
mov    (%rax),%edi
mov    0x2d68(%rip),%rax
mov    (%rax),%esi
call   1090 <signal@plt>
lea    0xd8a(%rip),%rdi
call   1050 <printf@plt>
movl   $0xc,-0x14(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
lea    -0x14(%rbp),%rdx
call   10c0 <accept@plt>
mov    %eax,-0xc(%rbp)
mov    $0x0,%al
call   10f0 <fork@plt>
cmp    $0x0,%rax
jne    12e9 <main+0x109>
mov    -0xc(%rbp),%edi
lea    -0x31(%rbp),%rsi
mov    $0x1,%edx
call   1080 <read@plt>
mov    $0x5,%edi
call   10e0 <sleep@plt>
mov    -0x31(%rbp),%al
add    $0x1,%al
mov    %al,-0x31(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x31(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@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    126f <main+0x8f>

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    $0x2606,%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>
lea    0xd95(%rip),%rdi
call   1030 <puts@plt>
movl   $0xc,0xc(%rsp)
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     12e3 <main+0x103>
lea    0xd61(%rip),%r15
lea    0x20(%rsp),%r12
lea    0xc(%rsp),%rbx
nopl   0x0(%rax)
mov    %ebp,%edi
call   1070 <close@plt>
mov    %r15,%rdi
call   1030 <puts@plt>
movl   $0xc,0xc(%rsp)
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   $0x1,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 = [16 x i8] c"server waiting\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = alloca %struct.sockaddr_in
%8 = alloca i8
store i32 0, i32* %1
%9 = load i32, i32* @AF_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* @AF_INET
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %12, i32* %13
%14 = load i32, i32* @INADDR_ANY
%15 = call i32 @htonl(i32 %14)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
%17 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %16, i32 0, i32 0
store i32 %15, i32* %17
%18 = call i32 @htons(i32 9734)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 %18, i32* %19
store i32 12, i32* %4
%20 = load i32, i32* %2
%21 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%22 = load i32, i32* %4
%23 = call i32 @bind(i32 %20, %struct.sockaddr* %21, i32 %22)
%24 = load i32, i32* %2
%25 = call i32 @listen(i32 %24, i32 5)
%26 = load i32, i32* @SIGCHLD
%27 = load i32, i32* @SIG_IGN
%28 = call i32 @signal(i32 %26, i32 %27)
br label %29
29: ; preds = %0, %50
%30 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
store i32 12, i32* %5
%31 = load i32, i32* %2
%32 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%33 = call i32 @accept(i32 %31, %struct.sockaddr* %32, i32* %5)
store i32 %33, i32* %3
%34 = call i64 (...) @fork()
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %47
36: ; preds = %29
%37 = load i32, i32* %3
%38 = call i32 @read(i32 %37, i8* %8, i32 1)
%39 = call i32 @sleep(i32 5)
%40 = load i8, i8* %8
%41 = add i8 %40, 1
store i8 %41, i8* %8
%42 = load i32, i32* %3
%43 = call i32 @write(i32 %42, i8* %8, i32 1)
%44 = load i32, i32* %3
%45 = call i32 @close(i32 %44)
%46 = call i32 @exit(i32 0)
unreachable
47: ; preds = %29
%48 = load i32, i32* %3
%49 = call i32 @close(i32 %48)
br label %50
50: ; preds = %47
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
@SIGCHLD = global i32 0
@SIG_IGN = global i32 0
@str = [15 x i8] c"server waiting\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_in
%3 = alloca %struct.sockaddr_in
%4 = alloca i8
%5 = bitcast i32* %1 to i8*
%6 = bitcast %struct.sockaddr_in* %2 to i8*
%7 = bitcast %struct.sockaddr_in* %3 to i8*
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = tail call i32 @socket(i32 %8, i32 %9, i32 0)
%11 = load i32, i32* @AF_INET
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %11, i32* %12
%13 = load i32, i32* @INADDR_ANY
%14 = tail call i32 @htonl(i32 %13)
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1, i32 0
store i32 %14, i32* %15
%16 = tail call i32 @htons(i32 9734)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i32 %16, i32* %17
%18 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%19 = call i32 @bind(i32 %10, %struct.sockaddr* %18, i32 12)
%20 = call i32 @listen(i32 %10, i32 5)
%21 = load i32, i32* @SIGCHLD
%22 = load i32, i32* @SIG_IGN
%23 = call i32 @signal(i32 %21, i32 %22)
%24 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
store i32 12, i32* %1
%25 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%26 = call i32 @accept(i32 %10, %struct.sockaddr* %25, i32* %1)
%27 = call i64 (...) @fork()
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %38
29: ; preds = %38, %0
%30 = phi i32 [ %26, %0 ], [ %42, %38 ]
%31 = call i32 @read(i32 %30, i8* %4, i32 1)
%32 = call i32 @sleep(i32 5)
%33 = load i8, i8* %4
%34 = add i8 %33, 1
store i8 %34, i8* %4
%35 = call i32 @write(i32 %30, i8* %4, i32 1)
%36 = call i32 @close(i32 %30)
%37 = call i32 @exit(i32 0)
unreachable
38: ; preds = %0, %38
%39 = phi i32 [ %42, %38 ], [ %26, %0 ]
%40 = call i32 @close(i32 %39)
%41 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
store i32 12, i32* %1
%42 = call i32 @accept(i32 %10, %struct.sockaddr* %25, i32* %1)
%43 = call i64 (...) @fork()
%44 = icmp eq i64 %43, 0
br i1 %44, label %29, label %38
}