#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*/ PATH_MAX;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ snprintf(int /*<<< orphan*/, int /*<<< orphan*/, char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ unlink(char *);
int make_socket() {
  int sfd;
  struct sockaddr_un sa;
  unlink("./testsock");
  memset(&sa, 0, sizeof(struct sockaddr_un));
  sa.sun_family = AF_UNIX;
  snprintf(sa.sun_path, PATH_MAX, "./testsock");
  sfd = socket(AF_UNIX, SOCK_STREAM, 0);
  if (sfd == -1) {
    printf("socket failed.\n");
    return -1;
  }
  if (bind(sfd, (struct sockaddr *)&sa, sizeof(struct sockaddr_un)) != 0) {
    printf("bind failed.\n");
    return -1;
  }
  return sfd;
}


x86 assembly (clang, -O0):
<make_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    0xe91(%rip),%rdi
call   1030 <unlink@plt>
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    0x2e55(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    -0x10(%rbp),%edi
mov    0x2e56(%rip),%rax
mov    (%rax),%esi
lea    0xe5d(%rip),%rdx
call   1050 <snprintf@plt>
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
mov    0x2e18(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11e6 <make_socket+0x86>
lea    0xe36(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    121f <make_socket+0xbf>
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x8,%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
je     1219 <make_socket+0xb9>
lea    0xe13(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    121f <make_socket+0xbf>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<make_socket>:
push   %r14
push   %rbx
push   %rax
lea    0xe95(%rip),%r14
mov    %r14,%rdi
call   1030 <unlink@plt>
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    0x2e57(%rip),%rbx
mov    (%rbx),%eax
mov    %eax,0x4(%rsp)
mov    (%rsp),%edi
mov    0x2e57(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdx
call   1050 <snprintf@plt>
mov    (%rbx),%edi
mov    0x2e24(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
cmp    $0xffffffff,%eax
je     11d9 <make_socket+0x79>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x8,%edx
call   1070 <bind@plt>
test   %rax,%rax
je     11ea <make_socket+0x8a>
lea    0xe34(%rip),%rdi
jmp    11e0 <make_socket+0x80>
lea    0xe38(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [11 x i8] c"./testsock\00"
@AF_UNIX = global i32 0
@PATH_MAX = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [16 x i8] c"socket failed.\0A\00"
@.str.2 = [14 x i8] c"bind failed.\0A\00"
define i32 @make_socket() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_un
%4 = call i32 @unlink(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%5 = call i32 @memset(%struct.sockaddr_un* %3, i32 0, i32 8)
%6 = load i32, i32* @AF_UNIX
%7 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i32 0, i32 1
store i32 %6, i32* %7
%8 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i32 0, i32 0
%9 = load i32, i32* %8
%10 = load i32, i32* @PATH_MAX
%11 = call i32 @snprintf(i32 %9, i32 %10, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%12 = load i32, i32* @AF_UNIX
%13 = load i32, i32* @SOCK_STREAM
%14 = call i32 @socket(i32 %12, i32 %13, i32 0)
store i32 %14, i32* %2
%15 = load i32, i32* %2
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %0
%18 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %28
19: ; preds = %0
%20 = load i32, i32* %2
%21 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%22 = call i64 @bind(i32 %20, %struct.sockaddr* %21, i32 8)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %19
%25 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %1
br label %28
26: ; preds = %19
%27 = load i32, i32* %2
store i32 %27, i32* %1
br label %28
28: ; preds = %26, %24, %17
%29 = load i32, i32* %1
ret i32 %29
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [11 x i8] c"./testsock\00"
@AF_UNIX = global i32 0
@PATH_MAX = global i32 0
@SOCK_STREAM = global i32 0
@str = [13 x i8] c"bind failed.\00"
@str.3 = [15 x i8] c"socket failed.\00"
define i32 @make_socket() {
%1 = alloca %struct.sockaddr_un
%2 = bitcast %struct.sockaddr_un* %1 to i8*
%3 = tail call i32 @unlink(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%4 = call i32 @memset(%struct.sockaddr_un* %1, i32 0, i32 8)
%5 = load i32, i32* @AF_UNIX
%6 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %1, i64 0, i32 1
store i32 %5, i32* %6
%7 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %1, i64 0, i32 0
%8 = load i32, i32* %7
%9 = load i32, i32* @PATH_MAX
%10 = call i32 @snprintf(i32 %8, i32 %9, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 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 eq i32 %13, -1
br i1 %14, label %19, label %15
15: ; preds = %0
%16 = bitcast %struct.sockaddr_un* %1 to %struct.sockaddr*
%17 = call i64 @bind(i32 %13, %struct.sockaddr* %16, i32 8)
%18 = icmp eq i64 %17, 0
br i1 %18, label %22, label %19
19: ; preds = %15, %0
%20 = phi i8* [ getelementptr inbounds ([15 x i8], [15 x i8]* @str.3, i64 0, i64 0), %0 ], [ getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0), %15 ]
%21 = call i32 @puts(i8* dereferenceable(1) %20)
br label %22
22: ; preds = %19, %15
%23 = phi i32 [ %13, %15 ], [ -1, %19 ]
ret i32 %23
}