#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 scalar_t__ socklen_t;
typedef int /*<<< orphan*/ server_unix_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_LOCAL;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ bind(int, struct sockaddr *, scalar_t__);
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char const *);
scalar_t__ strlen(char const *);
int /*<<< orphan*/ unlink(char const *);
int setup_server(const char *name) {
  int sockfd;
  socklen_t len;
  struct sockaddr_un server_unix_addr;
  if ((sockfd = socket(AF_LOCAL, SOCK_STREAM, 0)) < 0) {
    perror("socket");
    return sockfd;
  }
  unlink(name);
  bzero((char *)&server_unix_addr, sizeof(server_unix_addr));
  server_unix_addr.sun_family = AF_LOCAL;
  strcpy(server_unix_addr.sun_path, name);
  len = (socklen_t)strlen(name) + 1;
  len += sizeof(server_unix_addr.sun_family);
  if (bind(sockfd, (struct sockaddr *)&server_unix_addr, len) < 0) {
    close(sockfd);
    return -1;
  }
  return sockfd;
}


x86 assembly (clang, -O0):
<setup_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
mov    0x2e3c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11c8 <setup_server+0x48>
lea    0xe48(%rip),%rdi
call   1080 <perror@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1248 <setup_server+0xc8>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
lea    -0x28(%rbp),%rdi
mov    $0x8,%esi
call   1090 <bzero@plt>
mov    0x2e02(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x28(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1070 <bind@plt>
cmp    $0x0,%rax
jge    1242 <setup_server+0xc2>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1248 <setup_server+0xc8>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setup_server>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2e57(%rip),%r15
mov    (%r15),%edi
mov    0x2e3d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,%ebp
test   %eax,%eax
js     1208 <setup_server+0x88>
mov    %rbx,%rdi
call   1030 <unlink@plt>
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x8,%esi
call   1090 <bzero@plt>
mov    (%r15),%eax
mov    %eax,0x4(%rsp)
mov    (%rsp),%edi
mov    %rbx,%rsi
call   1040 <strcpy@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
lea    0x5(%rax),%rdx
mov    %ebp,%edi
mov    %r14,%rsi
call   1070 <bind@plt>
test   %rax,%rax
jns    11fb <setup_server+0x7b>
mov    %ebp,%edi
call   1060 <close@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdf1(%rip),%rdi
call   1080 <perror@plt>
jmp    11fb <setup_server+0x7b>

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

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_LOCAL = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
define i32 @setup_server(i8* %0) {
%2 = alloca %struct.sockaddr_un
%3 = bitcast %struct.sockaddr_un* %2 to i8*
%4 = load i32, i32* @AF_LOCAL
%5 = load i32, i32* @SOCK_STREAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = icmp slt i32 %6, 0
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 %25
10: ; preds = %1
%11 = tail call i32 @unlink(i8* %0)
%12 = call i32 @bzero(i8* %3, i32 8)
%13 = load i32, i32* @AF_LOCAL
%14 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
store i32 %13, i32* %14
%15 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 0
%16 = load i32, i32* %15
%17 = tail call i32 @strcpy(i32 %16, i8* %0)
%18 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%19 = add i64 %18, 5
%20 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%21 = call i64 @bind(i32 %6, %struct.sockaddr* %20, i64 %19)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %10
%24 = call i32 @close(i32 %6)
br label %25
25: ; preds = %10, %23, %8
%26 = phi i32 [ %6, %8 ], [ -1, %23 ], [ %6, %10 ]
ret i32 %26
}