#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_storage {
  int /*<<< orphan*/ ss_family;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;

/* Variables and functions */
int INET6_ADDRSTRLEN;
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fork();
void *get_in_addr(struct sockaddr *);
int /*<<< orphan*/ inet_ntop(int /*<<< orphan*/, void *, char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int send(int, char *, int, int /*<<< orphan*/);
void accept_clients(int sockfd) {
  struct sockaddr_storage client_addr;
  socklen_t sin_size;
  int client_sock_fd;
  char client_in_addr_s[INET6_ADDRSTRLEN];
  int is_child_process;
  while (1) {
    sin_size = sizeof client_addr;
    client_sock_fd = accept(sockfd, (struct sockaddr *)&client_addr, &sin_size);
    if (client_sock_fd == -1) {
      perror("accept");
      continue;
    }
    void *client_in_addr = get_in_addr((struct sockaddr *)&client_addr);
    inet_ntop(client_addr.ss_family, client_in_addr, client_in_addr_s,
              sizeof client_in_addr_s);
    printf("server: got connection from %s\n", client_in_addr_s);
    is_child_process = !fork();
    if (is_child_process) {
      close(sockfd);
      int sent = send(client_sock_fd, "Hello, world!", 14, 0);
      if (sent < 0)
        perror("send");
      close(client_sock_fd);
      exit(0);
    }
    close(client_sock_fd);
  }
}


x86 assembly (clang, -O0):
<accept_clients>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2e46(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x48(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
jmp    11d0 <accept_clients+0x40>
movl   $0x4,-0xc(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x8(%rbp),%rsi
lea    -0xc(%rbp),%rdx
call   1090 <accept@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    1205 <accept_clients+0x75>
lea    0xe05(%rip),%rdi
call   1080 <perror@plt>
jmp    11cb <accept_clients+0x3b>
lea    -0x8(%rbp),%rdi
call   1040 <get_in_addr@plt>
mov    -0x40(%rbp),%rdx
mov    %rax,%rcx
mov    -0x48(%rbp),%rax
mov    %rcx,-0x30(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    %eax,%ecx
call   1030 <inet_ntop@plt>
mov    -0x40(%rbp),%rsi
lea    0xdd1(%rip),%rdi
call   1060 <printf@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     12a2 <accept_clients+0x112>
mov    -0x4(%rbp),%edi
call   1070 <close@plt>
mov    -0x10(%rbp),%edi
lea    0xdb9(%rip),%rsi
mov    $0xe,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    1293 <accept_clients+0x103>
lea    0xda7(%rip),%rdi
call   1080 <perror@plt>
mov    -0x10(%rbp),%edi
call   1070 <close@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%edi
call   1070 <close@plt>
jmp    11cb <accept_clients+0x3b>

x86 assembly (clang, -O2):
<accept_clients>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edi,%r15d
mov    0x2e3d(%rip),%rax
mov    (%rax),%r14d
mov    %rsp,%r12
mov    %r14,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
lea    -0x30(%rbp),%r13
jmp    11e0 <accept_clients+0x50>
lea    0xe31(%rip),%rdi
call   1080 <perror@plt>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movl   $0x4,-0x34(%rbp)
mov    %r15d,%edi
mov    %r13,%rsi
lea    -0x34(%rbp),%rdx
call   1090 <accept@plt>
cmp    $0xffffffff,%eax
je     11c8 <accept_clients+0x38>
mov    %eax,%ebx
mov    %r13,%rdi
call   1040 <get_in_addr@plt>
mov    -0x30(%rbp),%edi
mov    %rax,%rsi
mov    %r12,%rdx
mov    %r14d,%ecx
call   1030 <inet_ntop@plt>
lea    0xdea(%rip),%rdi
mov    %r12,%rsi
call   1060 <printf@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
je     1239 <accept_clients+0xa9>
mov    %ebx,%edi
call   1070 <close@plt>
jmp    11e0 <accept_clients+0x50>
mov    %r15d,%edi
call   1070 <close@plt>
lea    0xddf(%rip),%rsi
mov    %ebx,%edi
mov    $0xe,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
test   %eax,%eax
jns    1266 <accept_clients+0xd6>
lea    0xdd4(%rip),%rdi
call   1080 <perror@plt>
mov    %ebx,%edi
call   1070 <close@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
@INET6_ADDRSTRLEN = global i32 0
@.str = [7 x i8] c"accept\00"
@.str.1 = [32 x i8] c"server: got connection from %s\0A\00"
@.str.2 = [14 x i8] c"Hello, world!\00"
@.str.3 = [5 x i8] c"send\00"
define void @accept_clients(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.sockaddr_storage
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
store i32 %0, i32* %2
%11 = load i32, i32* @INET6_ADDRSTRLEN
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %6
%14 = alloca i8, i64 %12
store i64 %12, i64* %7
br label %15
15: ; preds = %52, %22, %1
br label %16
16: ; preds = %15
store i32 4, i32* %4
%17 = load i32, i32* %2
%18 = bitcast %struct.sockaddr_storage* %3 to %struct.sockaddr*
%19 = call i32 @accept(i32 %17, %struct.sockaddr* %18, i32* %4)
store i32 %19, i32* %5
%20 = load i32, i32* %5
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %16
%23 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %15
24: ; preds = %16
%25 = bitcast %struct.sockaddr_storage* %3 to %struct.sockaddr*
%26 = call i8* @get_in_addr(%struct.sockaddr* %25)
store i8* %26, i8** %9
%27 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %3, i32 0, i32 0
%28 = load i32, i32* %27
%29 = load i8*, i8** %9
%30 = trunc i64 %12 to i32
%31 = call i32 @inet_ntop(i32 %28, i8* %29, i8* %14, i32 %30)
%32 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %14)
%33 = call i32 (...) @fork()
%34 = icmp ne i32 %33, 0
%35 = xor i1 %34, true
%36 = zext i1 %35 to i32
store i32 %36, i32* %8
%37 = load i32, i32* %8
%38 = icmp ne i32 %37, 0
br i1 %38, label %39, label %52
39: ; preds = %24
%40 = load i32, i32* %2
%41 = call i32 @close(i32 %40)
%42 = load i32, i32* %5
%43 = call i32 @send(i32 %42, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 14, i32 0)
store i32 %43, i32* %10
%44 = load i32, i32* %10
%45 = icmp slt i32 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %39
%47 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %48
48: ; preds = %46, %39
%49 = load i32, i32* %5
%50 = call i32 @close(i32 %49)
%51 = call i32 @exit(i32 0)
unreachable
52: ; preds = %24
%53 = load i32, i32* %5
%54 = call i32 @close(i32 %53)
br label %15
}

LLVM IR (-O2):
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
@INET6_ADDRSTRLEN = global i32 0
@.str = [7 x i8] c"accept\00"
@.str.1 = [32 x i8] c"server: got connection from %s\0A\00"
@.str.2 = [14 x i8] c"Hello, world!\00"
@.str.3 = [5 x i8] c"send\00"
define void @accept_clients(i32 %0) {
%2 = alloca %struct.sockaddr_storage
%3 = alloca i32
%4 = bitcast %struct.sockaddr_storage* %2 to i8*
%5 = bitcast i32* %3 to i8*
%6 = load i32, i32* @INET6_ADDRSTRLEN
%7 = zext i32 %6 to i64
%8 = alloca i8, i64 %7
%9 = bitcast %struct.sockaddr_storage* %2 to %struct.sockaddr*
%10 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %2, i64 0, i32 0
br label %11
11: ; preds = %16, %1
store i32 4, i32* %3
%12 = call i32 @accept(i32 %0, %struct.sockaddr* %9, i32* %3)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %14, %33
br label %11
17: ; preds = %11
%18 = call i8* @get_in_addr(%struct.sockaddr* %9)
%19 = load i32, i32* %10
%20 = call i32 @inet_ntop(i32 %19, i8* %18, i8* %8, i32 %6)
%21 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %8)
%22 = call i32 (...) @fork()
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %33
24: ; preds = %17
%25 = call i32 @close(i32 %0)
%26 = call i32 @send(i32 %12, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 14, i32 0)
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %24
%29 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %30
30: ; preds = %28, %24
%31 = call i32 @close(i32 %12)
%32 = call i32 @exit(i32 0)
unreachable
33: ; preds = %17
%34 = call i32 @close(i32 %12)
br label %16
}