#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 */

/* Variables and functions */
int /*<<< orphan*/ SOMAXCONN;
int *calloc(int, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
char *getenv(char *);
int listen(int, int /*<<< orphan*/);
int make_listen_socket(char const *, char const *);
int *parse_listen_fds_env(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ set_nonblock(int);
int /*<<< orphan*/ stderr;
int *get_listen_fds(int worker_count, const char *addr, const char *port) {
  char *listen_fds_env = getenv("listen_fds");
  if (listen_fds_env == NULL) {
    printf("creating new fd set\n");
    int *listen_fds = calloc(worker_count, sizeof(int));
    if (listen_fds == NULL) {
      fprintf(stderr, "failed to allocated mem for listen fds, aborting ...");
      exit(1);
    }
    for (int i = 0; i < worker_count; ++i) {
      listen_fds[i] = make_listen_socket(addr, port);
      set_nonblock(listen_fds[i]);
      int status = listen(listen_fds[i], SOMAXCONN);
      if (status == -1) {
        perror("error on listening");
        exit(1);
      }
    }
    return listen_fds;
  } else {
    printf("parsing existing fd set\n");
    return parse_listen_fds_env(listen_fds_env, worker_count);
  }
}


x86 assembly (clang, -O0):
<get_listen_fds>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
lea    0xe46(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    12ae <get_listen_fds+0x10e>
lea    0xe36(%rip),%rdi
call   1050 <printf@plt>
mov    -0xc(%rbp),%edi
mov    $0x4,%esi
call   1060 <calloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1215 <get_listen_fds+0x75>
mov    0x2de3(%rip),%rax
mov    (%rax),%edi
lea    0xe1a(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    12a1 <get_listen_fds+0x101>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <make_listen_socket@plt>
mov    %eax,%edx
mov    -0x30(%rbp),%rax
movslq -0x34(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x30(%rbp),%rax
movslq -0x34(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
call   1090 <set_nonblock@plt>
mov    -0x30(%rbp),%rax
movslq -0x34(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
mov    0x2d74(%rip),%rax
mov    (%rax),%esi
call   10a0 <listen@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0xffffffff,-0x38(%rbp)
jne    128e <get_listen_fds+0xee>
lea    0xdd6(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
jmp    1293 <get_listen_fds+0xf3>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    121c <get_listen_fds+0x7c>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12ca <get_listen_fds+0x12a>
lea    0xdb3(%rip),%rdi
call   1050 <printf@plt>
mov    -0x28(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1080 <parse_listen_fds_env@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_listen_fds>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%ebp
lea    0xe46(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11eb <get_listen_fds+0x4b>
mov    %rax,%rbx
lea    0xe85(%rip),%rdi
call   1050 <puts@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1080 <parse_listen_fds_env@plt>
lea    0xe79(%rip),%rdi
call   1050 <puts@plt>
mov    %ebp,%edi
mov    $0x4,%esi
call   1060 <calloc@plt>
test   %rax,%rax
je     127e <get_listen_fds+0xde>
mov    %rax,%r12
test   %ebp,%ebp
jle    1256 <get_listen_fds+0xb6>
mov    %ebp,%r13d
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r15,%rdi
mov    %r14,%rsi
call   1040 <make_listen_socket@plt>
mov    %eax,%ebp
mov    %eax,(%r12,%rbx,4)
mov    %eax,%edi
call   1090 <set_nonblock@plt>
mov    0x2d99(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   10a0 <listen@plt>
cmp    $0xffffffff,%eax
je     1268 <get_listen_fds+0xc8>
add    $0x1,%rbx
cmp    %rbx,%r13
jne    1220 <get_listen_fds+0x80>
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdd1(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    0x2d5b(%rip),%rax
mov    (%rax),%edi
lea    0xd7d(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"listen_fds\00"
@.str.1 = [21 x i8] c"creating new fd set\0A\00"
@stderr = global i32 0
@.str.2 = [53 x i8] c"failed to allocated mem for listen fds, aborting ...\00"
@SOMAXCONN = global i32 0
@.str.3 = [19 x i8] c"error on listening\00"
@.str.4 = [25 x i8] c"parsing existing fd set\0A\00"
define i32* @get_listen_fds(i32 %0, i8* %1, i8* %2) {
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%12 = call i8* @getenv(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
store i8* %12, i8** %8
%13 = load i8*, i8** %8
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %62
15: ; preds = %3
%16 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%17 = load i32, i32* %5
%18 = call i32* @calloc(i32 %17, i32 4)
store i32* %18, i32** %9
%19 = load i32*, i32** %9
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %25
21: ; preds = %15
%22 = load i32, i32* @stderr
%23 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.2, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %15
store i32 0, i32* %10
br label %26
26: ; preds = %57, %25
%27 = load i32, i32* %10
%28 = load i32, i32* %5
%29 = icmp slt i32 %27, %28
br i1 %29, label %30, label %60
30: ; preds = %26
%31 = load i8*, i8** %6
%32 = load i8*, i8** %7
%33 = call i32 @make_listen_socket(i8* %31, i8* %32)
%34 = load i32*, i32** %9
%35 = load i32, i32* %10
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i32, i32* %34, i64 %36
store i32 %33, i32* %37
%38 = load i32*, i32** %9
%39 = load i32, i32* %10
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i32, i32* %38, i64 %40
%42 = load i32, i32* %41
%43 = call i32 @set_nonblock(i32 %42)
%44 = load i32*, i32** %9
%45 = load i32, i32* %10
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i32, i32* %44, i64 %46
%48 = load i32, i32* %47
%49 = load i32, i32* @SOMAXCONN
%50 = call i32 @listen(i32 %48, i32 %49)
store i32 %50, i32* %11
%51 = load i32, i32* %11
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %56
53: ; preds = %30
%54 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
%55 = call i32 @exit(i32 1)
unreachable
56: ; preds = %30
br label %57
57: ; preds = %56
%58 = load i32, i32* %10
%59 = add nsw i32 %58, 1
store i32 %59, i32* %10
br label %26
60: ; preds = %26
%61 = load i32*, i32** %9
store i32* %61, i32** %4
br label %67
62: ; preds = %3
%63 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
%64 = load i8*, i8** %8
%65 = load i32, i32* %5
%66 = call i32* @parse_listen_fds_env(i8* %64, i32 %65)
store i32* %66, i32** %4
br label %67
67: ; preds = %62, %60
%68 = load i32*, i32** %4
ret i32* %68
}

LLVM IR (-O2):
@.str = [11 x i8] c"listen_fds\00"
@stderr = global i32 0
@.str.2 = [53 x i8] c"failed to allocated mem for listen fds, aborting ...\00"
@SOMAXCONN = global i32 0
@.str.3 = [19 x i8] c"error on listening\00"
@str = [24 x i8] c"parsing existing fd set\00"
@str.5 = [20 x i8] c"creating new fd set\00"
define i32* @get_listen_fds(i32 %0, i8* %1, i8* %2) {
%4 = tail call i8* @getenv(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %32
6: ; preds = %3
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.5, i64 0, i64 0))
%8 = tail call i32* @calloc(i32 %0, i32 4)
%9 = icmp eq i32* %8, null
br i1 %9, label %14, label %10
10: ; preds = %6
%11 = icmp sgt i32 %0, 0
br i1 %11, label %12, label %35
12: ; preds = %10
%13 = zext i32 %0 to i64
br label %20
14: ; preds = %6
%15 = load i32, i32* @stderr
%16 = tail call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.2, i64 0, i64 0))
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %20
%19 = icmp eq i64 %28, %13
br i1 %19, label %35, label %20
20: ; preds = %12, %18
%21 = phi i64 [ 0, %12 ], [ %28, %18 ]
%22 = tail call i32 @make_listen_socket(i8* %1, i8* %2)
%23 = getelementptr inbounds i32, i32* %8, i64 %21
store i32 %22, i32* %23
%24 = tail call i32 @set_nonblock(i32 %22)
%25 = load i32, i32* @SOMAXCONN
%26 = tail call i32 @listen(i32 %22, i32 %25)
%27 = icmp eq i32 %26, -1
%28 = add nuw nsw i64 %21, 1
br i1 %27, label %29, label %18
29: ; preds = %20
%30 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
%31 = tail call i32 @exit(i32 1)
unreachable
32: ; preds = %3
%33 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
%34 = tail call i32* @parse_listen_fds_env(i8* %4, i32 %0)
br label %35
35: ; preds = %18, %10, %32
%36 = phi i32* [ %34, %32 ], [ %8, %10 ], [ %8, %18 ]
ret i32* %36
}