#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;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ unlink(char *);
int listenOnFile(char *file) {
  int s;
  struct sockaddr_un sa;
  unlink(file);
  memset(&sa, 0, sizeof(struct sockaddr_un));
  sa.sun_family = AF_UNIX;
  strcpy(sa.sun_path, file);
  if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
    fprintf(stderr, "s < 0\n");
    return -1;
  }
  if (bind(s, (struct sockaddr *)&sa, sizeof(struct sockaddr_un)) < 0) {
    fprintf(stderr, "bind < 0 errno: %s\n", strerror(errno));
    close(s);
    return -1;
  }
  listen(s, 1);
  return s;
}


x86 assembly (clang, -O0):
<listenOnFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x2e2c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
mov    0x2df3(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1215 <listenOnFile+0x85>
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
lea    0xdfe(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1285 <listenOnFile+0xf5>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
call   1090 <bind@plt>
cmp    $0x0,%rax
jge    1272 <listenOnFile+0xe2>
mov    0x2da1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2d85(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    %rax,%rdx
lea    0xdb0(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1285 <listenOnFile+0xf5>
mov    -0x14(%rbp),%edi
mov    $0x1,%esi
call   1080 <listen@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<listenOnFile>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1030 <unlink@plt>
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x2e37(%rip),%rbp
mov    0x0(%rbp),%eax
mov    %eax,0x4(%rsp)
mov    (%rsp),%edi
mov    %rbx,%rsi
call   1040 <strcpy@plt>
mov    0x0(%rbp),%edi
mov    0x2e03(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
test   %eax,%eax
js     11fe <listenOnFile+0x6e>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x8,%edx
call   1090 <bind@plt>
test   %rax,%rax
js     1217 <listenOnFile+0x87>
mov    %ebx,%edi
mov    $0x1,%esi
call   1080 <listen@plt>
jmp    124d <listenOnFile+0xbd>
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
lea    0xdf2(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    1248 <listenOnFile+0xb8>
mov    0x2dba(%rip),%rax
mov    (%rax),%ebp
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xdd2(%rip),%rsi
mov    %ebp,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
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
@stderr = global i32 0
@.str = [7 x i8] c"s < 0\0A\00"
@.str.1 = [20 x i8] c"bind < 0 errno: %s\0A\00"
@errno = global i32 0
define i32 @listenOnFile(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.sockaddr_un
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i32 @unlink(i8* %6)
%8 = call i32 @memset(%struct.sockaddr_un* %5, i32 0, i32 8)
%9 = load i32, i32* @AF_UNIX
%10 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 1
store i32 %9, i32* %10
%11 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 0
%12 = load i32, i32* %11
%13 = load i8*, i8** %3
%14 = call i32 @strcpy(i32 %12, i8* %13)
%15 = load i32, i32* @AF_UNIX
%16 = load i32, i32* @SOCK_STREAM
%17 = call i32 @socket(i32 %15, i32 %16, i32 0)
store i32 %17, i32* %4
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %1
%20 = load i32, i32* @stderr
%21 = call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %38
22: ; preds = %1
%23 = load i32, i32* %4
%24 = bitcast %struct.sockaddr_un* %5 to %struct.sockaddr*
%25 = call i64 @bind(i32 %23, %struct.sockaddr* %24, i32 8)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %34
27: ; preds = %22
%28 = load i32, i32* @stderr
%29 = load i32, i32* @errno
%30 = call i8* @strerror(i32 %29)
%31 = call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %30)
%32 = load i32, i32* %4
%33 = call i32 @close(i32 %32)
store i32 -1, i32* %2
br label %38
34: ; preds = %22
%35 = load i32, i32* %4
%36 = call i32 @listen(i32 %35, i32 1)
%37 = load i32, i32* %4
store i32 %37, i32* %2
br label %38
38: ; preds = %34, %27, %19
%39 = load i32, i32* %2
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
@stderr = global i32 0
@.str = [7 x i8] c"s < 0\0A\00"
@.str.1 = [20 x i8] c"bind < 0 errno: %s\0A\00"
@errno = global i32 0
define i32 @listenOnFile(i8* %0) {
%2 = alloca %struct.sockaddr_un
%3 = bitcast %struct.sockaddr_un* %2 to i8*
%4 = tail call i32 @unlink(i8* %0)
%5 = call i32 @memset(%struct.sockaddr_un* %2, i32 0, i32 8)
%6 = load i32, i32* @AF_UNIX
%7 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
store i32 %6, i32* %7
%8 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 0
%9 = load i32, i32* %8
%10 = call i32 @strcpy(i32 %9, i8* %0)
%11 = load i32, i32* @AF_UNIX
%12 = load i32, i32* @SOCK_STREAM
%13 = call i32 @socket(i32 %11, i32 %12, i32 0)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %1
%16 = load i32, i32* @stderr
%17 = call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %30
18: ; preds = %1
%19 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%20 = call i64 @bind(i32 %13, %struct.sockaddr* %19, i32 8)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %28
22: ; preds = %18
%23 = load i32, i32* @stderr
%24 = load i32, i32* @errno
%25 = call i8* @strerror(i32 %24)
%26 = call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %25)
%27 = call i32 @close(i32 %13)
br label %30
28: ; preds = %18
%29 = call i32 @listen(i32 %13, i32 1)
br label %30
30: ; preds = %28, %22, %15
%31 = phi i32 [ -1, %15 ], [ -1, %22 ], [ %13, %28 ]
ret i32 %31
}