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

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EWOULDBLOCK;
int NI_MAXHOST;
int NI_MAXSERV;
int accept(int, struct sockaddr *, int *);
scalar_t__ errno;
int getnameinfo(struct sockaddr *, int, char *, int, char *, int,
                int /*<<< orphan*/);
int make_socket_non_blocking(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *, char *);
int process_new_connection(int sfd) {
  char host[NI_MAXHOST];
  char service[NI_MAXSERV];
  struct sockaddr_in cli_addr;
  socklen_t socklen;
  int clifd, ret;
  socklen = sizeof(cli_addr);
  clifd = accept(sfd, (struct sockaddr *)&cli_addr, &socklen);
  if (clifd == -1) {
    if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) {
      return -1;
    } else {
      perror("accept() failed");
      return -1;
    }
  }
  ret = getnameinfo((struct sockaddr *)&cli_addr, socklen, host, NI_MAXHOST,
                    service, NI_MAXSERV, 0);
  if (ret == 0)
    printf("Accepted connection from: %s (%s)\n", host, service);
  if (make_socket_non_blocking(clifd) == -1)
    return -1;
  return clifd;
}


x86 assembly (clang, -O0):
<process_new_connection>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e66(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x10(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x18(%rbp)
mov    0x2e5a(%rip),%rax
mov    (%rax),%eax
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
movl   $0x4,-0x2c(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
lea    -0x2c(%rbp),%rdx
call   1070 <accept@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0xffffffff,-0x30(%rbp)
jne    1236 <process_new_connection+0xe6>
mov    0x2e01(%rip),%rax
mov    (%rax),%rax
mov    0x2dd7(%rip),%rcx
cmp    (%rcx),%rax
je     1204 <process_new_connection+0xb4>
mov    0x2de7(%rip),%rax
mov    (%rax),%rax
mov    0x2dd5(%rip),%rcx
cmp    (%rcx),%rax
jne    1217 <process_new_connection+0xc7>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x38(%rbp)
jmp    12c0 <process_new_connection+0x170>
lea    0xde2(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x38(%rbp)
jmp    12c0 <process_new_connection+0x170>
mov    -0x40(%rbp),%r8
mov    -0x48(%rbp),%rdx
lea    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%esi
mov    0x2d7c(%rip),%rax
mov    (%rax),%ecx
mov    0x2d93(%rip),%rax
mov    (%rax),%r9d
xor    %eax,%eax
sub    $0x10,%rsp
movl   $0x0,(%rsp)
call   1050 <getnameinfo@plt>
add    $0x10,%rsp
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jne    128f <process_new_connection+0x13f>
mov    -0x40(%rbp),%rdx
mov    -0x48(%rbp),%rsi
lea    0xd86(%rip),%rdi
call   1040 <printf@plt>
mov    -0x30(%rbp),%edi
call   1030 <make_socket_non_blocking@plt>
cmp    $0xffffffff,%eax
jne    12b3 <process_new_connection+0x163>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x38(%rbp)
jmp    12c0 <process_new_connection+0x170>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<process_new_connection>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e63(%rip),%r12
mov    (%r12),%eax
mov    %rsp,%r15
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
mov    0x2e67(%rip),%r13
mov    0x0(%r13),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
movl   $0x4,-0x2c(%rbp)
lea    -0x30(%rbp),%rsi
lea    -0x2c(%rbp),%rdx
call   1070 <accept@plt>
cmp    $0xffffffff,%eax
je     11fd <process_new_connection+0xad>
mov    %eax,%r14d
mov    -0x2c(%rbp),%esi
mov    (%r12),%ecx
mov    0x0(%r13),%r9d
sub    $0x8,%rsp
lea    -0x30(%rbp),%rdi
mov    %r15,%rdx
mov    %rbx,%r8
push   $0x0
call   1050 <getnameinfo@plt>
add    $0x10,%rsp
test   %eax,%eax
jne    11ec <process_new_connection+0x9c>
lea    0xe2f(%rip),%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1040 <printf@plt>
mov    %r14d,%edi
call   1030 <make_socket_non_blocking@plt>
cmp    $0xffffffff,%eax
cmove  %eax,%r14d
jmp    1225 <process_new_connection+0xd5>
mov    0x2dd4(%rip),%rax
mov    (%rax),%rax
mov    0x2daa(%rip),%rcx
mov    $0xffffffff,%r14d
cmp    (%rcx),%rax
je     1225 <process_new_connection+0xd5>
mov    0x2db0(%rip),%rcx
cmp    (%rcx),%rax
jne    1237 <process_new_connection+0xe7>
mov    %r14d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdc2(%rip),%rdi
call   1060 <perror@plt>
jmp    1225 <process_new_connection+0xd5>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@NI_MAXHOST = global i32 0
@NI_MAXSERV = global i32 0
@errno = global i64 0
@EAGAIN = global i64 0
@EWOULDBLOCK = global i64 0
@.str = [16 x i8] c"accept() failed\00"
@.str.1 = [35 x i8] c"Accepted connection from: %s (%s)\0A\00"
define i32 @process_new_connection(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
%7 = alloca %struct.sockaddr_in
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %3
%12 = load i32, i32* @NI_MAXHOST
%13 = zext i32 %12 to i64
%14 = call i8* @llvm.stacksave()
store i8* %14, i8** %4
%15 = alloca i8, i64 %13
store i64 %13, i64* %5
%16 = load i32, i32* @NI_MAXSERV
%17 = zext i32 %16 to i64
%18 = alloca i8, i64 %17
store i64 %17, i64* %6
store i32 4, i32* %8
%19 = load i32, i32* %3
%20 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%21 = call i32 @accept(i32 %19, %struct.sockaddr* %20, i32* %8)
store i32 %21, i32* %9
%22 = load i32, i32* %9
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %35
24: ; preds = %1
%25 = load i64, i64* @errno
%26 = load i64, i64* @EAGAIN
%27 = icmp eq i64 %25, %26
br i1 %27, label %32, label %28
28: ; preds = %24
%29 = load i64, i64* @errno
%30 = load i64, i64* @EWOULDBLOCK
%31 = icmp eq i64 %29, %30
br i1 %31, label %32, label %33
32: ; preds = %28, %24
store i32 -1, i32* %2
store i32 1, i32* %11
br label %52
33: ; preds = %28
%34 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
store i32 1, i32* %11
br label %52
35: ; preds = %1
%36 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%37 = load i32, i32* %8
%38 = load i32, i32* @NI_MAXHOST
%39 = load i32, i32* @NI_MAXSERV
%40 = call i32 @getnameinfo(%struct.sockaddr* %36, i32 %37, i8* %15, i32 %38, i8* %18, i32 %39, i32 0)
store i32 %40, i32* %10
%41 = load i32, i32* %10
%42 = icmp eq i32 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %35
%44 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %15, i8* %18)
br label %45
45: ; preds = %43, %35
%46 = load i32, i32* %9
%47 = call i32 @make_socket_non_blocking(i32 %46)
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %50
49: ; preds = %45
store i32 -1, i32* %2
store i32 1, i32* %11
br label %52
50: ; preds = %45
%51 = load i32, i32* %9
store i32 %51, i32* %2
store i32 1, i32* %11
br label %52
52: ; preds = %50, %49, %33, %32
%53 = load i8*, i8** %4
%54 = load i32, i32* %2
ret i32 %54
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@NI_MAXHOST = global i32 0
@NI_MAXSERV = global i32 0
@errno = global i64 0
@EAGAIN = global i64 0
@EWOULDBLOCK = global i64 0
@.str = [16 x i8] c"accept() failed\00"
@.str.1 = [35 x i8] c"Accepted connection from: %s (%s)\0A\00"
define i32 @process_new_connection(i32 %0) {
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = load i32, i32* @NI_MAXHOST
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = load i32, i32* @NI_MAXSERV
%8 = zext i32 %7 to i64
%9 = alloca i8, i64 %8
%10 = bitcast %struct.sockaddr_in* %2 to i8*
%11 = bitcast i32* %3 to i8*
store i32 4, i32* %3
%12 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%13 = call i32 @accept(i32 %0, %struct.sockaddr* %12, i32* %3)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %24
15: ; preds = %1
%16 = load i64, i64* @errno
%17 = load i64, i64* @EAGAIN
%18 = icmp eq i64 %16, %17
%19 = load i64, i64* @EWOULDBLOCK
%20 = icmp eq i64 %16, %19
%21 = select i1 %18, i1 true, i1 %20
br i1 %21, label %36, label %22
22: ; preds = %15
%23 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %36
24: ; preds = %1
%25 = load i32, i32* %3
%26 = load i32, i32* @NI_MAXHOST
%27 = load i32, i32* @NI_MAXSERV
%28 = call i32 @getnameinfo(%struct.sockaddr* %12, i32 %25, i8* %6, i32 %26, i8* %9, i32 %27, i32 0)
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %24
%31 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %6, i8* %9)
br label %32
32: ; preds = %30, %24
%33 = call i32 @make_socket_non_blocking(i32 %13)
%34 = icmp eq i32 %33, -1
%35 = select i1 %34, i32 -1, i32 %13
br label %36
36: ; preds = %32, %15, %22
%37 = phi i32 [ -1, %22 ], [ -1, %15 ], [ %35, %32 ]
ret i32 %37
}