#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
typedef int /*<<< orphan*/ uint8_t;
struct TYPE_2__ {
  int /*<<< orphan*/ s6_addr;
};
struct sockaddr_in6 {
  TYPE_1__ sin6_addr;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int EAGAIN;
int EINTR;
int EWOULDBLOCK;
int FD_CLOEXEC;
int /*<<< orphan*/ F_GETFD;
int /*<<< orphan*/ F_SETFD;
int /*<<< orphan*/ Sactivate_thread();
int /*<<< orphan*/ Sdeactivate_thread();
int /*<<< orphan*/ Slock_object(void *);
int /*<<< orphan*/ Sunlock_object(void *);
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ close(int);
int errno;
int fcntl(int, int /*<<< orphan*/, ...);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ memset(struct sockaddr_in6 *, int /*<<< orphan*/, int);
int ss_accept_ipv6_connection_impl(int sock, uint8_t *connection) {
  struct sockaddr_in6 addr;
  memset(&addr, 0, sizeof(addr));
  socklen_t addr_len = sizeof(addr);
  if (connection)
    Slock_object((void *)connection);
  Sdeactivate_thread();
  int connect_sock;
  do {
    connect_sock = accept(sock, (struct sockaddr *)&addr, &addr_len);
    if (connect_sock != -1) {
      fcntl(connect_sock, F_SETFD, fcntl(connect_sock, F_GETFD) | FD_CLOEXEC);
    }
  } while (connect_sock == -1 && errno == EINTR);
  int saved_errno = errno;
  Sactivate_thread();
  if (connection)
    Sunlock_object((void *)connection);
  if (addr_len > sizeof(addr)) {
    close(connect_sock);
    errno = saved_errno;
    return -1;
  }
  if (connect_sock == -1) {
    errno = saved_errno;
    if (saved_errno == EAGAIN || saved_errno == EWOULDBLOCK)
      return -2;
    return -1;
  }
  if (connection)
    memcpy(connection, &addr.sin6_addr.s6_addr, sizeof(addr.sin6_addr.s6_addr));
  return connect_sock;
}


x86 assembly (clang, -O0):
<ss_accept_ipv6_connection_impl>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
movl   $0x4,-0x1c(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11ca <ss_accept_ipv6_connection_impl+0x3a>
mov    -0x10(%rbp),%rdi
call   1080 <Slock_object@plt>
mov    $0x0,%al
call   1030 <Sdeactivate_thread@plt>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
call   10b0 <accept@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
je     122b <ss_accept_ipv6_connection_impl+0x9b>
mov    -0x20(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2de5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%edi
mov    0x2db6(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1040 <fcntl@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%esi
mov    %eax,%edx
mov    0x2db6(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1040 <fcntl@plt>
jmp    1230 <ss_accept_ipv6_connection_impl+0xa0>
xor    %eax,%eax
cmpl   $0xffffffff,-0x20(%rbp)
mov    %al,-0x2d(%rbp)
jne    1257 <ss_accept_ipv6_connection_impl+0xc7>
mov    0x2d8a(%rip),%rax
mov    (%rax),%eax
mov    0x2d69(%rip),%rcx
cmp    (%rcx),%eax
sete   %al
mov    %al,-0x2d(%rbp)
mov    -0x2d(%rbp),%al
test   $0x1,%al
jne    11d1 <ss_accept_ipv6_connection_impl+0x41>
mov    0x2d67(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    $0x0,%al
call   10a0 <Sactivate_thread@plt>
cmpq   $0x0,-0x10(%rbp)
je     1289 <ss_accept_ipv6_connection_impl+0xf9>
mov    -0x10(%rbp),%rdi
call   1090 <Sunlock_object@plt>
movslq -0x1c(%rbp),%rax
cmp    $0x4,%rax
jbe    12b7 <ss_accept_ipv6_connection_impl+0x127>
mov    -0x20(%rbp),%edi
call   1060 <close@plt>
mov    -0x24(%rbp),%ecx
mov    0x2d27(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    132c <ss_accept_ipv6_connection_impl+0x19c>
cmpl   $0xffffffff,-0x20(%rbp)
jne    1309 <ss_accept_ipv6_connection_impl+0x179>
mov    -0x24(%rbp),%ecx
mov    0x2d05(%rip),%rax
mov    %ecx,(%rax)
mov    -0x24(%rbp),%eax
mov    0x2cd1(%rip),%rcx
cmp    (%rcx),%eax
je     12f1 <ss_accept_ipv6_connection_impl+0x161>
mov    -0x24(%rbp),%eax
mov    0x2cdf(%rip),%rcx
cmp    (%rcx),%eax
jne    12fd <ss_accept_ipv6_connection_impl+0x16d>
movl   $0xfffffffe,-0x4(%rbp)
jmp    132c <ss_accept_ipv6_connection_impl+0x19c>
movl   $0xffffffff,-0x4(%rbp)
jmp    132c <ss_accept_ipv6_connection_impl+0x19c>
cmpq   $0x0,-0x10(%rbp)
je     1326 <ss_accept_ipv6_connection_impl+0x196>
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1070 <memcpy@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ss_accept_ipv6_connection_impl>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbp
mov    %edi,%ebx
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
movl   $0x4,0xc(%rsp)
test   %rbp,%rbp
je     11c9 <ss_accept_ipv6_connection_impl+0x39>
mov    %rbp,%rdi
call   1080 <Slock_object@plt>
mov    %rbp,0x10(%rsp)
xor    %eax,%eax
call   1030 <Sdeactivate_thread@plt>
lea    0x8(%rsp),%r15
lea    0xc(%rsp),%r12
mov    0x2dd2(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %r15,%rsi
mov    %r12,%rdx
call   10b0 <accept@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    1216 <ss_accept_ipv6_connection_impl+0x86>
mov    0x2dc5(%rip),%rax
mov    (%rax),%r14d
cmp    0x0(%r13),%r14d
je     11f0 <ss_accept_ipv6_connection_impl+0x60>
jmp    1251 <ss_accept_ipv6_connection_impl+0xc1>
mov    0x2dc3(%rip),%rax
mov    (%rax),%ebx
mov    0x2d9a(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
xor    %eax,%eax
call   1040 <fcntl@plt>
mov    0x2da0(%rip),%rcx
or     (%rcx),%eax
mov    %ebp,%edi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <fcntl@plt>
mov    0x2d82(%rip),%rax
mov    (%rax),%r14d
xor    %eax,%eax
call   10a0 <Sactivate_thread@plt>
mov    0x10(%rsp),%rbx
test   %rbx,%rbx
je     126a <ss_accept_ipv6_connection_impl+0xda>
mov    %rbx,%rdi
call   1090 <Sunlock_object@plt>
cmpl   $0x5,0xc(%rsp)
jb     1289 <ss_accept_ipv6_connection_impl+0xf9>
mov    %ebp,%edi
call   1060 <close@plt>
mov    0x2d51(%rip),%rax
mov    %r14d,(%rax)
mov    $0xffffffff,%ebp
jmp    12d2 <ss_accept_ipv6_connection_impl+0x142>
cmp    $0xffffffff,%ebp
je     12a7 <ss_accept_ipv6_connection_impl+0x117>
test   %rbx,%rbx
je     12d2 <ss_accept_ipv6_connection_impl+0x142>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
mov    $0x4,%edx
call   1070 <memcpy@plt>
jmp    12d2 <ss_accept_ipv6_connection_impl+0x142>
mov    0x2d22(%rip),%rax
mov    %r14d,(%rax)
mov    0x2cf0(%rip),%rax
cmp    (%rax),%r14d
sete   %al
mov    0x2d03(%rip),%rcx
cmp    (%rcx),%r14d
sete   %cl
or     %al,%cl
movzbl %cl,%ebp
not    %ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in6 = type { %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@F_SETFD = global i32 0
@F_GETFD = global i32 0
@FD_CLOEXEC = global i32 0
@errno = global i32 0
@EINTR = global i32 0
@EAGAIN = global i32 0
@EWOULDBLOCK = global i32 0
define i32 @ss_accept_ipv6_connection_impl(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32*
%6 = alloca %struct.sockaddr_in6
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %4
store i32* %1, i32** %5
%10 = call i32 @memset(%struct.sockaddr_in6* %6, i32 0, i32 4)
store i32 4, i32* %7
%11 = load i32*, i32** %5
%12 = icmp ne i32* %11, null
br i1 %12, label %13, label %17
13: ; preds = %2
%14 = load i32*, i32** %5
%15 = bitcast i32* %14 to i8*
%16 = call i32 @Slock_object(i8* %15)
br label %17
17: ; preds = %13, %2
%18 = call i32 (...) @Sdeactivate_thread()
br label %19
19: ; preds = %42, %17
%20 = load i32, i32* %4
%21 = bitcast %struct.sockaddr_in6* %6 to %struct.sockaddr*
%22 = call i32 @accept(i32 %20, %struct.sockaddr* %21, i32* %7)
store i32 %22, i32* %8
%23 = load i32, i32* %8
%24 = icmp ne i32 %23, -1
br i1 %24, label %25, label %34
25: ; preds = %19
%26 = load i32, i32* %8
%27 = load i32, i32* @F_SETFD
%28 = load i32, i32* %8
%29 = load i32, i32* @F_GETFD
%30 = call i32 (i32, i32, ...) @fcntl(i32 %28, i32 %29)
%31 = load i32, i32* @FD_CLOEXEC
%32 = or i32 %30, %31
%33 = call i32 (i32, i32, ...) @fcntl(i32 %26, i32 %27, i32 %32)
br label %34
34: ; preds = %25, %19
br label %35
35: ; preds = %34
%36 = load i32, i32* %8
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %42
38: ; preds = %35
%39 = load i32, i32* @errno
%40 = load i32, i32* @EINTR
%41 = icmp eq i32 %39, %40
br label %42
42: ; preds = %38, %35
%43 = phi i1 [ false, %35 ], [ %41, %38 ]
br i1 %43, label %19, label %44
44: ; preds = %42
%45 = load i32, i32* @errno
store i32 %45, i32* %9
%46 = call i32 (...) @Sactivate_thread()
%47 = load i32*, i32** %5
%48 = icmp ne i32* %47, null
br i1 %48, label %49, label %53
49: ; preds = %44
%50 = load i32*, i32** %5
%51 = bitcast i32* %50 to i8*
%52 = call i32 @Sunlock_object(i8* %51)
br label %53
53: ; preds = %49, %44
%54 = load i32, i32* %7
%55 = sext i32 %54 to i64
%56 = icmp ugt i64 %55, 4
br i1 %56, label %57, label %61
57: ; preds = %53
%58 = load i32, i32* %8
%59 = call i32 @close(i32 %58)
%60 = load i32, i32* %9
store i32 %60, i32* @errno
store i32 -1, i32* %3
br label %85
61: ; preds = %53
%62 = load i32, i32* %8
%63 = icmp eq i32 %62, -1
br i1 %63, label %64, label %75
64: ; preds = %61
%65 = load i32, i32* %9
store i32 %65, i32* @errno
%66 = load i32, i32* %9
%67 = load i32, i32* @EAGAIN
%68 = icmp eq i32 %66, %67
br i1 %68, label %73, label %69
69: ; preds = %64
%70 = load i32, i32* %9
%71 = load i32, i32* @EWOULDBLOCK
%72 = icmp eq i32 %70, %71
br i1 %72, label %73, label %74
73: ; preds = %69, %64
store i32 -2, i32* %3
br label %85
74: ; preds = %69
store i32 -1, i32* %3
br label %85
75: ; preds = %61
%76 = load i32*, i32** %5
%77 = icmp ne i32* %76, null
br i1 %77, label %78, label %83
78: ; preds = %75
%79 = load i32*, i32** %5
%80 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %6, i32 0, i32 0
%81 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %80, i32 0, i32 0
%82 = call i32 @memcpy(i32* %79, i32* %81, i32 4)
br label %83
83: ; preds = %78, %75
%84 = load i32, i32* %8
store i32 %84, i32* %3
br label %85
85: ; preds = %83, %74, %73, %57
%86 = load i32, i32* %3
ret i32 %86
}

LLVM IR (-O2):
%struct.sockaddr_in6 = type { %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@F_SETFD = global i32 0
@F_GETFD = global i32 0
@FD_CLOEXEC = global i32 0
@errno = global i32 0
@EINTR = global i32 0
@EAGAIN = global i32 0
@EWOULDBLOCK = global i32 0
define i32 @ss_accept_ipv6_connection_impl(i32 %0, i32* %1) {
%3 = alloca %struct.sockaddr_in6
%4 = alloca i32
%5 = bitcast %struct.sockaddr_in6* %3 to i8*
%6 = call i32 @memset(%struct.sockaddr_in6* %3, i32 0, i32 4)
%7 = bitcast i32* %4 to i8*
store i32 4, i32* %4
%8 = icmp eq i32* %1, null
br i1 %8, label %12, label %9
9: ; preds = %2
%10 = bitcast i32* %1 to i8*
%11 = call i32 @Slock_object(i8* %10)
br label %12
12: ; preds = %9, %2
%13 = call i32 (...) @Sdeactivate_thread()
%14 = bitcast %struct.sockaddr_in6* %3 to %struct.sockaddr*
br label %15
15: ; preds = %26, %12
%16 = call i32 @accept(i32 %0, %struct.sockaddr* %14, i32* %4)
%17 = icmp eq i32 %16, -1
br i1 %17, label %26, label %18
18: ; preds = %15
%19 = load i32, i32* @F_SETFD
%20 = load i32, i32* @F_GETFD
%21 = call i32 (i32, i32, ...) @fcntl(i32 %16, i32 %20)
%22 = load i32, i32* @FD_CLOEXEC
%23 = or i32 %22, %21
%24 = call i32 (i32, i32, ...) @fcntl(i32 %16, i32 %19, i32 %23)
%25 = load i32, i32* @errno
br label %30
26: ; preds = %15
%27 = load i32, i32* @errno
%28 = load i32, i32* @EINTR
%29 = icmp eq i32 %27, %28
br i1 %29, label %15, label %30
30: ; preds = %26, %18
%31 = phi i32 [ %25, %18 ], [ %27, %26 ]
%32 = call i32 (...) @Sactivate_thread()
br i1 %8, label %36, label %33
33: ; preds = %30
%34 = bitcast i32* %1 to i8*
%35 = call i32 @Sunlock_object(i8* %34)
br label %36
36: ; preds = %33, %30
%37 = load i32, i32* %4
%38 = icmp ugt i32 %37, 4
br i1 %38, label %39, label %41
39: ; preds = %36
%40 = call i32 @close(i32 %16)
store i32 %31, i32* @errno
br label %53
41: ; preds = %36
br i1 %17, label %42, label %49
42: ; preds = %41
store i32 %31, i32* @errno
%43 = load i32, i32* @EAGAIN
%44 = icmp eq i32 %31, %43
%45 = load i32, i32* @EWOULDBLOCK
%46 = icmp eq i32 %31, %45
%47 = select i1 %44, i1 true, i1 %46
%48 = select i1 %47, i32 -2, i32 -1
br label %53
49: ; preds = %41
br i1 %8, label %53, label %50
50: ; preds = %49
%51 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %3, i64 0, i32 0, i32 0
%52 = call i32 @memcpy(i32* %1, i32* %51, i32 4)
br label %53
53: ; preds = %42, %49, %50, %39
%54 = phi i32 [ -1, %39 ], [ %16, %50 ], [ %16, %49 ], [ %48, %42 ]
ret i32 %54
}