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

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ PF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int listen(int, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int);
int do_open_unix_server_socket(char *fileName) {
  int sock;
  struct sockaddr_un sun;
  int backlogQueueLength = 5;
  sock = socket(PF_UNIX, SOCK_STREAM, 0);
  if (sock == -1) {
    fprintf(stderr, "socket failed:  %s\n", strerror(errno));
    return -1;
  }
  sun.sun_family = AF_UNIX;
  strncpy(sun.sun_path, fileName, 108);
  if (bind(sock, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
    fprintf(stderr,
            "bind failed:  %s (file already exists or cannot be created?)\n",
            strerror(errno));
    return -1;
  }
  if (listen(sock, backlogQueueLength) == -1) {
    fprintf(stderr, "listen failed:  %s\n", strerror(errno));
    return -1;
  }
  return sock;
}


x86 assembly (clang, -O0):
<do_open_unix_server_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x5,-0x24(%rbp)
mov    0x2e7e(%rip),%rax
mov    (%rax),%edi
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11d1 <do_open_unix_server_socket+0x71>
mov    0x2e30(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x28(%rbp),%edi
mov    %rax,%rdx
lea    0xe40(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1292 <do_open_unix_server_socket+0x132>
mov    0x2e08(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x6c,%edx
call   1030 <strncpy@plt>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
call   1060 <bind@plt>
cmp    $0xffffffff,%eax
jne    1240 <do_open_unix_server_socket+0xe0>
mov    0x2dc1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2da5(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    %rax,%rdx
lea    0xde5(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1292 <do_open_unix_server_socket+0x132>
mov    -0x14(%rbp),%edi
mov    -0x24(%rbp),%esi
call   1050 <listen@plt>
cmp    $0xffffffff,%eax
jne    128c <do_open_unix_server_socket+0x12c>
mov    0x2d75(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2d59(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x30(%rbp),%edi
mov    %rax,%rdx
lea    0xdd7(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1292 <do_open_unix_server_socket+0x132>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_open_unix_server_socket>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2e8a(%rip),%rax
mov    (%rax),%edi
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
cmp    $0xffffffff,%eax
je     11e6 <do_open_unix_server_socket+0x86>
mov    %eax,%ebx
mov    0x2e52(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
mov    %r14,%rsi
mov    $0x6c,%edx
call   1030 <strncpy@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x8,%edx
call   1060 <bind@plt>
cmp    $0xffffffff,%eax
je     1206 <do_open_unix_server_socket+0xa6>
mov    %ebx,%edi
mov    $0x5,%esi
call   1050 <listen@plt>
cmp    $0xffffffff,%eax
jne    1233 <do_open_unix_server_socket+0xd3>
mov    0x2e03(%rip),%rax
mov    (%rax),%ebx
mov    0x2dea(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe6e(%rip),%rsi
jmp    1224 <do_open_unix_server_socket+0xc4>
mov    0x2de3(%rip),%rax
mov    (%rax),%ebx
mov    0x2dca(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xdfc(%rip),%rsi
jmp    1224 <do_open_unix_server_socket+0xc4>
mov    0x2dc3(%rip),%rax
mov    (%rax),%ebx
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xdf0(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1040 <fprintf@plt>
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 }
@PF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str = [20 x i8] c"socket failed: %s\0A\00"
@errno = global i32 0
@AF_UNIX = global i32 0
@.str.1 = [62 x i8] c"bind failed: %s (file already exists or cannot be created?)\0A\00"
@.str.2 = [20 x i8] c"listen failed: %s\0A\00"
define i32 @do_open_unix_server_socket(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.sockaddr_un
%6 = alloca i32
store i8* %0, i8** %3
store i32 5, i32* %6
%7 = load i32, i32* @PF_UNIX
%8 = load i32, i32* @SOCK_STREAM
%9 = call i32 @socket(i32 %7, i32 %8, i32 0)
store i32 %9, i32* %4
%10 = load i32, i32* %4
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %17
12: ; preds = %1
%13 = load i32, i32* @stderr
%14 = load i32, i32* @errno
%15 = call i8* @strerror(i32 %14)
%16 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %15)
store i32 -1, i32* %2
br label %45
17: ; preds = %1
%18 = load i32, i32* @AF_UNIX
%19 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 1
store i32 %18, i32* %19
%20 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 0
%21 = load i32, i32* %20
%22 = load i8*, i8** %3
%23 = call i32 @strncpy(i32 %21, i8* %22, i32 108)
%24 = load i32, i32* %4
%25 = bitcast %struct.sockaddr_un* %5 to %struct.sockaddr*
%26 = call i32 @bind(i32 %24, %struct.sockaddr* %25, i32 8)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %33
28: ; preds = %17
%29 = load i32, i32* @stderr
%30 = load i32, i32* @errno
%31 = call i8* @strerror(i32 %30)
%32 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.1, i64 0, i64 0), i8* %31)
store i32 -1, i32* %2
br label %45
33: ; preds = %17
%34 = load i32, i32* %4
%35 = load i32, i32* %6
%36 = call i32 @listen(i32 %34, i32 %35)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %43
38: ; preds = %33
%39 = load i32, i32* @stderr
%40 = load i32, i32* @errno
%41 = call i8* @strerror(i32 %40)
%42 = call i32 @fprintf(i32 %39, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %41)
store i32 -1, i32* %2
br label %45
43: ; preds = %33
%44 = load i32, i32* %4
store i32 %44, i32* %2
br label %45
45: ; preds = %43, %38, %28, %12
%46 = load i32, i32* %2
ret i32 %46
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@PF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str = [20 x i8] c"socket failed: %s\0A\00"
@errno = global i32 0
@AF_UNIX = global i32 0
@.str.1 = [62 x i8] c"bind failed: %s (file already exists or cannot be created?)\0A\00"
@.str.2 = [20 x i8] c"listen failed: %s\0A\00"
define i32 @do_open_unix_server_socket(i8* %0) {
%2 = alloca %struct.sockaddr_un
%3 = bitcast %struct.sockaddr_un* %2 to i8*
%4 = load i32, i32* @PF_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 %13
8: ; preds = %1
%9 = load i32, i32* @stderr
%10 = load i32, i32* @errno
%11 = tail call i8* @strerror(i32 %10)
%12 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %11)
br label %33
13: ; preds = %1
%14 = load i32, i32* @AF_UNIX
%15 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
store i32 %14, i32* %15
%16 = tail call i32 @strncpy(i32 undef, i8* %0, i32 108)
%17 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%18 = call i32 @bind(i32 %6, %struct.sockaddr* %17, i32 8)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %25
20: ; preds = %13
%21 = load i32, i32* @stderr
%22 = load i32, i32* @errno
%23 = call i8* @strerror(i32 %22)
%24 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.1, i64 0, i64 0), i8* %23)
br label %33
25: ; preds = %13
%26 = call i32 @listen(i32 %6, i32 5)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %33
28: ; preds = %25
%29 = load i32, i32* @stderr
%30 = load i32, i32* @errno
%31 = call i8* @strerror(i32 %30)
%32 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %31)
br label %33
33: ; preds = %25, %28, %20, %8
%34 = phi i32 [ -1, %8 ], [ -1, %20 ], [ -1, %28 ], [ %6, %25 ]
ret i32 %34
}