#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 {
  int dummy;
};

/* Variables and functions */
scalar_t__ EBADF;
scalar_t__ EINTR;
int accept(int, struct sockaddr *, int /*<<< orphan*/ *);
scalar_t__ errno;
int sock_accept(int sockfd) {
  int acceptfd;
  if (sockfd < 0) {
    errno = EBADF;
    return -1;
  }
  while ((acceptfd = accept(sockfd, (struct sockaddr *)NULL, NULL)) < 0) {
    if (errno == EINTR) {
      continue;
    }
    return -1;
  }
  return acceptfd;
}


x86 assembly (clang, -O0):
<sock_accept>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1145 <sock_accept+0x35>
mov    0x2ea4(%rip),%rax
mov    (%rax),%rcx
mov    0x2eaa(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1196 <sock_accept+0x86>
jmp    114a <sock_accept+0x3a>
mov    -0x8(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   1030 <accept@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    1190 <sock_accept+0x80>
mov    0x2e74(%rip),%rax
mov    (%rax),%rax
mov    0x2e62(%rip),%rcx
cmp    (%rcx),%rax
jne    1184 <sock_accept+0x74>
jmp    114a <sock_accept+0x3a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1196 <sock_accept+0x86>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_accept>:
push   %r15
push   %r14
push   %rbx
test   %edi,%edi
js     1149 <sock_accept+0x39>
mov    %edi,%ebx
mov    0x2ebe(%rip),%r14
mov    0x2eaf(%rip),%r15
nopl   0x0(%rax)
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1030 <accept@plt>
test   %eax,%eax
jns    1162 <sock_accept+0x52>
mov    (%r14),%rax
cmp    (%r15),%rax
je     1130 <sock_accept+0x20>
jmp    115d <sock_accept+0x4d>
mov    0x2e80(%rip),%rax
mov    (%rax),%rax
mov    0x2e86(%rip),%rcx
mov    %rax,(%rcx)
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
@EBADF = global i64 0
@errno = global i64 0
@EINTR = global i64 0
define i32 @sock_accept(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %3
%5 = load i32, i32* %3
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = load i64, i64* @EBADF
store i64 %8, i64* @errno
store i32 -1, i32* %2
br label %22
9: ; preds = %1
br label %10
10: ; preds = %18, %9
%11 = load i32, i32* %3
%12 = call i32 @accept(i32 %11, %struct.sockaddr* null, i32* null)
store i32 %12, i32* %4
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %20
14: ; preds = %10
%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 %10
19: ; preds = %14
store i32 -1, i32* %2
br label %22
20: ; preds = %10
%21 = load i32, i32* %4
store i32 %21, i32* %2
br label %22
22: ; preds = %20, %19, %7
%23 = load i32, i32* %2
ret i32 %23
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@EBADF = global i64 0
@errno = global i64 0
@EINTR = global i64 0
define i32 @sock_accept(i32 %0) {
%2 = icmp slt i32 %0, 0
br i1 %2, label %3, label %5
3: ; preds = %1
%4 = load i64, i64* @EBADF
store i64 %4, i64* @errno
br label %12
5: ; preds = %1, %8
%6 = tail call i32 @accept(i32 %0, %struct.sockaddr* null, i32* null)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %12
8: ; preds = %5
%9 = load i64, i64* @errno
%10 = load i64, i64* @EINTR
%11 = icmp eq i64 %9, %10
br i1 %11, label %5, label %12
12: ; preds = %5, %8, %3
%13 = phi i32 [ -1, %3 ], [ %6, %5 ], [ -1, %8 ]
ret i32 %13
}