#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 {
  scalar_t__ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ sin;
typedef int /*<<< orphan*/ s;
typedef int /*<<< orphan*/ h;

/* Variables and functions */
scalar_t__ AF_INET;
int /*<<< orphan*/ BIO_R_ACCEPT_ERROR;
scalar_t__ BIO_sock_should_retry(int);
int /*<<< orphan*/ BIOerror(int /*<<< orphan*/);
int /*<<< orphan*/ ERR_R_MALLOC_FAILURE;
int NI_MAXHOST;
int NI_MAXSERV;
int NI_NUMERICHOST;
int NI_NUMERICSERV;
int /*<<< orphan*/ SYSerror(int /*<<< orphan*/);
int accept(int, struct sockaddr *, int *);
int asprintf(char **, char *, char *, char *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ free(char *);
scalar_t__ getnameinfo(struct sockaddr *, int, char *, int, char *, int, int);
int BIO_accept(int sock, char **addr) {
  char h[NI_MAXHOST], s[NI_MAXSERV];
  struct sockaddr_in sin;
  socklen_t sin_len = sizeof(sin);
  int ret = -1;
  if (addr == NULL)
    goto end;
  ret = accept(sock, (struct sockaddr *)&sin, &sin_len);
  if (ret == -1) {
    if (BIO_sock_should_retry(ret))
      return -2;
    SYSerror(errno);
    BIOerror(BIO_R_ACCEPT_ERROR);
    goto end;
  }
  if (*addr != NULL) {
    free(*addr);
    *addr = NULL;
  }
  if (sin.sin_family != AF_INET)
    goto end;
  if (getnameinfo((struct sockaddr *)&sin, sin_len, h, sizeof(h), s, sizeof(s),
                  NI_NUMERICHOST | NI_NUMERICSERV) != 0)
    goto end;
  if ((asprintf(addr, "%s:%s", h, s)) == -1) {
    BIOerror(ERR_R_MALLOC_FAILURE);
    *addr = NULL;
    goto end;
  }
end:
  return (ret);
}


x86 assembly (clang, -O0):
<BIO_accept>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e22(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x60(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x58(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
mov    0x2e1a(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x50(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
movl   $0x8,-0x34(%rbp)
movl   $0xffffffff,-0x38(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11f6 <BIO_accept+0x86>
jmp    1327 <BIO_accept+0x1b7>
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
lea    -0x34(%rbp),%rdx
call   1080 <accept@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0xffffffff,-0x38(%rbp)
jne    1259 <BIO_accept+0xe9>
mov    -0x38(%rbp),%edi
call   1060 <BIO_sock_should_retry@plt>
cmp    $0x0,%rax
je     1238 <BIO_accept+0xc8>
movl   $0xfffffffe,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    1334 <BIO_accept+0x1c4>
mov    0x2d71(%rip),%rax
mov    (%rax),%edi
call   1050 <SYSerror@plt>
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
call   1090 <BIOerror@plt>
jmp    1327 <BIO_accept+0x1b7>
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
je     127e <BIO_accept+0x10e>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x30(%rbp),%rax
mov    0x2d57(%rip),%rcx
cmp    (%rcx),%rax
je     1297 <BIO_accept+0x127>
jmp    1327 <BIO_accept+0x1b7>
mov    -0x48(%rbp),%r8
mov    -0x58(%rbp),%rdx
mov    -0x50(%rbp),%rax
mov    -0x60(%rbp),%rcx
lea    -0x30(%rbp),%rdi
mov    -0x34(%rbp),%esi
mov    %eax,%r9d
mov    0x2d00(%rip),%rax
mov    (%rax),%eax
mov    0x2d27(%rip),%r10
or     (%r10),%eax
sub    $0x10,%rsp
mov    %eax,(%rsp)
call   1040 <getnameinfo@plt>
add    $0x10,%rsp
cmp    $0x0,%rax
je     12e3 <BIO_accept+0x173>
jmp    1327 <BIO_accept+0x1b7>
mov    -0x48(%rbp),%rcx
mov    -0x58(%rbp),%rdx
mov    -0x10(%rbp),%rdi
lea    0xd0a(%rip),%rsi
call   1070 <asprintf@plt>
cmp    $0xffffffff,%eax
jne    1322 <BIO_accept+0x1b2>
mov    0x2cbd(%rip),%rax
mov    (%rax),%edi
call   1090 <BIOerror@plt>
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
jmp    1327 <BIO_accept+0x1b7>
jmp    1327 <BIO_accept+0x1b7>
mov    -0x38(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<BIO_accept>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    0x2e20(%rip),%rax
mov    (%rax),%r12d
mov    %rsp,%r13
lea    0xf(%r12),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r13
mov    %r13,%rsp
mov    0x2e2c(%rip),%rax
mov    (%rax),%r14d
mov    %rsp,%rbx
lea    0xf(%r14),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
movl   $0x8,-0x2c(%rbp)
test   %rsi,%rsi
je     12b3 <BIO_accept+0x143>
mov    %rsi,-0x38(%rbp)
lea    -0x40(%rbp),%rsi
lea    -0x2c(%rbp),%rdx
call   1080 <accept@plt>
cmp    $0xffffffff,%eax
je     1282 <BIO_accept+0x112>
mov    %eax,%r15d
mov    -0x38(%rbp),%rax
mov    (%rax),%rdi
test   %rdi,%rdi
je     1201 <BIO_accept+0x91>
call   1030 <free@plt>
mov    -0x38(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x40(%rbp),%rax
mov    0x2dd4(%rip),%rcx
cmp    (%rcx),%rax
jne    12b9 <BIO_accept+0x149>
mov    -0x2c(%rbp),%esi
mov    0x2d99(%rip),%rdx
mov    0x2dc2(%rip),%rcx
mov    (%rcx),%eax
or     (%rdx),%eax
sub    $0x8,%rsp
lea    -0x40(%rbp),%rdi
mov    %r13,%rdx
mov    %r12d,%ecx
mov    %rbx,%r8
mov    %r14d,%r9d
push   %rax
call   1040 <getnameinfo@plt>
add    $0x10,%rsp
test   %rax,%rax
jne    12b9 <BIO_accept+0x149>
lea    0xdac(%rip),%rsi
mov    -0x38(%rbp),%r14
mov    %r14,%rdi
mov    %r13,%rdx
mov    %rbx,%rcx
call   1070 <asprintf@plt>
cmp    $0xffffffff,%eax
jne    12b9 <BIO_accept+0x149>
mov    0x2d56(%rip),%rax
mov    (%rax),%edi
call   1090 <BIOerror@plt>
movq   $0x0,(%r14)
jmp    12b9 <BIO_accept+0x149>
mov    $0xffffffff,%edi
call   1060 <BIO_sock_should_retry@plt>
mov    $0xfffffffe,%r15d
test   %rax,%rax
jne    12b9 <BIO_accept+0x149>
mov    0x2d12(%rip),%rax
mov    (%rax),%edi
call   1050 <SYSerror@plt>
mov    0x2d2c(%rip),%rax
mov    (%rax),%edi
call   1090 <BIOerror@plt>
mov    $0xffffffff,%r15d
mov    %r15d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i64 }
%struct.sockaddr = type { i32 }
@NI_MAXHOST = global i32 0
@NI_MAXSERV = global i32 0
@errno = global i32 0
@BIO_R_ACCEPT_ERROR = global i32 0
@AF_INET = global i64 0
@NI_NUMERICHOST = global i32 0
@NI_NUMERICSERV = global i32 0
@.str = [6 x i8] c"%s:%s\00"
@ERR_R_MALLOC_FAILURE = global i32 0
define i32 @BIO_accept(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca %struct.sockaddr_in
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* @NI_MAXHOST
%14 = zext i32 %13 to i64
%15 = call i8* @llvm.stacksave()
store i8* %15, i8** %6
%16 = alloca i8, i64 %14
store i64 %14, i64* %7
%17 = load i32, i32* @NI_MAXSERV
%18 = zext i32 %17 to i64
%19 = alloca i8, i64 %18
store i64 %18, i64* %8
store i32 8, i32* %10
store i32 -1, i32* %11
%20 = load i8**, i8*** %5
%21 = icmp eq i8** %20, null
br i1 %21, label %22, label %23
22: ; preds = %2
br label %74
23: ; preds = %2
%24 = load i32, i32* %4
%25 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%26 = call i32 @accept(i32 %24, %struct.sockaddr* %25, i32* %10)
store i32 %26, i32* %11
%27 = load i32, i32* %11
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %39
29: ; preds = %23
%30 = load i32, i32* %11
%31 = call i64 @BIO_sock_should_retry(i32 %30)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %29
store i32 -2, i32* %3
store i32 1, i32* %12
br label %76
34: ; preds = %29
%35 = load i32, i32* @errno
%36 = call i32 @SYSerror(i32 %35)
%37 = load i32, i32* @BIO_R_ACCEPT_ERROR
%38 = call i32 @BIOerror(i32 %37)
br label %74
39: ; preds = %23
%40 = load i8**, i8*** %5
%41 = load i8*, i8** %40
%42 = icmp ne i8* %41, null
br i1 %42, label %43, label %48
43: ; preds = %39
%44 = load i8**, i8*** %5
%45 = load i8*, i8** %44
%46 = call i32 @free(i8* %45)
%47 = load i8**, i8*** %5
store i8* null, i8** %47
br label %48
48: ; preds = %43, %39
%49 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
%50 = load i64, i64* %49
%51 = load i64, i64* @AF_INET
%52 = icmp ne i64 %50, %51
br i1 %52, label %53, label %54
53: ; preds = %48
br label %74
54: ; preds = %48
%55 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%56 = load i32, i32* %10
%57 = trunc i64 %14 to i32
%58 = trunc i64 %18 to i32
%59 = load i32, i32* @NI_NUMERICHOST
%60 = load i32, i32* @NI_NUMERICSERV
%61 = or i32 %59, %60
%62 = call i64 @getnameinfo(%struct.sockaddr* %55, i32 %56, i8* %16, i32 %57, i8* %19, i32 %58, i32 %61)
%63 = icmp ne i64 %62, 0
br i1 %63, label %64, label %65
64: ; preds = %54
br label %74
65: ; preds = %54
%66 = load i8**, i8*** %5
%67 = call i32 @asprintf(i8** %66, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %16, i8* %19)
%68 = icmp eq i32 %67, -1
br i1 %68, label %69, label %73
69: ; preds = %65
%70 = load i32, i32* @ERR_R_MALLOC_FAILURE
%71 = call i32 @BIOerror(i32 %70)
%72 = load i8**, i8*** %5
store i8* null, i8** %72
br label %74
73: ; preds = %65
br label %74
74: ; preds = %73, %69, %64, %53, %34, %22
%75 = load i32, i32* %11
store i32 %75, i32* %3
store i32 1, i32* %12
br label %76
76: ; preds = %74, %33
%77 = load i8*, i8** %6
%78 = load i32, i32* %3
ret i32 %78
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i64 }
%struct.sockaddr = type { i32 }
@NI_MAXHOST = global i32 0
@NI_MAXSERV = global i32 0
@errno = global i32 0
@BIO_R_ACCEPT_ERROR = global i32 0
@AF_INET = global i64 0
@NI_NUMERICHOST = global i32 0
@NI_NUMERICSERV = global i32 0
@.str = [6 x i8] c"%s:%s\00"
@ERR_R_MALLOC_FAILURE = global i32 0
define i32 @BIO_accept(i32 %0, i8** %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = load i32, i32* @NI_MAXHOST
%6 = zext i32 %5 to i64
%7 = alloca i8, i64 %6
%8 = load i32, i32* @NI_MAXSERV
%9 = zext i32 %8 to i64
%10 = alloca i8, i64 %9
%11 = bitcast %struct.sockaddr_in* %3 to i8*
%12 = bitcast i32* %4 to i8*
store i32 8, i32* %4
%13 = icmp eq i8** %1, null
br i1 %13, label %49, label %14
14: ; preds = %2
%15 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%16 = call i32 @accept(i32 %0, %struct.sockaddr* %15, i32* %4)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %26
18: ; preds = %14
%19 = call i64 @BIO_sock_should_retry(i32 -1)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %49
21: ; preds = %18
%22 = load i32, i32* @errno
%23 = call i32 @SYSerror(i32 %22)
%24 = load i32, i32* @BIO_R_ACCEPT_ERROR
%25 = call i32 @BIOerror(i32 %24)
br label %49
26: ; preds = %14
%27 = load i8*, i8** %1
%28 = icmp eq i8* %27, null
br i1 %28, label %31, label %29
29: ; preds = %26
%30 = call i32 @free(i8* %27)
store i8* null, i8** %1
br label %31
31: ; preds = %29, %26
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%33 = load i64, i64* %32
%34 = load i64, i64* @AF_INET
%35 = icmp eq i64 %33, %34
br i1 %35, label %36, label %49
36: ; preds = %31
%37 = load i32, i32* %4
%38 = load i32, i32* @NI_NUMERICHOST
%39 = load i32, i32* @NI_NUMERICSERV
%40 = or i32 %39, %38
%41 = call i64 @getnameinfo(%struct.sockaddr* %15, i32 %37, i8* %7, i32 %5, i8* %10, i32 %8, i32 %40)
%42 = icmp eq i64 %41, 0
br i1 %42, label %43, label %49
43: ; preds = %36
%44 = call i32 @asprintf(i8** %1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %7, i8* %10)
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %49
46: ; preds = %43
%47 = load i32, i32* @ERR_R_MALLOC_FAILURE
%48 = call i32 @BIOerror(i32 %47)
store i8* null, i8** %1
br label %49
49: ; preds = %21, %46, %2, %31, %36, %43, %18
%50 = phi i32 [ -2, %18 ], [ -1, %2 ], [ -1, %21 ], [ %16, %31 ], [ %16, %36 ], [ %16, %46 ], [ %16, %43 ]
ret i32 %50
}