#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 {
  scalar_t__ ss_family;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_addr;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ sa;

/* Variables and functions */
scalar_t__ AF_INET;
int anetGenericAccept(char *, int, struct sockaddr *, int *);
int /*<<< orphan*/ inet_ntop(scalar_t__, void *, char *, size_t);
int ntohs(int /*<<< orphan*/);
int anetTcpAccept(char *err, int s, char *ip, size_t ip_len, int *port) {
  int fd;
  struct sockaddr_storage sa;
  socklen_t salen = sizeof(sa);
  if ((fd = anetGenericAccept(err, s, (struct sockaddr *)&sa, &salen)) == -1)
    return -1;
  if (sa.ss_family == AF_INET) {
    struct sockaddr_in *s = (struct sockaddr_in *)&sa;
    if (ip)
      inet_ntop(AF_INET, (void *)&(s->sin_addr), ip, ip_len);
    if (port)
      *port = ntohs(s->sin_port);
  }
  return fd;
}


x86 assembly (clang, -O0):
<anetTcpAccept>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
movl   $0x8,-0x44(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
lea    -0x40(%rbp),%rdx
lea    -0x44(%rbp),%rcx
call   1050 <anetGenericAccept@plt>
mov    %eax,-0x34(%rbp)
cmp    $0xffffffff,%eax
jne    117e <anetTcpAccept+0x4e>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ed <anetTcpAccept+0xbd>
mov    -0x40(%rbp),%rax
mov    0x2e5f(%rip),%rcx
cmp    (%rcx),%rax
jne    11e7 <anetTcpAccept+0xb7>
lea    -0x40(%rbp),%rax
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11c4 <anetTcpAccept+0x94>
mov    0x2e3c(%rip),%rax
mov    (%rax),%rdi
mov    -0x50(%rbp),%rsi
add    $0x4,%rsi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
call   1030 <inet_ntop@plt>
cmpq   $0x0,-0x30(%rbp)
je     11e2 <anetTcpAccept+0xb2>
mov    -0x50(%rbp),%rax
mov    (%rax),%edi
call   1040 <ntohs@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,(%rax)
jmp    11e7 <anetTcpAccept+0xb7>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<anetTcpAccept>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %r8,%r14
mov    %rcx,%r15
mov    %rdx,%rbx
movl   $0x8,0x14(%rsp)
lea    0x8(%rsp),%rdx
lea    0x14(%rsp),%rcx
call   1050 <anetGenericAccept@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     1198 <anetTcpAccept+0x68>
mov    0x8(%rsp),%rdi
mov    0x2e7b(%rip),%rax
cmp    (%rax),%rdi
jne    1198 <anetTcpAccept+0x68>
test   %rbx,%rbx
je     1187 <anetTcpAccept+0x57>
lea    0xc(%rsp),%rsi
mov    %rbx,%rdx
mov    %r15,%rcx
call   1030 <inet_ntop@plt>
test   %r14,%r14
je     1198 <anetTcpAccept+0x68>
mov    0x8(%rsp),%edi
call   1040 <ntohs@plt>
mov    %eax,(%r14)
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_storage = type { i64 }
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i64 0
define i32 @anetTcpAccept(i8* %0, i32 %1, i8* %2, i64 %3, i32* %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i32*
%12 = alloca i32
%13 = alloca %struct.sockaddr_storage
%14 = alloca i32
%15 = alloca %struct.sockaddr_in*
store i8* %0, i8** %7
store i32 %1, i32* %8
store i8* %2, i8** %9
store i64 %3, i64* %10
store i32* %4, i32** %11
store i32 8, i32* %14
%16 = load i8*, i8** %7
%17 = load i32, i32* %8
%18 = bitcast %struct.sockaddr_storage* %13 to %struct.sockaddr*
%19 = call i32 @anetGenericAccept(i8* %16, i32 %17, %struct.sockaddr* %18, i32* %14)
store i32 %19, i32* %12
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %22
21: ; preds = %5
store i32 -1, i32* %6
br label %51
22: ; preds = %5
%23 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %13, i32 0, i32 0
%24 = load i64, i64* %23
%25 = load i64, i64* @AF_INET
%26 = icmp eq i64 %24, %25
br i1 %26, label %27, label %49
27: ; preds = %22
%28 = bitcast %struct.sockaddr_storage* %13 to %struct.sockaddr_in*
store %struct.sockaddr_in* %28, %struct.sockaddr_in** %15
%29 = load i8*, i8** %9
%30 = icmp ne i8* %29, null
br i1 %30, label %31, label %39
31: ; preds = %27
%32 = load i64, i64* @AF_INET
%33 = load %struct.sockaddr_in*, %struct.sockaddr_in** %15
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %33, i32 0, i32 1
%35 = bitcast i32* %34 to i8*
%36 = load i8*, i8** %9
%37 = load i64, i64* %10
%38 = call i32 @inet_ntop(i64 %32, i8* %35, i8* %36, i64 %37)
br label %39
39: ; preds = %31, %27
%40 = load i32*, i32** %11
%41 = icmp ne i32* %40, null
br i1 %41, label %42, label %48
42: ; preds = %39
%43 = load %struct.sockaddr_in*, %struct.sockaddr_in** %15
%44 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %43, i32 0, i32 0
%45 = load i32, i32* %44
%46 = call i32 @ntohs(i32 %45)
%47 = load i32*, i32** %11
store i32 %46, i32* %47
br label %48
48: ; preds = %42, %39
br label %49
49: ; preds = %48, %22
%50 = load i32, i32* %12
store i32 %50, i32* %6
br label %51
51: ; preds = %49, %21
%52 = load i32, i32* %6
ret i32 %52
}

LLVM IR (-O2):
%struct.sockaddr_storage = type { i64 }
%struct.sockaddr = type { i32 }
%struct.sockaddr_in = type { i32, i32 }
@AF_INET = global i64 0
define i32 @anetTcpAccept(i8* %0, i32 %1, i8* %2, i64 %3, i32* writeonly %4) {
%6 = alloca %struct.sockaddr_storage
%7 = alloca i32
%8 = bitcast %struct.sockaddr_storage* %6 to i8*
%9 = bitcast i32* %7 to i8*
store i32 8, i32* %7
%10 = bitcast %struct.sockaddr_storage* %6 to %struct.sockaddr*
%11 = call i32 @anetGenericAccept(i8* %0, i32 %1, %struct.sockaddr* %10, i32* %7)
%12 = icmp eq i32 %11, -1
br i1 %12, label %31, label %13
13: ; preds = %5
%14 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %6, i64 0, i32 0
%15 = load i64, i64* %14
%16 = load i64, i64* @AF_INET
%17 = icmp eq i64 %15, %16
br i1 %17, label %18, label %31
18: ; preds = %13
%19 = icmp eq i8* %2, null
br i1 %19, label %25, label %20
20: ; preds = %18
%21 = bitcast %struct.sockaddr_storage* %6 to %struct.sockaddr_in*
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %21, i64 0, i32 1
%23 = bitcast i32* %22 to i8*
%24 = call i32 @inet_ntop(i64 %15, i8* %23, i8* %2, i64 %3)
br label %25
25: ; preds = %20, %18
%26 = icmp eq i32* %4, null
br i1 %26, label %31, label %27
27: ; preds = %25
%28 = bitcast %struct.sockaddr_storage* %6 to i32*
%29 = load i32, i32* %28
%30 = call i32 @ntohs(i32 %29)
store i32 %30, i32* %4
br label %31
31: ; preds = %13, %27, %25, %5
ret i32 %11
}