#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 *, int);
int listen(int, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
int strlen(int /*<<< orphan*/);
int /*<<< orphan*/ unlink(int /*<<< orphan*/);
int create_main_socket(void) {
  int len, main_socket;
  struct sockaddr_un local;
  if ((main_socket = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
    return -1;
  local.sun_family = AF_UNIX;
  strcpy(local.sun_path, "/tmp/q.socket");
  unlink(local.sun_path);
  len = strlen(local.sun_path) + sizeof(local.sun_family);
  if (bind(main_socket, (struct sockaddr *)&local, len) == -1)
    return -1;
  if (listen(main_socket, 5) == -1)
    return -1;
  return main_socket;
}


x86 assembly (clang, -O0):
<create_main_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e79(%rip),%rax
mov    (%rax),%edi
mov    0x2e60(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    1199 <create_main_socket+0x39>
movl   $0xffffffff,-0x4(%rbp)
jmp    1219 <create_main_socket+0xb9>
mov    0x2e48(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%edi
lea    0xe51(%rip),%rsi
call   1040 <strcpy@plt>
mov    -0x18(%rbp),%edi
call   1030 <unlink@plt>
mov    -0x18(%rbp),%edi
call   1050 <strlen@plt>
cltq
add    $0x4,%rax
mov    %eax,-0x8(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    -0x8(%rbp),%edx
call   1070 <bind@plt>
cmp    $0xffffffff,%eax
jne    11f1 <create_main_socket+0x91>
movl   $0xffffffff,-0x4(%rbp)
jmp    1219 <create_main_socket+0xb9>
mov    -0xc(%rbp),%edi
mov    $0x5,%esi
call   1060 <listen@plt>
cmp    $0xffffffff,%eax
jne    1213 <create_main_socket+0xb3>
movl   $0xffffffff,-0x4(%rbp)
jmp    1219 <create_main_socket+0xb9>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_main_socket>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e79(%rip),%r14
mov    (%r14),%edi
mov    0x2e5f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    $0xffffffff,%ebp
cmp    $0xffffffff,%eax
je     11d3 <create_main_socket+0x73>
mov    %eax,%ebx
mov    (%r14),%eax
mov    %eax,0xc(%rsp)
lea    0xe64(%rip),%rsi
call   1040 <strcpy@plt>
call   1030 <unlink@plt>
call   1050 <strlen@plt>
lea    0x4(%rax),%edx
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
call   1070 <bind@plt>
cmp    $0xffffffff,%eax
je     11d3 <create_main_socket+0x73>
mov    %ebx,%edi
mov    $0x5,%esi
call   1060 <listen@plt>
cmp    $0xffffffff,%eax
cmove  %eax,%ebx
mov    %ebx,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
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
@.str = [14 x i8] c"/tmp/q.socket\00"
define i32 @create_main_socket() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_un
%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* %3
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %10
9: ; preds = %0
store i32 -1, i32* %1
br label %38
10: ; preds = %0
%11 = load i32, i32* @AF_UNIX
%12 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 1
store i32 %11, i32* %12
%13 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 0
%14 = load i32, i32* %13
%15 = call i32 @strcpy(i32 %14, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%16 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 0
%17 = load i32, i32* %16
%18 = call i32 @unlink(i32 %17)
%19 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 0
%20 = load i32, i32* %19
%21 = call i32 @strlen(i32 %20)
%22 = sext i32 %21 to i64
%23 = add i64 %22, 4
%24 = trunc i64 %23 to i32
store i32 %24, i32* %2
%25 = load i32, i32* %3
%26 = bitcast %struct.sockaddr_un* %4 to %struct.sockaddr*
%27 = load i32, i32* %2
%28 = call i32 @bind(i32 %25, %struct.sockaddr* %26, i32 %27)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %31
30: ; preds = %10
store i32 -1, i32* %1
br label %38
31: ; preds = %10
%32 = load i32, i32* %3
%33 = call i32 @listen(i32 %32, i32 5)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %36
35: ; preds = %31
store i32 -1, i32* %1
br label %38
36: ; preds = %31
%37 = load i32, i32* %3
store i32 %37, i32* %1
br label %38
38: ; preds = %36, %35, %30, %9
%39 = load i32, i32* %1
ret i32 %39
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [14 x i8] c"/tmp/q.socket\00"
define i32 @create_main_socket() {
%1 = alloca %struct.sockaddr_un
%2 = bitcast %struct.sockaddr_un* %1 to i8*
%3 = load i32, i32* @AF_UNIX
%4 = load i32, i32* @SOCK_STREAM
%5 = tail call i32 @socket(i32 %3, i32 %4, i32 0)
%6 = icmp eq i32 %5, -1
br i1 %6, label %21, label %7
7: ; preds = %0
%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 ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 @unlink(i32 undef)
%12 = tail call i32 @strlen(i32 undef)
%13 = add i32 %12, 4
%14 = bitcast %struct.sockaddr_un* %1 to %struct.sockaddr*
%15 = call i32 @bind(i32 %5, %struct.sockaddr* %14, i32 %13)
%16 = icmp eq i32 %15, -1
br i1 %16, label %21, label %17
17: ; preds = %7
%18 = call i32 @listen(i32 %5, i32 5)
%19 = icmp eq i32 %18, -1
%20 = select i1 %19, i32 -1, i32 %5
br label %21
21: ; preds = %17, %7, %0
%22 = phi i32 [ -1, %0 ], [ -1, %7 ], [ %20, %17 ]
ret i32 %22
}