#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 {
  int dummy;
};
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 */
int /*<<< orphan*/ AF_INET;
int cv_accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ inet_ntop(int /*<<< orphan*/, void *, char *, size_t);
int ntohs(int /*<<< orphan*/);
int socket_accept(int fd, char *ip, size_t ip_len, int *port) {
  int s;
  struct sockaddr_storage sa;
  socklen_t salen = sizeof(sa);
  s = cv_accept(fd, (struct sockaddr *)&sa, &salen);
  if (s == -2 || s == -1)
    return s;
  struct sockaddr_in *addr = (struct sockaddr_in *)&sa;
  if (ip)
    inet_ntop(AF_INET, (void *)&(addr->sin_addr), ip, ip_len);
  if (port)
    *port = ntohs(addr->sin_port);
  return s;
}


x86 assembly (clang, -O0):
<socket_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   $0x4,-0x2c(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
lea    -0x2c(%rbp),%rdx
call   1050 <cv_accept@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xfffffffe,-0x24(%rbp)
je     1175 <socket_accept+0x45>
cmpl   $0xffffffff,-0x24(%rbp)
jne    1180 <socket_accept+0x50>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11d5 <socket_accept+0xa5>
lea    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11b1 <socket_accept+0x81>
mov    0x2e4e(%rip),%rax
mov    (%rax),%edi
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     11cf <socket_accept+0x9f>
mov    -0x38(%rbp),%rax
mov    (%rax),%edi
call   1040 <ntohs@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_accept>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rcx,%r15
mov    %rdx,%r14
mov    %rsi,%rbx
movl   $0x4,0x4(%rsp)
mov    %rsp,%rsi
lea    0x4(%rsp),%rdx
call   1050 <cv_accept@plt>
mov    %eax,%ebp
cmp    $0xfffffffd,%eax
ja     118a <socket_accept+0x5a>
test   %rbx,%rbx
je     117a <socket_accept+0x4a>
mov    0x2e80(%rip),%rax
mov    (%rax),%edi
lea    0x4(%rsp),%rsi
mov    %rbx,%rdx
mov    %r14,%rcx
call   1030 <inet_ntop@plt>
test   %r15,%r15
je     118a <socket_accept+0x5a>
mov    (%rsp),%edi
call   1040 <ntohs@plt>
mov    %eax,(%r15)
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
define i32 @socket_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 4, i32* %6
%9 = bitcast %struct.sockaddr_storage* %5 to %struct.sockaddr*
%10 = call i32 @cv_accept(i32 %0, %struct.sockaddr* %9, i32* %6)
%11 = icmp ugt i32 %10, -3
br i1 %11, label %25, label %12
12: ; preds = %4
%13 = icmp eq i8* %1, null
br i1 %13, label %19, label %14
14: ; preds = %12
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %5, i64 1, i32 0
%17 = bitcast i32* %16 to i8*
%18 = call i32 @inet_ntop(i32 %15, i8* %17, i8* %1, i64 %2)
br label %19
19: ; preds = %14, %12
%20 = icmp eq i32* %3, null
br i1 %20, label %25, label %21
21: ; preds = %19
%22 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %5, i64 0, i32 0
%23 = load i32, i32* %22
%24 = call i32 @ntohs(i32 %23)
store i32 %24, i32* %3
br label %25
25: ; preds = %19, %21, %4
ret i32 %10
}