#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_un {
  int /*<<< orphan*/ sun_path;
  int /*<<< orphan*/ sun_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ sendChar;
typedef int /*<<< orphan*/ remote;
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ LOG_CRIT;
int /*<<< orphan*/ LOG_ERR;
int /*<<< orphan*/ LOG_WARNING;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
scalar_t__ send(int, char *, int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *);
int handleNewConnection(int namedFifo, char *readBuf, fd_set *active_fdset) {
  char sendChar = 'd';
  struct sockaddr_un remote;
  int len, connectSock;
  memset(&remote, 0, sizeof(remote));
  if ((connectSock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
    syslog(LOG_CRIT, "Could not open a unix domain socket");
    return -1;
  }
  remote.sun_family = AF_UNIX;
  strncpy(remote.sun_path, readBuf, strlen(readBuf));
  len = sizeof(remote);
  if ((connect(connectSock, (struct sockaddr *)&remote, len)) < 0) {
    syslog(LOG_WARNING, "Could not connect to requested unix domain socket");
    return -1;
  }
  if ((send(connectSock, &sendChar, sizeof(sendChar), 0)) < 0) {
    syslog(LOG_ERR, "Could not send credentials across unix domain socket");
    return -1;
  }
  FD_SET(connectSock, active_fdset);
  return 0;
}


x86 assembly (clang, -O0):
<handleNewConnection>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movb   $0x64,-0x19(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
mov    0x2e11(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x30(%rbp)
cmp    $0x0,%eax
jge    11ed <handleNewConnection+0x6d>
mov    0x2e15(%rip),%rax
mov    (%rax),%edi
lea    0xe24(%rip),%rsi
call   1070 <syslog@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b0 <handleNewConnection+0x130>
mov    0x2dec(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x28(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
call   1030 <strncpy@plt>
movl   $0x8,-0x2c(%rbp)
mov    -0x30(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   1080 <connect@plt>
cmp    $0x0,%rax
jge    125f <handleNewConnection+0xdf>
mov    0x2d93(%rip),%rax
mov    (%rax),%edi
lea    0xdd6(%rip),%rsi
call   1070 <syslog@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b0 <handleNewConnection+0x130>
mov    -0x30(%rbp),%edi
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
cmp    $0x0,%rax
jge    129d <handleNewConnection+0x11d>
mov    0x2d35(%rip),%rax
mov    (%rax),%edi
lea    0xdca(%rip),%rsi
call   1070 <syslog@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b0 <handleNewConnection+0x130>
mov    -0x30(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1090 <FD_SET@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handleNewConnection>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
movb   $0x64,0xf(%rsp)
lea    0x10(%rsp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    0x2e33(%rip),%rbx
mov    (%rbx),%edi
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
test   %eax,%eax
js     121f <handleNewConnection+0x9f>
mov    %eax,%ebp
mov    (%rbx),%eax
mov    %eax,0x14(%rsp)
mov    0x10(%rsp),%ebx
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    %eax,%edx
call   1030 <strncpy@plt>
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    $0x8,%edx
call   1080 <connect@plt>
test   %rax,%rax
js     1231 <handleNewConnection+0xb1>
lea    0xf(%rsp),%rsi
xor    %ebx,%ebx
mov    %ebp,%edi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
test   %rax,%rax
js     1243 <handleNewConnection+0xc3>
mov    %ebp,%edi
mov    %r14,%rsi
call   1090 <FD_SET@plt>
jmp    125d <handleNewConnection+0xdd>
mov    0x2dc2(%rip),%rax
mov    (%rax),%edi
lea    0xdd1(%rip),%rsi
jmp    1253 <handleNewConnection+0xd3>
mov    0x2da0(%rip),%rax
mov    (%rax),%edi
lea    0xde3(%rip),%rsi
jmp    1253 <handleNewConnection+0xd3>
mov    0x2d6e(%rip),%rax
mov    (%rax),%edi
lea    0xe03(%rip),%rsi
call   1070 <syslog@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@LOG_CRIT = global i32 0
@.str = [36 x i8] c"Could not open a unix domain socket\00"
@LOG_WARNING = global i32 0
@.str.1 = [50 x i8] c"Could not connect to requested unix domain socket\00"
@LOG_ERR = global i32 0
@.str.2 = [53 x i8] c"Could not send credentials across unix domain socket\00"
define i32 @handleNewConnection(i32 %0, i8* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i8
%9 = alloca %struct.sockaddr_un
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32* %2, i32** %7
store i8 100, i8* %8
%12 = call i32 @memset(%struct.sockaddr_un* %9, i32 0, i32 8)
%13 = load i32, i32* @AF_UNIX
%14 = load i32, i32* @SOCK_STREAM
%15 = call i32 @socket(i32 %13, i32 %14, i32 0)
store i32 %15, i32* %11
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %3
%18 = load i32, i32* @LOG_CRIT
%19 = call i32 @syslog(i32 %18, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %48
20: ; preds = %3
%21 = load i32, i32* @AF_UNIX
%22 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %9, i32 0, i32 1
store i32 %21, i32* %22
%23 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %9, i32 0, i32 0
%24 = load i32, i32* %23
%25 = load i8*, i8** %6
%26 = load i8*, i8** %6
%27 = call i32 @strlen(i8* %26)
%28 = call i32 @strncpy(i32 %24, i8* %25, i32 %27)
store i32 8, i32* %10
%29 = load i32, i32* %11
%30 = bitcast %struct.sockaddr_un* %9 to %struct.sockaddr*
%31 = load i32, i32* %10
%32 = call i64 @connect(i32 %29, %struct.sockaddr* %30, i32 %31)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %20
%35 = load i32, i32* @LOG_WARNING
%36 = call i32 @syslog(i32 %35, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %4
br label %48
37: ; preds = %20
%38 = load i32, i32* %11
%39 = call i64 @send(i32 %38, i8* %8, i32 1, i32 0)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %37
%42 = load i32, i32* @LOG_ERR
%43 = call i32 @syslog(i32 %42, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %4
br label %48
44: ; preds = %37
%45 = load i32, i32* %11
%46 = load i32*, i32** %7
%47 = call i32 @FD_SET(i32 %45, i32* %46)
store i32 0, i32* %4
br label %48
48: ; preds = %44, %41, %34, %17
%49 = load i32, i32* %4
ret i32 %49
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@LOG_CRIT = global i32 0
@.str = [36 x i8] c"Could not open a unix domain socket\00"
@LOG_WARNING = global i32 0
@.str.1 = [50 x i8] c"Could not connect to requested unix domain socket\00"
@LOG_ERR = global i32 0
@.str.2 = [53 x i8] c"Could not send credentials across unix domain socket\00"
define i32 @handleNewConnection(i32 %0, i8* %1, i32* %2) {
%4 = alloca i8
%5 = alloca %struct.sockaddr_un
store i8 100, i8* %4
%6 = bitcast %struct.sockaddr_un* %5 to i8*
%7 = call i32 @memset(%struct.sockaddr_un* %5, i32 0, i32 8)
%8 = load i32, i32* @AF_UNIX
%9 = load i32, i32* @SOCK_STREAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %3
%13 = load i32, i32* @LOG_CRIT
%14 = call i32 @syslog(i32 %13, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
br label %36
15: ; preds = %3
%16 = load i32, i32* @AF_UNIX
%17 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i64 0, i32 1
store i32 %16, i32* %17
%18 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i64 0, i32 0
%19 = load i32, i32* %18
%20 = call i32 @strlen(i8* %1)
%21 = call i32 @strncpy(i32 %19, i8* %1, i32 %20)
%22 = bitcast %struct.sockaddr_un* %5 to %struct.sockaddr*
%23 = call i64 @connect(i32 %10, %struct.sockaddr* %22, i32 8)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %15
%26 = load i32, i32* @LOG_WARNING
%27 = call i32 @syslog(i32 %26, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.1, i64 0, i64 0))
br label %36
28: ; preds = %15
%29 = call i64 @send(i32 %10, i8* %4, i32 1, i32 0)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %28
%32 = load i32, i32* @LOG_ERR
%33 = call i32 @syslog(i32 %32, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.2, i64 0, i64 0))
br label %36
34: ; preds = %28
%35 = call i32 @FD_SET(i32 %10, i32* %2)
br label %36
36: ; preds = %34, %31, %25, %12
%37 = phi i32 [ -1, %12 ], [ -1, %25 ], [ -1, %31 ], [ 0, %34 ]
ret i32 %37
}