#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 dummy;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EINTR;
int accept(int, struct sockaddr *, int *);
scalar_t__ errno;
int /*<<< orphan*/ fnet_set_recv_timeout(int, int);
int /*<<< orphan*/ memset(struct sockaddr *, int /*<<< orphan*/, int);
int fnet_accept(int listen_fd) {
  struct sockaddr addr;
  socklen_t addrlen = sizeof(struct sockaddr_in);
  memset(&addr, 0, addrlen);
  int sock_fd = -1;
  do {
    sock_fd = accept(listen_fd, (struct sockaddr *)&addr, &addrlen);
    if (sock_fd == -1) {
      if (errno == EINTR)
        continue;
      else if (errno == EAGAIN)
        return -2;
      else
        return -1;
    } else
      break;
  } while (0);
  fnet_set_recv_timeout(sock_fd, 2);
  return sock_fd;
}


x86 assembly (clang, -O0):
<fnet_accept>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x4,-0x14(%rbp)
mov    -0x14(%rbp),%edx
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
call   1030 <memset@plt>
movl   $0xffffffff,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
lea    -0x14(%rbp),%rdx
call   1040 <accept@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11c5 <fnet_accept+0x95>
mov    0x2e65(%rip),%rax
mov    (%rax),%rax
mov    0x2e53(%rip),%rcx
cmp    (%rcx),%rax
jne    1193 <fnet_accept+0x63>
jmp    11ca <fnet_accept+0x9a>
mov    0x2e46(%rip),%rax
mov    (%rax),%rax
mov    0x2e24(%rip),%rcx
cmp    (%rcx),%rax
jne    11b9 <fnet_accept+0x89>
movl   $0xfffffffe,-0x4(%rbp)
jmp    11dd <fnet_accept+0xad>
movl   $0xffffffff,-0x4(%rbp)
jmp    11dd <fnet_accept+0xad>
jmp    11ca <fnet_accept+0x9a>
mov    -0x18(%rbp),%edi
mov    $0x2,%esi
call   1050 <fnet_set_recv_timeout@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fnet_accept>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebx
movl   $0x4,0xc(%rsp)
lea    0x10(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1030 <memset@plt>
lea    0xc(%rsp),%rdx
mov    %ebx,%edi
mov    %r14,%rsi
call   1040 <accept@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    1181 <fnet_accept+0x51>
mov    0x2e6e(%rip),%rax
mov    (%rax),%rax
mov    0x2e5c(%rip),%rcx
cmp    (%rcx),%rax
jne    118f <fnet_accept+0x5f>
mov    %ebx,%edi
mov    $0x2,%esi
call   1050 <fnet_set_recv_timeout@plt>
jmp    11a0 <fnet_accept+0x70>
mov    0x2e32(%rip),%rcx
xor    %ebx,%ebx
cmp    (%rcx),%rax
sete   %bl
not    %ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
define i32 @fnet_accept(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
store i32 4, i32* %5
%7 = load i32, i32* %5
%8 = call i32 @memset(%struct.sockaddr* %4, i32 0, i32 %7)
store i32 -1, i32* %6
br label %9
9: ; preds = %1
%10 = load i32, i32* %3
%11 = call i32 @accept(i32 %10, %struct.sockaddr* %4, i32* %5)
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %25
14: ; preds = %9
%15 = load i64, i64* @errno
%16 = load i64, i64* @EINTR
%17 = icmp eq i64 %15, %16
br i1 %17, label %18, label %19
18: ; preds = %14
br label %26
19: ; preds = %14
%20 = load i64, i64* @errno
%21 = load i64, i64* @EAGAIN
%22 = icmp eq i64 %20, %21
br i1 %22, label %23, label %24
23: ; preds = %19
store i32 -2, i32* %2
br label %30
24: ; preds = %19
store i32 -1, i32* %2
br label %30
25: ; preds = %9
br label %26
26: ; preds = %18, %25
%27 = load i32, i32* %6
%28 = call i32 @fnet_set_recv_timeout(i32 %27, i32 2)
%29 = load i32, i32* %6
store i32 %29, i32* %2
br label %30
30: ; preds = %26, %24, %23
%31 = load i32, i32* %2
ret i32 %31
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
define i32 @fnet_accept(i32 %0) {
%2 = alloca %struct.sockaddr
%3 = alloca i32
%4 = bitcast %struct.sockaddr* %2 to i8*
%5 = bitcast i32* %3 to i8*
store i32 4, i32* %3
%6 = call i32 @memset(%struct.sockaddr* %2, i32 0, i32 4)
%7 = call i32 @accept(i32 %0, %struct.sockaddr* %2, i32* %3)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %17
9: ; preds = %1
%10 = load i64, i64* @errno
%11 = load i64, i64* @EINTR
%12 = icmp eq i64 %10, %11
br i1 %12, label %17, label %13
13: ; preds = %9
%14 = load i64, i64* @EAGAIN
%15 = icmp eq i64 %10, %14
%16 = select i1 %15, i32 -2, i32 -1
br label %19
17: ; preds = %9, %1
%18 = call i32 @fnet_set_recv_timeout(i32 %7, i32 2)
br label %19
19: ; preds = %13, %17
%20 = phi i32 [ %7, %17 ], [ %16, %13 ]
ret i32 %20
}