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

/* Variables and functions */
scalar_t__ AF_INET;
scalar_t__ AF_INET6;
int INET6_ADDRSTRLEN;
int NI_MAXHOST;
int NI_MAXSERV;
int /*<<< orphan*/ NI_NUMERICHOST;
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ getnameinfo(struct sockaddr *, int, char *, int, char *, int,
                       int /*<<< orphan*/);
char *inet_ntoa(int /*<<< orphan*/);
int /*<<< orphan*/ inet_ntop(scalar_t__, int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ memcpy(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strlen(char *);
int tcpsocket_accept(int socketfd, char *remoteip, int *remoteport) {
  char clienthost[NI_MAXHOST];
  char clientservice[NI_MAXSERV];
  struct sockaddr_storage clientaddr;
  socklen_t addrlen = sizeof(clientaddr);
  int connfd = accept(socketfd, (struct sockaddr *)&clientaddr, &addrlen);
  if (getnameinfo((struct sockaddr *)&clientaddr, addrlen, clienthost,
                  NI_MAXHOST, clientservice, NI_MAXSERV, NI_NUMERICHOST)) {
    fprintf(stderr, "can not resolve name");
  } else {
    if (clientaddr.ss_family == AF_INET) {
      struct sockaddr_in *ipv4 = (struct sockaddr_in *)&clientaddr;
      char *clientip = inet_ntoa(ipv4->sin_addr);
      memcpy(remoteip, clientip, strlen(clientip));
      *remoteport = ipv4->sin_port;
    } else if (clientaddr.ss_family == AF_INET6) {
      struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)&clientaddr;
      char clientip[INET6_ADDRSTRLEN];
      inet_ntop(AF_INET6, &(ipv6->sin6_addr), clientip, INET6_ADDRSTRLEN);
      memcpy(remoteip, clientip, strlen(clientip));
      *remoteport = ipv6->sin6_port;
    }
  }
  return connfd > 0 ? connfd : -1;
}


x86 assembly (clang, -O0):
<tcpsocket_accept>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2e23(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x78(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
mov    0x2e1f(%rip),%rax
mov    (%rax),%eax
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x70(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
movl   $0x8,-0x3c(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x38(%rbp),%rsi
lea    -0x3c(%rbp),%rdx
call   1090 <accept@plt>
mov    -0x78(%rbp),%rdx
mov    -0x70(%rbp),%r8
mov    %eax,-0x40(%rbp)
lea    -0x38(%rbp),%rdi
mov    -0x3c(%rbp),%esi
mov    0x2da9(%rip),%rax
mov    (%rax),%ecx
mov    0x2dc8(%rip),%rax
mov    (%rax),%r9d
mov    0x2d9e(%rip),%rax
mov    (%rax),%eax
sub    $0x10,%rsp
mov    %eax,(%rsp)
call   1060 <getnameinfo@plt>
add    $0x10,%rsp
cmp    $0x0,%rax
je     1250 <tcpsocket_accept+0xe0>
mov    0x2d83(%rip),%rax
mov    (%rax),%edi
lea    0xdba(%rip),%rsi
call   1070 <fprintf@plt>
jmp    136f <tcpsocket_accept+0x1ff>
mov    -0x38(%rbp),%rax
mov    0x2d85(%rip),%rcx
cmp    (%rcx),%rax
jne    12bb <tcpsocket_accept+0x14b>
lea    -0x38(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
mov    0x4(%rax),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,-0x50(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x88(%rbp)
mov    -0x50(%rbp),%rax
mov    %rax,-0x80(%rbp)
mov    -0x50(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x88(%rbp),%rdi
mov    -0x80(%rbp),%rsi
mov    %eax,%edx
call   1080 <memcpy@plt>
mov    -0x48(%rbp),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
jmp    136a <tcpsocket_accept+0x1fa>
mov    -0x38(%rbp),%rax
mov    0x2d0a(%rip),%rcx
cmp    (%rcx),%rax
jne    1365 <tcpsocket_accept+0x1f5>
lea    -0x38(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    0x2d0a(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x60(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdx
sub    %rcx,%rdx
mov    %rdx,-0x90(%rbp)
mov    %rdx,%rsp
mov    %rax,-0x68(%rbp)
mov    0x2cc3(%rip),%rax
mov    (%rax),%rdi
mov    -0x58(%rbp),%rsi
add    $0x4,%rsi
mov    0x2cc9(%rip),%rax
mov    (%rax),%ecx
call   1030 <inet_ntop@plt>
mov    -0x90(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    %rax,-0x98(%rbp)
call   1050 <strlen@plt>
mov    -0x98(%rbp),%rdi
mov    -0x90(%rbp),%rsi
mov    %eax,%edx
call   1080 <memcpy@plt>
mov    -0x58(%rbp),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x60(%rbp),%rax
mov    %rax,%rsp
jmp    136a <tcpsocket_accept+0x1fa>
jmp    136f <tcpsocket_accept+0x1ff>
cmpl   $0x0,-0x40(%rbp)
jle    1387 <tcpsocket_accept+0x217>
mov    -0x40(%rbp),%eax
mov    %eax,-0x9c(%rbp)
jmp    1397 <tcpsocket_accept+0x227>
mov    $0xffffffff,%eax
mov    %eax,-0x9c(%rbp)
jmp    1397 <tcpsocket_accept+0x227>
mov    -0x9c(%rbp),%eax
mov    -0x20(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcpsocket_accept>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdx,-0x48(%rbp)
mov    %rsi,-0x40(%rbp)
mov    0x2e20(%rip),%r15
mov    (%r15),%eax
mov    %rsp,%r13
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r13
mov    %r13,%rsp
mov    0x2e2d(%rip),%r12
mov    (%r12),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
movl   $0x8,-0x30(%rbp)
lea    -0x38(%rbp),%r14
lea    -0x30(%rbp),%rdx
mov    %r14,%rsi
call   1090 <accept@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x30(%rbp),%esi
mov    (%r15),%ecx
mov    (%r12),%r9d
mov    0x2dcd(%rip),%rax
mov    (%rax),%eax
sub    $0x8,%rsp
mov    %r14,%rdi
mov    %r13,%rdx
mov    %rbx,%r8
push   %rax
call   1060 <getnameinfo@plt>
add    $0x10,%rsp
test   %rax,%rax
je     1226 <tcpsocket_accept+0xb6>
mov    0x2db0(%rip),%rax
mov    (%rax),%edi
lea    0xde7(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x2c(%rbp),%ebx
jmp    12c5 <tcpsocket_accept+0x155>
mov    -0x40(%rbp),%r12
mov    -0x48(%rbp),%r15
mov    -0x38(%rbp),%rdi
mov    0x2da7(%rip),%rax
cmp    (%rax),%rdi
jne    126a <tcpsocket_accept+0xfa>
shr    $0x20,%rdi
call   1040 <inet_ntoa@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <strlen@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1080 <memcpy@plt>
mov    -0x38(%rbp),%eax
mov    %eax,(%r15)
mov    -0x2c(%rbp),%ebx
jmp    12c5 <tcpsocket_accept+0x155>
mov    0x2d5f(%rip),%rax
cmp    (%rax),%rdi
mov    -0x2c(%rbp),%ebx
jne    12c5 <tcpsocket_accept+0x155>
mov    %rsp,%r14
mov    0x2d65(%rip),%rcx
mov    (%rcx),%edx
mov    %rsp,%r13
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
sub    %rdx,%r13
mov    %r13,%rsp
mov    (%rax),%rdi
lea    -0x34(%rbp),%rsi
mov    (%rcx),%ecx
mov    %r13,%rdx
call   1030 <inet_ntop@plt>
mov    %r13,%rdi
call   1050 <strlen@plt>
mov    %r12,%rdi
mov    %r13,%rsi
mov    %eax,%edx
call   1080 <memcpy@plt>
mov    -0x38(%rbp),%eax
mov    %eax,(%r15)
mov    %r14,%rsp
test   %ebx,%ebx
mov    $0xffffffff,%eax
cmovg  %ebx,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
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 }
@NI_MAXHOST = global i32 0
@NI_MAXSERV = global i32 0
@NI_NUMERICHOST = global i32 0
@stderr = global i32 0
@.str = [21 x i8] c"can not resolve name\00"
@AF_INET = global i64 0
@AF_INET6 = global i64 0
@INET6_ADDRSTRLEN = global i32 0
define i32 @tcpsocket_accept(i32 %0, i8* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
%10 = alloca %struct.sockaddr_storage
%11 = alloca i32
%12 = alloca i32
%13 = alloca %struct.sockaddr_in*
%14 = alloca i8*
%15 = alloca %struct.sockaddr_in6*
%16 = alloca i8*
%17 = alloca i64
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32* %2, i32** %6
%18 = load i32, i32* @NI_MAXHOST
%19 = zext i32 %18 to i64
%20 = call i8* @llvm.stacksave()
store i8* %20, i8** %7
%21 = alloca i8, i64 %19
store i64 %19, i64* %8
%22 = load i32, i32* @NI_MAXSERV
%23 = zext i32 %22 to i64
%24 = alloca i8, i64 %23
store i64 %23, i64* %9
store i32 8, i32* %11
%25 = load i32, i32* %4
%26 = bitcast %struct.sockaddr_storage* %10 to %struct.sockaddr*
%27 = call i32 @accept(i32 %25, %struct.sockaddr* %26, i32* %11)
store i32 %27, i32* %12
%28 = bitcast %struct.sockaddr_storage* %10 to %struct.sockaddr*
%29 = load i32, i32* %11
%30 = load i32, i32* @NI_MAXHOST
%31 = load i32, i32* @NI_MAXSERV
%32 = load i32, i32* @NI_NUMERICHOST
%33 = call i64 @getnameinfo(%struct.sockaddr* %28, i32 %29, i8* %21, i32 %30, i8* %24, i32 %31, i32 %32)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %3
%36 = load i32, i32* @stderr
%37 = call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %84
38: ; preds = %3
%39 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %10, i32 0, i32 0
%40 = load i64, i64* %39
%41 = load i64, i64* @AF_INET
%42 = icmp eq i64 %40, %41
br i1 %42, label %43, label %58
43: ; preds = %38
%44 = bitcast %struct.sockaddr_storage* %10 to %struct.sockaddr_in*
store %struct.sockaddr_in* %44, %struct.sockaddr_in** %13
%45 = load %struct.sockaddr_in*, %struct.sockaddr_in** %13
%46 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %45, i32 0, i32 1
%47 = load i32, i32* %46
%48 = call i8* @inet_ntoa(i32 %47)
store i8* %48, i8** %14
%49 = load i8*, i8** %5
%50 = load i8*, i8** %14
%51 = load i8*, i8** %14
%52 = call i32 @strlen(i8* %51)
%53 = call i32 @memcpy(i8* %49, i8* %50, i32 %52)
%54 = load %struct.sockaddr_in*, %struct.sockaddr_in** %13
%55 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %54, i32 0, i32 0
%56 = load i32, i32* %55
%57 = load i32*, i32** %6
store i32 %56, i32* %57
br label %83
58: ; preds = %38
%59 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %10, i32 0, i32 0
%60 = load i64, i64* %59
%61 = load i64, i64* @AF_INET6
%62 = icmp eq i64 %60, %61
br i1 %62, label %63, label %82
63: ; preds = %58
%64 = bitcast %struct.sockaddr_storage* %10 to %struct.sockaddr_in6*
store %struct.sockaddr_in6* %64, %struct.sockaddr_in6** %15
%65 = load i32, i32* @INET6_ADDRSTRLEN
%66 = zext i32 %65 to i64
%67 = call i8* @llvm.stacksave()
store i8* %67, i8** %16
%68 = alloca i8, i64 %66
store i64 %66, i64* %17
%69 = load i64, i64* @AF_INET6
%70 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %15
%71 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %70, i32 0, i32 1
%72 = load i32, i32* @INET6_ADDRSTRLEN
%73 = call i32 @inet_ntop(i64 %69, i32* %71, i8* %68, i32 %72)
%74 = load i8*, i8** %5
%75 = call i32 @strlen(i8* %68)
%76 = call i32 @memcpy(i8* %74, i8* %68, i32 %75)
%77 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %15
%78 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %77, i32 0, i32 0
%79 = load i32, i32* %78
%80 = load i32*, i32** %6
store i32 %79, i32* %80
%81 = load i8*, i8** %16
br label %82
82: ; preds = %63, %58
br label %83
83: ; preds = %82, %43
br label %84
84: ; preds = %83, %35
%85 = load i32, i32* %12
%86 = icmp sgt i32 %85, 0
br i1 %86, label %87, label %89
87: ; preds = %84
%88 = load i32, i32* %12
br label %90
89: ; preds = %84
br label %90
90: ; preds = %89, %87
%91 = phi i32 [ %88, %87 ], [ -1, %89 ]
%92 = load i8*, i8** %7
ret i32 %91
}

LLVM IR (-O2):
%struct.sockaddr_storage = type { i64 }
%struct.sockaddr = type { i32 }
%struct.sockaddr_in6 = type { i32, i32 }
@NI_MAXHOST = global i32 0
@NI_MAXSERV = global i32 0
@NI_NUMERICHOST = global i32 0
@stderr = global i32 0
@.str = [21 x i8] c"can not resolve name\00"
@AF_INET = global i64 0
@AF_INET6 = global i64 0
@INET6_ADDRSTRLEN = global i32 0
define i32 @tcpsocket_accept(i32 %0, i8* %1, i32* nocapture writeonly %2) {
%4 = alloca %struct.sockaddr_storage
%5 = alloca i32
%6 = load i32, i32* @NI_MAXHOST
%7 = zext i32 %6 to i64
%8 = alloca i8, i64 %7
%9 = load i32, i32* @NI_MAXSERV
%10 = zext i32 %9 to i64
%11 = alloca i8, i64 %10
%12 = bitcast %struct.sockaddr_storage* %4 to i8*
%13 = bitcast i32* %5 to i8*
store i32 8, i32* %5
%14 = bitcast %struct.sockaddr_storage* %4 to %struct.sockaddr*
%15 = call i32 @accept(i32 %0, %struct.sockaddr* %14, i32* %5)
%16 = load i32, i32* %5
%17 = load i32, i32* @NI_MAXHOST
%18 = load i32, i32* @NI_MAXSERV
%19 = load i32, i32* @NI_NUMERICHOST
%20 = call i64 @getnameinfo(%struct.sockaddr* %14, i32 %16, i8* %8, i32 %17, i8* %11, i32 %18, i32 %19)
%21 = icmp eq i64 %20, 0
br i1 %21, label %25, label %22
22: ; preds = %3
%23 = load i32, i32* @stderr
%24 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %55
25: ; preds = %3
%26 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %4, i64 0, i32 0
%27 = load i64, i64* %26
%28 = load i64, i64* @AF_INET
%29 = icmp eq i64 %27, %28
br i1 %29, label %30, label %38
30: ; preds = %25
%31 = lshr i64 %27, 32
%32 = trunc i64 %31 to i32
%33 = call i8* @inet_ntoa(i32 %32)
%34 = call i32 @strlen(i8* %33)
%35 = call i32 @memcpy(i8* %1, i8* %33, i32 %34)
%36 = bitcast %struct.sockaddr_storage* %4 to i32*
%37 = load i32, i32* %36
store i32 %37, i32* %2
br label %55
38: ; preds = %25
%39 = load i64, i64* @AF_INET6
%40 = icmp eq i64 %27, %39
br i1 %40, label %41, label %55
41: ; preds = %38
%42 = bitcast %struct.sockaddr_storage* %4 to %struct.sockaddr_in6*
%43 = load i32, i32* @INET6_ADDRSTRLEN
%44 = zext i32 %43 to i64
%45 = call i8* @llvm.stacksave()
%46 = alloca i8, i64 %44
%47 = load i64, i64* @AF_INET6
%48 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %42, i64 0, i32 1
%49 = load i32, i32* @INET6_ADDRSTRLEN
%50 = call i32 @inet_ntop(i64 %47, i32* %48, i8* %46, i32 %49)
%51 = call i32 @strlen(i8* %46)
%52 = call i32 @memcpy(i8* %1, i8* %46, i32 %51)
%53 = bitcast %struct.sockaddr_storage* %4 to i32*
%54 = load i32, i32* %53
store i32 %54, i32* %2
br label %55
55: ; preds = %30, %41, %38, %22
%56 = icmp sgt i32 %15, 0
%57 = select i1 %56, i32 %15, i32 -1
ret i32 %57
}