#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 */

/* Variables and functions */
#define ECONNABORTED 129
#define EINTR 128
int accept(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, ...);
int /*<<< orphan*/ stderr;
char *strerror(int);
int SK_accept_connection(int listening_socket) {
  int connected_socket = -1;
  int num_errors = 0;
  for (;;) {
    connected_socket = accept(listening_socket, NULL, NULL);
    if (connected_socket < 0) {
      switch (errno) {
      case EINTR:
      case ECONNABORTED:
        fprintf(stderr, "(%d) %s\n", errno, strerror(errno));
        continue;
      default:
        if (num_errors++ < 3) {
          fprintf(stderr, "(%d) %s\n", errno, strerror(errno));
        } else {
          fprintf(stderr, "too many accept() errors (maximum is %d)", 3);
          return -1;
        }
      }
    } else {
      return connected_socket;
    }
  }
}


x86 assembly (clang, -O0):
<SK_accept_connection>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
movl   $0xffffffff,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   1040 <accept@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1249 <SK_accept_connection+0x119>
mov    0x2e6c(%rip),%rax
mov    (%rax),%eax
add    $0xffffff80,%eax
sub    $0x1,%eax
ja     11c1 <SK_accept_connection+0x91>
jmp    117f <SK_accept_connection+0x4f>
mov    0x2e5a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e46(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2e3a(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    -0x18(%rbp),%edi
mov    -0x14(%rbp),%edx
mov    %rax,%rcx
lea    0xe4b(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
jmp    1149 <SK_accept_connection+0x19>
mov    -0x10(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x10(%rbp)
cmp    $0x3,%eax
jge    1217 <SK_accept_connection+0xe7>
mov    0x2e04(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2df0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2de4(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    -0x20(%rbp),%edi
mov    -0x1c(%rbp),%edx
mov    %rax,%rcx
lea    0xdf5(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
jmp    123f <SK_accept_connection+0x10f>
mov    0x2dc2(%rip),%rax
mov    (%rax),%edi
lea    0xde2(%rip),%rsi
mov    $0x3,%edx
mov    $0x0,%al
call   1030 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1259 <SK_accept_connection+0x129>
jmp    1244 <SK_accept_connection+0x114>
jmp    1254 <SK_accept_connection+0x124>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1259 <SK_accept_connection+0x129>
jmp    1149 <SK_accept_connection+0x19>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<SK_accept_connection>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%r12d
xor    %esi,%esi
xor    %edx,%edx
call   1040 <accept@plt>
test   %eax,%eax
jns    12cb <SK_accept_connection+0x19b>
mov    0x2e8a(%rip),%r15
lea    0xea3(%rip),%r14
nopl   (%rax)
mov    0x2e71(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%r13d
and    $0xfffffffe,%r13d
mov    (%r15),%ebx
mov    %ebp,%edi
call   1050 <strerror@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %ebp,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    %r12d,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1040 <accept@plt>
cmp    $0x80,%r13d
jne    11a9 <SK_accept_connection+0x79>
test   %eax,%eax
js     1160 <SK_accept_connection+0x30>
jmp    12cb <SK_accept_connection+0x19b>
test   %eax,%eax
jns    12cb <SK_accept_connection+0x19b>
lea    0xe48(%rip),%r14
nopl   0x0(%rax,%rax,1)
mov    0x2e11(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%ebx
and    $0xfffffffe,%ebx
mov    (%r15),%r13d
mov    %ebp,%edi
call   1050 <strerror@plt>
mov    %r13d,%edi
mov    %r14,%rsi
mov    %ebp,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    %r12d,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1040 <accept@plt>
cmp    $0x80,%ebx
jne    1207 <SK_accept_connection+0xd7>
test   %eax,%eax
js     11c0 <SK_accept_connection+0x90>
jmp    12cb <SK_accept_connection+0x19b>
test   %eax,%eax
jns    12cb <SK_accept_connection+0x19b>
lea    0xdea(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
mov    0x2db1(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%ebp
and    $0xfffffffe,%ebp
mov    (%r15),%r13d
mov    %ebx,%edi
call   1050 <strerror@plt>
mov    %r13d,%edi
mov    %r14,%rsi
mov    %ebx,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    %r12d,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1040 <accept@plt>
cmp    $0x80,%ebp
jne    1264 <SK_accept_connection+0x134>
test   %eax,%eax
js     1220 <SK_accept_connection+0xf0>
jmp    12cb <SK_accept_connection+0x19b>
test   %eax,%eax
jns    12cb <SK_accept_connection+0x19b>
lea    0xd91(%rip),%r14
mov    0x2d62(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%eax
and    $0xfffffffe,%eax
mov    (%r15),%ebp
cmp    $0x80,%eax
jne    12b1 <SK_accept_connection+0x181>
mov    %ebx,%edi
call   1050 <strerror@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %ebx,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    %r12d,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1040 <accept@plt>
test   %eax,%eax
js     126f <SK_accept_connection+0x13f>
jmp    12cb <SK_accept_connection+0x19b>
lea    0xd51(%rip),%rsi
mov    %ebp,%edi
mov    $0x3,%edx
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i32 0
@stderr = global i32 0
@.str = [9 x i8] c"(%d) %s\0A\00"
@.str.1 = [41 x i8] c"too many accept() errors (maximum is %d)\00"
define i32 @SK_accept_connection(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %3
store i32 -1, i32* %4
store i32 0, i32* %5
br label %6
6: ; preds = %36, %13, %1
%7 = load i32, i32* %3
%8 = call i32 @accept(i32 %7, i32* null, i32* null)
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %34
11: ; preds = %6
%12 = load i32, i32* @errno
switch i32 %12, label %19 [
i32 128, label %13
i32 129, label %13
]
13: ; preds = %11, %11
%14 = load i32, i32* @stderr
%15 = load i32, i32* @errno
%16 = load i32, i32* @errno
%17 = call i8* @strerror(i32 %16)
%18 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %15, i8* %17)
br label %6
19: ; preds = %11
%20 = load i32, i32* %5
%21 = add nsw i32 %20, 1
store i32 %21, i32* %5
%22 = icmp slt i32 %20, 3
br i1 %22, label %23, label %29
23: ; preds = %19
%24 = load i32, i32* @stderr
%25 = load i32, i32* @errno
%26 = load i32, i32* @errno
%27 = call i8* @strerror(i32 %26)
%28 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %25, i8* %27)
br label %32
29: ; preds = %19
%30 = load i32, i32* @stderr
%31 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i32 3)
store i32 -1, i32* %2
br label %37
32: ; preds = %23
br label %33
33: ; preds = %32
br label %36
34: ; preds = %6
%35 = load i32, i32* %4
store i32 %35, i32* %2
br label %37
36: ; preds = %33
br label %6
37: ; preds = %34, %29
%38 = load i32, i32* %2
ret i32 %38
}

LLVM IR (-O2):
@errno = global i32 0
@stderr = global i32 0
@.str = [9 x i8] c"(%d) %s\0A\00"
@.str.1 = [41 x i8] c"too many accept() errors (maximum is %d)\00"
define i32 @SK_accept_connection(i32 %0) {
%2 = tail call i32 @accept(i32 %0, i32* null, i32* null)
%3 = icmp slt i32 %2, 0
br i1 %3, label %4, label %49
4: ; preds = %1, %13
%5 = load i32, i32* @errno
%6 = and i32 %5, -2
%7 = icmp eq i32 %6, 128
%8 = load i32, i32* @stderr
%9 = tail call i8* @strerror(i32 %5)
%10 = tail call i32 (i32, i8*, i32, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %5, i8* %9)
%11 = tail call i32 @accept(i32 %0, i32* null, i32* null)
%12 = icmp slt i32 %11, 0
br i1 %7, label %13, label %14
13: ; preds = %4
br i1 %12, label %4, label %49
14: ; preds = %4
br i1 %12, label %15, label %49
15: ; preds = %14, %25
%16 = load i32, i32* @errno
%17 = and i32 %16, -2
%18 = icmp eq i32 %17, 128
%19 = load i32, i32* @stderr
%20 = tail call i8* @strerror(i32 %16)
%21 = tail call i32 (i32, i8*, i32, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %16, i8* %20)
%22 = tail call i32 @accept(i32 %0, i32* null, i32* null)
%23 = icmp slt i32 %22, 0
br i1 %18, label %25, label %24
24: ; preds = %15
br i1 %23, label %26, label %49
25: ; preds = %15
br i1 %23, label %15, label %49
26: ; preds = %24, %36
%27 = load i32, i32* @errno
%28 = and i32 %27, -2
%29 = icmp eq i32 %28, 128
%30 = load i32, i32* @stderr
%31 = tail call i8* @strerror(i32 %27)
%32 = tail call i32 (i32, i8*, i32, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %27, i8* %31)
%33 = tail call i32 @accept(i32 %0, i32* null, i32* null)
%34 = icmp slt i32 %33, 0
br i1 %29, label %36, label %35
35: ; preds = %26
br i1 %34, label %37, label %49
36: ; preds = %26
br i1 %34, label %26, label %49
37: ; preds = %35, %42
%38 = load i32, i32* @errno
%39 = and i32 %38, -2
%40 = icmp eq i32 %39, 128
%41 = load i32, i32* @stderr
br i1 %40, label %42, label %47
42: ; preds = %37
%43 = tail call i8* @strerror(i32 %38)
%44 = tail call i32 (i32, i8*, i32, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %38, i8* %43)
%45 = tail call i32 @accept(i32 %0, i32* null, i32* null)
%46 = icmp slt i32 %45, 0
br i1 %46, label %37, label %49
47: ; preds = %37
%48 = tail call i32 (i32, i8*, i32, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i32 3)
br label %49
49: ; preds = %13, %25, %36, %42, %14, %24, %35, %1, %47
%50 = phi i32 [ -1, %47 ], [ %2, %1 ], [ %11, %14 ], [ %22, %24 ], [ %33, %35 ], [ %45, %42 ], [ %33, %36 ], [ %22, %25 ], [ %11, %13 ]
ret i32 %50
}