#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;
};

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int bind(int, struct sockaddr *, scalar_t__);
int listen(int, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
scalar_t__ strlen(int /*<<< orphan*/);
int ipc_start_listener() {
  int sock = socket(AF_UNIX, SOCK_STREAM, 0);
  if (sock == -1)
    return -1;
  struct sockaddr_un local;
  local.sun_family = AF_UNIX;
  strcpy(local.sun_path, "/var/run/kropkid_sock");
  int bres = bind(sock, (struct sockaddr *)&local,
                  strlen(local.sun_path) + sizeof(local.sun_family));
  if (bres == -1) {
    return -1;
  }
  if (listen(sock, 10) == -1)
    return -1;
  return sock;
}


x86 assembly (clang, -O0):
<ipc_start_listener>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    0x2e89(%rip),%rax
mov    (%rax),%edi
mov    0x2e70(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    118a <ipc_start_listener+0x3a>
movl   $0xffffffff,-0x4(%rbp)
jmp    120f <ipc_start_listener+0xbf>
mov    0x2e57(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    -0x10(%rbp),%edi
lea    0xe60(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x24(%rbp)
lea    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%edi
call   1040 <strlen@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    %rax,%rdx
add    $0x4,%rdx
call   1060 <bind@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11e7 <ipc_start_listener+0x97>
movl   $0xffffffff,-0x4(%rbp)
jmp    120f <ipc_start_listener+0xbf>
mov    -0x8(%rbp),%edi
mov    $0xa,%esi
call   1050 <listen@plt>
cmp    $0xffffffff,%eax
jne    1209 <ipc_start_listener+0xb9>
movl   $0xffffffff,-0x4(%rbp)
jmp    120f <ipc_start_listener+0xbf>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ipc_start_listener>:
push   %r14
push   %rbx
push   %rax
mov    0x2e8d(%rip),%r14
mov    (%r14),%edi
mov    0x2e73(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
cmp    $0xffffffff,%eax
je     11b4 <ipc_start_listener+0x64>
mov    %eax,%ebx
mov    (%r14),%eax
mov    %eax,0x4(%rsp)
lea    0xe7d(%rip),%rsi
call   1030 <strcpy@plt>
call   1040 <strlen@plt>
lea    0x4(%rax),%rdx
mov    %rsp,%rsi
mov    %ebx,%edi
call   1060 <bind@plt>
cmp    $0xffffffff,%eax
je     11b4 <ipc_start_listener+0x64>
mov    %ebx,%edi
mov    $0xa,%esi
call   1050 <listen@plt>
cmp    $0xffffffff,%eax
cmove  %eax,%ebx
jmp    11b9 <ipc_start_listener+0x69>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [22 x i8] c"/var/run/kropkid_sock\00"
define i32 @ipc_start_listener() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_un
%4 = alloca i32
%5 = load i32, i32* @AF_UNIX
%6 = load i32, i32* @SOCK_STREAM
%7 = call i32 @socket(i32 %5, i32 %6, i32 0)
store i32 %7, i32* %2
%8 = load i32, i32* %2
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %11
10: ; preds = %0
store i32 -1, i32* %1
br label %34
11: ; preds = %0
%12 = load i32, i32* @AF_UNIX
%13 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i32 0, i32 1
store i32 %12, i32* %13
%14 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i32 0, i32 0
%15 = load i32, i32* %14
%16 = call i32 @strcpy(i32 %15, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%17 = load i32, i32* %2
%18 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%19 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i32 0, i32 0
%20 = load i32, i32* %19
%21 = call i64 @strlen(i32 %20)
%22 = add i64 %21, 4
%23 = call i32 @bind(i32 %17, %struct.sockaddr* %18, i64 %22)
store i32 %23, i32* %4
%24 = load i32, i32* %4
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %27
26: ; preds = %11
store i32 -1, i32* %1
br label %34
27: ; preds = %11
%28 = load i32, i32* %2
%29 = call i32 @listen(i32 %28, i32 10)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %32
31: ; preds = %27
store i32 -1, i32* %1
br label %34
32: ; preds = %27
%33 = load i32, i32* %2
store i32 %33, i32* %1
br label %34
34: ; preds = %32, %31, %26, %10
%35 = load i32, i32* %1
ret i32 %35
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [22 x i8] c"/var/run/kropkid_sock\00"
define i32 @ipc_start_listener() {
%1 = alloca %struct.sockaddr_un
%2 = load i32, i32* @AF_UNIX
%3 = load i32, i32* @SOCK_STREAM
%4 = tail call i32 @socket(i32 %2, i32 %3, i32 0)
%5 = icmp eq i32 %4, -1
br i1 %5, label %22, label %6
6: ; preds = %0
%7 = bitcast %struct.sockaddr_un* %1 to i8*
%8 = load i32, i32* @AF_UNIX
%9 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %1, i64 0, i32 1
store i32 %8, i32* %9
%10 = tail call i32 @strcpy(i32 undef, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%11 = bitcast %struct.sockaddr_un* %1 to %struct.sockaddr*
%12 = tail call i64 @strlen(i32 undef)
%13 = add i64 %12, 4
%14 = call i32 @bind(i32 %4, %struct.sockaddr* %11, i64 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %20, label %16
16: ; preds = %6
%17 = call i32 @listen(i32 %4, i32 10)
%18 = icmp eq i32 %17, -1
%19 = select i1 %18, i32 -1, i32 %4
br label %20
20: ; preds = %16, %6
%21 = phi i32 [ -1, %6 ], [ %19, %16 ]
br label %22
22: ; preds = %0, %20
%23 = phi i32 [ %21, %20 ], [ -1, %0 ]
ret i32 %23
}