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

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ ENAMETOOLONG;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ bind(int, struct sockaddr *, size_t);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
scalar_t__ listen(int, int);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, char *, size_t);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, char *, char const *);
size_t strlen(char const *);
int /*<<< orphan*/ unlink(char *);
int new_server(const char *name) {
  int sockfd;
  if ((sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
    return -1;
  }
  size_t name_len = strlen(name) + sizeof("/tmp") + 1;
  struct sockaddr_un addr;
  addr.sun_family = AF_UNIX;
  if (name_len > sizeof(addr.sun_path)) {
    errno = ENAMETOOLONG;
    goto errout;
  }
  static char buff[sizeof(addr.sun_path) + 1];
  sprintf(buff, "%s/%s", "/tmp", name);
  unlink(buff);
  memcpy(&addr.sun_path, buff, name_len);
  size_t addr_sz = sizeof(addr) - sizeof(addr.sun_path) + name_len;
  if (bind(sockfd, (struct sockaddr *)&addr, addr_sz) < 0) {
    goto errout;
  }
  if (listen(sockfd, 10) < 0) {
    goto errout;
  }
  return sockfd;
errout:
  close(sockfd);
  return -1;
}


x86 assembly (clang, -O0):
<new_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    11bd <new_server+0x3d>
movl   $0xffffffff,-0x4(%rbp)
jmp    129f <new_server+0x11f>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x5,%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    0x2e0f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
cmpq   $0x4,-0x20(%rbp)
jbe    1200 <new_server+0x80>
mov    0x2dd8(%rip),%rax
mov    (%rax),%ecx
mov    0x2dd7(%rip),%rax
mov    %ecx,(%rax)
jmp    1290 <new_server+0x110>
mov    -0x10(%rbp),%rcx
lea    0x2e69(%rip),%rdi
lea    0xdee(%rip),%rsi
lea    0xded(%rip),%rdx
call   1090 <sprintf@plt>
lea    0x2e4f(%rip),%rdi
call   1030 <unlink@plt>
lea    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    0x2e3b(%rip),%rsi
call   1060 <memcpy@plt>
mov    -0x20(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x30(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1080 <bind@plt>
cmp    $0x0,%rax
jge    1269 <new_server+0xe9>
jmp    1290 <new_server+0x110>
mov    -0x14(%rbp),%edi
mov    $0xa,%esi
call   1070 <listen@plt>
cmp    $0x0,%rax
jge    1285 <new_server+0x105>
jmp    1290 <new_server+0x110>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    129f <new_server+0x11f>
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):
<new_server>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
mov    0x2e51(%rip),%r13
mov    0x0(%r13),%edi
mov    0x2e36(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
test   %eax,%eax
js     124f <new_server+0xcf>
mov    %eax,%ebx
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %rax,%r15
lea    0x6(%rax),%r12
mov    0x0(%r13),%eax
mov    %eax,0xc(%rsp)
cmp    $0x5,%r12
jb     11e6 <new_server+0x66>
mov    0x2def(%rip),%rax
mov    (%rax),%eax
mov    0x2dee(%rip),%rcx
mov    %eax,(%rcx)
jmp    1248 <new_server+0xc8>
lea    0x2e87(%rip),%r13
lea    0xe0c(%rip),%rsi
lea    0xe0b(%rip),%rdx
mov    %r13,%rdi
mov    %r14,%rcx
call   1090 <sprintf@plt>
mov    %r13,%rdi
call   1030 <unlink@plt>
lea    0x8(%rsp),%r14
mov    %r14,%rdi
mov    %r13,%rsi
mov    %r12,%rdx
call   1060 <memcpy@plt>
add    $0xa,%r15
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   1080 <bind@plt>
test   %rax,%rax
js     1248 <new_server+0xc8>
mov    %ebx,%edi
mov    $0xa,%esi
call   1070 <listen@plt>
test   %rax,%rax
jns    1254 <new_server+0xd4>
mov    %ebx,%edi
call   1050 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@ENAMETOOLONG = global i32 0
@errno = global i32 0
@new_server.buff = internal global [5 x i8] zeroinitializer
@.str = [6 x i8] c"%s/%s\00"
@.str.1 = [5 x i8] c"/tmp\00"
define i32 @new_server(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i64
%6 = alloca %struct.sockaddr_un
%7 = alloca i64
store i8* %0, i8** %3
%8 = load i32, i32* @AF_UNIX
%9 = load i32, i32* @SOCK_STREAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
store i32 %10, 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 %49
13: ; preds = %1
%14 = load i8*, i8** %3
%15 = call i64 @strlen(i8* %14)
%16 = add i64 %15, 5
%17 = add i64 %16, 1
store i64 %17, i64* %5
%18 = load i32, i32* @AF_UNIX
%19 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 1
store i32 %18, i32* %19
%20 = load i64, i64* %5
%21 = icmp ugt i64 %20, 4
br i1 %21, label %22, label %24
22: ; preds = %13
%23 = load i32, i32* @ENAMETOOLONG
store i32 %23, i32* @errno
br label %46
24: ; preds = %13
%25 = load i8*, i8** %3
%26 = call i32 @sprintf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @new_server.buff, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %25)
%27 = call i32 @unlink(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @new_server.buff, i64 0, i64 0))
%28 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 0
%29 = load i64, i64* %5
%30 = call i32 @memcpy(i32* %28, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @new_server.buff, i64 0, i64 0), i64 %29)
%31 = load i64, i64* %5
%32 = add i64 4, %31
store i64 %32, i64* %7
%33 = load i32, i32* %4
%34 = bitcast %struct.sockaddr_un* %6 to %struct.sockaddr*
%35 = load i64, i64* %7
%36 = call i64 @bind(i32 %33, %struct.sockaddr* %34, i64 %35)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %24
br label %46
39: ; preds = %24
%40 = load i32, i32* %4
%41 = call i64 @listen(i32 %40, i32 10)
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %44
43: ; preds = %39
br label %46
44: ; preds = %39
%45 = load i32, i32* %4
store i32 %45, i32* %2
br label %49
46: ; preds = %43, %38, %22
%47 = load i32, i32* %4
%48 = call i32 @close(i32 %47)
store i32 -1, i32* %2
br label %49
49: ; preds = %46, %44, %12
%50 = load i32, i32* %2
ret i32 %50
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@ENAMETOOLONG = global i32 0
@errno = global i32 0
@new_server.buff = internal global [5 x i8] zeroinitializer
@.str = [6 x i8] c"%s/%s\00"
@.str.1 = [5 x i8] c"/tmp\00"
define i32 @new_server(i8* %0) {
%2 = alloca %struct.sockaddr_un
%3 = load i32, i32* @AF_UNIX
%4 = load i32, i32* @SOCK_STREAM
%5 = tail call i32 @socket(i32 %3, i32 %4, i32 0)
%6 = icmp slt i32 %5, 0
br i1 %6, label %32, label %7
7: ; preds = %1
%8 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%9 = add i64 %8, 6
%10 = bitcast %struct.sockaddr_un* %2 to i8*
%11 = load i32, i32* @AF_UNIX
%12 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
store i32 %11, i32* %12
%13 = icmp ugt i64 %9, 4
br i1 %13, label %14, label %16
14: ; preds = %7
%15 = load i32, i32* @ENAMETOOLONG
store i32 %15, i32* @errno
br label %28
16: ; preds = %7
%17 = tail call i32 @sprintf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @new_server.buff, i64 0, i64 0), i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%18 = tail call i32 @unlink(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @new_server.buff, i64 0, i64 0))
%19 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 0
%20 = call i32 @memcpy(i32* %19, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @new_server.buff, i64 0, i64 0), i64 %9)
%21 = add nsw i64 %8, 10
%22 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%23 = call i64 @bind(i32 %5, %struct.sockaddr* %22, i64 %21)
%24 = icmp slt i64 %23, 0
br i1 %24, label %28, label %25
25: ; preds = %16
%26 = call i64 @listen(i32 %5, i32 10)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %25, %16, %14
%29 = call i32 @close(i32 %5)
br label %30
30: ; preds = %25, %28
%31 = phi i32 [ -1, %28 ], [ %5, %25 ]
br label %32
32: ; preds = %1, %30
%33 = phi i32 [ %31, %30 ], [ -1, %1 ]
ret i32 %33
}