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

/* Variables and functions */
int /*<<< orphan*/ F_GETFL;
int O_NONBLOCK;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_KEEPALIVE;
int accept(int, struct sockaddr *, int *);
int fcntl(int, int /*<<< orphan*/);
int /*<<< orphan*/ inet_ntoa(int /*<<< orphan*/);
int /*<<< orphan*/ logDebug(char *, int, int);
int /*<<< orphan*/ logNorm(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ logSysError(char *);
int /*<<< orphan*/ ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int ipAcceptConnection(int serversd) {
  struct sockaddr_in sinRemote;
  socklen_t nAddrSize = sizeof(struct sockaddr_in);
  int fd;
  fd = accept(serversd, (struct sockaddr *)&sinRemote, &nAddrSize);
  if (fd == -1) {
    perror("accept failed");
    return -1;
  }
  logNorm("Accepted connection from %s:%d\n", inet_ntoa(sinRemote.sin_addr),
          ntohs(sinRemote.sin_port));
  if (0) {
    int opt = 1;
    if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &opt, sizeof(opt)) == -1)
      perror("setsockopt(SO_KEEPALIVE) failed");
  }
  int opts = fcntl(fd, F_GETFL);
  if (opts < 0)
    logSysError("fcntl(F_GETFL)");
  logDebug("Socket %d has blocking = %d\n", fd, opts & O_NONBLOCK);
  return fd;
}


x86 assembly (clang, -O0):
<ipAcceptConnection>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x8,-0x14(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
lea    -0x14(%rbp),%rdx
call   1090 <accept@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11c7 <ipAcceptConnection+0x47>
lea    0xe4a(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1236 <ipAcceptConnection+0xb6>
mov    -0xc(%rbp),%edi
call   1040 <inet_ntoa@plt>
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%edi
call   1070 <ntohs@plt>
mov    -0x20(%rbp),%esi
mov    %eax,%edx
lea    0xe28(%rip),%rdi
call   1060 <logNorm@plt>
mov    -0x18(%rbp),%edi
mov    0x2de3(%rip),%rax
mov    (%rax),%esi
call   1030 <fcntl@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1215 <ipAcceptConnection+0x95>
lea    0xe1e(%rip),%rdi
call   1050 <logSysError@plt>
mov    -0x18(%rbp),%esi
mov    -0x1c(%rbp),%edx
mov    0x2dc6(%rip),%rax
and    (%rax),%edx
lea    0xe12(%rip),%rdi
call   10a0 <logDebug@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ipAcceptConnection>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
movl   $0x8,0xc(%rsp)
lea    0x10(%rsp),%rsi
lea    0xc(%rsp),%rdx
call   1090 <accept@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     120c <ipAcceptConnection+0x8c>
mov    0x14(%rsp),%edi
call   1040 <inet_ntoa@plt>
mov    %eax,%ebp
mov    0x10(%rsp),%edi
call   1070 <ntohs@plt>
lea    0xe4f(%rip),%rdi
mov    %ebp,%esi
mov    %eax,%edx
call   1060 <logNorm@plt>
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1030 <fcntl@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    11ea <ipAcceptConnection+0x6a>
lea    0xe49(%rip),%rdi
call   1050 <logSysError@plt>
mov    0x2df7(%rip),%rax
mov    (%rax),%edx
and    %ebp,%edx
lea    0xe41(%rip),%rdi
mov    %ebx,%esi
call   10a0 <logDebug@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xded(%rip),%rdi
call   1080 <perror@plt>
jmp    1203 <ipAcceptConnection+0x83>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [14 x i8] c"accept failed\00"
@.str.1 = [32 x i8] c"Accepted connection from %s:%d\0A\00"
@F_GETFL = global i32 0
@.str.2 = [15 x i8] c"fcntl(F_GETFL)\00"
@.str.3 = [29 x i8] c"Socket %d has blocking = %d\0A\00"
@O_NONBLOCK = global i32 0
@SOL_SOCKET = global i32 0
@SO_KEEPALIVE = global i32 0
define i32 @ipAcceptConnection(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %3
store i32 8, i32* %5
%8 = load i32, i32* %3
%9 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%10 = call i32 @accept(i32 %8, %struct.sockaddr* %9, i32* %5)
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %1
%14 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %37
15: ; preds = %1
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
%17 = load i32, i32* %16
%18 = call i32 @inet_ntoa(i32 %17)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
%20 = load i32, i32* %19
%21 = call i32 @ntohs(i32 %20)
%22 = call i32 @logNorm(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i32 %18, i32 %21)
%23 = load i32, i32* %6
%24 = load i32, i32* @F_GETFL
%25 = call i32 @fcntl(i32 %23, i32 %24)
store i32 %25, i32* %7
%26 = load i32, i32* %7
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %15
%29 = call i32 @logSysError(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %30
30: ; preds = %28, %15
%31 = load i32, i32* %6
%32 = load i32, i32* %7
%33 = load i32, i32* @O_NONBLOCK
%34 = and i32 %32, %33
%35 = call i32 @logDebug(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i32 %31, i32 %34)
%36 = load i32, i32* %6
store i32 %36, i32* %2
br label %37
37: ; preds = %30, %13
%38 = load i32, i32* %2
ret i32 %38
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [14 x i8] c"accept failed\00"
@.str.1 = [32 x i8] c"Accepted connection from %s:%d\0A\00"
@F_GETFL = global i32 0
@.str.2 = [15 x i8] c"fcntl(F_GETFL)\00"
@.str.3 = [29 x i8] c"Socket %d has blocking = %d\0A\00"
@O_NONBLOCK = global i32 0
@SOL_SOCKET = global i32 0
@SO_KEEPALIVE = global i32 0
define i32 @ipAcceptConnection(i32 %0) {
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = bitcast %struct.sockaddr_in* %2 to i8*
%5 = bitcast i32* %3 to i8*
store i32 8, i32* %3
%6 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%7 = call i32 @accept(i32 %0, %struct.sockaddr* %6, i32* %3)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %28
11: ; preds = %1
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
%13 = load i32, i32* %12
%14 = call i32 @inet_ntoa(i32 %13)
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
%16 = load i32, i32* %15
%17 = call i32 @ntohs(i32 %16)
%18 = call i32 @logNorm(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i32 %14, i32 %17)
%19 = load i32, i32* @F_GETFL
%20 = call i32 @fcntl(i32 %7, i32 %19)
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %11
%23 = call i32 @logSysError(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %24
24: ; preds = %22, %11
%25 = load i32, i32* @O_NONBLOCK
%26 = and i32 %25, %20
%27 = call i32 @logDebug(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i32 %7, i32 %26)
br label %28
28: ; preds = %24, %9
ret i32 %7
}