#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_in6 {
  int /*<<< orphan*/ sin6_port;
  int /*<<< orphan*/ sin6_addr;
};
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;
scalar_t__ AF_INET6;
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);
  } else {
    struct sockaddr_in6 *s = (struct sockaddr_in6 *)&sa;
    if (ip)
      inet_ntop(AF_INET6, (void *)&(s->sin6_addr), ip, ip_len);
    if (port)
      *port = ntohs(s->sin6_port);
  }
  return fd;
}


x86 assembly (clang, -O0):
<anetTcpAccept>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%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    1242 <anetTcpAccept+0x112>
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    123c <anetTcpAccept+0x10c>
lea    -0x40(%rbp),%rax
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1219 <anetTcpAccept+0xe9>
mov    0x2ddf(%rip),%rax
mov    (%rax),%rdi
mov    -0x58(%rbp),%rsi
add    $0x4,%rsi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
call   1030 <inet_ntop@plt>
cmpq   $0x0,-0x30(%rbp)
je     1237 <anetTcpAccept+0x107>
mov    -0x58(%rbp),%rax
mov    (%rax),%edi
call   1040 <ntohs@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,(%rax)
jmp    123c <anetTcpAccept+0x10c>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<anetTcpAccept>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %r8,%r15
mov    %rcx,%r14
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     11a9 <anetTcpAccept+0x79>
mov    0x8(%rsp),%rdi
mov    0x2e7b(%rip),%rax
cmp    (%rax),%rdi
jne    1179 <anetTcpAccept+0x49>
test   %rbx,%rbx
jne    1188 <anetTcpAccept+0x58>
jmp    1198 <anetTcpAccept+0x68>
test   %rbx,%rbx
je     1198 <anetTcpAccept+0x68>
mov    0x2e5b(%rip),%rax
mov    (%rax),%rdi
lea    0xc(%rsp),%rsi
mov    %rbx,%rdx
mov    %r14,%rcx
call   1030 <inet_ntop@plt>
test   %r15,%r15
je     11a9 <anetTcpAccept+0x79>
mov    0x8(%rsp),%edi
call   1040 <ntohs@plt>
mov    %eax,(%r15)
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_in6 = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i64 0
@AF_INET6 = 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*
%16 = alloca %struct.sockaddr_in6*
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
%17 = load i8*, i8** %7
%18 = load i32, i32* %8
%19 = bitcast %struct.sockaddr_storage* %13 to %struct.sockaddr*
%20 = call i32 @anetGenericAccept(i8* %17, i32 %18, %struct.sockaddr* %19, i32* %14)
store i32 %20, i32* %12
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %23
22: ; preds = %5
store i32 -1, i32* %6
br label %74
23: ; preds = %5
%24 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %13, i32 0, i32 0
%25 = load i64, i64* %24
%26 = load i64, i64* @AF_INET
%27 = icmp eq i64 %25, %26
br i1 %27, label %28, label %50
28: ; preds = %23
%29 = bitcast %struct.sockaddr_storage* %13 to %struct.sockaddr_in*
store %struct.sockaddr_in* %29, %struct.sockaddr_in** %15
%30 = load i8*, i8** %9
%31 = icmp ne i8* %30, null
br i1 %31, label %32, label %40
32: ; preds = %28
%33 = load i64, i64* @AF_INET
%34 = load %struct.sockaddr_in*, %struct.sockaddr_in** %15
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %34, i32 0, i32 1
%36 = bitcast i32* %35 to i8*
%37 = load i8*, i8** %9
%38 = load i64, i64* %10
%39 = call i32 @inet_ntop(i64 %33, i8* %36, i8* %37, i64 %38)
br label %40
40: ; preds = %32, %28
%41 = load i32*, i32** %11
%42 = icmp ne i32* %41, null
br i1 %42, label %43, label %49
43: ; preds = %40
%44 = load %struct.sockaddr_in*, %struct.sockaddr_in** %15
%45 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %44, i32 0, i32 0
%46 = load i32, i32* %45
%47 = call i32 @ntohs(i32 %46)
%48 = load i32*, i32** %11
store i32 %47, i32* %48
br label %49
49: ; preds = %43, %40
br label %72
50: ; preds = %23
%51 = bitcast %struct.sockaddr_storage* %13 to %struct.sockaddr_in6*
store %struct.sockaddr_in6* %51, %struct.sockaddr_in6** %16
%52 = load i8*, i8** %9
%53 = icmp ne i8* %52, null
br i1 %53, label %54, label %62
54: ; preds = %50
%55 = load i64, i64* @AF_INET6
%56 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %16
%57 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %56, i32 0, i32 1
%58 = bitcast i32* %57 to i8*
%59 = load i8*, i8** %9
%60 = load i64, i64* %10
%61 = call i32 @inet_ntop(i64 %55, i8* %58, i8* %59, i64 %60)
br label %62
62: ; preds = %54, %50
%63 = load i32*, i32** %11
%64 = icmp ne i32* %63, null
br i1 %64, label %65, label %71
65: ; preds = %62
%66 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %16
%67 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %66, i32 0, i32 0
%68 = load i32, i32* %67
%69 = call i32 @ntohs(i32 %68)
%70 = load i32*, i32** %11
store i32 %69, i32* %70
br label %71
71: ; preds = %65, %62
br label %72
72: ; preds = %71, %49
%73 = load i32, i32* %12
store i32 %73, i32* %6
br label %74
74: ; preds = %72, %22
%75 = load i32, i32* %6
ret i32 %75
}

LLVM IR (-O2):
%struct.sockaddr_storage = type { i64 }
%struct.sockaddr = type { i32 }
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr_in6 = type { i32, i32 }
@AF_INET = global i64 0
@AF_INET6 = 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 %40, 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
%18 = icmp eq i8* %2, null
br i1 %17, label %19, label %27
19: ; preds = %13
br i1 %18, label %25, label %20
20: ; preds = %19
%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, %19
%26 = icmp eq i32* %4, null
br i1 %26, label %40, label %36
27: ; preds = %13
br i1 %18, label %34, label %28
28: ; preds = %27
%29 = bitcast %struct.sockaddr_storage* %6 to %struct.sockaddr_in6*
%30 = load i64, i64* @AF_INET6
%31 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %29, i64 0, i32 1
%32 = bitcast i32* %31 to i8*
%33 = call i32 @inet_ntop(i64 %30, i8* %32, i8* %2, i64 %3)
br label %34
34: ; preds = %28, %27
%35 = icmp eq i32* %4, null
br i1 %35, label %40, label %36
36: ; preds = %34, %25
%37 = bitcast %struct.sockaddr_storage* %6 to i32*
%38 = load i32, i32* %37
%39 = call i32 @ntohs(i32 %38)
store i32 %39, i32* %4
br label %40
40: ; preds = %36, %25, %34, %5
ret i32 %11
}