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

/* Variables and functions */
scalar_t__ EINTR;
int accept(int, struct sockaddr *, unsigned int *);
int /*<<< orphan*/ anetSetError(char *, char *, int /*<<< orphan*/);
scalar_t__ errno;
int /*<<< orphan*/ inet_ntoa(int /*<<< orphan*/);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(char *, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(scalar_t__);
int anetAccept(char *err, int serversock, char *ip, int *port) {
  int fd;
  struct sockaddr_in sa;
  unsigned int saLen;
  while (1) {
    saLen = sizeof(sa);
    fd = accept(serversock, (struct sockaddr *)&sa, &saLen);
    if (fd == -1) {
      if (errno == EINTR)
        continue;
      else {
        anetSetError(err, "accept: %s\n", strerror(errno));
        return -1;
      }
    }
    break;
  }
  if (ip)
    strcpy(ip, inet_ntoa(sa.sin_addr));
  if (port)
    *port = ntohs(sa.sin_port);
  return fd;
}


x86 assembly (clang, -O0):
<anetAccept>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movl   $0x8,-0x3c(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x38(%rbp),%rsi
lea    -0x3c(%rbp),%rdx
call   1070 <accept@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    11ef <anetAccept+0x8f>
mov    0x2e3e(%rip),%rax
mov    (%rax),%rax
mov    0x2e2c(%rip),%rcx
cmp    (%rcx),%rax
jne    11ba <anetAccept+0x5a>
jmp    1177 <anetAccept+0x17>
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    0x2e17(%rip),%rax
mov    (%rax),%rdi
call   1080 <strerror@plt>
mov    -0x48(%rbp),%rdi
mov    %eax,%edx
lea    0xe22(%rip),%rsi
call   1050 <anetSetError@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    123b <anetAccept+0xdb>
jmp    11f4 <anetAccept+0x94>
cmpq   $0x0,-0x20(%rbp)
je     121a <anetAccept+0xba>
mov    -0x20(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x34(%rbp),%edi
call   1040 <inet_ntoa@plt>
mov    -0x50(%rbp),%rdi
mov    %eax,%esi
call   1030 <strcpy@plt>
cmpq   $0x0,-0x28(%rbp)
je     1235 <anetAccept+0xd5>
mov    -0x38(%rbp),%edi
call   1060 <ntohs@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<anetAccept>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rcx,0x20(%rsp)
mov    %rdx,0x18(%rsp)
mov    %esi,%ebp
mov    %rdi,0x10(%rsp)
lea    0x8(%rsp),%r13
lea    0x4(%rsp),%r14
mov    0x2e50(%rip),%r12
mov    0x2e41(%rip),%r15
nopw   0x0(%rax,%rax,1)
movl   $0x8,0x4(%rsp)
mov    %ebp,%edi
mov    %r13,%rsi
mov    %r14,%rdx
call   1070 <accept@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    11df <anetAccept+0x7f>
mov    (%r12),%rdi
cmp    (%r15),%rdi
je     11a0 <anetAccept+0x40>
call   1080 <strerror@plt>
lea    0xe2f(%rip),%rsi
mov    0x10(%rsp),%rdi
mov    %eax,%edx
call   1050 <anetSetError@plt>
jmp    1212 <anetAccept+0xb2>
mov    0x18(%rsp),%rbp
test   %rbp,%rbp
je     11fc <anetAccept+0x9c>
mov    0xc(%rsp),%edi
call   1040 <inet_ntoa@plt>
mov    %rbp,%rdi
mov    %eax,%esi
call   1030 <strcpy@plt>
mov    0x20(%rsp),%rbp
test   %rbp,%rbp
je     1212 <anetAccept+0xb2>
mov    0x8(%rsp),%edi
call   1060 <ntohs@plt>
mov    %eax,0x0(%rbp)
mov    %ebx,%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 i64 0
@EINTR = global i64 0
@.str = [12 x i8] c"accept: %s\0A\00"
define i32 @anetAccept(i8* %0, i32 %1, i8* %2, i32* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32*
%10 = alloca i32
%11 = alloca %struct.sockaddr_in
%12 = alloca i32
store i8* %0, i8** %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i32* %3, i32** %9
br label %13
13: ; preds = %4, %23
store i32 8, i32* %12
%14 = load i32, i32* %7
%15 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%16 = call i32 @accept(i32 %14, %struct.sockaddr* %15, i32* %12)
store i32 %16, i32* %10
%17 = load i32, i32* %10
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %29
19: ; preds = %13
%20 = load i64, i64* @errno
%21 = load i64, i64* @EINTR
%22 = icmp eq i64 %20, %21
br i1 %22, label %23, label %24
23: ; preds = %19
br label %13
24: ; preds = %19
%25 = load i8*, i8** %6
%26 = load i64, i64* @errno
%27 = call i32 @strerror(i64 %26)
%28 = call i32 @anetSetError(i8* %25, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %27)
store i32 -1, i32* %5
br label %49
29: ; preds = %13
br label %30
30: ; preds = %29
%31 = load i8*, i8** %8
%32 = icmp ne i8* %31, null
br i1 %32, label %33, label %39
33: ; preds = %30
%34 = load i8*, i8** %8
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 1
%36 = load i32, i32* %35
%37 = call i32 @inet_ntoa(i32 %36)
%38 = call i32 @strcpy(i8* %34, i32 %37)
br label %39
39: ; preds = %33, %30
%40 = load i32*, i32** %9
%41 = icmp ne i32* %40, null
br i1 %41, label %42, label %47
42: ; preds = %39
%43 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
%44 = load i32, i32* %43
%45 = call i32 @ntohs(i32 %44)
%46 = load i32*, i32** %9
store i32 %45, i32* %46
br label %47
47: ; preds = %42, %39
%48 = load i32, i32* %10
store i32 %48, i32* %5
br label %49
49: ; preds = %47, %24
%50 = load i32, i32* %5
ret i32 %50
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@errno = global i64 0
@EINTR = global i64 0
@.str = [12 x i8] c"accept: %s\0A\00"
define i32 @anetAccept(i8* %0, i32 %1, i8* %2, i32* writeonly %3) {
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
%7 = bitcast %struct.sockaddr_in* %5 to i8*
%8 = bitcast i32* %6 to i8*
%9 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
br label %10
10: ; preds = %13, %4
store i32 8, i32* %6
%11 = call i32 @accept(i32 %1, %struct.sockaddr* %9, i32* %6)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %20
13: ; preds = %10
%14 = load i64, i64* @errno
%15 = load i64, i64* @EINTR
%16 = icmp eq i64 %14, %15
br i1 %16, label %10, label %17
17: ; preds = %13
%18 = call i32 @strerror(i64 %14)
%19 = call i32 @anetSetError(i8* %0, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %18)
br label %33
20: ; preds = %10
%21 = icmp eq i8* %2, null
br i1 %21, label %27, label %22
22: ; preds = %20
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1
%24 = load i32, i32* %23
%25 = call i32 @inet_ntoa(i32 %24)
%26 = call i32 @strcpy(i8* %2, i32 %25)
br label %27
27: ; preds = %22, %20
%28 = icmp eq i32* %3, null
br i1 %28, label %33, label %29
29: ; preds = %27
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0
%31 = load i32, i32* %30
%32 = call i32 @ntohs(i32 %31)
store i32 %32, i32* %3
br label %33
33: ; preds = %27, %29, %17
ret i32 %11
}