#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 ez_net_tcp_generic_accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ inet_ntop(scalar_t__, void *, char *, int);
int ntohs(int /*<<< orphan*/);
int ez_net_tcp_accept2(int s, char *ip, size_t ip_len, int *port) {
  int fd;
  struct sockaddr_storage sa;
  socklen_t salen = sizeof(sa);
  fd = ez_net_tcp_generic_accept(s, (struct sockaddr *)&sa, &salen);
  if (fd < 0)
    return fd;
  if (sa.ss_family == AF_INET) {
    struct sockaddr_in *si4 = (struct sockaddr_in *)&sa;
    if (ip)
      inet_ntop(AF_INET, (void *)&(si4->sin_addr), ip, (socklen_t)ip_len);
    if (port)
      *port = ntohs(si4->sin_port);
  } else {
    struct sockaddr_in6 *si6 = (struct sockaddr_in6 *)&sa;
    if (ip)
      inet_ntop(AF_INET6, (void *)&(si6->sin6_addr), ip, (socklen_t)ip_len);
    if (port)
      *port = ntohs(si6->sin6_port);
  }
  return fd;
}


x86 assembly (clang, -O0):
<ez_net_tcp_accept2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movl   $0x8,-0x34(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
lea    -0x34(%rbp),%rdx
call   1050 <ez_net_tcp_generic_accept@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1176 <ez_net_tcp_accept2+0x46>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    123e <ez_net_tcp_accept2+0x10e>
mov    -0x30(%rbp),%rax
mov    0x2e67(%rip),%rcx
cmp    (%rcx),%rax
jne    11e1 <ez_net_tcp_accept2+0xb1>
lea    -0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11be <ez_net_tcp_accept2+0x8e>
mov    0x2e44(%rip),%rax
mov    (%rax),%rdi
mov    -0x40(%rbp),%rsi
add    $0x4,%rsi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rax
mov    %eax,%ecx
call   1030 <inet_ntop@plt>
cmpq   $0x0,-0x20(%rbp)
je     11dc <ez_net_tcp_accept2+0xac>
mov    -0x40(%rbp),%rax
mov    (%rax),%edi
call   1040 <ntohs@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
jmp    1238 <ez_net_tcp_accept2+0x108>
lea    -0x30(%rbp),%rax
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1215 <ez_net_tcp_accept2+0xe5>
mov    0x2de5(%rip),%rax
mov    (%rax),%rdi
mov    -0x48(%rbp),%rsi
add    $0x4,%rsi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rax
mov    %eax,%ecx
call   1030 <inet_ntop@plt>
cmpq   $0x0,-0x20(%rbp)
je     1233 <ez_net_tcp_accept2+0x103>
mov    -0x48(%rbp),%rax
mov    (%rax),%edi
call   1040 <ntohs@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
jmp    1238 <ez_net_tcp_accept2+0x108>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ez_net_tcp_accept2>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%r15
mov    %rdx,%r14
mov    %rsi,%rbx
movl   $0x8,0x14(%rsp)
lea    0x8(%rsp),%rsi
lea    0x14(%rsp),%rdx
call   1050 <ez_net_tcp_generic_accept@plt>
mov    %eax,%ebp
test   %eax,%eax
js     11a8 <ez_net_tcp_accept2+0x78>
mov    0x8(%rsp),%rdi
mov    0x2e7c(%rip),%rax
cmp    (%rax),%rdi
jne    1178 <ez_net_tcp_accept2+0x48>
test   %rbx,%rbx
jne    1187 <ez_net_tcp_accept2+0x57>
jmp    1197 <ez_net_tcp_accept2+0x67>
test   %rbx,%rbx
je     1197 <ez_net_tcp_accept2+0x67>
mov    0x2e5c(%rip),%rax
mov    (%rax),%rdi
lea    0xc(%rsp),%rsi
mov    %rbx,%rdx
mov    %r14d,%ecx
call   1030 <inet_ntop@plt>
test   %r15,%r15
je     11a8 <ez_net_tcp_accept2+0x78>
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 @ez_net_tcp_accept2(i32 %0, i8* %1, i64 %2, i32* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32*
%10 = alloca i32
%11 = alloca %struct.sockaddr_storage
%12 = alloca i32
%13 = alloca %struct.sockaddr_in*
%14 = alloca %struct.sockaddr_in6*
store i32 %0, i32* %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i32* %3, i32** %9
store i32 8, i32* %12
%15 = load i32, i32* %6
%16 = bitcast %struct.sockaddr_storage* %11 to %struct.sockaddr*
%17 = call i32 @ez_net_tcp_generic_accept(i32 %15, %struct.sockaddr* %16, i32* %12)
store i32 %17, i32* %10
%18 = load i32, i32* %10
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %4
%21 = load i32, i32* %10
store i32 %21, i32* %5
br label %75
22: ; preds = %4
%23 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %11, 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 %50
27: ; preds = %22
%28 = bitcast %struct.sockaddr_storage* %11 to %struct.sockaddr_in*
store %struct.sockaddr_in* %28, %struct.sockaddr_in** %13
%29 = load i8*, i8** %7
%30 = icmp ne i8* %29, null
br i1 %30, label %31, label %40
31: ; preds = %27
%32 = load i64, i64* @AF_INET
%33 = load %struct.sockaddr_in*, %struct.sockaddr_in** %13
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %33, i32 0, i32 1
%35 = bitcast i32* %34 to i8*
%36 = load i8*, i8** %7
%37 = load i64, i64* %8
%38 = trunc i64 %37 to i32
%39 = call i32 @inet_ntop(i64 %32, i8* %35, i8* %36, i32 %38)
br label %40
40: ; preds = %31, %27
%41 = load i32*, i32** %9
%42 = icmp ne i32* %41, null
br i1 %42, label %43, label %49
43: ; preds = %40
%44 = load %struct.sockaddr_in*, %struct.sockaddr_in** %13
%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** %9
store i32 %47, i32* %48
br label %49
49: ; preds = %43, %40
br label %73
50: ; preds = %22
%51 = bitcast %struct.sockaddr_storage* %11 to %struct.sockaddr_in6*
store %struct.sockaddr_in6* %51, %struct.sockaddr_in6** %14
%52 = load i8*, i8** %7
%53 = icmp ne i8* %52, null
br i1 %53, label %54, label %63
54: ; preds = %50
%55 = load i64, i64* @AF_INET6
%56 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %14
%57 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %56, i32 0, i32 1
%58 = bitcast i32* %57 to i8*
%59 = load i8*, i8** %7
%60 = load i64, i64* %8
%61 = trunc i64 %60 to i32
%62 = call i32 @inet_ntop(i64 %55, i8* %58, i8* %59, i32 %61)
br label %63
63: ; preds = %54, %50
%64 = load i32*, i32** %9
%65 = icmp ne i32* %64, null
br i1 %65, label %66, label %72
66: ; preds = %63
%67 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %14
%68 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %67, i32 0, i32 0
%69 = load i32, i32* %68
%70 = call i32 @ntohs(i32 %69)
%71 = load i32*, i32** %9
store i32 %70, i32* %71
br label %72
72: ; preds = %66, %63
br label %73
73: ; preds = %72, %49
%74 = load i32, i32* %10
store i32 %74, i32* %5
br label %75
75: ; preds = %73, %20
%76 = load i32, i32* %5
ret i32 %76
}

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 @ez_net_tcp_accept2(i32 %0, i8* %1, i64 %2, i32* writeonly %3) {
%5 = alloca %struct.sockaddr_storage
%6 = alloca i32
%7 = bitcast %struct.sockaddr_storage* %5 to i8*
%8 = bitcast i32* %6 to i8*
store i32 8, i32* %6
%9 = bitcast %struct.sockaddr_storage* %5 to %struct.sockaddr*
%10 = call i32 @ez_net_tcp_generic_accept(i32 %0, %struct.sockaddr* %9, i32* %6)
%11 = icmp slt i32 %10, 0
br i1 %11, label %41, label %12
12: ; preds = %4
%13 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %5, i64 0, i32 0
%14 = load i64, i64* %13
%15 = load i64, i64* @AF_INET
%16 = icmp eq i64 %14, %15
%17 = icmp eq i8* %1, null
br i1 %16, label %18, label %27
18: ; preds = %12
br i1 %17, label %25, label %19
19: ; preds = %18
%20 = bitcast %struct.sockaddr_storage* %5 to %struct.sockaddr_in*
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %20, i64 0, i32 1
%22 = bitcast i32* %21 to i8*
%23 = trunc i64 %2 to i32
%24 = call i32 @inet_ntop(i64 %14, i8* %22, i8* %1, i32 %23)
br label %25
25: ; preds = %19, %18
%26 = icmp eq i32* %3, null
br i1 %26, label %41, label %37
27: ; preds = %12
br i1 %17, label %35, label %28
28: ; preds = %27
%29 = bitcast %struct.sockaddr_storage* %5 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 = trunc i64 %2 to i32
%34 = call i32 @inet_ntop(i64 %30, i8* %32, i8* %1, i32 %33)
br label %35
35: ; preds = %28, %27
%36 = icmp eq i32* %3, null
br i1 %36, label %41, label %37
37: ; preds = %35, %25
%38 = bitcast %struct.sockaddr_storage* %5 to i32*
%39 = load i32, i32* %38
%40 = call i32 @ntohs(i32 %39)
store i32 %40, i32* %3
br label %41
41: ; preds = %37, %25, %35, %4
ret i32 %10
}