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

/* Variables and functions */
int /*<<< orphan*/ do_listener(int, char const *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fork();
scalar_t__ listen_socket(char *, char *, int, int *);
int fork_listener(const char *port, int backlog, pid_t *cpid,
                  const char *dbname) {
  int sockfd;
  if (listen_socket("localhost", (char *)port, backlog, &sockfd)) {
    return 1;
  }
  if (cpid == NULL || !(*cpid = fork()))
    exit(do_listener(sockfd, dbname));
  return 0;
}


x86 assembly (clang, -O0):
<fork_listener>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
lea    0xe9b(%rip),%rdi
lea    -0x2c(%rbp),%rcx
call   1040 <listen_socket@plt>
cmp    $0x0,%rax
je     1184 <fork_listener+0x44>
movl   $0x1,-0x4(%rbp)
jmp    11bf <fork_listener+0x7f>
cmpq   $0x0,-0x20(%rbp)
je     11a5 <fork_listener+0x65>
mov    $0x0,%al
call   1060 <fork@plt>
mov    -0x20(%rbp),%rcx
mov    %eax,(%rcx)
cmp    $0x0,%eax
jne    11b8 <fork_listener+0x78>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rsi
call   1030 <do_listener@plt>
mov    %eax,%edi
call   1050 <exit@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fork_listener>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rcx,%r14
mov    %rdx,%rbx
mov    %esi,%edx
mov    %rdi,%rsi
lea    0xea6(%rip),%rdi
lea    0xc(%rsp),%rcx
call   1040 <listen_socket@plt>
mov    $0x1,%ebp
test   %rax,%rax
jne    1182 <fork_listener+0x42>
test   %rbx,%rbx
je     118d <fork_listener+0x4d>
xor    %ebp,%ebp
xor    %eax,%eax
call   1060 <fork@plt>
mov    %eax,(%rbx)
test   %eax,%eax
je     118d <fork_listener+0x4d>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0xc(%rsp),%edi
mov    %r14,%rsi
call   1030 <do_listener@plt>
mov    %eax,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@.str = [10 x i8] c"localhost\00"
define i32 @fork_listener(i8* %0, i32 %1, i32* %2, i8* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i8*
%10 = alloca i32
store i8* %0, i8** %6
store i32 %1, i32* %7
store i32* %2, i32** %8
store i8* %3, i8** %9
%11 = load i8*, i8** %6
%12 = load i32, i32* %7
%13 = call i64 @listen_socket(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %11, i32 %12, i32* %10)
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %4
store i32 1, i32* %5
br label %29
16: ; preds = %4
%17 = load i32*, i32** %8
%18 = icmp eq i32* %17, null
br i1 %18, label %23, label %19
19: ; preds = %16
%20 = call i32 (...) @fork()
%21 = load i32*, i32** %8
store i32 %20, i32* %21
%22 = icmp ne i32 %20, 0
br i1 %22, label %28, label %23
23: ; preds = %19, %16
%24 = load i32, i32* %10
%25 = load i8*, i8** %9
%26 = call i32 @do_listener(i32 %24, i8* %25)
%27 = call i32 @exit(i32 %26)
unreachable
28: ; preds = %19
store i32 0, i32* %5
br label %29
29: ; preds = %28, %15
%30 = load i32, i32* %5
ret i32 %30
}

LLVM IR (-O2):
@.str = [10 x i8] c"localhost\00"
define i32 @fork_listener(i8* %0, i32 %1, i32* writeonly %2, i8* %3) {
%5 = alloca i32
%6 = bitcast i32* %5 to i8*
%7 = call i64 @listen_socket(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %1, i32* %5)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %18
9: ; preds = %4
%10 = icmp eq i32* %2, null
br i1 %10, label %14, label %11
11: ; preds = %9
%12 = call i32 (...) @fork()
store i32 %12, i32* %2
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %11, %9
%15 = load i32, i32* %5
%16 = call i32 @do_listener(i32 %15, i8* %3)
%17 = call i32 @exit(i32 %16)
unreachable
18: ; preds = %11, %4
%19 = phi i32 [ 1, %4 ], [ 0, %11 ]
ret i32 %19
}