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

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int S_IRWXG;
int S_IRWXO;
int S_IRWXU;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ chmod(int /*<<< orphan*/, int);
int /*<<< orphan*/ close(int);
int listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char const *);
int strlen(int /*<<< orphan*/);
int /*<<< orphan*/ unlink(int /*<<< orphan*/);
int init_socket(const char *sockname) {
  struct sockaddr_un local;
  int len;
  int s = socket(AF_UNIX, SOCK_STREAM, 0);
  if (s == -1) {
    perror("socket");
    return s;
  }
  memset(&local, 0, sizeof(local));
  local.sun_family = AF_UNIX;
  strcpy(local.sun_path, sockname);
  unlink(local.sun_path);
  len = strlen(local.sun_path) + sizeof(local.sun_family);
  if (bind(s, (struct sockaddr *)&local, len) == -1) {
    perror("bind");
    close(s);
    unlink(local.sun_path);
    return -1;
  }
  chmod(local.sun_path, S_IRWXU | S_IRWXG | S_IRWXO);
  if (listen(s, 5) == -1) {
    perror("listen");
    close(s);
    unlink(local.sun_path);
    return -1;
  }
  return s;
}


x86 assembly (clang, -O0):
<init_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
mov    0x2e04(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    11e9 <init_socket+0x49>
lea    0xe27(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12d1 <init_socket+0x131>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    0x2dd0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%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,-0x1c(%rbp)
mov    -0x20(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   10a0 <bind@plt>
cmp    $0xffffffff,%eax
jne    126a <init_socket+0xca>
lea    0xdbe(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x20(%rbp),%edi
call   1070 <close@plt>
mov    -0x18(%rbp),%edi
call   1030 <unlink@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d1 <init_socket+0x131>
mov    -0x18(%rbp),%edi
mov    0x2d6c(%rip),%rax
mov    (%rax),%esi
mov    0x2d5b(%rip),%rax
or     (%rax),%esi
mov    0x2d6a(%rip),%rax
or     (%rax),%esi
call   1090 <chmod@plt>
mov    -0x20(%rbp),%edi
mov    $0x5,%esi
call   1080 <listen@plt>
cmp    $0xffffffff,%eax
jne    12cb <init_socket+0x12b>
lea    0xd62(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x20(%rbp),%edi
call   1070 <close@plt>
mov    -0x18(%rbp),%edi
call   1030 <unlink@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d1 <init_socket+0x131>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_socket>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2e1e(%rip),%r12
mov    (%r12),%edi
mov    0x2e03(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
cmp    $0xffffffff,%eax
je     125a <init_socket+0xba>
mov    %eax,%ebx
mov    %rsp,%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    (%r12),%eax
mov    %eax,0x4(%rsp)
mov    (%rsp),%edi
mov    %r14,%rsi
call   1040 <strcpy@plt>
mov    (%rsp),%edi
call   1030 <unlink@plt>
mov    (%rsp),%edi
call   1050 <strlen@plt>
lea    0x4(%rax),%edx
mov    %ebx,%edi
mov    %r15,%rsi
call   10a0 <bind@plt>
cmp    $0xffffffff,%eax
je     1268 <init_socket+0xc8>
mov    0x2dc1(%rip),%rax
mov    0x2db2(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    (%rsp),%edi
mov    0x2dbc(%rip),%rax
or     (%rax),%esi
call   1090 <chmod@plt>
mov    %ebx,%edi
mov    $0x5,%esi
call   1080 <listen@plt>
cmp    $0xffffffff,%eax
je     1271 <init_socket+0xd1>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xd9f(%rip),%rdi
call   10b0 <perror@plt>
jmp    128c <init_socket+0xec>
lea    0xd98(%rip),%rdi
jmp    1278 <init_socket+0xd8>
lea    0xd94(%rip),%rdi
call   10b0 <perror@plt>
mov    %ebx,%edi
call   1070 <close@plt>
mov    (%rsp),%edi
call   1030 <unlink@plt>
mov    $0xffffffff,%ebx
jmp    124c <init_socket+0xac>

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [5 x i8] c"bind\00"
@S_IRWXU = global i32 0
@S_IRWXG = global i32 0
@S_IRWXO = global i32 0
@.str.2 = [7 x i8] c"listen\00"
define i32 @init_socket(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.sockaddr_un
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
%7 = load i32, i32* @AF_UNIX
%8 = load i32, i32* @SOCK_STREAM
%9 = call i32 @socket(i32 %7, i32 %8, i32 0)
store i32 %9, i32* %6
%10 = load i32, i32* %6
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %1
%13 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%14 = load i32, i32* %6
store i32 %14, i32* %2
br label %65
15: ; preds = %1
%16 = call i32 @memset(%struct.sockaddr_un* %4, i32 0, i32 8)
%17 = load i32, i32* @AF_UNIX
%18 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 1
store i32 %17, i32* %18
%19 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 0
%20 = load i32, i32* %19
%21 = load i8*, i8** %3
%22 = call i32 @strcpy(i32 %20, i8* %21)
%23 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 0
%24 = load i32, i32* %23
%25 = call i32 @unlink(i32 %24)
%26 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 0
%27 = load i32, i32* %26
%28 = call i32 @strlen(i32 %27)
%29 = sext i32 %28 to i64
%30 = add i64 %29, 4
%31 = trunc i64 %30 to i32
store i32 %31, i32* %5
%32 = load i32, i32* %6
%33 = bitcast %struct.sockaddr_un* %4 to %struct.sockaddr*
%34 = load i32, i32* %5
%35 = call i32 @bind(i32 %32, %struct.sockaddr* %33, i32 %34)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %44
37: ; preds = %15
%38 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%39 = load i32, i32* %6
%40 = call i32 @close(i32 %39)
%41 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 0
%42 = load i32, i32* %41
%43 = call i32 @unlink(i32 %42)
store i32 -1, i32* %2
br label %65
44: ; preds = %15
%45 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 0
%46 = load i32, i32* %45
%47 = load i32, i32* @S_IRWXU
%48 = load i32, i32* @S_IRWXG
%49 = or i32 %47, %48
%50 = load i32, i32* @S_IRWXO
%51 = or i32 %49, %50
%52 = call i32 @chmod(i32 %46, i32 %51)
%53 = load i32, i32* %6
%54 = call i32 @listen(i32 %53, i32 5)
%55 = icmp eq i32 %54, -1
br i1 %55, label %56, label %63
56: ; preds = %44
%57 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%58 = load i32, i32* %6
%59 = call i32 @close(i32 %58)
%60 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 0
%61 = load i32, i32* %60
%62 = call i32 @unlink(i32 %61)
store i32 -1, i32* %2
br label %65
63: ; preds = %44
%64 = load i32, i32* %6
store i32 %64, i32* %2
br label %65
65: ; preds = %63, %56, %37, %12
%66 = load i32, i32* %2
ret i32 %66
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [5 x i8] c"bind\00"
@S_IRWXU = global i32 0
@S_IRWXG = global i32 0
@S_IRWXO = global i32 0
@.str.2 = [7 x i8] c"listen\00"
define i32 @init_socket(i8* %0) {
%2 = alloca %struct.sockaddr_un
%3 = bitcast %struct.sockaddr_un* %2 to i8*
%4 = load i32, i32* @AF_UNIX
%5 = load i32, i32* @SOCK_STREAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %45
10: ; preds = %1
%11 = call i32 @memset(%struct.sockaddr_un* %2, i32 0, i32 8)
%12 = load i32, i32* @AF_UNIX
%13 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
store i32 %12, i32* %13
%14 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 0
%15 = load i32, i32* %14
%16 = call i32 @strcpy(i32 %15, i8* %0)
%17 = load i32, i32* %14
%18 = call i32 @unlink(i32 %17)
%19 = load i32, i32* %14
%20 = call i32 @strlen(i32 %19)
%21 = add i32 %20, 4
%22 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%23 = call i32 @bind(i32 %6, %struct.sockaddr* %22, i32 %21)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %30
25: ; preds = %10
%26 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%27 = call i32 @close(i32 %6)
%28 = load i32, i32* %14
%29 = call i32 @unlink(i32 %28)
br label %45
30: ; preds = %10
%31 = load i32, i32* %14
%32 = load i32, i32* @S_IRWXU
%33 = load i32, i32* @S_IRWXG
%34 = or i32 %33, %32
%35 = load i32, i32* @S_IRWXO
%36 = or i32 %34, %35
%37 = call i32 @chmod(i32 %31, i32 %36)
%38 = call i32 @listen(i32 %6, i32 5)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %45
40: ; preds = %30
%41 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%42 = call i32 @close(i32 %6)
%43 = load i32, i32* %14
%44 = call i32 @unlink(i32 %43)
br label %45
45: ; preds = %30, %40, %25, %8
%46 = phi i32 [ -1, %8 ], [ -1, %25 ], [ -1, %40 ], [ %6, %30 ]
ret i32 %46
}