#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 */
typedef int /*<<< orphan*/ tmp;
struct sockaddr_storage {
  int dummy;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;

/* Variables and functions */
scalar_t__ EINTR;
int accept(int, struct sockaddr *, int *);
scalar_t__ errno;
int SockAPI_TCPAccept(int ld, struct sockaddr_storage *dst) {
  if (ld == -1)
    return -1;
  struct sockaddr_storage tmp;
  if (!dst)
    dst = &tmp;
  int sd = -1;
  while (1) {
    socklen_t len = sizeof(tmp);
    sd = accept(ld, (struct sockaddr *)dst, &len);
    if (sd >= 0)
      break;
    if (sd == -1) {
      if (errno == EINTR)
        continue;
      else
        return -1;
    }
  }
  return sd;
}


x86 assembly (clang, -O0):
<SockAPI_TCPAccept>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1135 <SockAPI_TCPAccept+0x25>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b8 <SockAPI_TCPAccept+0xa8>
cmpq   $0x0,-0x10(%rbp)
jne    1148 <SockAPI_TCPAccept+0x38>
lea    -0x18(%rbp),%rax
mov    %rax,-0x10(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
movl   $0x4,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   1030 <accept@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jl     1178 <SockAPI_TCPAccept+0x68>
jmp    11b2 <SockAPI_TCPAccept+0xa2>
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11ad <SockAPI_TCPAccept+0x9d>
mov    0x2e57(%rip),%rax
mov    (%rax),%rax
mov    0x2e45(%rip),%rcx
cmp    (%rcx),%rax
jne    11a1 <SockAPI_TCPAccept+0x91>
jmp    114f <SockAPI_TCPAccept+0x3f>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b8 <SockAPI_TCPAccept+0xa8>
jmp    114f <SockAPI_TCPAccept+0x3f>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<SockAPI_TCPAccept>:
cmp    $0xffffffff,%edi
je     1155 <SockAPI_TCPAccept+0x45>
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebp
test   %rsi,%rsi
lea    0x8(%rsp),%rbx
cmovne %rsi,%rbx
movl   $0x4,0x4(%rsp)
lea    0x4(%rsp),%rdx
mov    %rbx,%rsi
call   1030 <accept@plt>
test   %eax,%eax
js     115b <SockAPI_TCPAccept+0x4b>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xffffffff,%eax
ret
mov    0x2e7e(%rip),%r15
mov    0x2e6f(%rip),%r12
lea    0x4(%rsp),%r14
jmp    1189 <SockAPI_TCPAccept+0x79>
movl   $0x4,0x4(%rsp)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1030 <accept@plt>
test   %eax,%eax
jns    1148 <SockAPI_TCPAccept+0x38>
cmp    $0xffffffff,%eax
jne    1170 <SockAPI_TCPAccept+0x60>
mov    (%r15),%rax
cmp    (%r12),%rax
je     1170 <SockAPI_TCPAccept+0x60>
mov    $0xffffffff,%eax
jmp    1148 <SockAPI_TCPAccept+0x38>

LLVM IR (-O0):
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
@errno = global i64 0
@EINTR = global i64 0
define i32 @SockAPI_TCPAccept(i32 %0, %struct.sockaddr_storage* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_storage*
%6 = alloca %struct.sockaddr_storage
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store %struct.sockaddr_storage* %1, %struct.sockaddr_storage** %5
%9 = load i32, i32* %4
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %12
11: ; preds = %2
store i32 -1, i32* %3
br label %37
12: ; preds = %2
%13 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %5
%14 = icmp ne %struct.sockaddr_storage* %13, null
br i1 %14, label %16, label %15
15: ; preds = %12
store %struct.sockaddr_storage* %6, %struct.sockaddr_storage** %5
br label %16
16: ; preds = %15, %12
store i32 -1, i32* %7
br label %17
17: ; preds = %16, %32, %34
store i32 4, i32* %8
%18 = load i32, i32* %4
%19 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %5
%20 = bitcast %struct.sockaddr_storage* %19 to %struct.sockaddr*
%21 = call i32 @accept(i32 %18, %struct.sockaddr* %20, i32* %8)
store i32 %21, i32* %7
%22 = load i32, i32* %7
%23 = icmp sge i32 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %17
br label %35
25: ; preds = %17
%26 = load i32, i32* %7
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %34
28: ; preds = %25
%29 = load i64, i64* @errno
%30 = load i64, i64* @EINTR
%31 = icmp eq i64 %29, %30
br i1 %31, label %32, label %33
32: ; preds = %28
br label %17
33: ; preds = %28
store i32 -1, i32* %3
br label %37
34: ; preds = %25
br label %17
35: ; preds = %24
%36 = load i32, i32* %7
store i32 %36, i32* %3
br label %37
37: ; preds = %35, %33, %11
%38 = load i32, i32* %3
ret i32 %38
}

LLVM IR (-O2):
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
@errno = global i64 0
@EINTR = global i64 0
define i32 @SockAPI_TCPAccept(i32 %0, %struct.sockaddr_storage* %1) {
%3 = alloca %struct.sockaddr_storage
%4 = alloca i32
%5 = icmp eq i32 %0, -1
br i1 %5, label %29, label %6
6: ; preds = %2
%7 = bitcast %struct.sockaddr_storage* %3 to i8*
%8 = icmp eq %struct.sockaddr_storage* %1, null
%9 = select i1 %8, %struct.sockaddr_storage* %3, %struct.sockaddr_storage* %1
%10 = bitcast i32* %4 to i8*
%11 = bitcast %struct.sockaddr_storage* %9 to %struct.sockaddr*
store i32 4, i32* %4
%12 = call i32 @accept(i32 %0, %struct.sockaddr* %11, i32* %4)
%13 = icmp sgt i32 %12, -1
br i1 %13, label %25, label %14
14: ; preds = %6, %22
%15 = phi i32 [ %23, %22 ], [ %12, %6 ]
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %22
17: ; preds = %14
%18 = load i64, i64* @errno
%19 = load i64, i64* @EINTR
%20 = icmp eq i64 %18, %19
br i1 %20, label %22, label %21
21: ; preds = %17
br label %27
22: ; preds = %17, %14
store i32 4, i32* %4
%23 = call i32 @accept(i32 %0, %struct.sockaddr* %11, i32* %4)
%24 = icmp sgt i32 %23, -1
br i1 %24, label %25, label %14
25: ; preds = %22, %6
%26 = phi i32 [ %12, %6 ], [ %23, %22 ]
br label %27
27: ; preds = %21, %25
%28 = phi i32 [ -1, %21 ], [ %26, %25 ]
br label %29
29: ; preds = %2, %27
%30 = phi i32 [ %28, %27 ], [ -1, %2 ]
ret i32 %30
}