#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 {
  scalar_t__ sa_family;
};
typedef int /*<<< orphan*/ socklen_t;
typedef int /*<<< orphan*/ on;

/* Variables and functions */
scalar_t__ AF_INET;
scalar_t__ AF_INET6;
#define EAGAIN 138
#define ECONNABORTED 137
#define ECONNREFUSED 136
#define ECONNRESET 135
#define EHOSTDOWN 134
#define EHOSTUNREACH 133
int EINTR;
#define ENETDOWN 132
#define ENETUNREACH 131
#define ENOBUFS 130
#define ENOTCONN 129
#define EWOULDBLOCK 128
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_KEEPALIVE;
int accept(int, struct sockaddr *, int /*<<< orphan*/ *);
int errno;
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              char *, int);
int sane_accept(int sock, struct sockaddr *sa, socklen_t *len) {
  static int accept_ok_errors[] = {
      EAGAIN,      ECONNREFUSED, ECONNRESET,  EHOSTDOWN, EHOSTUNREACH, ENETDOWN,
      ENETUNREACH, ENOTCONN,     EWOULDBLOCK, ENOBUFS,   ECONNABORTED, 0,
  };
  int count;
  int err;
  int fd;
  int errno2;
  while (1) {
    if ((fd = accept(sock, sa, len)) < 0) {
      errno2 = errno;
      if (errno2 == EINTR) {
        continue;
      }
      for (count = 0; (err = accept_ok_errors[count]) != 0; count++) {
        if (errno2 == err) {
          errno = EAGAIN;
          break;
        }
      }
    } else if (sa && (sa->sa_family == AF_INET || sa->sa_family == AF_INET6)) {
      int on = 1;
      (void)setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (char *)&on, sizeof(on));
    }
    break;
  }
  return (fd);
}


x86 assembly (clang, -O0):
<sane_accept>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <accept@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    11c9 <sane_accept+0xa9>
mov    0x2e72(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
mov    0x2e5b(%rip),%rcx
cmp    (%rcx),%eax
jne    1172 <sane_accept+0x52>
jmp    1133 <sane_accept+0x13>
movl   $0x0,-0x1c(%rbp)
movslq -0x1c(%rbp),%rcx
lea    0x2ebc(%rip),%rax
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
je     11c4 <sane_accept+0xa4>
mov    -0x28(%rbp),%eax
cmp    -0x20(%rbp),%eax
jne    11b1 <sane_accept+0x91>
mov    0x2e22(%rip),%rax
movl   $0x8a,(%rax)
jmp    11c4 <sane_accept+0xa4>
jmp    11b6 <sane_accept+0x96>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1179 <sane_accept+0x59>
jmp    1232 <sane_accept+0x112>
cmpq   $0x0,-0x10(%rbp)
je     122d <sane_accept+0x10d>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x2dfe(%rip),%rcx
cmp    (%rcx),%rax
je     1202 <sane_accept+0xe2>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x2ddf(%rip),%rcx
cmp    (%rcx),%rax
jne    122d <sane_accept+0x10d>
movl   $0x1,-0x2c(%rbp)
mov    -0x24(%rbp),%edi
mov    0x2dd5(%rip),%rax
mov    (%rax),%esi
mov    0x2d94(%rip),%rax
mov    (%rax),%edx
lea    -0x2c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
jmp    1232 <sane_accept+0x112>
jmp    1237 <sane_accept+0x117>
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sane_accept>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r14
mov    %edi,%ebp
mov    0x2e8e(%rip),%r12
mov    0x2e7f(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   1040 <accept@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    117f <sane_accept+0x5f>
mov    (%r12),%eax
cmp    0x0(%r13),%eax
je     1150 <sane_accept+0x30>
add    $0xffffff80,%eax
cmp    $0xa,%eax
ja     11cb <sane_accept+0xab>
movl   $0x8a,(%r12)
jmp    11cb <sane_accept+0xab>
test   %r14,%r14
je     11cb <sane_accept+0xab>
mov    (%r14),%rax
mov    0x2e52(%rip),%rcx
cmp    (%rcx),%rax
je     119f <sane_accept+0x7f>
mov    0x2e3e(%rip),%rcx
cmp    (%rcx),%rax
jne    11cb <sane_accept+0xab>
movl   $0x1,0x4(%rsp)
mov    0x2e3a(%rip),%rax
mov    (%rax),%esi
mov    0x2df9(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i64 }
@sane_accept.accept_ok_errors = internal global [12 x i32] [i32 138, i32 136, i32 135, i32 134, i32 133, i32 132, i32 131, i32 129, i32 128, i32 130, i32 137, i32 0]
@errno = global i32 0
@EINTR = global i32 0
@AF_INET = global i64 0
@AF_INET6 = global i64 0
@SOL_SOCKET = global i32 0
@SO_KEEPALIVE = global i32 0
define i32 @sane_accept(i32 %0, %struct.sockaddr* %1, i32* %2) {
%4 = alloca i32
%5 = alloca %struct.sockaddr*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %4
store %struct.sockaddr* %1, %struct.sockaddr** %5
store i32* %2, i32** %6
br label %12
12: ; preds = %3, %23
%13 = load i32, i32* %4
%14 = load %struct.sockaddr*, %struct.sockaddr** %5
%15 = load i32*, i32** %6
%16 = call i32 @accept(i32 %13, %struct.sockaddr* %14, i32* %15)
store i32 %16, i32* %9
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %41
18: ; preds = %12
%19 = load i32, i32* @errno
store i32 %19, i32* %10
%20 = load i32, i32* %10
%21 = load i32, i32* @EINTR
%22 = icmp eq i32 %20, %21
br i1 %22, label %23, label %24
23: ; preds = %18
br label %12
24: ; preds = %18
store i32 0, i32* %7
br label %25
25: ; preds = %37, %24
%26 = load i32, i32* %7
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds [12 x i32], [12 x i32]* @sane_accept.accept_ok_errors, i64 0, i64 %27
%29 = load i32, i32* %28
store i32 %29, i32* %8
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %40
31: ; preds = %25
%32 = load i32, i32* %10
%33 = load i32, i32* %8
%34 = icmp eq i32 %32, %33
br i1 %34, label %35, label %36
35: ; preds = %31
store i32 138, i32* @errno
br label %40
36: ; preds = %31
br label %37
37: ; preds = %36
%38 = load i32, i32* %7
%39 = add nsw i32 %38, 1
store i32 %39, i32* %7
br label %25
40: ; preds = %35, %25
br label %63
41: ; preds = %12
%42 = load %struct.sockaddr*, %struct.sockaddr** %5
%43 = icmp ne %struct.sockaddr* %42, null
br i1 %43, label %44, label %62
44: ; preds = %41
%45 = load %struct.sockaddr*, %struct.sockaddr** %5
%46 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %45, i32 0, i32 0
%47 = load i64, i64* %46
%48 = load i64, i64* @AF_INET
%49 = icmp eq i64 %47, %48
br i1 %49, label %56, label %50
50: ; preds = %44
%51 = load %struct.sockaddr*, %struct.sockaddr** %5
%52 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %51, i32 0, i32 0
%53 = load i64, i64* %52
%54 = load i64, i64* @AF_INET6
%55 = icmp eq i64 %53, %54
br i1 %55, label %56, label %62
56: ; preds = %50, %44
store i32 1, i32* %11
%57 = load i32, i32* %9
%58 = load i32, i32* @SOL_SOCKET
%59 = load i32, i32* @SO_KEEPALIVE
%60 = bitcast i32* %11 to i8*
%61 = call i32 @setsockopt(i32 %57, i32 %58, i32 %59, i8* %60, i32 4)
br label %62
62: ; preds = %56, %50, %41
br label %63
63: ; preds = %62, %40
br label %64
64: ; preds = %63
%65 = load i32, i32* %9
ret i32 %65
}

LLVM IR (-O2):
%struct.sockaddr = type { i64 }
@errno = global i32 0
@EINTR = global i32 0
@AF_INET = global i64 0
@AF_INET6 = global i64 0
@SOL_SOCKET = global i32 0
@SO_KEEPALIVE = global i32 0
define i32 @sane_accept(i32 %0, %struct.sockaddr* %1, i32* %2) {
%4 = alloca i32
br label %5
5: ; preds = %8, %3
%6 = tail call i32 @accept(i32 %0, %struct.sockaddr* %1, i32* %2)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %16
8: ; preds = %5
%9 = load i32, i32* @errno
%10 = load i32, i32* @EINTR
%11 = icmp eq i32 %9, %10
br i1 %11, label %5, label %12
12: ; preds = %8
%13 = add i32 %9, -128
%14 = icmp ult i32 %13, 11
br i1 %14, label %15, label %31
15: ; preds = %12
store i32 138, i32* @errno
br label %31
16: ; preds = %5
%17 = icmp eq %struct.sockaddr* %1, null
br i1 %17, label %31, label %18
18: ; preds = %16
%19 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %1, i64 0, i32 0
%20 = load i64, i64* %19
%21 = load i64, i64* @AF_INET
%22 = icmp eq i64 %20, %21
%23 = load i64, i64* @AF_INET6
%24 = icmp eq i64 %20, %23
%25 = select i1 %22, i1 true, i1 %24
br i1 %25, label %26, label %31
26: ; preds = %18
%27 = bitcast i32* %4 to i8*
store i32 1, i32* %4
%28 = load i32, i32* @SOL_SOCKET
%29 = load i32, i32* @SO_KEEPALIVE
%30 = call i32 @setsockopt(i32 %6, i32 %28, i32 %29, i8* %27, i32 4)
br label %31
31: ; preds = %12, %18, %15, %26, %16
ret i32 %6
}