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

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int ez_net_bind(int, struct sockaddr *, int);
int /*<<< orphan*/ ez_net_close_socket(int);
int ez_net_listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int);
int ez_net_unix_server(char *path, int backlog) {
  int s;
  struct sockaddr_un sa;
  if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
    return -1;
  }
  memset(&sa, 0, sizeof(sa));
  sa.sun_family = AF_UNIX;
  strncpy(sa.sun_path, path, sizeof(sa.sun_path) - 1);
  if (ez_net_bind(s, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
    ez_net_close_socket(s);
    return -1;
  }
  if (ez_net_listen(s, backlog) == -1) {
    ez_net_close_socket(s);
    return -1;
  }
  return s;
}


x86 assembly (clang, -O0):
<ez_net_unix_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e72(%rip),%rax
mov    (%rax),%edi
mov    0x2e59(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    11a0 <ez_net_unix_server+0x40>
movl   $0xffffffff,-0x4(%rbp)
jmp    1229 <ez_net_unix_server+0xc9>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x2e31(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x3,%edx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
call   1060 <ez_net_bind@plt>
cmp    $0xffffffff,%eax
jne    11fb <ez_net_unix_server+0x9b>
mov    -0x18(%rbp),%edi
call   1070 <ez_net_close_socket@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1229 <ez_net_unix_server+0xc9>
mov    -0x18(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1040 <ez_net_listen@plt>
cmp    $0xffffffff,%eax
jne    1223 <ez_net_unix_server+0xc3>
mov    -0x18(%rbp),%edi
call   1070 <ez_net_close_socket@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1229 <ez_net_unix_server+0xc9>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ez_net_unix_server>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%rbp
mov    0x2e70(%rip),%r13
mov    0x0(%r13),%edi
mov    0x2e55(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    $0xffffffff,%r15d
cmp    $0xffffffff,%eax
je     11f2 <ez_net_unix_server+0x92>
mov    %eax,%ebx
mov    %rsp,%r12
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x0(%r13),%eax
mov    %eax,0x4(%rsp)
mov    (%rsp),%edi
mov    %rbp,%rsi
mov    $0x3,%edx
call   1030 <strncpy@plt>
mov    %ebx,%edi
mov    %r12,%rsi
mov    $0x8,%edx
call   1060 <ez_net_bind@plt>
cmp    $0xffffffff,%eax
je     11eb <ez_net_unix_server+0x8b>
mov    %ebx,%edi
mov    %r14d,%esi
call   1040 <ez_net_listen@plt>
cmp    $0xffffffff,%eax
je     11eb <ez_net_unix_server+0x8b>
mov    %ebx,%r15d
jmp    11f2 <ez_net_unix_server+0x92>
mov    %ebx,%edi
call   1070 <ez_net_close_socket@plt>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
define i32 @ez_net_unix_server(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.sockaddr_un
store i8* %0, i8** %4
store i32 %1, i32* %5
%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* %6
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %13
12: ; preds = %2
store i32 -1, i32* %3
br label %38
13: ; preds = %2
%14 = call i32 @memset(%struct.sockaddr_un* %7, i32 0, i32 8)
%15 = load i32, i32* @AF_UNIX
%16 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %7, i32 0, i32 1
store i32 %15, i32* %16
%17 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %7, i32 0, i32 0
%18 = load i32, i32* %17
%19 = load i8*, i8** %4
%20 = call i32 @strncpy(i32 %18, i8* %19, i32 3)
%21 = load i32, i32* %6
%22 = bitcast %struct.sockaddr_un* %7 to %struct.sockaddr*
%23 = call i32 @ez_net_bind(i32 %21, %struct.sockaddr* %22, i32 8)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %28
25: ; preds = %13
%26 = load i32, i32* %6
%27 = call i32 @ez_net_close_socket(i32 %26)
store i32 -1, i32* %3
br label %38
28: ; preds = %13
%29 = load i32, i32* %6
%30 = load i32, i32* %5
%31 = call i32 @ez_net_listen(i32 %29, i32 %30)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %36
33: ; preds = %28
%34 = load i32, i32* %6
%35 = call i32 @ez_net_close_socket(i32 %34)
store i32 -1, i32* %3
br label %38
36: ; preds = %28
%37 = load i32, i32* %6
store i32 %37, i32* %3
br label %38
38: ; preds = %36, %33, %25, %12
%39 = load i32, i32* %3
ret i32 %39
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
define i32 @ez_net_unix_server(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_un
%4 = bitcast %struct.sockaddr_un* %3 to i8*
%5 = load i32, i32* @AF_UNIX
%6 = load i32, i32* @SOCK_STREAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = icmp eq i32 %7, -1
br i1 %8, label %24, label %9
9: ; preds = %2
%10 = call i32 @memset(%struct.sockaddr_un* %3, i32 0, i32 8)
%11 = load i32, i32* @AF_UNIX
%12 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i64 0, i32 1
store i32 %11, i32* %12
%13 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i64 0, i32 0
%14 = load i32, i32* %13
%15 = call i32 @strncpy(i32 %14, i8* %0, i32 3)
%16 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%17 = call i32 @ez_net_bind(i32 %7, %struct.sockaddr* %16, i32 8)
%18 = icmp eq i32 %17, -1
br i1 %18, label %22, label %19
19: ; preds = %9
%20 = call i32 @ez_net_listen(i32 %7, i32 %1)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %19, %9
%23 = call i32 @ez_net_close_socket(i32 %7)
br label %24
24: ; preds = %22, %19, %2
%25 = phi i32 [ -1, %2 ], [ %7, %19 ], [ -1, %22 ]
ret i32 %25
}