#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*/ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ sin4;
typedef int /*<<< orphan*/ one;
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int /*<<< orphan*/ IPPROTO_TCP;
int O_NONBLOCK;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int accept(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int atoi(char *);
int /*<<< orphan*/ bind(int, struct sockaddr *, int);
int *calloc(int, int);
int /*<<< orphan*/ close(int);
int dup(int);
int fcntl(int, int /*<<< orphan*/, int);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_pton(int /*<<< orphan*/, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int pipe(int *);
int /*<<< orphan*/ printf(char *, int, int);
int /*<<< orphan*/ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                          int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              char *, int);
int /*<<< orphan*/ sleep(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  int forks = 0;
  int fd_max = 1;
  int port = 1024 + 1;
  if (argc > 1) {
    forks = atoi(argv[1]);
    if (forks < 0 || forks > 1000000) {
      forks = 0;
    }
  }
  if (argc > 2) {
    fd_max = atoi(argv[2]);
    if (fd_max < 0 || fd_max > 10000) {
      fd_max = 1;
    }
  }
  int dup_pipe = 0;
  if (argc > 3) {
    dup_pipe = 1;
  }
  fprintf(stderr, "forks = %d, dupes per fork = %d, total = %d, dup_pipe=%d\n",
          forks, fd_max, forks * fd_max, dup_pipe);
  int *list_of_sd = calloc(1, sizeof(int) * fd_max);
  int *list_of_pids = calloc(1, sizeof(int) * fd_max);
  struct sockaddr_in sin4;
  memset(&sin4, 0, sizeof(sin4));
  sin4.sin_family = AF_INET;
  sin4.sin_port = htons(port);
  inet_pton(AF_INET, "127.0.0.1", &(sin4.sin_addr));
  struct sockaddr *sockaddr = (struct sockaddr *)&sin4;
  int sockaddr_len = sizeof(sin4);
  if (1) {
    int sd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
    int one = 1;
    setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, (char *)&one, sizeof(one));
    bind(sd, sockaddr, sockaddr_len);
    listen(sd, 128);
    int flags = fcntl(sd, F_GETFL, 0);
    fcntl(sd, F_SETFL, flags | O_NONBLOCK);
    list_of_sd[0] = sd;
  }
  int i;
  for (i = 0; i < forks; i++) {
    int r = fork();
    if (r == 0) {
      break;
    }
    list_of_pids[i] = r;
  }
  int proc_num = i;
  if (i == forks) {
    fprintf(stderr, "[+] started sd=%d\n", list_of_sd[0]);
    while (1) {
      sleep(100);
    }
    fprintf(stderr, "[+] done\n");
    return 0;
  }
  int pipefd[2];
  int r = pipe(pipefd);
  if (r < 0) {
    perror("pipe");
  }
  for (i = 1; i < fd_max; i++) {
    if (dup_pipe) {
      list_of_sd[i] = dup(pipefd[0]);
    } else {
      list_of_sd[i] = dup(list_of_sd[0]);
    }
  }
  while (1) {
    fd_set rfds;
    FD_ZERO(&rfds);
    for (i = 0; i < fd_max; i++) {
      int sd = list_of_sd[i];
      FD_SET(sd, &rfds);
    }
    select(list_of_sd[fd_max - 1] + 1, &rfds, NULL, NULL, NULL);
    for (i = 0; i < fd_max; i++) {
      int sd = list_of_sd[i];
      if (FD_ISSET(sd, &rfds)) {
        int l = accept(sd, NULL, 0);
        if (l >= 0) {
          printf("received. fd=%d, procnum=%d\n", sd, proc_num);
          close(l);
        }
      }
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x1,-0x18(%rbp)
movl   $0x401,-0x1c(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    12db <main+0x6b>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1150 <atoi@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     12cf <main+0x5f>
cmpl   $0xf4240,-0x14(%rbp)
jle    12d6 <main+0x66>
movl   $0x0,-0x14(%rbp)
jmp    12db <main+0x6b>
cmpl   $0x2,-0x8(%rbp)
jle    1318 <main+0xa8>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1150 <atoi@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jl     130c <main+0x9c>
cmpl   $0x2710,-0x18(%rbp)
jle    1313 <main+0xa3>
movl   $0x1,-0x18(%rbp)
jmp    1318 <main+0xa8>
movl   $0x0,-0x20(%rbp)
cmpl   $0x3,-0x8(%rbp)
jle    1330 <main+0xc0>
movl   $0x1,-0x20(%rbp)
mov    0x2c89(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
mov    -0x18(%rbp),%ecx
mov    -0x14(%rbp),%r8d
imul   -0x18(%rbp),%r8d
mov    -0x20(%rbp),%r9d
lea    0xcad(%rip),%rsi
mov    $0x0,%al
call   10d0 <fprintf@plt>
movslq -0x18(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%esi
mov    $0x1,%edi
call   10c0 <calloc@plt>
mov    %rax,-0x28(%rbp)
movslq -0x18(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%esi
mov    $0x1,%edi
call   10c0 <calloc@plt>
mov    %rax,-0x30(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1090 <memset@plt>
mov    0x2c3f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    -0x1c(%rbp),%edi
call   1060 <htons@plt>
mov    %eax,-0x3c(%rbp)
mov    0x2c28(%rip),%rax
mov    (%rax),%edi
lea    -0x40(%rbp),%rdx
lea    0xc75(%rip),%rsi
call   10e0 <inet_pton@plt>
lea    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
movl   $0xc,-0x4c(%rbp)
mov    0x2bc0(%rip),%rax
mov    (%rax),%edi
mov    0x2bc7(%rip),%rax
mov    (%rax),%esi
mov    0x2bde(%rip),%rax
mov    (%rax),%edx
call   1190 <socket@plt>
mov    %eax,-0x50(%rbp)
movl   $0x1,-0x54(%rbp)
mov    -0x50(%rbp),%edi
mov    0x2bdb(%rip),%rax
mov    (%rax),%esi
mov    0x2b82(%rip),%rax
mov    (%rax),%edx
lea    -0x54(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0x50(%rbp),%edi
mov    -0x48(%rbp),%rsi
mov    -0x4c(%rbp),%edx
call   1120 <bind@plt>
mov    -0x50(%rbp),%edi
mov    $0x80,%esi
call   1110 <listen@plt>
mov    -0x50(%rbp),%edi
mov    0x2b5b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <fcntl@plt>
mov    %eax,-0x58(%rbp)
mov    -0x50(%rbp),%edi
mov    0x2b55(%rip),%rax
mov    (%rax),%esi
mov    -0x58(%rbp),%edx
mov    0x2b69(%rip),%rax
or     (%rax),%edx
call   1040 <fcntl@plt>
mov    -0x50(%rbp),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x5c(%rbp)
mov    -0x5c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    14c7 <main+0x257>
mov    $0x0,%al
call   1180 <fork@plt>
mov    %eax,-0x60(%rbp)
cmpl   $0x0,-0x60(%rbp)
jne    14ab <main+0x23b>
jmp    14c7 <main+0x257>
mov    -0x60(%rbp),%edx
mov    -0x30(%rbp),%rax
movslq -0x5c(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x5c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x5c(%rbp)
jmp    1486 <main+0x216>
mov    -0x5c(%rbp),%eax
mov    %eax,-0x64(%rbp)
mov    -0x5c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jne    1505 <main+0x295>
mov    0x2ae0(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rax
mov    (%rax),%edx
lea    0xb55(%rip),%rsi
mov    $0x0,%al
call   10d0 <fprintf@plt>
mov    $0x64,%edi
call   1170 <sleep@plt>
jmp    14f6 <main+0x286>
lea    -0x6c(%rbp),%rdi
call   10b0 <pipe@plt>
mov    %eax,-0x70(%rbp)
cmpl   $0x0,-0x70(%rbp)
jge    1527 <main+0x2b7>
lea    0xb35(%rip),%rdi
call   1130 <perror@plt>
movl   $0x1,-0x5c(%rbp)
mov    -0x5c(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    1589 <main+0x319>
cmpl   $0x0,-0x20(%rbp)
je     155e <main+0x2ee>
mov    -0x6c(%rbp),%edi
call   1080 <dup@plt>
mov    %eax,%edx
mov    -0x28(%rbp),%rax
movslq -0x5c(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
jmp    1576 <main+0x306>
mov    -0x28(%rbp),%rax
mov    (%rax),%edi
call   1080 <dup@plt>
mov    %eax,%edx
mov    -0x28(%rbp),%rax
movslq -0x5c(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
jmp    157b <main+0x30b>
mov    -0x5c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x5c(%rbp)
jmp    152e <main+0x2be>
jmp    158e <main+0x31e>
lea    -0x74(%rbp),%rdi
call   10f0 <FD_ZERO@plt>
movl   $0x0,-0x5c(%rbp)
mov    -0x5c(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    15d2 <main+0x362>
mov    -0x28(%rbp),%rax
movslq -0x5c(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x78(%rbp)
mov    -0x78(%rbp),%edi
lea    -0x74(%rbp),%rsi
call   1160 <FD_SET@plt>
mov    -0x5c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x5c(%rbp)
jmp    159e <main+0x32e>
mov    -0x28(%rbp),%rax
mov    -0x18(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%edi
add    $0x1,%edi
lea    -0x74(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   1100 <select@plt>
movl   $0x0,-0x5c(%rbp)
mov    -0x5c(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    167d <main+0x40d>
mov    -0x28(%rbp),%rax
movslq -0x5c(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x7c(%rbp)
mov    -0x7c(%rbp),%edi
lea    -0x74(%rbp),%rsi
call   1050 <FD_ISSET@plt>
cmp    $0x0,%rax
je     166a <main+0x3fa>
mov    -0x7c(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1140 <accept@plt>
mov    %eax,-0x80(%rbp)
cmpl   $0x0,-0x80(%rbp)
jl     1665 <main+0x3f5>
mov    -0x7c(%rbp),%esi
mov    -0x64(%rbp),%edx
lea    0xa04(%rip),%rdi
call   1070 <printf@plt>
mov    -0x80(%rbp),%edi
call   10a0 <close@plt>
jmp    166a <main+0x3fa>
jmp    166f <main+0x3ff>
mov    -0x5c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x5c(%rbp)
jmp    1600 <main+0x390>
jmp    158e <main+0x31e>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %edi,%r14d
mov    $0x1,%eax
mov    %rax,0x8(%rsp)
xor    %eax,%eax
mov    %rax,0x10(%rsp)
cmp    $0x2,%edi
jl     12d2 <main+0x62>
mov    %rsi,%rbp
mov    0x8(%rsi),%rdi
call   1150 <atoi@plt>
xor    %r15d,%r15d
cmp    $0xf4241,%eax
cmovb  %eax,%r15d
cmp    $0x2,%r14d
je     12d5 <main+0x65>
mov    0x10(%rbp),%rdi
call   1150 <atoi@plt>
cmp    $0x2711,%eax
mov    $0x1,%ecx
cmovb  %eax,%ecx
mov    %rcx,0x8(%rsp)
jmp    12d5 <main+0x65>
xor    %r15d,%r15d
xor    %r9d,%r9d
cmp    $0x4,%r14d
setge  %r9b
mov    0x2cd9(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rsp),%rbx
mov    %ebx,%r8d
imul   %r15d,%r8d
lea    0xd04(%rip),%rsi
mov    %r15d,%edx
mov    %ebx,%ecx
xor    %eax,%eax
call   10d0 <fprintf@plt>
lea    0x0(,%rbx,4),%ebp
mov    $0x1,%edi
mov    %ebp,%esi
call   10c0 <calloc@plt>
mov    %rax,%r13
mov    $0x1,%edi
mov    %ebp,%esi
call   10c0 <calloc@plt>
mov    %rax,%r12
lea    0x28(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1090 <memset@plt>
mov    0x2c98(%rip),%rbx
mov    (%rbx),%eax
mov    %eax,0x30(%rsp)
mov    $0x401,%edi
call   1060 <htons@plt>
mov    %eax,0x2c(%rsp)
mov    (%rbx),%edi
lea    0xcd5(%rip),%rsi
mov    %rbp,%rdx
mov    %rbp,%rbx
call   10e0 <inet_pton@plt>
mov    0x2c29(%rip),%rax
mov    (%rax),%edi
mov    0x2c30(%rip),%rax
mov    (%rax),%esi
mov    0x2c47(%rip),%rax
mov    (%rax),%edx
call   1190 <socket@plt>
mov    %eax,%ebp
movl   $0x1,0x18(%rsp)
mov    0x2c47(%rip),%rax
mov    (%rax),%esi
mov    0x2bee(%rip),%rax
mov    (%rax),%edx
lea    0x18(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   1120 <bind@plt>
mov    %ebp,%edi
mov    $0x80,%esi
call   1110 <listen@plt>
mov    0x2bc8(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
xor    %edx,%edx
call   1040 <fcntl@plt>
mov    0x2bc6(%rip),%rcx
mov    (%rcx),%esi
mov    0x2bdd(%rip),%rcx
or     (%rcx),%eax
mov    %ebp,%edi
mov    %eax,%edx
call   1040 <fcntl@plt>
mov    %r15d,%eax
mov    %ebp,0x0(%r13)
test   %r15d,%r15d
je     1447 <main+0x1d7>
mov    %eax,%ebx
xor    %eax,%eax
mov    %rax,0x10(%rsp)
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1180 <fork@plt>
test   %eax,%eax
je     1444 <main+0x1d4>
mov    0x10(%rsp),%rcx
mov    %eax,(%r12,%rcx,4)
add    $0x1,%rcx
mov    %rcx,0x10(%rsp)
cmp    %rcx,%rbx
jne    1420 <main+0x1b0>
jmp    144d <main+0x1dd>
mov    %r15d,%eax
cmp    %eax,0x10(%rsp)
jne    147c <main+0x20c>
mov    0x2b6c(%rip),%rax
mov    (%rax),%edi
lea    0xbe7(%rip),%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   10d0 <fprintf@plt>
cs nopw 0x0(%rax,%rax,1)
mov    $0x64,%edi
call   1170 <sleep@plt>
jmp    1470 <main+0x200>
lea    0x18(%rsp),%rdi
call   10b0 <pipe@plt>
test   %eax,%eax
js     15ac <main+0x33c>
mov    0x8(%rsp),%rax
mov    %eax,%r15d
cmp    $0x2,%eax
jl     14d1 <main+0x261>
mov    $0x1,%ebx
jmp    14c3 <main+0x253>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
call   1080 <dup@plt>
mov    %eax,0x0(%r13,%rbx,4)
add    $0x1,%rbx
cmp    %rbx,%r15
je     14d1 <main+0x261>
mov    %ebp,%edi
cmp    $0x4,%r14d
jl     14b0 <main+0x240>
mov    0x18(%rsp),%edi
jmp    14b0 <main+0x240>
mov    0x8(%rsp),%rax
add    $0xffffffff,%eax
cltq
mov    %rax,0x20(%rsp)
lea    0x34(%rsp),%rbp
jmp    150c <main+0x29c>
nopw   0x0(%rax,%rax,1)
mov    0x20(%rsp),%rax
mov    0x0(%r13,%rax,4),%edi
add    $0x1,%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1100 <select@plt>
mov    %rbp,%rdi
call   10f0 <FD_ZERO@plt>
cmpl   $0x0,0x8(%rsp)
jle    14f0 <main+0x280>
xor    %ebx,%ebx
nopl   (%rax)
mov    0x0(%r13,%rbx,4),%edi
mov    %rbp,%rsi
call   1160 <FD_SET@plt>
add    $0x1,%rbx
cmp    %rbx,%r15
jne    1520 <main+0x2b0>
mov    0x20(%rsp),%rax
mov    0x0(%r13,%rax,4),%edi
add    $0x1,%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1100 <select@plt>
cmpl   $0x0,0x8(%rsp)
jle    150c <main+0x29c>
xor    %r12d,%r12d
jmp    1569 <main+0x2f9>
xchg   %ax,%ax
add    $0x1,%r12
cmp    %r12,%r15
je     150c <main+0x29c>
mov    0x0(%r13,%r12,4),%ebx
mov    %ebx,%edi
mov    %rbp,%rsi
call   1050 <FD_ISSET@plt>
test   %rax,%rax
je     1560 <main+0x2f0>
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1140 <accept@plt>
test   %eax,%eax
js     1560 <main+0x2f0>
mov    %eax,%r14d
lea    0xac6(%rip),%rdi
mov    %ebx,%esi
mov    0x10(%rsp),%rdx
call   1070 <printf@plt>
mov    %r14d,%edi
call   10a0 <close@plt>
jmp    1560 <main+0x2f0>
lea    0xaa4(%rip),%rdi
call   1130 <perror@plt>
jmp    148e <main+0x21e>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [58 x i8] c"forks = %d, dupes per fork = %d, total = %d, dup_pipe=%d\0A\00"
@AF_INET = global i32 0
@.str.1 = [10 x i8] c"127.0.0.1\00"
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str.2 = [19 x i8] c"[+] started sd=%d\0A\00"
@.str.3 = [5 x i8] c"pipe\00"
@.str.4 = [29 x i8] c"received. fd=%d, procnum=%d\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 i32*
%11 = alloca i32*
%12 = alloca %struct.sockaddr_in
%13 = alloca %struct.sockaddr*
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
%18 = alloca i32
%19 = alloca i32
%20 = alloca i32
%21 = alloca [2 x i32]
%22 = alloca i32
%23 = alloca i32
%24 = alloca i32
%25 = alloca i32
%26 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
store i32 1, i32* %7
store i32 1025, i32* %8
%27 = load i32, i32* %4
%28 = icmp sgt i32 %27, 1
br i1 %28, label %29, label %41
29: ; preds = %2
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 1
%32 = load i8*, i8** %31
%33 = call i32 @atoi(i8* %32)
store i32 %33, i32* %6
%34 = load i32, i32* %6
%35 = icmp slt i32 %34, 0
br i1 %35, label %39, label %36
36: ; preds = %29
%37 = load i32, i32* %6
%38 = icmp sgt i32 %37, 1000000
br i1 %38, label %39, label %40
39: ; preds = %36, %29
store i32 0, i32* %6
br label %40
40: ; preds = %39, %36
br label %41
41: ; preds = %40, %2
%42 = load i32, i32* %4
%43 = icmp sgt i32 %42, 2
br i1 %43, label %44, label %56
44: ; preds = %41
%45 = load i8**, i8*** %5
%46 = getelementptr inbounds i8*, i8** %45, i64 2
%47 = load i8*, i8** %46
%48 = call i32 @atoi(i8* %47)
store i32 %48, i32* %7
%49 = load i32, i32* %7
%50 = icmp slt i32 %49, 0
br i1 %50, label %54, label %51
51: ; preds = %44
%52 = load i32, i32* %7
%53 = icmp sgt i32 %52, 10000
br i1 %53, label %54, label %55
54: ; preds = %51, %44
store i32 1, i32* %7
br label %55
55: ; preds = %54, %51
br label %56
56: ; preds = %55, %41
store i32 0, i32* %9
%57 = load i32, i32* %4
%58 = icmp sgt i32 %57, 3
br i1 %58, label %59, label %60
59: ; preds = %56
store i32 1, i32* %9
br label %60
60: ; preds = %59, %56
%61 = load i32, i32* @stderr
%62 = load i32, i32* %6
%63 = load i32, i32* %7
%64 = load i32, i32* %6
%65 = load i32, i32* %7
%66 = mul nsw i32 %64, %65
%67 = load i32, i32* %9
%68 = call i32 (i32, i8*, ...) @fprintf(i32 %61, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str, i64 0, i64 0), i32 %62, i32 %63, i32 %66, i32 %67)
%69 = load i32, i32* %7
%70 = sext i32 %69 to i64
%71 = mul i64 4, %70
%72 = trunc i64 %71 to i32
%73 = call i32* @calloc(i32 1, i32 %72)
store i32* %73, i32** %10
%74 = load i32, i32* %7
%75 = sext i32 %74 to i64
%76 = mul i64 4, %75
%77 = trunc i64 %76 to i32
%78 = call i32* @calloc(i32 1, i32 %77)
store i32* %78, i32** %11
%79 = call i32 @memset(%struct.sockaddr_in* %12, i32 0, i32 12)
%80 = load i32, i32* @AF_INET
%81 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 2
store i32 %80, i32* %81
%82 = load i32, i32* %8
%83 = call i32 @htons(i32 %82)
%84 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 1
store i32 %83, i32* %84
%85 = load i32, i32* @AF_INET
%86 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 0
%87 = call i32 @inet_pton(i32 %85, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32* %86)
%88 = bitcast %struct.sockaddr_in* %12 to %struct.sockaddr*
store %struct.sockaddr* %88, %struct.sockaddr** %13
store i32 12, i32* %14
%89 = load i32, i32* @PF_INET
%90 = load i32, i32* @SOCK_STREAM
%91 = load i32, i32* @IPPROTO_TCP
%92 = call i32 @socket(i32 %89, i32 %90, i32 %91)
store i32 %92, i32* %15
store i32 1, i32* %16
%93 = load i32, i32* %15
%94 = load i32, i32* @SOL_SOCKET
%95 = load i32, i32* @SO_REUSEADDR
%96 = bitcast i32* %16 to i8*
%97 = call i32 @setsockopt(i32 %93, i32 %94, i32 %95, i8* %96, i32 4)
%98 = load i32, i32* %15
%99 = load %struct.sockaddr*, %struct.sockaddr** %13
%100 = load i32, i32* %14
%101 = call i32 @bind(i32 %98, %struct.sockaddr* %99, i32 %100)
%102 = load i32, i32* %15
%103 = call i32 @listen(i32 %102, i32 128)
%104 = load i32, i32* %15
%105 = load i32, i32* @F_GETFL
%106 = call i32 @fcntl(i32 %104, i32 %105, i32 0)
store i32 %106, i32* %17
%107 = load i32, i32* %15
%108 = load i32, i32* @F_SETFL
%109 = load i32, i32* %17
%110 = load i32, i32* @O_NONBLOCK
%111 = or i32 %109, %110
%112 = call i32 @fcntl(i32 %107, i32 %108, i32 %111)
%113 = load i32, i32* %15
%114 = load i32*, i32** %10
%115 = getelementptr inbounds i32, i32* %114, i64 0
store i32 %113, i32* %115
store i32 0, i32* %18
br label %116
116: ; preds = %131, %60
%117 = load i32, i32* %18
%118 = load i32, i32* %6
%119 = icmp slt i32 %117, %118
br i1 %119, label %120, label %134
120: ; preds = %116
%121 = call i32 (...) @fork()
store i32 %121, i32* %19
%122 = load i32, i32* %19
%123 = icmp eq i32 %122, 0
br i1 %123, label %124, label %125
124: ; preds = %120
br label %134
125: ; preds = %120
%126 = load i32, i32* %19
%127 = load i32*, i32** %11
%128 = load i32, i32* %18
%129 = sext i32 %128 to i64
%130 = getelementptr inbounds i32, i32* %127, i64 %129
store i32 %126, i32* %130
br label %131
131: ; preds = %125
%132 = load i32, i32* %18
%133 = add nsw i32 %132, 1
store i32 %133, i32* %18
br label %116
134: ; preds = %124, %116
%135 = load i32, i32* %18
store i32 %135, i32* %20
%136 = load i32, i32* %18
%137 = load i32, i32* %6
%138 = icmp eq i32 %136, %137
br i1 %138, label %139, label %147
139: ; preds = %134
%140 = load i32, i32* @stderr
%141 = load i32*, i32** %10
%142 = getelementptr inbounds i32, i32* %141, i64 0
%143 = load i32, i32* %142
%144 = call i32 (i32, i8*, ...) @fprintf(i32 %140, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %143)
br label %145
145: ; preds = %139, %145
%146 = call i32 @sleep(i32 100)
br label %145
147: ; preds = %134
%148 = getelementptr inbounds [2 x i32], [2 x i32]* %21, i64 0, i64 0
%149 = call i32 @pipe(i32* %148)
store i32 %149, i32* %22
%150 = load i32, i32* %22
%151 = icmp slt i32 %150, 0
br i1 %151, label %152, label %154
152: ; preds = %147
%153 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %154
154: ; preds = %152, %147
store i32 1, i32* %18
br label %155
155: ; preds = %180, %154
%156 = load i32, i32* %18
%157 = load i32, i32* %7
%158 = icmp slt i32 %156, %157
br i1 %158, label %159, label %183
159: ; preds = %155
%160 = load i32, i32* %9
%161 = icmp ne i32 %160, 0
br i1 %161, label %162, label %170
162: ; preds = %159
%163 = getelementptr inbounds [2 x i32], [2 x i32]* %21, i64 0, i64 0
%164 = load i32, i32* %163
%165 = call i32 @dup(i32 %164)
%166 = load i32*, i32** %10
%167 = load i32, i32* %18
%168 = sext i32 %167 to i64
%169 = getelementptr inbounds i32, i32* %166, i64 %168
store i32 %165, i32* %169
br label %179
170: ; preds = %159
%171 = load i32*, i32** %10
%172 = getelementptr inbounds i32, i32* %171, i64 0
%173 = load i32, i32* %172
%174 = call i32 @dup(i32 %173)
%175 = load i32*, i32** %10
%176 = load i32, i32* %18
%177 = sext i32 %176 to i64
%178 = getelementptr inbounds i32, i32* %175, i64 %177
store i32 %174, i32* %178
br label %179
179: ; preds = %170, %162
br label %180
180: ; preds = %179
%181 = load i32, i32* %18
%182 = add nsw i32 %181, 1
store i32 %182, i32* %18
br label %155
183: ; preds = %155
br label %184
184: ; preds = %183, %239
%185 = call i32 @FD_ZERO(i32* %23)
store i32 0, i32* %18
br label %186
186: ; preds = %198, %184
%187 = load i32, i32* %18
%188 = load i32, i32* %7
%189 = icmp slt i32 %187, %188
br i1 %189, label %190, label %201
190: ; preds = %186
%191 = load i32*, i32** %10
%192 = load i32, i32* %18
%193 = sext i32 %192 to i64
%194 = getelementptr inbounds i32, i32* %191, i64 %193
%195 = load i32, i32* %194
store i32 %195, i32* %24
%196 = load i32, i32* %24
%197 = call i32 @FD_SET(i32 %196, i32* %23)
br label %198
198: ; preds = %190
%199 = load i32, i32* %18
%200 = add nsw i32 %199, 1
store i32 %200, i32* %18
br label %186
201: ; preds = %186
%202 = load i32*, i32** %10
%203 = load i32, i32* %7
%204 = sub nsw i32 %203, 1
%205 = sext i32 %204 to i64
%206 = getelementptr inbounds i32, i32* %202, i64 %205
%207 = load i32, i32* %206
%208 = add nsw i32 %207, 1
%209 = call i32 @select(i32 %208, i32* %23, i32* null, i32* null, i32* null)
store i32 0, i32* %18
br label %210
210: ; preds = %236, %201
%211 = load i32, i32* %18
%212 = load i32, i32* %7
%213 = icmp slt i32 %211, %212
br i1 %213, label %214, label %239
214: ; preds = %210
%215 = load i32*, i32** %10
%216 = load i32, i32* %18
%217 = sext i32 %216 to i64
%218 = getelementptr inbounds i32, i32* %215, i64 %217
%219 = load i32, i32* %218
store i32 %219, i32* %25
%220 = load i32, i32* %25
%221 = call i64 @FD_ISSET(i32 %220, i32* %23)
%222 = icmp ne i64 %221, 0
br i1 %222, label %223, label %235
223: ; preds = %214
%224 = load i32, i32* %25
%225 = call i32 @accept(i32 %224, i32* null, i32 0)
store i32 %225, i32* %26
%226 = load i32, i32* %26
%227 = icmp sge i32 %226, 0
br i1 %227, label %228, label %234
228: ; preds = %223
%229 = load i32, i32* %25
%230 = load i32, i32* %20
%231 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i32 %229, i32 %230)
%232 = load i32, i32* %26
%233 = call i32 @close(i32 %232)
br label %234
234: ; preds = %228, %223
br label %235
235: ; preds = %234, %214
br label %236
236: ; preds = %235
%237 = load i32, i32* %18
%238 = add nsw i32 %237, 1
store i32 %238, i32* %18
br label %210
239: ; preds = %210
br label %184
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [58 x i8] c"forks = %d, dupes per fork = %d, total = %d, dup_pipe=%d\0A\00"
@AF_INET = global i32 0
@.str.1 = [10 x i8] c"127.0.0.1\00"
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str.2 = [19 x i8] c"[+] started sd=%d\0A\00"
@.str.3 = [5 x i8] c"pipe\00"
@.str.4 = [29 x i8] c"received. fd=%d, procnum=%d\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = alloca [2 x i32]
%6 = alloca i32
%7 = icmp sgt i32 %0, 1
br i1 %7, label %8, label %21
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call i32 @atoi(i8* %10)
%12 = icmp ugt i32 %11, 1000000
%13 = select i1 %12, i32 0, i32 %11
%14 = icmp eq i32 %0, 2
br i1 %14, label %21, label %15
15: ; preds = %8
%16 = getelementptr inbounds i8*, i8** %1, i64 2
%17 = load i8*, i8** %16
%18 = tail call i32 @atoi(i8* %17)
%19 = icmp ugt i32 %18, 10000
%20 = select i1 %19, i32 1, i32 %18
br label %21
21: ; preds = %2, %15, %8
%22 = phi i32 [ %13, %15 ], [ %13, %8 ], [ 0, %2 ]
%23 = phi i32 [ %20, %15 ], [ 1, %8 ], [ 1, %2 ]
%24 = icmp sgt i32 %0, 3
%25 = zext i1 %24 to i32
%26 = load i32, i32* @stderr
%27 = mul nsw i32 %23, %22
%28 = tail call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str, i64 0, i64 0), i32 %22, i32 %23, i32 %27, i32 %25)
%29 = shl nuw nsw i32 %23, 2
%30 = tail call i32* @calloc(i32 1, i32 %29)
%31 = tail call i32* @calloc(i32 1, i32 %29)
%32 = bitcast %struct.sockaddr_in* %3 to i8*
%33 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%34 = load i32, i32* @AF_INET
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %34, i32* %35
%36 = call i32 @htons(i32 1025)
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %36, i32* %37
%38 = load i32, i32* @AF_INET
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%40 = call i32 @inet_pton(i32 %38, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32* %39)
%41 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%42 = load i32, i32* @PF_INET
%43 = load i32, i32* @SOCK_STREAM
%44 = load i32, i32* @IPPROTO_TCP
%45 = call i32 @socket(i32 %42, i32 %43, i32 %44)
%46 = bitcast i32* %4 to i8*
store i32 1, i32* %4
%47 = load i32, i32* @SOL_SOCKET
%48 = load i32, i32* @SO_REUSEADDR
%49 = call i32 @setsockopt(i32 %45, i32 %47, i32 %48, i8* %46, i32 4)
%50 = call i32 @bind(i32 %45, %struct.sockaddr* %41, i32 12)
%51 = call i32 @listen(i32 %45, i32 128)
%52 = load i32, i32* @F_GETFL
%53 = call i32 @fcntl(i32 %45, i32 %52, i32 0)
%54 = load i32, i32* @F_SETFL
%55 = load i32, i32* @O_NONBLOCK
%56 = or i32 %55, %53
%57 = call i32 @fcntl(i32 %45, i32 %54, i32 %56)
store i32 %45, i32* %30
%58 = icmp eq i32 %22, 0
br i1 %58, label %71, label %59
59: ; preds = %21
%60 = zext i32 %22 to i64
br label %61
61: ; preds = %59, %65
%62 = phi i64 [ 0, %59 ], [ %67, %65 ]
%63 = call i32 (...) @fork()
%64 = icmp eq i32 %63, 0
br i1 %64, label %69, label %65
65: ; preds = %61
%66 = getelementptr inbounds i32, i32* %31, i64 %62
store i32 %63, i32* %66
%67 = add nuw nsw i64 %62, 1
%68 = icmp eq i64 %67, %60
br i1 %68, label %74, label %61
69: ; preds = %61
%70 = trunc i64 %62 to i32
br label %71
71: ; preds = %69, %21
%72 = phi i32 [ 0, %21 ], [ %70, %69 ]
%73 = icmp eq i32 %72, %22
br i1 %73, label %74, label %79
74: ; preds = %65, %71
%75 = load i32, i32* @stderr
%76 = call i32 (i32, i8*, ...) @fprintf(i32 %75, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %45)
br label %77
77: ; preds = %77, %74
%78 = call i32 @sleep(i32 100)
br label %77
79: ; preds = %71
%80 = bitcast [2 x i32]* %5 to i8*
%81 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%82 = call i32 @pipe(i32* %81)
%83 = icmp slt i32 %82, 0
br i1 %83, label %84, label %86
84: ; preds = %79
%85 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %86
86: ; preds = %84, %79
%87 = icmp sgt i32 %23, 1
br i1 %87, label %88, label %90
88: ; preds = %86
%89 = zext i32 %23 to i64
br label %98
90: ; preds = %98, %86
%91 = bitcast i32* %6 to i8*
%92 = icmp sgt i32 %23, 0
%93 = add nsw i32 %23, -1
%94 = sext i32 %93 to i64
%95 = getelementptr inbounds i32, i32* %30, i64 %94
%96 = zext i32 %23 to i64
%97 = zext i32 %23 to i64
br label %106
98: ; preds = %88, %98
%99 = phi i64 [ 1, %88 ], [ %104, %98 ]
%100 = load i32, i32* %81
%101 = select i1 %24, i32 %100, i32 %45
%102 = call i32 @dup(i32 %101)
%103 = getelementptr inbounds i32, i32* %30, i64 %99
store i32 %102, i32* %103
%104 = add nuw nsw i64 %99, 1
%105 = icmp eq i64 %104, %89
br i1 %105, label %90, label %98
106: ; preds = %90, %138
%107 = call i32 @FD_ZERO(i32* %6)
br i1 %92, label %112, label %108
108: ; preds = %106
%109 = load i32, i32* %95
%110 = add nsw i32 %109, 1
%111 = call i32 @select(i32 %110, i32* %6, i32* null, i32* null, i32* null)
br label %138
112: ; preds = %106, %112
%113 = phi i64 [ %117, %112 ], [ 0, %106 ]
%114 = getelementptr inbounds i32, i32* %30, i64 %113
%115 = load i32, i32* %114
%116 = call i32 @FD_SET(i32 %115, i32* %6)
%117 = add nuw nsw i64 %113, 1
%118 = icmp eq i64 %117, %96
br i1 %118, label %119, label %112
119: ; preds = %112
%120 = load i32, i32* %95
%121 = add nsw i32 %120, 1
%122 = call i32 @select(i32 %121, i32* %6, i32* null, i32* null, i32* null)
br i1 %92, label %123, label %138
123: ; preds = %119, %135
%124 = phi i64 [ %136, %135 ], [ 0, %119 ]
%125 = getelementptr inbounds i32, i32* %30, i64 %124
%126 = load i32, i32* %125
%127 = call i64 @FD_ISSET(i32 %126, i32* %6)
%128 = icmp eq i64 %127, 0
br i1 %128, label %135, label %129
129: ; preds = %123
%130 = call i32 @accept(i32 %126, i32* null, i32 0)
%131 = icmp sgt i32 %130, -1
br i1 %131, label %132, label %135
132: ; preds = %129
%133 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i32 %126, i32 %72)
%134 = call i32 @close(i32 %130)
br label %135
135: ; preds = %129, %132, %123
%136 = add nuw nsw i64 %124, 1
%137 = icmp eq i64 %136, %97
br i1 %137, label %138, label %123
138: ; preds = %135, %108, %119
br label %106
}