#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*/ addr;

/* Variables and functions */
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ TCP_NODELAY;
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ log_err(char *);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              char *, int);
int serverSocketAccept(int serverSocket) {
  struct sockaddr_in addr;
  socklen_t addrlen = sizeof(addr);
  if (serverSocket == -1) {
    log_err("Daemon: Invalid server socket\n");
    return -1;
  }
  int clientSocket = accept(serverSocket, (struct sockaddr *)&addr, &addrlen);
  if (clientSocket == -1) {
    log_err("Daemon: Accept failed\n");
    return -1;
  }
  int flag = 1;
  setsockopt(clientSocket, IPPROTO_TCP, TCP_NODELAY, (char *)&flag,
             sizeof(int));
  return clientSocket;
}


x86 assembly (clang, -O0):
<serverSocketAccept>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x4,-0x14(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1164 <serverSocketAccept+0x34>
lea    0xead(%rip),%rdi
call   1050 <log_err@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ca <serverSocketAccept+0x9a>
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
lea    -0x14(%rbp),%rdx
call   1040 <accept@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1199 <serverSocketAccept+0x69>
lea    0xe97(%rip),%rdi
call   1050 <log_err@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ca <serverSocketAccept+0x9a>
movl   $0x1,-0x1c(%rbp)
mov    -0x18(%rbp),%edi
mov    0x2e36(%rip),%rax
mov    (%rax),%esi
mov    0x2e35(%rip),%rax
mov    (%rax),%edx
lea    -0x1c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serverSocketAccept>:
push   %rbx
sub    $0x10,%rsp
movl   $0x4,0x4(%rsp)
cmp    $0xffffffff,%edi
je     1183 <serverSocketAccept+0x53>
lea    0x8(%rsp),%rsi
lea    0x4(%rsp),%rdx
call   1040 <accept@plt>
cmp    $0xffffffff,%eax
je     118c <serverSocketAccept+0x5c>
mov    %eax,%ebx
movl   $0x1,(%rsp)
mov    0x2e7a(%rip),%rax
mov    (%rax),%esi
mov    0x2e79(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
jmp    119d <serverSocketAccept+0x6d>
lea    0xe76(%rip),%rdi
jmp    1193 <serverSocketAccept+0x63>
lea    0xe8c(%rip),%rdi
call   1050 <log_err@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [31 x i8] c"Daemon: Invalid server socket\0A\00"
@.str.1 = [23 x i8] c"Daemon: Accept failed\0A\00"
@IPPROTO_TCP = global i32 0
@TCP_NODELAY = global i32 0
define i32 @serverSocketAccept(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 4, i32* %5
%8 = load i32, i32* %3
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = call i32 @log_err(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %27
12: ; preds = %1
%13 = load i32, i32* %3
%14 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%15 = call i32 @accept(i32 %13, %struct.sockaddr* %14, i32* %5)
store i32 %15, i32* %6
%16 = load i32, i32* %6
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %12
%19 = call i32 @log_err(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %27
20: ; preds = %12
store i32 1, i32* %7
%21 = load i32, i32* %6
%22 = load i32, i32* @IPPROTO_TCP
%23 = load i32, i32* @TCP_NODELAY
%24 = bitcast i32* %7 to i8*
%25 = call i32 @setsockopt(i32 %21, i32 %22, i32 %23, i8* %24, i32 4)
%26 = load i32, i32* %6
store i32 %26, i32* %2
br label %27
27: ; preds = %20, %18, %10
%28 = load i32, i32* %2
ret i32 %28
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [31 x i8] c"Daemon: Invalid server socket\0A\00"
@.str.1 = [23 x i8] c"Daemon: Accept failed\0A\00"
@IPPROTO_TCP = global i32 0
@TCP_NODELAY = global i32 0
define i32 @serverSocketAccept(i32 %0) {
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = alloca i32
%5 = bitcast %struct.sockaddr_in* %2 to i8*
%6 = bitcast i32* %3 to i8*
store i32 4, i32* %3
%7 = icmp eq i32 %0, -1
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = tail call i32 @log_err(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
br label %21
10: ; preds = %1
%11 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%12 = call i32 @accept(i32 %0, %struct.sockaddr* %11, i32* %3)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %10
%15 = call i32 @log_err(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
br label %21
16: ; preds = %10
%17 = bitcast i32* %4 to i8*
store i32 1, i32* %4
%18 = load i32, i32* @IPPROTO_TCP
%19 = load i32, i32* @TCP_NODELAY
%20 = call i32 @setsockopt(i32 %12, i32 %18, i32 %19, i8* %17, i32 4)
br label %21
21: ; preds = %14, %16, %8
%22 = phi i32 [ -1, %8 ], [ -1, %14 ], [ %12, %16 ]
ret i32 %22
}