#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;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ sa;

/* Variables and functions */
scalar_t__ EINTR;
int accept(int, struct sockaddr_in *, int *);
scalar_t__ errno;
int /*<<< orphan*/ inet_ntoa(int /*<<< orphan*/);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ set_error(char *, int, char *, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(char *, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(scalar_t__);
int tcp_accept(int server_fd, char *ip, int *port, char *ebuf, int ebuf_sz) {
  int fd = -1;
  struct sockaddr_in sa;
  socklen_t salen = sizeof(sa);
  while (1) {
    fd = accept(server_fd, &sa, &salen);
    if (fd == -1) {
      if (errno == EINTR) {
        continue;
      } else {
        set_error(ebuf, ebuf_sz, "Accept error: %s", strerror(errno));
        return -1;
      }
    }
    break;
  }
  if (ip != NULL) {
    strcpy(ip, inet_ntoa(sa.sin_addr));
  }
  if (port != NULL) {
    *port = ntohs(sa.sin_port);
  }
  return fd;
}


x86 assembly (clang, -O0):
<tcp_accept>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
movl   $0xffffffff,-0x28(%rbp)
movl   $0x8,-0x34(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
lea    -0x34(%rbp),%rdx
call   1070 <accept@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    1203 <tcp_accept+0xa3>
mov    0x2e33(%rip),%rax
mov    (%rax),%rax
mov    0x2e21(%rip),%rcx
cmp    (%rcx),%rax
jne    11c5 <tcp_accept+0x65>
jmp    1189 <tcp_accept+0x29>
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    0x2e06(%rip),%rax
mov    (%rax),%rdi
call   1080 <strerror@plt>
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%esi
mov    %eax,%ecx
lea    0xe0e(%rip),%rdx
call   1050 <set_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    124f <tcp_accept+0xef>
jmp    1208 <tcp_accept+0xa8>
cmpq   $0x0,-0x10(%rbp)
je     122e <tcp_accept+0xce>
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x2c(%rbp),%edi
call   1040 <inet_ntoa@plt>
mov    -0x48(%rbp),%rdi
mov    %eax,%esi
call   1030 <strcpy@plt>
cmpq   $0x0,-0x18(%rbp)
je     1249 <tcp_accept+0xe9>
mov    -0x30(%rbp),%edi
call   1060 <ntohs@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcp_accept>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r8d,(%rsp)
mov    %rcx,0x10(%rsp)
mov    %rdx,0x20(%rsp)
mov    %rsi,0x18(%rsp)
mov    %edi,%ebp
movl   $0x8,0x4(%rsp)
lea    0x8(%rsp),%r14
lea    0x4(%rsp),%r12
mov    0x2e44(%rip),%r13
mov    0x2e35(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %ebp,%edi
mov    %r14,%rsi
mov    %r12,%rdx
call   1070 <accept@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    11ea <tcp_accept+0x8a>
mov    0x0(%r13),%rdi
cmp    (%r15),%rdi
je     11b0 <tcp_accept+0x50>
call   1080 <strerror@plt>
lea    0xe27(%rip),%rdx
mov    0x10(%rsp),%rdi
mov    (%rsp),%esi
mov    %eax,%ecx
call   1050 <set_error@plt>
jmp    121d <tcp_accept+0xbd>
mov    0x18(%rsp),%rbp
test   %rbp,%rbp
je     1207 <tcp_accept+0xa7>
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     121d <tcp_accept+0xbd>
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 }
@errno = global i64 0
@EINTR = global i64 0
@.str = [17 x i8] c"Accept error: %s\00"
define i32 @tcp_accept(i32 %0, i8* %1, i32* %2, i8* %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32*
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
%13 = alloca %struct.sockaddr_in
%14 = alloca i32
store i32 %0, i32* %7
store i8* %1, i8** %8
store i32* %2, i32** %9
store i8* %3, i8** %10
store i32 %4, i32* %11
store i32 -1, i32* %12
store i32 8, i32* %14
br label %15
15: ; preds = %5, %24
%16 = load i32, i32* %7
%17 = call i32 @accept(i32 %16, %struct.sockaddr_in* %13, i32* %14)
store i32 %17, i32* %12
%18 = load i32, i32* %12
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %31
20: ; preds = %15
%21 = load i64, i64* @errno
%22 = load i64, i64* @EINTR
%23 = icmp eq i64 %21, %22
br i1 %23, label %24, label %25
24: ; preds = %20
br label %15
25: ; preds = %20
%26 = load i8*, i8** %10
%27 = load i32, i32* %11
%28 = load i64, i64* @errno
%29 = call i32 @strerror(i64 %28)
%30 = call i32 @set_error(i8* %26, i32 %27, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %29)
store i32 -1, i32* %6
br label %51
31: ; preds = %15
br label %32
32: ; preds = %31
%33 = load i8*, i8** %8
%34 = icmp ne i8* %33, null
br i1 %34, label %35, label %41
35: ; preds = %32
%36 = load i8*, i8** %8
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %13, i32 0, i32 1
%38 = load i32, i32* %37
%39 = call i32 @inet_ntoa(i32 %38)
%40 = call i32 @strcpy(i8* %36, i32 %39)
br label %41
41: ; preds = %35, %32
%42 = load i32*, i32** %9
%43 = icmp ne i32* %42, null
br i1 %43, label %44, label %49
44: ; preds = %41
%45 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %13, i32 0, i32 0
%46 = load i32, i32* %45
%47 = call i32 @ntohs(i32 %46)
%48 = load i32*, i32** %9
store i32 %47, i32* %48
br label %49
49: ; preds = %44, %41
%50 = load i32, i32* %12
store i32 %50, i32* %6
br label %51
51: ; preds = %49, %25
%52 = load i32, i32* %6
ret i32 %52
}

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