#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 fd_setup_server(void) {
  int sockfd;
  socklen_t len;
  struct sockaddr_un server_unix_addr;
  const char *name = "/tmp/fd_trans_socket";
  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):
<fd_setup_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xe71(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e4e(%rip),%rax
mov    (%rax),%edi
mov    0x2e35(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    11cf <fd_setup_server+0x4f>
lea    0xe4f(%rip),%rdi
call   1080 <perror@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    124f <fd_setup_server+0xcf>
mov    -0x20(%rbp),%rdi
call   1030 <unlink@plt>
lea    -0x18(%rbp),%rdi
mov    $0x8,%esi
call   1090 <bzero@plt>
mov    0x2dfb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%edi
mov    -0x20(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1070 <bind@plt>
cmp    $0x0,%rax
jge    1249 <fd_setup_server+0xc9>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    124f <fd_setup_server+0xcf>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fd_setup_server>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    0x2e69(%rip),%r12
mov    (%r12),%edi
mov    0x2e4e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11f9 <fd_setup_server+0x89>
lea    0xe60(%rip),%r14
mov    %r14,%rdi
call   1030 <unlink@plt>
mov    %rsp,%r15
mov    %r15,%rdi
mov    $0x8,%esi
call   1080 <bzero@plt>
mov    (%r12),%eax
mov    %eax,0x4(%rsp)
mov    (%rsp),%edi
mov    %r14,%rsi
call   1040 <strcpy@plt>
mov    $0x19,%edx
mov    %ebx,%edi
mov    %r15,%rsi
call   1060 <bind@plt>
test   %rax,%rax
jns    11eb <fd_setup_server+0x7b>
mov    %ebx,%edi
call   1050 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xe0e(%rip),%rdi
call   1070 <perror@plt>
jmp    11eb <fd_setup_server+0x7b>

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

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