#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 */
int /*<<< orphan*/ F_SETFL;
int /*<<< orphan*/ O_NONBLOCK;
int accept(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ read(int, char *, int);
int conaccept(int listenfd) {
  int status;
  int connfd;
  if ((connfd = accept(listenfd, NULL, NULL)) < 0)
    return -1;
  do {
    read(connfd, (char *)&status, sizeof(int));
  } while (status != 0);
  fcntl(connfd, F_SETFL, O_NONBLOCK);
  return connfd;
}


x86 assembly (clang, -O0):
<conaccept>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   1050 <accept@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jge    1162 <conaccept+0x32>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a2 <conaccept+0x72>
jmp    1167 <conaccept+0x37>
mov    -0x10(%rbp),%edi
lea    -0xc(%rbp),%rsi
mov    $0x4,%edx
call   1040 <read@plt>
cmpl   $0x0,-0xc(%rbp)
jne    1167 <conaccept+0x37>
mov    -0x10(%rbp),%edi
mov    0x2e4c(%rip),%rax
mov    (%rax),%esi
mov    0x2e53(%rip),%rax
mov    (%rax),%edx
call   1030 <fcntl@plt>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<conaccept>:
push   %r14
push   %rbx
push   %rax
xor    %esi,%esi
xor    %edx,%edx
call   1050 <accept@plt>
test   %eax,%eax
js     1181 <conaccept+0x51>
mov    %eax,%ebx
lea    0x4(%rsp),%r14
nopl   0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   1040 <read@plt>
cmpl   $0x0,0x4(%rsp)
jne    1150 <conaccept+0x20>
mov    0x2e6b(%rip),%rax
mov    (%rax),%esi
mov    0x2e72(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
call   1030 <fcntl@plt>
jmp    1186 <conaccept+0x56>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
define i32 @conaccept(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %3
%6 = load i32, i32* %3
%7 = call i32 @accept(i32 %6, i32* null, i32* null)
store i32 %7, i32* %5
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %10
9: ; preds = %1
store i32 -1, i32* %2
br label %24
10: ; preds = %1
br label %11
11: ; preds = %15, %10
%12 = load i32, i32* %5
%13 = bitcast i32* %4 to i8*
%14 = call i32 @read(i32 %12, i8* %13, i32 4)
br label %15
15: ; preds = %11
%16 = load i32, i32* %4
%17 = icmp ne i32 %16, 0
br i1 %17, label %11, label %18
18: ; preds = %15
%19 = load i32, i32* %5
%20 = load i32, i32* @F_SETFL
%21 = load i32, i32* @O_NONBLOCK
%22 = call i32 @fcntl(i32 %19, i32 %20, i32 %21)
%23 = load i32, i32* %5
store i32 %23, i32* %2
br label %24
24: ; preds = %18, %9
%25 = load i32, i32* %2
ret i32 %25
}

LLVM IR (-O2):
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
define i32 @conaccept(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = tail call i32 @accept(i32 %0, i32* null, i32* null)
%5 = icmp slt i32 %4, 0
br i1 %5, label %14, label %6
6: ; preds = %1, %6
%7 = call i32 @read(i32 %4, i8* %3, i32 4)
%8 = load i32, i32* %2
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %6
10: ; preds = %6
%11 = load i32, i32* @F_SETFL
%12 = load i32, i32* @O_NONBLOCK
%13 = tail call i32 @fcntl(i32 %4, i32 %11, i32 %12)
br label %14
14: ; preds = %1, %10
%15 = phi i32 [ %4, %10 ], [ -1, %1 ]
ret i32 %15
}