#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 */
typedef int /*<<< orphan*/ fd_set;
typedef int /*<<< orphan*/ buffer;

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SETSIZE;
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ SIGCHLD;
int /*<<< orphan*/ SIG_IGN;
int accept_connection(int);
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int fork();
int getpid();
int /*<<< orphan*/ handle_request(int, int);
int listen_on(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
scalar_t__ pipe(int *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int select(int /*<<< orphan*/, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  signal(SIGCHLD, SIG_IGN);
  int ret;
  int i;
  int pid;
  int sock_fd;
  int child_to_parent[2];
  char buffer[1024];
  if (pipe(child_to_parent) < 0) {
    perror("pipe");
    exit(-1);
  }
  printf("\nThis is the server with pid %d listening on port %d\n", getpid(),
         2000);
  sock_fd = listen_on(2000);
  fd_set set;
  while (1) {
    FD_ZERO(&set);
    FD_SET(sock_fd, &set);
    FD_SET(child_to_parent[0], &set);
    if ((ret = select(FD_SETSIZE, &set, NULL, NULL, NULL)) < 0) {
      if (errno == EINTR) {
        continue;
      } else {
        perror("select");
        exit(-1);
      }
    }
    if (FD_ISSET(sock_fd, &set)) {
      int msgsock = accept_connection(sock_fd);
      printf("Got connection from client!\n");
      if ((pid = fork()) < 0) {
        perror("Fork");
        exit(-1);
      } else if (pid == 0) {
        handle_request(msgsock, child_to_parent[1]);
        exit(0);
      } else {
        close(msgsock);
      }
    }
    if (FD_ISSET(child_to_parent[0], &set)) {
      memset(buffer, 0, sizeof(buffer));
      ret = read(child_to_parent[0], buffer, sizeof(buffer));
      if (ret > 0) {
        printf("PARENT RECV: [%s]\n", buffer);
      } else {
        break;
      }
    }
  }
  close(sock_fd);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2db8(%rip),%rax
mov    (%rax),%edi
mov    0x2da7(%rip),%rax
mov    (%rax),%esi
call   10b0 <signal@plt>
lea    -0x28(%rbp),%rdi
call   1090 <pipe@plt>
cmp    $0x0,%rax
jge    1269 <main+0x59>
lea    0xda6(%rip),%rdi
call   1100 <perror@plt>
mov    $0xffffffff,%edi
call   1110 <exit@plt>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xd8c(%rip),%rdi
mov    $0x7d0,%edx
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x7d0,%edi
call   1030 <listen_on@plt>
mov    %eax,-0x20(%rbp)
lea    -0x434(%rbp),%rdi
call   10d0 <FD_ZERO@plt>
mov    -0x20(%rbp),%edi
lea    -0x434(%rbp),%rsi
call   1120 <FD_SET@plt>
mov    -0x28(%rbp),%edi
lea    -0x434(%rbp),%rsi
call   1120 <FD_SET@plt>
mov    0x2d0d(%rip),%rax
mov    (%rax),%edi
lea    -0x434(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   10e0 <select@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    131d <main+0x10d>
mov    0x2cd9(%rip),%rax
mov    (%rax),%rax
mov    0x2cc7(%rip),%rcx
cmp    (%rcx),%rax
jne    1307 <main+0xf7>
jmp    1292 <main+0x82>
lea    0xd2d(%rip),%rdi
call   1100 <perror@plt>
mov    $0xffffffff,%edi
call   1110 <exit@plt>
mov    -0x20(%rbp),%edi
lea    -0x434(%rbp),%rsi
call   1050 <FD_ISSET@plt>
cmp    $0x0,%rax
je     13af <main+0x19f>
mov    -0x20(%rbp),%edi
call   10f0 <accept_connection@plt>
mov    %eax,-0x438(%rbp)
lea    0xcf7(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x0,%al
call   1130 <fork@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    137b <main+0x16b>
lea    0xcf3(%rip),%rdi
call   1100 <perror@plt>
mov    $0xffffffff,%edi
call   1110 <exit@plt>
cmpl   $0x0,-0x1c(%rbp)
jne    139a <main+0x18a>
mov    -0x438(%rbp),%edi
mov    -0x24(%rbp),%esi
call   10c0 <handle_request@plt>
xor    %edi,%edi
call   1110 <exit@plt>
mov    -0x438(%rbp),%edi
call   1080 <close@plt>
jmp    13aa <main+0x19a>
jmp    13af <main+0x19f>
mov    -0x28(%rbp),%edi
lea    -0x434(%rbp),%rsi
call   1050 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1420 <main+0x210>
lea    -0x430(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
mov    -0x28(%rbp),%edi
lea    -0x430(%rbp),%rsi
mov    $0x400,%edx
call   10a0 <read@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    1416 <main+0x206>
lea    -0x430(%rbp),%rsi
lea    0xc5a(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    141b <main+0x20b>
jmp    1425 <main+0x215>
jmp    1420 <main+0x210>
jmp    1292 <main+0x82>
mov    -0x20(%rbp),%edi
call   1080 <close@plt>
xor    %edi,%edi
call   1110 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    0x2db0(%rip),%rax
mov    (%rax),%edi
mov    0x2d9f(%rip),%rax
mov    (%rax),%esi
call   10c0 <signal@plt>
lea    0x4(%rsp),%rdi
call   10a0 <pipe@plt>
test   %rax,%rax
jns    1263 <main+0x43>
lea    0xda2(%rip),%rdi
jmp    1390 <main+0x170>
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xd94(%rip),%rdi
mov    %eax,%esi
mov    $0x7d0,%edx
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x7d0,%edi
call   1030 <listen_on@plt>
mov    %eax,%r13d
lea    0xc(%rsp),%rbx
mov    0x2d38(%rip),%r15
lea    0x10(%rsp),%r14
lea    0xdb6(%rip),%r12
jmp    12ca <main+0xaa>
cs nopw 0x0(%rax,%rax,1)
mov    0x2d11(%rip),%rax
mov    (%rax),%rax
mov    0x2cff(%rip),%rcx
cmp    (%rcx),%rax
jne    1380 <main+0x160>
mov    %rbx,%rdi
call   10e0 <FD_ZERO@plt>
mov    %r13d,%edi
mov    %rbx,%rsi
call   1130 <FD_SET@plt>
mov    0x4(%rsp),%edi
mov    %rbx,%rsi
call   1130 <FD_SET@plt>
mov    (%r15),%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   10f0 <select@plt>
test   %eax,%eax
js     12b0 <main+0x90>
mov    %r13d,%edi
mov    %rbx,%rsi
call   1060 <FD_ISSET@plt>
test   %rax,%rax
je     1335 <main+0x115>
mov    %r13d,%edi
call   1100 <accept_connection@plt>
mov    %eax,%ebp
mov    %r12,%rdi
call   1040 <puts@plt>
xor    %eax,%eax
call   1140 <fork@plt>
test   %eax,%eax
js     1389 <main+0x169>
je     139f <main+0x17f>
mov    %ebp,%edi
call   1090 <close@plt>
mov    0x4(%rsp),%edi
mov    %rbx,%rsi
call   1060 <FD_ISSET@plt>
test   %rax,%rax
je     12ca <main+0xaa>
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1080 <memset@plt>
mov    0x4(%rsp),%edi
mov    %r14,%rsi
mov    $0x400,%edx
call   10b0 <read@plt>
test   %eax,%eax
jle    13b1 <main+0x191>
lea    0xcd6(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
jmp    12ca <main+0xaa>
lea    0xcb4(%rip),%rdi
jmp    1390 <main+0x170>
lea    0xcb2(%rip),%rdi
call   1110 <perror@plt>
mov    $0xffffffff,%edi
call   1120 <exit@plt>
mov    0x8(%rsp),%esi
mov    %ebp,%edi
call   10d0 <handle_request@plt>
xor    %edi,%edi
call   1120 <exit@plt>
mov    %r13d,%edi
call   1090 <close@plt>
xor    %edi,%edi
call   1120 <exit@plt>

LLVM IR (-O0):
@SIGCHLD = global i32 0
@SIG_IGN = global i32 0
@.str = [5 x i8] c"pipe\00"
@.str.1 = [54 x i8] c"\0AThis is the server with pid %d listening on port %d\0A\00"
@FD_SETSIZE = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@.str.2 = [7 x i8] c"select\00"
@.str.3 = [29 x i8] c"Got connection from client!\0A\00"
@.str.4 = [5 x i8] c"Fork\00"
@.str.5 = [19 x i8] c"PARENT RECV: [%s]\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 [2 x i32]
%11 = alloca [1024 x i8]
%12 = alloca i32
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = load i32, i32* @SIGCHLD
%15 = load i32, i32* @SIG_IGN
%16 = call i32 @signal(i32 %14, i32 %15)
%17 = getelementptr inbounds [2 x i32], [2 x i32]* %10, i64 0, i64 0
%18 = call i64 @pipe(i32* %17)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %2
%21 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%22 = call i32 @exit(i32 -1)
unreachable
23: ; preds = %2
%24 = call i32 (...) @getpid()
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.1, i64 0, i64 0), i32 %24, i32 2000)
%26 = call i32 @listen_on(i32 2000)
store i32 %26, i32* %9
br label %27
27: ; preds = %23, %41, %91
%28 = call i32 @FD_ZERO(i32* %12)
%29 = load i32, i32* %9
%30 = call i32 @FD_SET(i32 %29, i32* %12)
%31 = getelementptr inbounds [2 x i32], [2 x i32]* %10, i64 0, i64 0
%32 = load i32, i32* %31
%33 = call i32 @FD_SET(i32 %32, i32* %12)
%34 = load i32, i32* @FD_SETSIZE
%35 = call i32 @select(i32 %34, i32* %12, i32* null, i32* null, i32* null)
store i32 %35, i32* %6
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %45
37: ; preds = %27
%38 = load i64, i64* @errno
%39 = load i64, i64* @EINTR
%40 = icmp eq i64 %38, %39
br i1 %40, label %41, label %42
41: ; preds = %37
br label %27
42: ; preds = %37
%43 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%44 = call i32 @exit(i32 -1)
unreachable
45: ; preds = %27
%46 = load i32, i32* %9
%47 = call i64 @FD_ISSET(i32 %46, i32* %12)
%48 = icmp ne i64 %47, 0
br i1 %48, label %49, label %72
49: ; preds = %45
%50 = load i32, i32* %9
%51 = call i32 @accept_connection(i32 %50)
store i32 %51, i32* %13
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0))
%53 = call i32 (...) @fork()
store i32 %53, i32* %8
%54 = icmp slt i32 %53, 0
br i1 %54, label %55, label %58
55: ; preds = %49
%56 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%57 = call i32 @exit(i32 -1)
unreachable
58: ; preds = %49
%59 = load i32, i32* %8
%60 = icmp eq i32 %59, 0
br i1 %60, label %61, label %67
61: ; preds = %58
%62 = load i32, i32* %13
%63 = getelementptr inbounds [2 x i32], [2 x i32]* %10, i64 0, i64 1
%64 = load i32, i32* %63
%65 = call i32 @handle_request(i32 %62, i32 %64)
%66 = call i32 @exit(i32 0)
unreachable
67: ; preds = %58
%68 = load i32, i32* %13
%69 = call i32 @close(i32 %68)
br label %70
70: ; preds = %67
br label %71
71: ; preds = %70
br label %72
72: ; preds = %71, %45
%73 = getelementptr inbounds [2 x i32], [2 x i32]* %10, i64 0, i64 0
%74 = load i32, i32* %73
%75 = call i64 @FD_ISSET(i32 %74, i32* %12)
%76 = icmp ne i64 %75, 0
br i1 %76, label %77, label %91
77: ; preds = %72
%78 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%79 = call i32 @memset(i8* %78, i32 0, i32 1024)
%80 = getelementptr inbounds [2 x i32], [2 x i32]* %10, i64 0, i64 0
%81 = load i32, i32* %80
%82 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%83 = call i32 @read(i32 %81, i8* %82, i32 1024)
store i32 %83, i32* %6
%84 = load i32, i32* %6
%85 = icmp sgt i32 %84, 0
br i1 %85, label %86, label %89
86: ; preds = %77
%87 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%88 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i8* %87)
br label %90
89: ; preds = %77
br label %92
90: ; preds = %86
br label %91
91: ; preds = %90, %72
br label %27
92: ; preds = %89
%93 = load i32, i32* %9
%94 = call i32 @close(i32 %93)
%95 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@SIGCHLD = global i32 0
@SIG_IGN = global i32 0
@.str = [5 x i8] c"pipe\00"
@.str.1 = [54 x i8] c"\0AThis is the server with pid %d listening on port %d\0A\00"
@FD_SETSIZE = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@.str.2 = [7 x i8] c"select\00"
@.str.4 = [5 x i8] c"Fork\00"
@.str.5 = [19 x i8] c"PARENT RECV: [%s]\0A\00"
@str = [28 x i8] c"Got connection from client!\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [2 x i32]
%4 = alloca [1024 x i8]
%5 = alloca i32
%6 = load i32, i32* @SIGCHLD
%7 = load i32, i32* @SIG_IGN
%8 = tail call i32 @signal(i32 %6, i32 %7)
%9 = bitcast [2 x i32]* %3 to i8*
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%11 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%12 = call i64 @pipe(i32* %11)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 -1)
unreachable
17: ; preds = %2
%18 = call i32 (...) @getpid()
%19 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([54 x i8], [54 x i8]* @.str.1, i64 0, i64 0), i32 %18, i32 2000)
%20 = call i32 @listen_on(i32 2000)
%21 = bitcast i32* %5 to i8*
br label %22
22: ; preds = %61, %17
%23 = call i32 @FD_ZERO(i32* %5)
%24 = call i32 @FD_SET(i32 %20, i32* %5)
%25 = load i32, i32* %11
%26 = call i32 @FD_SET(i32 %25, i32* %5)
%27 = load i32, i32* @FD_SETSIZE
%28 = call i32 @select(i32 %27, i32* %5, i32* null, i32* null, i32* null)
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %37
30: ; preds = %22
%31 = load i64, i64* @errno
%32 = load i64, i64* @EINTR
%33 = icmp eq i64 %31, %32
br i1 %33, label %61, label %34
34: ; preds = %30
%35 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%36 = call i32 @exit(i32 -1)
unreachable
37: ; preds = %22
%38 = call i64 @FD_ISSET(i32 %20, i32* %5)
%39 = icmp eq i64 %38, 0
br i1 %39, label %57, label %40
40: ; preds = %37
%41 = call i32 @accept_connection(i32 %20)
%42 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str, i64 0, i64 0))
%43 = call i32 (...) @fork()
%44 = icmp slt i32 %43, 0
br i1 %44, label %45, label %48
45: ; preds = %40
%46 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%47 = call i32 @exit(i32 -1)
unreachable
48: ; preds = %40
%49 = icmp eq i32 %43, 0
br i1 %49, label %50, label %55
50: ; preds = %48
%51 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%52 = load i32, i32* %51
%53 = call i32 @handle_request(i32 %41, i32 %52)
%54 = call i32 @exit(i32 0)
unreachable
55: ; preds = %48
%56 = call i32 @close(i32 %41)
br label %57
57: ; preds = %55, %37
%58 = load i32, i32* %11
%59 = call i64 @FD_ISSET(i32 %58, i32* %5)
%60 = icmp eq i64 %59, 0
br i1 %60, label %61, label %62
61: ; preds = %57, %67, %30
br label %22
62: ; preds = %57
%63 = call i32 @memset(i8* %10, i32 0, i32 1024)
%64 = load i32, i32* %11
%65 = call i32 @read(i32 %64, i8* %10, i32 1024)
%66 = icmp sgt i32 %65, 0
br i1 %66, label %67, label %69
67: ; preds = %62
%68 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i8* %10)
br label %61
69: ; preds = %62
%70 = call i32 @close(i32 %20)
%71 = call i32 @exit(i32 0)
unreachable
}