#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_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 /*<<< orphan*/ INET_ADDRSTRLEN;
int /*<<< orphan*/ inet_ntop(int /*<<< orphan*/, int /*<<< orphan*/ *, char *,
                             int /*<<< orphan*/);
int ntohs(int /*<<< orphan*/);
int wheatGenericAccept(char *, int, struct sockaddr *, int *);
int wheatTcpAccept(char *err, int s, char *ip, int *port) {
  int fd;
  struct sockaddr_in sa;
  socklen_t salen = sizeof(sa);
  if ((fd = wheatGenericAccept(err, s, (struct sockaddr *)&sa, &salen)) == -1)
    return -1;
  if (ip)
    inet_ntop(AF_INET, &sa.sin_addr, ip, INET_ADDRSTRLEN);
  if (port)
    *port = ntohs(sa.sin_port);
  return fd;
}


x86 assembly (clang, -O0):
<wheatTcpAccept>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movl   $0x8,-0x3c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
lea    -0x38(%rbp),%rdx
lea    -0x3c(%rbp),%rcx
call   1050 <wheatGenericAccept@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0xffffffff,%eax
jne    117a <wheatTcpAccept+0x4a>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c9 <wheatTcpAccept+0x99>
cmpq   $0x0,-0x20(%rbp)
je     11a8 <wheatTcpAccept+0x78>
mov    0x2e5c(%rip),%rax
mov    (%rax),%edi
lea    -0x38(%rbp),%rsi
add    $0x4,%rsi
mov    -0x20(%rbp),%rdx
mov    0x2e37(%rip),%rax
mov    (%rax),%ecx
call   1030 <inet_ntop@plt>
cmpq   $0x0,-0x28(%rbp)
je     11c3 <wheatTcpAccept+0x93>
mov    -0x38(%rbp),%edi
call   1040 <ntohs@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wheatTcpAccept>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rcx,%r14
mov    %rdx,%rbx
movl   $0x8,0x4(%rsp)
lea    0x8(%rsp),%rdx
lea    0x4(%rsp),%rcx
call   1050 <wheatGenericAccept@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     1191 <wheatTcpAccept+0x61>
test   %rbx,%rbx
je     1180 <wheatTcpAccept+0x50>
mov    0x2e80(%rip),%rax
mov    (%rax),%edi
lea    0xc(%rsp),%rsi
mov    0x2e62(%rip),%rax
mov    (%rax),%ecx
mov    %rbx,%rdx
call   1030 <inet_ntop@plt>
test   %r14,%r14
je     1191 <wheatTcpAccept+0x61>
mov    0x8(%rsp),%edi
call   1040 <ntohs@plt>
mov    %eax,(%r14)
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@INET_ADDRSTRLEN = global i32 0
define i32 @wheatTcpAccept(i8* %0, i32 %1, i8* %2, i32* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32*
%10 = alloca i32
%11 = alloca %struct.sockaddr_in
%12 = alloca i32
store i8* %0, i8** %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i32* %3, i32** %9
store i32 8, i32* %12
%13 = load i8*, i8** %6
%14 = load i32, i32* %7
%15 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%16 = call i32 @wheatGenericAccept(i8* %13, i32 %14, %struct.sockaddr* %15, i32* %12)
store i32 %16, i32* %10
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %19
18: ; preds = %4
store i32 -1, i32* %5
br label %38
19: ; preds = %4
%20 = load i8*, i8** %8
%21 = icmp ne i8* %20, null
br i1 %21, label %22, label %28
22: ; preds = %19
%23 = load i32, i32* @AF_INET
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 1
%25 = load i8*, i8** %8
%26 = load i32, i32* @INET_ADDRSTRLEN
%27 = call i32 @inet_ntop(i32 %23, i32* %24, i8* %25, i32 %26)
br label %28
28: ; preds = %22, %19
%29 = load i32*, i32** %9
%30 = icmp ne i32* %29, null
br i1 %30, label %31, label %36
31: ; preds = %28
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
%33 = load i32, i32* %32
%34 = call i32 @ntohs(i32 %33)
%35 = load i32*, i32** %9
store i32 %34, i32* %35
br label %36
36: ; preds = %31, %28
%37 = load i32, i32* %10
store i32 %37, i32* %5
br label %38
38: ; preds = %36, %18
%39 = load i32, i32* %5
ret i32 %39
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@INET_ADDRSTRLEN = global i32 0
define i32 @wheatTcpAccept(i8* %0, i32 %1, i8* %2, i32* writeonly %3) {
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
%7 = bitcast %struct.sockaddr_in* %5 to i8*
%8 = bitcast i32* %6 to i8*
store i32 8, i32* %6
%9 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%10 = call i32 @wheatGenericAccept(i8* %0, i32 %1, %struct.sockaddr* %9, i32* %6)
%11 = icmp eq i32 %10, -1
br i1 %11, label %25, label %12
12: ; preds = %4
%13 = icmp eq i8* %2, null
br i1 %13, label %19, label %14
14: ; preds = %12
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1
%17 = load i32, i32* @INET_ADDRSTRLEN
%18 = call i32 @inet_ntop(i32 %15, i32* %16, i8* %2, i32 %17)
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_in, %struct.sockaddr_in* %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
}