#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 {
  TYPE_1__ sin_addr;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ caddr;

/* Variables and functions */
int accept(int, struct sockaddr *, unsigned int *);
int /*<<< orphan*/ close(int);
scalar_t__ fork();
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/,
                          unsigned int);
int /*<<< orphan*/ print_info(char *, ...);
void listen_and_accept(int socket, int queue, void (*handler)(int)) {
  struct sockaddr_in caddr;
  unsigned int caddrLen = sizeof(caddr);
  int client = 0;
  memset(&caddr, 0, caddrLen);
  listen(socket, queue);
  print_info("Now accepting incoming connection");
  while ((client = accept(socket, (struct sockaddr *)&caddr, &caddrLen)) !=
         -1) {
    print_info("Connection accepted from %d", caddr.sin_addr.s_addr);
    if (fork() == 0) {
      close(socket);
      handler(client);
    } else {
      close(client);
    }
  }
}


x86 assembly (clang, -O0):
<listen_and_accept>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
movl   $0x4,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x1c(%rbp),%edx
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
call   1030 <memset@plt>
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
call   1050 <listen@plt>
lea    0xe60(%rip),%rdi
mov    $0x0,%al
call   1080 <print_info@plt>
mov    -0x4(%rbp),%edi
lea    -0x18(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
call   1060 <accept@plt>
mov    %eax,-0x20(%rbp)
cmp    $0xffffffff,%eax
je     1208 <listen_and_accept+0xa8>
mov    -0x18(%rbp),%esi
lea    0xe55(%rip),%rdi
mov    $0x0,%al
call   1080 <print_info@plt>
mov    $0x0,%al
call   1070 <fork@plt>
cmp    $0x0,%rax
jne    11fb <listen_and_accept+0x9b>
mov    -0x4(%rbp),%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%edi
call   *%rax
jmp    1203 <listen_and_accept+0xa3>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
jmp    11a7 <listen_and_accept+0x47>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<listen_and_accept>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %esi,%ebx
mov    %edi,%ebp
movl   $0x4,0x4(%rsp)
mov    %rsp,%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1030 <memset@plt>
mov    %ebp,%edi
mov    %ebx,%esi
call   1050 <listen@plt>
lea    0xe64(%rip),%rdi
xor    %eax,%eax
call   1080 <print_info@plt>
lea    0x4(%rsp),%rdx
mov    %ebp,%edi
mov    %r15,%rsi
call   1060 <accept@plt>
cmp    $0xffffffff,%eax
je     1212 <listen_and_accept+0xb2>
mov    %eax,%ebx
lea    0xe62(%rip),%r15
mov    %rsp,%r12
lea    0x4(%rsp),%r13
jmp    11eb <listen_and_accept+0x8b>
nopw   0x0(%rax,%rax,1)
mov    %ebx,%edi
call   1040 <close@plt>
mov    %ebp,%edi
mov    %r12,%rsi
mov    %r13,%rdx
call   1060 <accept@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     1212 <listen_and_accept+0xb2>
mov    (%rsp),%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1080 <print_info@plt>
xor    %eax,%eax
call   1070 <fork@plt>
test   %rax,%rax
jne    11d0 <listen_and_accept+0x70>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %ebx,%edi
call   *%r14
jmp    11d7 <listen_and_accept+0x77>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [34 x i8] c"Now accepting incoming connection\00"
@.str.1 = [28 x i8] c"Connection accepted from %d\00"
define void @listen_and_accept(i32 %0, i32 %1, void (i32)* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca void (i32)*
%7 = alloca %struct.sockaddr_in
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store void (i32)* %2, void (i32)** %6
store i32 4, i32* %8
store i32 0, i32* %9
%10 = load i32, i32* %8
%11 = call i32 @memset(%struct.sockaddr_in* %7, i32 0, i32 %10)
%12 = load i32, i32* %4
%13 = load i32, i32* %5
%14 = call i32 @listen(i32 %12, i32 %13)
%15 = call i32 (i8*, ...) @print_info(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %36, %3
%17 = load i32, i32* %4
%18 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%19 = call i32 @accept(i32 %17, %struct.sockaddr* %18, i32* %8)
store i32 %19, i32* %9
%20 = icmp ne i32 %19, -1
br i1 %20, label %21, label %37
21: ; preds = %16
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%23 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %22, i32 0, i32 0
%24 = load i32, i32* %23
%25 = call i32 (i8*, ...) @print_info(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %24)
%26 = call i64 (...) @fork()
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %33
28: ; preds = %21
%29 = load i32, i32* %4
%30 = call i32 @close(i32 %29)
%31 = load void (i32)*, void (i32)** %6
%32 = load i32, i32* %9
br label %36
33: ; preds = %21
%34 = load i32, i32* %9
%35 = call i32 @close(i32 %34)
br label %36
36: ; preds = %33, %28
br label %16
37: ; preds = %16
ret void
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [34 x i8] c"Now accepting incoming connection\00"
@.str.1 = [28 x i8] c"Connection accepted from %d\00"
define void @listen_and_accept(i32 %0, i32 %1, void (i32)* nocapture %2) {
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = bitcast %struct.sockaddr_in* %4 to i8*
%7 = bitcast i32* %5 to i8*
store i32 4, i32* %5
%8 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 4)
%9 = call i32 @listen(i32 %0, i32 %1)
%10 = call i32 (i8*, ...) @print_info(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
%11 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%12 = call i32 @accept(i32 %0, %struct.sockaddr* %11, i32* %5)
%13 = icmp eq i32 %12, -1
br i1 %13, label %29, label %14
14: ; preds = %3
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0, i32 0
br label %16
16: ; preds = %14, %26
%17 = phi i32 [ %12, %14 ], [ %27, %26 ]
%18 = load i32, i32* %15
%19 = call i32 (i8*, ...) @print_info(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %18)
%20 = call i64 (...) @fork()
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %16
%23 = call i32 @close(i32 %0)
br label %26
24: ; preds = %16
%25 = call i32 @close(i32 %17)
br label %26
26: ; preds = %24, %22
%27 = call i32 @accept(i32 %0, %struct.sockaddr* %11, i32* %5)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %16
29: ; preds = %26, %3
ret void
}