#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 accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ inet_ntop(scalar_t__, void *, char *, size_t);
int ntohs(int /*<<< orphan*/);
int tcp_accept(int listenfd, char *ip, size_t ip_len, int *port) {
  struct sockaddr_storage sa;
  socklen_t len = sizeof(sa);
  int fd = accept(listenfd, (struct sockaddr *)&sa, &len);
  if (fd < 0) {
    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):
<tcp_accept>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movl   $0x8,-0x2c(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
lea    -0x2c(%rbp),%rdx
call   1050 <accept@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jge    1177 <tcp_accept+0x47>
movl   $0xffffffff,-0x4(%rbp)
jmp    123b <tcp_accept+0x10b>
mov    -0x28(%rbp),%rax
mov    0x2e66(%rip),%rcx
cmp    (%rcx),%rax
jne    11e0 <tcp_accept+0xb0>
lea    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11bd <tcp_accept+0x8d>
mov    0x2e43(%rip),%rax
mov    (%rax),%rdi
mov    -0x38(%rbp),%rsi
add    $0x4,%rsi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
call   1030 <inet_ntop@plt>
cmpq   $0x0,-0x20(%rbp)
je     11db <tcp_accept+0xab>
mov    -0x38(%rbp),%rax
mov    (%rax),%edi
call   1040 <ntohs@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
jmp    1235 <tcp_accept+0x105>
lea    -0x28(%rbp),%rax
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1212 <tcp_accept+0xe2>
mov    0x2de6(%rip),%rax
mov    (%rax),%rdi
mov    -0x40(%rbp),%rsi
add    $0x4,%rsi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
call   1030 <inet_ntop@plt>
cmpq   $0x0,-0x20(%rbp)
je     1230 <tcp_accept+0x100>
mov    -0x40(%rbp),%rax
mov    (%rax),%edi
call   1040 <ntohs@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
jmp    1235 <tcp_accept+0x105>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcp_accept>:
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 <accept@plt>
test   %eax,%eax
js     1178 <tcp_accept+0x48>
mov    %eax,%ebp
mov    0x8(%rsp),%rdi
mov    0x2e7c(%rip),%rax
cmp    (%rax),%rdi
jne    117f <tcp_accept+0x4f>
test   %rbx,%rbx
jne    118e <tcp_accept+0x5e>
jmp    119e <tcp_accept+0x6e>
mov    $0xffffffff,%ebp
jmp    11af <tcp_accept+0x7f>
test   %rbx,%rbx
je     119e <tcp_accept+0x6e>
mov    0x2e55(%rip),%rax
mov    (%rax),%rdi
lea    0xc(%rsp),%rsi
mov    %rbx,%rdx
mov    %r14,%rcx
call   1030 <inet_ntop@plt>
test   %r15,%r15
je     11af <tcp_accept+0x7f>
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 @tcp_accept(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 %struct.sockaddr_storage
%11 = alloca i32
%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* %11
%15 = load i32, i32* %6
%16 = bitcast %struct.sockaddr_storage* %10 to %struct.sockaddr*
%17 = call i32 @accept(i32 %15, %struct.sockaddr* %16, i32* %11)
store i32 %17, i32* %12
%18 = load i32, i32* %12
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %4
store i32 -1, i32* %5
br label %72
21: ; preds = %4
%22 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %10, i32 0, i32 0
%23 = load i64, i64* %22
%24 = load i64, i64* @AF_INET
%25 = icmp eq i64 %23, %24
br i1 %25, label %26, label %48
26: ; preds = %21
%27 = bitcast %struct.sockaddr_storage* %10 to %struct.sockaddr_in*
store %struct.sockaddr_in* %27, %struct.sockaddr_in** %13
%28 = load i8*, i8** %7
%29 = icmp ne i8* %28, null
br i1 %29, label %30, label %38
30: ; preds = %26
%31 = load i64, i64* @AF_INET
%32 = load %struct.sockaddr_in*, %struct.sockaddr_in** %13
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %32, i32 0, i32 1
%34 = bitcast i32* %33 to i8*
%35 = load i8*, i8** %7
%36 = load i64, i64* %8
%37 = call i32 @inet_ntop(i64 %31, i8* %34, i8* %35, i64 %36)
br label %38
38: ; preds = %30, %26
%39 = load i32*, i32** %9
%40 = icmp ne i32* %39, null
br i1 %40, label %41, label %47
41: ; preds = %38
%42 = load %struct.sockaddr_in*, %struct.sockaddr_in** %13
%43 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %42, i32 0, i32 0
%44 = load i32, i32* %43
%45 = call i32 @ntohs(i32 %44)
%46 = load i32*, i32** %9
store i32 %45, i32* %46
br label %47
47: ; preds = %41, %38
br label %70
48: ; preds = %21
%49 = bitcast %struct.sockaddr_storage* %10 to %struct.sockaddr_in6*
store %struct.sockaddr_in6* %49, %struct.sockaddr_in6** %14
%50 = load i8*, i8** %7
%51 = icmp ne i8* %50, null
br i1 %51, label %52, label %60
52: ; preds = %48
%53 = load i64, i64* @AF_INET6
%54 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %14
%55 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %54, i32 0, i32 1
%56 = bitcast i32* %55 to i8*
%57 = load i8*, i8** %7
%58 = load i64, i64* %8
%59 = call i32 @inet_ntop(i64 %53, i8* %56, i8* %57, i64 %58)
br label %60
60: ; preds = %52, %48
%61 = load i32*, i32** %9
%62 = icmp ne i32* %61, null
br i1 %62, label %63, label %69
63: ; preds = %60
%64 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %14
%65 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %64, i32 0, i32 0
%66 = load i32, i32* %65
%67 = call i32 @ntohs(i32 %66)
%68 = load i32*, i32** %9
store i32 %67, i32* %68
br label %69
69: ; preds = %63, %60
br label %70
70: ; preds = %69, %47
%71 = load i32, i32* %12
store i32 %71, i32* %5
br label %72
72: ; preds = %70, %20
%73 = load i32, i32* %5
ret i32 %73
}

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 @tcp_accept(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 @accept(i32 %0, %struct.sockaddr* %9, i32* %6)
%11 = icmp slt i32 %10, 0
br i1 %11, label %39, 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 %26
18: ; preds = %12
br i1 %17, label %24, 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 = call i32 @inet_ntop(i64 %14, i8* %22, i8* %1, i64 %2)
br label %24
24: ; preds = %19, %18
%25 = icmp eq i32* %3, null
br i1 %25, label %39, label %35
26: ; preds = %12
br i1 %17, label %33, label %27
27: ; preds = %26
%28 = bitcast %struct.sockaddr_storage* %5 to %struct.sockaddr_in6*
%29 = load i64, i64* @AF_INET6
%30 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %28, i64 0, i32 1
%31 = bitcast i32* %30 to i8*
%32 = call i32 @inet_ntop(i64 %29, i8* %31, i8* %1, i64 %2)
br label %33
33: ; preds = %27, %26
%34 = icmp eq i32* %3, null
br i1 %34, label %39, label %35
35: ; preds = %33, %24
%36 = bitcast %struct.sockaddr_storage* %5 to i32*
%37 = load i32, i32* %36
%38 = call i32 @ntohs(i32 %37)
store i32 %38, i32* %3
br label %39
39: ; preds = %35, %24, %33, %4
%40 = phi i32 [ -1, %4 ], [ %10, %33 ], [ %10, %24 ], [ %10, %35 ]
ret i32 %40
}