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

/* Variables and functions */
int /*<<< orphan*/ AF_LOCAL;
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ perror(char *);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char const *);
int socket_start_server(const char *name) {
  int fd;
  struct sockaddr_un addr;
  fd = socket(AF_LOCAL, SOCK_STREAM, 0);
  if (fd < 0)
    return -1;
  addr.sun_family = AF_UNIX;
  strcpy(addr.sun_path, name);
  int on = 1;
  if ((setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on))) < 0) {
    perror("setsockopt failed");
    goto error;
  }
  if (bind(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_un)) < 0) {
    perror("bind");
    goto error;
  }
  return fd;
error:
  close(fd);
  return -1;
}


x86 assembly (clang, -O0):
<socket_start_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e65(%rip),%rax
mov    (%rax),%edi
mov    0x2e4c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    119e <socket_start_server+0x3e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1242 <socket_start_server+0xe2>
mov    0x2e3b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
movl   $0x1,-0x24(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2e21(%rip),%rax
mov    (%rax),%esi
mov    0x2df0(%rip),%rax
mov    (%rax),%edx
lea    -0x24(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
cmp    $0x0,%rax
jge    11fc <socket_start_server+0x9c>
lea    0xe0e(%rip),%rdi
call   1070 <perror@plt>
jmp    1233 <socket_start_server+0xd3>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
call   1060 <bind@plt>
cmp    $0x0,%rax
jge    1228 <socket_start_server+0xc8>
lea    0xdf4(%rip),%rdi
call   1070 <perror@plt>
jmp    1233 <socket_start_server+0xd3>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1242 <socket_start_server+0xe2>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_start_server>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
mov    0x2e67(%rip),%rax
mov    (%rax),%edi
mov    0x2e4e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
test   %eax,%eax
js     1201 <socket_start_server+0xa1>
mov    %eax,%ebx
mov    0x2e50(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    %r14,%rsi
call   1030 <strcpy@plt>
movl   $0x1,0xc(%rsp)
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
mov    0x2e0a(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
test   %rax,%rax
js     11ee <socket_start_server+0x8e>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0x8,%edx
call   1060 <bind@plt>
test   %rax,%rax
jns    1206 <socket_start_server+0xa6>
lea    0xe26(%rip),%rdi
jmp    11f5 <socket_start_server+0x95>
lea    0xe0b(%rip),%rdi
call   1070 <perror@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_LOCAL = global i32 0
@SOCK_STREAM = global i32 0
@AF_UNIX = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str = [18 x i8] c"setsockopt failed\00"
@.str.1 = [5 x i8] c"bind\00"
define i32 @socket_start_server(i8* %0) {
%2 = alloca %struct.sockaddr_un
%3 = alloca i32
%4 = bitcast %struct.sockaddr_un* %2 to i8*
%5 = load i32, i32* @AF_LOCAL
%6 = load i32, i32* @SOCK_STREAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = icmp slt i32 %7, 0
br i1 %8, label %28, label %9
9: ; preds = %1
%10 = load i32, i32* @AF_UNIX
%11 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
store i32 %10, i32* %11
%12 = tail call i32 @strcpy(i32 undef, i8* %0)
%13 = bitcast i32* %3 to i8*
store i32 1, i32* %3
%14 = load i32, i32* @SOL_SOCKET
%15 = load i32, i32* @SO_REUSEADDR
%16 = call i64 @setsockopt(i32 %7, i32 %14, i32 %15, i32* %3, i32 4)
%17 = icmp slt i64 %16, 0
br i1 %17, label %22, label %18
18: ; preds = %9
%19 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%20 = call i64 @bind(i32 %7, %struct.sockaddr* %19, i32 8)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %18, %9
%23 = phi i8* [ getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), %9 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), %18 ]
%24 = call i32 @perror(i8* %23)
%25 = call i32 @close(i32 %7)
br label %26
26: ; preds = %18, %22
%27 = phi i32 [ -1, %22 ], [ %7, %18 ]
br label %28
28: ; preds = %1, %26
%29 = phi i32 [ %27, %26 ], [ -1, %1 ]
ret i32 %29
}