#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;
  void *sun_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ server_addr;
typedef int /*<<< orphan*/ remote_addr;

/* Variables and functions */
void *AF_UNIX;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ UNLLog(int, char *, int, int);
scalar_t__ bind(int, struct sockaddr *, int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int socket(void *, int /*<<< orphan*/, int /*<<< orphan*/);
int strerror(int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int);
int afsocket_listen(char *server_socketfile, char *remote_socketfile,
                    int *server_socket, int *remote_socket) {
  int rc = -1;
  struct sockaddr_un remote_addr;
  memset(&remote_addr, 0, sizeof(remote_addr));
  struct sockaddr_un server_addr;
  memset(&server_addr, 0, sizeof(server_addr));
  *remote_socket = socket(AF_UNIX, SOCK_DGRAM, 0);
  if (*remote_socket < 0) {
    rc = 1;
    UNLLog(1, "Error while setting remote AF_UNIX: %s (%i)\n", strerror(errno),
           rc);
    return rc;
  }
  remote_addr.sun_family = AF_UNIX;
  strncpy(remote_addr.sun_path, remote_socketfile,
          sizeof(remote_addr.sun_path) - 1);
  while (connect(*remote_socket, (struct sockaddr *)&remote_addr,
                 sizeof(struct sockaddr_un)) < 0) {
    rc = 2;
    UNLLog(1, "Error while connecting local AF_UNIX: %s (%i)\n",
           strerror(errno), rc);
    return rc;
  }
  *server_socket = socket(AF_UNIX, SOCK_DGRAM, 0);
  if (*server_socket < 0) {
    rc = 3;
    UNLLog(1, "Error while setting local AF_UNIX: %s (%i)\n", strerror(errno),
           rc);
    return rc;
  }
  server_addr.sun_family = AF_UNIX;
  strncpy(server_addr.sun_path, server_socketfile,
          sizeof(server_addr.sun_path) - 1);
  if (bind(*server_socket, (struct sockaddr *)&server_addr,
           sizeof(struct sockaddr_un))) {
    rc = 4;
    UNLLog(1, "Error while binding local AF_UNIX: %s (%i)\n", strerror(errno),
           rc);
    return rc;
  }
  UNLLog(3, "Local (%i) and remote (%i) AF_UNIX are configured.\n",
         *server_socket, *remote_socket);
  return 0;
}


x86 assembly (clang, -O0):
<afsocket_listen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movl   $0xffffffff,-0x2c(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    0x2e32(%rip),%rax
mov    (%rax),%rdi
mov    0x2e20(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rax
cmpl   $0x0,(%rax)
jge    1214 <afsocket_listen+0xa4>
movl   $0x1,-0x2c(%rbp)
mov    0x2de4(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %eax,%edx
mov    -0x2c(%rbp),%ecx
mov    $0x1,%edi
lea    0xdfc(%rip),%rsi
call   1050 <UNLLog@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1383 <afsocket_listen+0x213>
mov    0x2dcd(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
mov    -0x40(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x3,%edx
call   1030 <strncpy@plt>
mov    -0x28(%rbp),%rax
mov    (%rax),%edi
lea    -0x40(%rbp),%rsi
mov    $0x10,%edx
call   1070 <connect@plt>
cmp    $0x0,%rax
jge    1287 <afsocket_listen+0x117>
movl   $0x2,-0x2c(%rbp)
mov    0x2d71(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %eax,%edx
mov    -0x2c(%rbp),%ecx
mov    $0x1,%edi
lea    0xdb6(%rip),%rsi
call   1050 <UNLLog@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1383 <afsocket_listen+0x213>
mov    0x2d5a(%rip),%rax
mov    (%rax),%rdi
mov    0x2d48(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
cmpl   $0x0,(%rax)
jge    12ec <afsocket_listen+0x17c>
movl   $0x3,-0x2c(%rbp)
mov    0x2d0c(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %eax,%edx
mov    -0x2c(%rbp),%ecx
mov    $0x1,%edi
lea    0xd80(%rip),%rsi
call   1050 <UNLLog@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1383 <afsocket_listen+0x213>
mov    0x2cf5(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x48(%rbp)
mov    -0x50(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x3,%edx
call   1030 <strncpy@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
lea    -0x50(%rbp),%rsi
mov    $0x10,%edx
call   1060 <bind@plt>
cmp    $0x0,%rax
je     135f <afsocket_listen+0x1ef>
movl   $0x4,-0x2c(%rbp)
mov    0x2c99(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %eax,%edx
mov    -0x2c(%rbp),%ecx
mov    $0x1,%edi
lea    0xd39(%rip),%rsi
call   1050 <UNLLog@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1383 <afsocket_listen+0x213>
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
mov    -0x28(%rbp),%rax
mov    (%rax),%ecx
mov    $0x3,%edi
lea    0xd3d(%rip),%rsi
call   1050 <UNLLog@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<afsocket_listen>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rcx,%r12
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdi,%r15
lea    0x18(%rsp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    0x2e35(%rip),%r13
mov    0x0(%r13),%rdi
mov    0x2e22(%rip),%rbp
mov    0x0(%rbp),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,(%r12)
test   %eax,%eax
js     1288 <afsocket_listen+0x118>
mov    0x0(%r13),%rax
mov    %rax,0x20(%rsp)
mov    0x18(%rsp),%edi
mov    %rbx,%rsi
mov    $0x3,%edx
call   1030 <strncpy@plt>
mov    (%r12),%edi
lea    0x18(%rsp),%rsi
mov    $0x10,%edx
call   1070 <connect@plt>
test   %rax,%rax
js     12b5 <afsocket_listen+0x145>
mov    0x0(%r13),%rdi
mov    0x0(%rbp),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,(%r14)
test   %eax,%eax
js     12e2 <afsocket_listen+0x172>
mov    0x0(%r13),%rax
mov    %rax,0x10(%rsp)
mov    0x8(%rsp),%edi
mov    %r15,%rsi
mov    $0x3,%edx
call   1030 <strncpy@plt>
mov    (%r14),%edi
lea    0x8(%rsp),%rsi
mov    $0x10,%edx
call   1060 <bind@plt>
test   %rax,%rax
je     130f <afsocket_listen+0x19f>
mov    0x2d71(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe1b(%rip),%rsi
mov    $0x4,%ebx
mov    $0x1,%edi
mov    %eax,%edx
mov    $0x4,%ecx
call   1050 <UNLLog@plt>
jmp    1329 <afsocket_listen+0x1b9>
mov    0x2d41(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xd63(%rip),%rsi
mov    $0x1,%ebx
mov    $0x1,%edi
mov    %eax,%edx
mov    $0x1,%ecx
call   1050 <UNLLog@plt>
jmp    1329 <afsocket_listen+0x1b9>
mov    0x2d14(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xd63(%rip),%rsi
mov    $0x2,%ebx
mov    $0x1,%edi
mov    %eax,%edx
mov    $0x2,%ecx
call   1050 <UNLLog@plt>
jmp    1329 <afsocket_listen+0x1b9>
mov    0x2ce7(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xd65(%rip),%rsi
mov    $0x3,%ebx
mov    $0x1,%edi
mov    %eax,%edx
mov    $0x3,%ecx
call   1050 <UNLLog@plt>
jmp    1329 <afsocket_listen+0x1b9>
mov    (%r14),%edx
mov    (%r12),%ecx
lea    0xd97(%rip),%rsi
mov    $0x3,%edi
call   1050 <UNLLog@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i8* }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i8* null
@SOCK_DGRAM = global i32 0
@.str = [45 x i8] c"Error while setting remote AF_UNIX: %s (%i)\0A\00"
@errno = global i32 0
@.str.1 = [47 x i8] c"Error while connecting local AF_UNIX: %s (%i)\0A\00"
@.str.2 = [44 x i8] c"Error while setting local AF_UNIX: %s (%i)\0A\00"
@.str.3 = [44 x i8] c"Error while binding local AF_UNIX: %s (%i)\0A\00"
@.str.4 = [52 x i8] c"Local (%i) and remote (%i) AF_UNIX are configured.\0A\00"
define i32 @afsocket_listen(i8* %0, i8* %1, i32* %2, i32* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i32
%11 = alloca %struct.sockaddr_un
%12 = alloca %struct.sockaddr_un
store i8* %0, i8** %6
store i8* %1, i8** %7
store i32* %2, i32** %8
store i32* %3, i32** %9
store i32 -1, i32* %10
%13 = call i32 @memset(%struct.sockaddr_un* %11, i32 0, i32 16)
%14 = call i32 @memset(%struct.sockaddr_un* %12, i32 0, i32 16)
%15 = load i8*, i8** @AF_UNIX
%16 = load i32, i32* @SOCK_DGRAM
%17 = call i32 @socket(i8* %15, i32 %16, i32 0)
%18 = load i32*, i32** %9
store i32 %17, i32* %18
%19 = load i32*, i32** %9
%20 = load i32, i32* %19
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %28
22: ; preds = %4
store i32 1, i32* %10
%23 = load i32, i32* @errno
%24 = call i32 @strerror(i32 %23)
%25 = load i32, i32* %10
%26 = call i32 @UNLLog(i32 1, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0), i32 %24, i32 %25)
%27 = load i32, i32* %10
store i32 %27, i32* %5
br label %85
28: ; preds = %4
%29 = load i8*, i8** @AF_UNIX
%30 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %11, i32 0, i32 1
store i8* %29, i8** %30
%31 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %11, i32 0, i32 0
%32 = load i32, i32* %31
%33 = load i8*, i8** %7
%34 = call i32 @strncpy(i32 %32, i8* %33, i32 3)
br label %35
35: ; preds = %28
%36 = load i32*, i32** %9
%37 = load i32, i32* %36
%38 = bitcast %struct.sockaddr_un* %11 to %struct.sockaddr*
%39 = call i64 @connect(i32 %37, %struct.sockaddr* %38, i32 16)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %47
41: ; preds = %35
store i32 2, i32* %10
%42 = load i32, i32* @errno
%43 = call i32 @strerror(i32 %42)
%44 = load i32, i32* %10
%45 = call i32 @UNLLog(i32 1, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.1, i64 0, i64 0), i32 %43, i32 %44)
%46 = load i32, i32* %10
store i32 %46, i32* %5
br label %85
47: ; preds = %35
%48 = load i8*, i8** @AF_UNIX
%49 = load i32, i32* @SOCK_DGRAM
%50 = call i32 @socket(i8* %48, i32 %49, i32 0)
%51 = load i32*, i32** %8
store i32 %50, i32* %51
%52 = load i32*, i32** %8
%53 = load i32, i32* %52
%54 = icmp slt i32 %53, 0
br i1 %54, label %55, label %61
55: ; preds = %47
store i32 3, i32* %10
%56 = load i32, i32* @errno
%57 = call i32 @strerror(i32 %56)
%58 = load i32, i32* %10
%59 = call i32 @UNLLog(i32 1, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0), i32 %57, i32 %58)
%60 = load i32, i32* %10
store i32 %60, i32* %5
br label %85
61: ; preds = %47
%62 = load i8*, i8** @AF_UNIX
%63 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %12, i32 0, i32 1
store i8* %62, i8** %63
%64 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %12, i32 0, i32 0
%65 = load i32, i32* %64
%66 = load i8*, i8** %6
%67 = call i32 @strncpy(i32 %65, i8* %66, i32 3)
%68 = load i32*, i32** %8
%69 = load i32, i32* %68
%70 = bitcast %struct.sockaddr_un* %12 to %struct.sockaddr*
%71 = call i64 @bind(i32 %69, %struct.sockaddr* %70, i32 16)
%72 = icmp ne i64 %71, 0
br i1 %72, label %73, label %79
73: ; preds = %61
store i32 4, i32* %10
%74 = load i32, i32* @errno
%75 = call i32 @strerror(i32 %74)
%76 = load i32, i32* %10
%77 = call i32 @UNLLog(i32 1, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0), i32 %75, i32 %76)
%78 = load i32, i32* %10
store i32 %78, i32* %5
br label %85
79: ; preds = %61
%80 = load i32*, i32** %8
%81 = load i32, i32* %80
%82 = load i32*, i32** %9
%83 = load i32, i32* %82
%84 = call i32 @UNLLog(i32 3, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.4, i64 0, i64 0), i32 %81, i32 %83)
store i32 0, i32* %5
br label %85
85: ; preds = %79, %73, %55, %41, %22
%86 = load i32, i32* %5
ret i32 %86
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i8* }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i8* null
@SOCK_DGRAM = global i32 0
@.str = [45 x i8] c"Error while setting remote AF_UNIX: %s (%i)\0A\00"
@errno = global i32 0
@.str.1 = [47 x i8] c"Error while connecting local AF_UNIX: %s (%i)\0A\00"
@.str.2 = [44 x i8] c"Error while setting local AF_UNIX: %s (%i)\0A\00"
@.str.3 = [44 x i8] c"Error while binding local AF_UNIX: %s (%i)\0A\00"
@.str.4 = [52 x i8] c"Local (%i) and remote (%i) AF_UNIX are configured.\0A\00"
define i32 @afsocket_listen(i8* %0, i8* %1, i32* nocapture %2, i32* nocapture %3) {
%5 = alloca %struct.sockaddr_un
%6 = alloca %struct.sockaddr_un
%7 = bitcast %struct.sockaddr_un* %5 to i8*
%8 = call i32 @memset(%struct.sockaddr_un* %5, i32 0, i32 16)
%9 = bitcast %struct.sockaddr_un* %6 to i8*
%10 = call i32 @memset(%struct.sockaddr_un* %6, i32 0, i32 16)
%11 = load i8*, i8** @AF_UNIX
%12 = load i32, i32* @SOCK_DGRAM
%13 = call i32 @socket(i8* %11, i32 %12, i32 0)
store i32 %13, i32* %3
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %4
%16 = load i32, i32* @errno
%17 = call i32 @strerror(i32 %16)
%18 = call i32 @UNLLog(i32 1, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0), i32 %17, i32 1)
br label %60
19: ; preds = %4
%20 = load i8*, i8** @AF_UNIX
%21 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i64 0, i32 1
store i8* %20, i8** %21
%22 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i64 0, i32 0
%23 = load i32, i32* %22
%24 = call i32 @strncpy(i32 %23, i8* %1, i32 3)
%25 = load i32, i32* %3
%26 = bitcast %struct.sockaddr_un* %5 to %struct.sockaddr*
%27 = call i64 @connect(i32 %25, %struct.sockaddr* %26, i32 16)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %19
%30 = load i32, i32* @errno
%31 = call i32 @strerror(i32 %30)
%32 = call i32 @UNLLog(i32 1, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.1, i64 0, i64 0), i32 %31, i32 2)
br label %60
33: ; preds = %19
%34 = load i8*, i8** @AF_UNIX
%35 = load i32, i32* @SOCK_DGRAM
%36 = call i32 @socket(i8* %34, i32 %35, i32 0)
store i32 %36, i32* %2
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %42
38: ; preds = %33
%39 = load i32, i32* @errno
%40 = call i32 @strerror(i32 %39)
%41 = call i32 @UNLLog(i32 1, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0), i32 %40, i32 3)
br label %60
42: ; preds = %33
%43 = load i8*, i8** @AF_UNIX
%44 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i64 0, i32 1
store i8* %43, i8** %44
%45 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i64 0, i32 0
%46 = load i32, i32* %45
%47 = call i32 @strncpy(i32 %46, i8* %0, i32 3)
%48 = load i32, i32* %2
%49 = bitcast %struct.sockaddr_un* %6 to %struct.sockaddr*
%50 = call i64 @bind(i32 %48, %struct.sockaddr* %49, i32 16)
%51 = icmp eq i64 %50, 0
br i1 %51, label %56, label %52
52: ; preds = %42
%53 = load i32, i32* @errno
%54 = call i32 @strerror(i32 %53)
%55 = call i32 @UNLLog(i32 1, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0), i32 %54, i32 4)
br label %60
56: ; preds = %42
%57 = load i32, i32* %2
%58 = load i32, i32* %3
%59 = call i32 @UNLLog(i32 3, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.4, i64 0, i64 0), i32 %57, i32 %58)
br label %60
60: ; preds = %56, %52, %38, %29, %15
%61 = phi i32 [ 1, %15 ], [ 2, %29 ], [ 3, %38 ], [ 4, %52 ], [ 0, %56 ]
ret i32 %61
}