#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 */
typedef scalar_t__ uint;
struct sockaddr_un {
  int /*<<< orphan*/ sun_path;
  int /*<<< orphan*/ sun_family;
};
struct sockaddr {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ F_SETFL;
int /*<<< orphan*/ O_NONBLOCK;
int /*<<< orphan*/ SOCK_STREAM;
int S_IRWXG;
int S_IRWXO;
int S_IRWXU;
int bind(scalar_t__, struct sockaddr *, scalar_t__);
int /*<<< orphan*/ chmod(char *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fcntl(scalar_t__, int /*<<< orphan*/, int /*<<< orphan*/);
int listen(scalar_t__, int);
int /*<<< orphan*/ perror(char *);
scalar_t__ socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
scalar_t__ strlen(int /*<<< orphan*/);
int /*<<< orphan*/ unlink(int /*<<< orphan*/);
uint init_socket() {
  uint s;
  struct sockaddr_un local;
  uint len;
  s = socket(AF_UNIX, SOCK_STREAM, 0);
  if (!s) {
    perror("Failed to create socket");
    exit(-1);
  }
  local.sun_family = AF_UNIX;
  strcpy(local.sun_path, "/tmp/chat_socket");
  unlink(local.sun_path);
  len = strlen(local.sun_path) + sizeof(local.sun_family);
  if (bind(s, (struct sockaddr *)&local, len) == -1) {
    perror("Failed to bind");
    exit(-2);
  }
  if (listen(s, 100) == -1) {
    perror("Failed to listen");
    exit(-3);
  }
  fcntl(s, F_SETFL, O_NONBLOCK);
  chmod("/tmp/chat_socket", S_IRWXU | S_IRWXG | S_IRWXO);
  return s;
}


x86 assembly (clang, -O0):
<init_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
mov    0x2df8(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    11e6 <init_socket+0x46>
lea    0xe29(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
mov    0x2ddb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    -0x10(%rbp),%edi
lea    0xe1c(%rip),%rsi
call   1040 <strcpy@plt>
mov    -0x10(%rbp),%edi
call   1030 <unlink@plt>
mov    -0x10(%rbp),%edi
call   1060 <strlen@plt>
add    $0x4,%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1090 <bind@plt>
cmp    $0xffffffff,%eax
jne    1249 <init_socket+0xa9>
lea    0xdef(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xfffffffe,%edi
call   10b0 <exit@plt>
mov    -0x8(%rbp),%rdi
mov    $0x64,%esi
call   1070 <listen@plt>
cmp    $0xffffffff,%eax
jne    1276 <init_socket+0xd6>
lea    0xdd1(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xfffffffd,%edi
call   10b0 <exit@plt>
mov    -0x8(%rbp),%rdi
mov    0x2d37(%rip),%rax
mov    (%rax),%esi
mov    0x2d4e(%rip),%rax
mov    (%rax),%edx
call   1050 <fcntl@plt>
mov    0x2d48(%rip),%rax
mov    (%rax),%esi
mov    0x2d2f(%rip),%rax
or     (%rax),%esi
mov    0x2d46(%rip),%rax
or     (%rax),%esi
lea    0xd65(%rip),%rdi
call   1080 <chmod@plt>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_socket>:
push   %r14
push   %rbx
push   %rax
mov    0x2e1d(%rip),%r14
mov    (%r14),%edi
mov    0x2dfb(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
test   %rax,%rax
je     1259 <init_socket+0xb9>
mov    %rax,%rbx
mov    (%r14),%eax
mov    %eax,0x4(%rsp)
lea    0xe40(%rip),%rsi
call   1040 <strcpy@plt>
call   1030 <unlink@plt>
call   1060 <strlen@plt>
lea    0x4(%rax),%rdx
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1090 <bind@plt>
cmp    $0xffffffff,%eax
je     126f <init_socket+0xcf>
mov    %rbx,%rdi
mov    $0x64,%esi
call   1070 <listen@plt>
cmp    $0xffffffff,%eax
je     1285 <init_socket+0xe5>
mov    0x2da4(%rip),%rax
mov    (%rax),%esi
mov    0x2dbb(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
call   1050 <fcntl@plt>
mov    0x2db2(%rip),%rax
mov    0x2d9b(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2db0(%rip),%rax
or     (%rax),%esi
lea    0xdcf(%rip),%rdi
call   1080 <chmod@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xda0(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
lea    0xdb3(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xfffffffe,%edi
call   10b0 <exit@plt>
lea    0xdac(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xfffffffd,%edi
call   10b0 <exit@plt>

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

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [24 x i8] c"Failed to create socket\00"
@.str.1 = [17 x i8] c"/tmp/chat_socket\00"
@.str.2 = [15 x i8] c"Failed to bind\00"
@.str.3 = [17 x i8] c"Failed to listen\00"
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@S_IRWXU = global i32 0
@S_IRWXG = global i32 0
@S_IRWXO = global i32 0
define i64 @init_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 i64 @socket(i32 %3, i32 %4, i32 0)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 -1)
unreachable
10: ; preds = %0
%11 = load i32, i32* @AF_UNIX
%12 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %1, i64 0, i32 1
store i32 %11, i32* %12
%13 = tail call i32 @strcpy(i32 undef, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%14 = tail call i32 @unlink(i32 undef)
%15 = tail call i64 @strlen(i32 undef)
%16 = add i64 %15, 4
%17 = bitcast %struct.sockaddr_un* %1 to %struct.sockaddr*
%18 = call i32 @bind(i64 %5, %struct.sockaddr* %17, i64 %16)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %23
20: ; preds = %10
%21 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%22 = call i32 @exit(i32 -2)
unreachable
23: ; preds = %10
%24 = call i32 @listen(i64 %5, i32 100)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %29
26: ; preds = %23
%27 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
%28 = call i32 @exit(i32 -3)
unreachable
29: ; preds = %23
%30 = load i32, i32* @F_SETFL
%31 = load i32, i32* @O_NONBLOCK
%32 = call i32 @fcntl(i64 %5, i32 %30, i32 %31)
%33 = load i32, i32* @S_IRWXU
%34 = load i32, i32* @S_IRWXG
%35 = or i32 %34, %33
%36 = load i32, i32* @S_IRWXO
%37 = or i32 %35, %36
%38 = call i32 @chmod(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %37)
ret i64 %5
}