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

/* Variables and functions */
int EAGAIN;
int ECONNABORTED;
int EINTR;
int accept(int, struct sockaddr *, int /*<<< orphan*/ *);
int errno;
int /*<<< orphan*/ inet_ntoa(int /*<<< orphan*/);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(char *, int /*<<< orphan*/);
int /*<<< orphan*/ x_set_sock_no_delay(int);
int x_accept(int listen_fd, char *ip, int *port) {
  int fd = -1;
  struct sockaddr_in remote;
  int len = 0;
ReAccept:
  len = sizeof(struct sockaddr_in);
  fd = accept(listen_fd, (struct sockaddr *)&remote, (socklen_t *)&len);
  if (fd == -1) {
    int err = errno;
    if (err == EINTR || err == EAGAIN) {
      goto ReAccept;
    }
    if (err == ECONNABORTED) {
      goto ReAccept;
    }
    return -1;
  }
  x_set_sock_no_delay(fd);
  if (port != NULL) {
    *port = ntohs(remote.sin_port);
  }
  if (ip != NULL) {
    strcpy(ip, inet_ntoa(remote.sin_addr));
  }
  return fd;
}


x86 assembly (clang, -O0):
<x_accept>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
movl   $0x0,-0x2c(%rbp)
movl   $0x8,-0x2c(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
lea    -0x2c(%rbp),%rdx
call   1070 <accept@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11ed <x_accept+0x9d>
mov    0x2e44(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%eax
mov    0x2e2d(%rip),%rcx
cmp    (%rcx),%eax
je     11c5 <x_accept+0x75>
mov    -0x30(%rbp),%eax
mov    0x2e03(%rip),%rcx
cmp    (%rcx),%eax
jne    11ca <x_accept+0x7a>
jmp    1171 <x_accept+0x21>
mov    -0x30(%rbp),%eax
mov    0x2dfc(%rip),%rcx
cmp    (%rcx),%eax
jne    11e1 <x_accept+0x91>
jmp    1171 <x_accept+0x21>
movl   $0xffffffff,-0x4(%rbp)
jmp    123c <x_accept+0xec>
mov    -0x1c(%rbp),%edi
call   1050 <x_set_sock_no_delay@plt>
cmpq   $0x0,-0x18(%rbp)
je     1210 <x_accept+0xc0>
mov    -0x24(%rbp),%edi
call   1060 <ntohs@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
cmpq   $0x0,-0x10(%rbp)
je     1236 <x_accept+0xe6>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%edi
call   1040 <inet_ntoa@plt>
mov    -0x38(%rbp),%rdi
mov    %eax,%esi
call   1030 <strcpy@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<x_accept>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdx,0x20(%rsp)
mov    %rsi,0x10(%rsp)
mov    %edi,%ebx
lea    0x18(%rsp),%r12
lea    0xc(%rsp),%r13
mov    0x2e65(%rip),%r14
mov    0x2e56(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movl   $0x8,0xc(%rsp)
mov    %ebx,%edi
mov    %r12,%rsi
mov    %r13,%rdx
call   1070 <accept@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    11cc <x_accept+0x7c>
mov    (%r14),%eax
cmp    (%r15),%eax
je     1190 <x_accept+0x40>
mov    0x2e05(%rip),%rcx
cmp    (%rcx),%eax
je     1190 <x_accept+0x40>
mov    0x2e0a(%rip),%rcx
cmp    (%rcx),%eax
je     1190 <x_accept+0x40>
jmp    1205 <x_accept+0xb5>
mov    %ebp,%edi
call   1050 <x_set_sock_no_delay@plt>
mov    0x20(%rsp),%rbx
test   %rbx,%rbx
je     11e8 <x_accept+0x98>
mov    0x1c(%rsp),%edi
call   1060 <ntohs@plt>
mov    %eax,(%rbx)
cmpq   $0x0,0x10(%rsp)
je     1205 <x_accept+0xb5>
mov    0x18(%rsp),%edi
call   1040 <inet_ntoa@plt>
mov    0x10(%rsp),%rdi
mov    %eax,%esi
call   1030 <strcpy@plt>
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@errno = global i32 0
@EINTR = global i32 0
@EAGAIN = global i32 0
@ECONNABORTED = global i32 0
define i32 @x_accept(i32 %0, i8* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca %struct.sockaddr_in
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32* %2, i32** %7
store i32 -1, i32* %8
store i32 0, i32* %10
br label %12
12: ; preds = %32, %27, %3
store i32 8, i32* %10
%13 = load i32, i32* %5
%14 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%15 = call i32 @accept(i32 %13, %struct.sockaddr* %14, i32* %10)
store i32 %15, i32* %8
%16 = load i32, i32* %8
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %34
18: ; preds = %12
%19 = load i32, i32* @errno
store i32 %19, i32* %11
%20 = load i32, i32* %11
%21 = load i32, i32* @EINTR
%22 = icmp eq i32 %20, %21
br i1 %22, label %27, label %23
23: ; preds = %18
%24 = load i32, i32* %11
%25 = load i32, i32* @EAGAIN
%26 = icmp eq i32 %24, %25
br i1 %26, label %27, label %28
27: ; preds = %23, %18
br label %12
28: ; preds = %23
%29 = load i32, i32* %11
%30 = load i32, i32* @ECONNABORTED
%31 = icmp eq i32 %29, %30
br i1 %31, label %32, label %33
32: ; preds = %28
br label %12
33: ; preds = %28
store i32 -1, i32* %4
br label %55
34: ; preds = %12
%35 = load i32, i32* %8
%36 = call i32 @x_set_sock_no_delay(i32 %35)
%37 = load i32*, i32** %7
%38 = icmp ne i32* %37, null
br i1 %38, label %39, label %44
39: ; preds = %34
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
%41 = load i32, i32* %40
%42 = call i32 @ntohs(i32 %41)
%43 = load i32*, i32** %7
store i32 %42, i32* %43
br label %44
44: ; preds = %39, %34
%45 = load i8*, i8** %6
%46 = icmp ne i8* %45, null
br i1 %46, label %47, label %53
47: ; preds = %44
%48 = load i8*, i8** %6
%49 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
%50 = load i32, i32* %49
%51 = call i32 @inet_ntoa(i32 %50)
%52 = call i32 @strcpy(i8* %48, i32 %51)
br label %53
53: ; preds = %47, %44
%54 = load i32, i32* %8
store i32 %54, i32* %4
br label %55
55: ; preds = %53, %33
%56 = load i32, i32* %4
ret i32 %56
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@errno = global i32 0
@EINTR = global i32 0
@EAGAIN = global i32 0
@ECONNABORTED = global i32 0
define i32 @x_accept(i32 %0, i8* %1, i32* writeonly %2) {
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = bitcast %struct.sockaddr_in* %4 to i8*
%7 = bitcast i32* %5 to i8*
%8 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
br label %9
9: ; preds = %12, %3
store i32 8, i32* %5
%10 = call i32 @accept(i32 %0, %struct.sockaddr* %8, i32* %5)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %22
12: ; preds = %9
%13 = load i32, i32* @errno
%14 = load i32, i32* @EINTR
%15 = icmp eq i32 %13, %14
%16 = load i32, i32* @EAGAIN
%17 = icmp eq i32 %13, %16
%18 = select i1 %15, i1 true, i1 %17
%19 = load i32, i32* @ECONNABORTED
%20 = icmp eq i32 %13, %19
%21 = select i1 %18, i1 true, i1 %20
br i1 %21, label %9, label %36
22: ; preds = %9
%23 = call i32 @x_set_sock_no_delay(i32 %10)
%24 = icmp eq i32* %2, null
br i1 %24, label %29, label %25
25: ; preds = %22
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
%27 = load i32, i32* %26
%28 = call i32 @ntohs(i32 %27)
store i32 %28, i32* %2
br label %29
29: ; preds = %25, %22
%30 = icmp eq i8* %1, null
br i1 %30, label %36, label %31
31: ; preds = %29
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
%33 = load i32, i32* %32
%34 = call i32 @inet_ntoa(i32 %33)
%35 = call i32 @strcpy(i8* %1, i32 %34)
br label %36
36: ; preds = %12, %29, %31
ret i32 %10
}