#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;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;

/* Variables and functions */
int /*<<< orphan*/ Log(char *);
int /*<<< orphan*/ SHUT_RDWR;
int accept(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ close(int);
scalar_t__ getpeername(int, struct sockaddr *, int *);
int /*<<< orphan*/ inet_ntoa(int /*<<< orphan*/);
scalar_t__ realloc(int *, int);
int /*<<< orphan*/ shutdown(int, int /*<<< orphan*/);
scalar_t__ strcmp(int /*<<< orphan*/, char *);
int accept_client(int servfd, int **client_fd, int *client_count) {
  Log("Incoming client connection");
  int cfd = accept(servfd, NULL, NULL);
  if (cfd < 0)
    return -1;
  Log("- Connection accepted");
  struct sockaddr_in client_addr;
  socklen_t ca_len = sizeof(struct sockaddr_in);
  if (getpeername(cfd, (struct sockaddr *)&client_addr, &ca_len) < 0)
    return -1;
  if (strcmp(inet_ntoa(client_addr.sin_addr), "127.0.0.1") != 0) {
    Log("- Remote client detected -- closing connection.");
    shutdown(cfd, SHUT_RDWR);
    close(cfd);
    return 0;
  }
  *client_fd = (int *)realloc(*client_fd, sizeof(int) * (*client_count + 1));
  (*client_fd)[(*client_count)++] = cfd;
  return cfd;
}


x86 assembly (clang, -O0):
<accept_client>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    0xe66(%rip),%rdi
call   1080 <Log@plt>
mov    -0x8(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   10a0 <accept@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11c7 <accept_client+0x47>
movl   $0xffffffff,-0x4(%rbp)
jmp    129b <accept_client+0x11b>
lea    0xe4d(%rip),%rdi
call   1080 <Log@plt>
movl   $0x4,-0x24(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x20(%rbp),%rsi
lea    -0x24(%rbp),%rdx
call   1040 <getpeername@plt>
cmp    $0x0,%rax
jge    1200 <accept_client+0x80>
movl   $0xffffffff,-0x4(%rbp)
jmp    129b <accept_client+0x11b>
mov    -0x20(%rbp),%edi
call   1030 <inet_ntoa@plt>
mov    %eax,%edi
lea    0xe20(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
je     1251 <accept_client+0xd1>
lea    0xe14(%rip),%rdi
call   1080 <Log@plt>
mov    -0x1c(%rbp),%edi
mov    0x2dba(%rip),%rax
mov    (%rax),%esi
call   1050 <shutdown@plt>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    129b <accept_client+0x11b>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
add    $0x1,%eax
cltq
shl    $0x2,%rax
mov    %eax,%esi
call   1090 <realloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x1c(%rbp),%edx
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    -0x18(%rbp),%rsi
mov    (%rsi),%ecx
mov    %ecx,%edi
add    $0x1,%edi
mov    %edi,(%rsi)
movslq %ecx,%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<accept_client>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%ebx
lea    0xe68(%rip),%rdi
call   1080 <Log@plt>
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10a0 <accept@plt>
test   %eax,%eax
js     121b <accept_client+0x9b>
mov    %eax,%ebx
lea    0xe66(%rip),%rdi
call   1080 <Log@plt>
movl   $0x4,0xc(%rsp)
lea    0x8(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %ebx,%edi
call   1040 <getpeername@plt>
test   %rax,%rax
js     121b <accept_client+0x9b>
mov    0x8(%rsp),%edi
call   1030 <inet_ntoa@plt>
lea    0xe49(%rip),%rsi
mov    %eax,%edi
call   1070 <strcmp@plt>
test   %rax,%rax
je     1222 <accept_client+0xa2>
lea    0xe40(%rip),%rdi
call   1080 <Log@plt>
mov    0x2de9(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1050 <shutdown@plt>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %ebx,%ebx
jmp    1243 <accept_client+0xc3>
mov    $0xffffffff,%ebx
jmp    1243 <accept_client+0xc3>
mov    (%r15),%rdi
mov    (%r14),%eax
lea    0x4(,%rax,4),%esi
call   1090 <realloc@plt>
mov    %rax,(%r15)
movslq (%r14),%rcx
lea    0x1(%rcx),%edx
mov    %edx,(%r14)
mov    %ebx,(%rax,%rcx,4)
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [27 x i8] c"Incoming client connection\00"
@.str.1 = [22 x i8] c"- Connection accepted\00"
@.str.2 = [10 x i8] c"127.0.0.1\00"
@.str.3 = [48 x i8] c"- Remote client detected -- closing connection.\00"
@SHUT_RDWR = global i32 0
define i32 @accept_client(i32 %0, i32** %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32**
%7 = alloca i32*
%8 = alloca i32
%9 = alloca %struct.sockaddr_in
%10 = alloca i32
store i32 %0, i32* %5
store i32** %1, i32*** %6
store i32* %2, i32** %7
%11 = call i32 @Log(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%12 = load i32, i32* %5
%13 = call i32 @accept(i32 %12, i32* null, i32* null)
store i32 %13, i32* %8
%14 = load i32, i32* %8
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %3
store i32 -1, i32* %4
br label %58
17: ; preds = %3
%18 = call i32 @Log(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
store i32 4, i32* %10
%19 = load i32, i32* %8
%20 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%21 = call i64 @getpeername(i32 %19, %struct.sockaddr* %20, i32* %10)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %17
store i32 -1, i32* %4
br label %58
24: ; preds = %17
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
%26 = load i32, i32* %25
%27 = call i32 @inet_ntoa(i32 %26)
%28 = call i64 @strcmp(i32 %27, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %37
30: ; preds = %24
%31 = call i32 @Log(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.3, i64 0, i64 0))
%32 = load i32, i32* %8
%33 = load i32, i32* @SHUT_RDWR
%34 = call i32 @shutdown(i32 %32, i32 %33)
%35 = load i32, i32* %8
%36 = call i32 @close(i32 %35)
store i32 0, i32* %4
br label %58
37: ; preds = %24
%38 = load i32**, i32*** %6
%39 = load i32*, i32** %38
%40 = load i32*, i32** %7
%41 = load i32, i32* %40
%42 = add nsw i32 %41, 1
%43 = sext i32 %42 to i64
%44 = mul i64 4, %43
%45 = trunc i64 %44 to i32
%46 = call i64 @realloc(i32* %39, i32 %45)
%47 = inttoptr i64 %46 to i32*
%48 = load i32**, i32*** %6
store i32* %47, i32** %48
%49 = load i32, i32* %8
%50 = load i32**, i32*** %6
%51 = load i32*, i32** %50
%52 = load i32*, i32** %7
%53 = load i32, i32* %52
%54 = add nsw i32 %53, 1
store i32 %54, i32* %52
%55 = sext i32 %53 to i64
%56 = getelementptr inbounds i32, i32* %51, i64 %55
store i32 %49, i32* %56
%57 = load i32, i32* %8
store i32 %57, i32* %4
br label %58
58: ; preds = %37, %30, %23, %16
%59 = load i32, i32* %4
ret i32 %59
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [27 x i8] c"Incoming client connection\00"
@.str.1 = [22 x i8] c"- Connection accepted\00"
@.str.2 = [10 x i8] c"127.0.0.1\00"
@.str.3 = [48 x i8] c"- Remote client detected -- closing connection.\00"
@SHUT_RDWR = global i32 0
define i32 @accept_client(i32 %0, i32** nocapture %1, i32* nocapture %2) {
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = tail call i32 @Log(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 @accept(i32 %0, i32* null, i32* null)
%8 = icmp slt i32 %7, 0
br i1 %8, label %40, label %9
9: ; preds = %3
%10 = tail call i32 @Log(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%11 = bitcast %struct.sockaddr_in* %4 to i8*
%12 = bitcast i32* %5 to i8*
store i32 4, i32* %5
%13 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%14 = call i64 @getpeername(i32 %7, %struct.sockaddr* %13, i32* %5)
%15 = icmp slt i64 %14, 0
br i1 %15, label %38, label %16
16: ; preds = %9
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
%18 = load i32, i32* %17
%19 = call i32 @inet_ntoa(i32 %18)
%20 = call i64 @strcmp(i32 %19, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%21 = icmp eq i64 %20, 0
br i1 %21, label %27, label %22
22: ; preds = %16
%23 = call i32 @Log(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.3, i64 0, i64 0))
%24 = load i32, i32* @SHUT_RDWR
%25 = call i32 @shutdown(i32 %7, i32 %24)
%26 = call i32 @close(i32 %7)
br label %38
27: ; preds = %16
%28 = load i32*, i32** %1
%29 = load i32, i32* %2
%30 = shl i32 %29, 2
%31 = add i32 %30, 4
%32 = call i64 @realloc(i32* %28, i32 %31)
%33 = inttoptr i64 %32 to i32*
store i32* %33, i32** %1
%34 = load i32, i32* %2
%35 = add nsw i32 %34, 1
store i32 %35, i32* %2
%36 = sext i32 %34 to i64
%37 = getelementptr inbounds i32, i32* %33, i64 %36
store i32 %7, i32* %37
br label %38
38: ; preds = %9, %27, %22
%39 = phi i32 [ 0, %22 ], [ %7, %27 ], [ -1, %9 ]
br label %40
40: ; preds = %3, %38
%41 = phi i32 [ %39, %38 ], [ -1, %3 ]
ret i32 %41
}