#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*/ sa_family_t;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_LOCAL;
int /*<<< orphan*/ SOCK_STREAM;
int bind(int, struct sockaddr *, scalar_t__);
int /*<<< orphan*/ close(int);
int listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/ *, char const *);
scalar_t__ strlen(int /*<<< orphan*/ *);
int client_listen(const char *path) {
  struct sockaddr_un addr;
  socklen_t addrlen;
  int rv, s;
  s = socket(AF_LOCAL, SOCK_STREAM, 0);
  if (s < 0) {
    return s;
  }
  memset(&addr, 0, sizeof(addr));
  addr.sun_family = AF_LOCAL;
  strcpy(&addr.sun_path[1], path);
  addrlen = sizeof(sa_family_t) + strlen(addr.sun_path + 1) + 1;
  rv = bind(s, (struct sockaddr *)&addr, addrlen);
  if (rv < 0) {
    close(s);
    return rv;
  }
  rv = listen(s, 5);
  if (rv < 0) {
    close(s);
    return rv;
  }
  return s;
}


x86 assembly (clang, -O0):
<client_listen>:
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   1090 <socket@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jge    11ad <client_listen+0x3d>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1256 <client_listen+0xe6>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    0x2e24(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x20(%rbp),%rdi
add    $0x4,%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x20(%rbp),%rdi
add    $0x4,%rdi
call   1040 <strlen@plt>
add    $0x4,%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1080 <bind@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    1223 <client_listen+0xb3>
mov    -0x30(%rbp),%edi
call   1060 <close@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1256 <client_listen+0xe6>
mov    -0x30(%rbp),%edi
mov    $0x5,%esi
call   1070 <listen@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    1250 <client_listen+0xe0>
mov    -0x30(%rbp),%edi
call   1060 <close@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1256 <client_listen+0xe6>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<client_listen>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
mov    0x2e64(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2e4a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1204 <client_listen+0x94>
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x10(%rsp)
mov    0x8(%rsp),%rdi
add    $0x4,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
mov    0x8(%rsp),%rdi
add    $0x4,%rdi
call   1040 <strlen@plt>
lea    0x5(%rax),%rdx
mov    %ebx,%edi
mov    %r15,%rsi
call   1080 <bind@plt>
test   %eax,%eax
js     11f9 <client_listen+0x89>
mov    %ebx,%edi
mov    $0x5,%esi
call   1070 <listen@plt>
test   %eax,%eax
jns    1204 <client_listen+0x94>
mov    %eax,%ebp
mov    %ebx,%edi
call   1060 <close@plt>
mov    %ebp,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i32*, i32 }
%struct.sockaddr = type { i32 }
@AF_LOCAL = global i32 0
@SOCK_STREAM = global i32 0
define i32 @client_listen(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.sockaddr_un
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
%8 = load i32, i32* @AF_LOCAL
%9 = load i32, i32* @SOCK_STREAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %7
%11 = load i32, i32* %7
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %1
%14 = load i32, i32* %7
store i32 %14, i32* %2
br label %51
15: ; preds = %1
%16 = call i32 @memset(%struct.sockaddr_un* %4, i32 0, i32 16)
%17 = load i32, i32* @AF_LOCAL
%18 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 1
store i32 %17, i32* %18
%19 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 0
%20 = load i32*, i32** %19
%21 = getelementptr inbounds i32, i32* %20, i64 1
%22 = load i8*, i8** %3
%23 = call i32 @strcpy(i32* %21, i8* %22)
%24 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 0
%25 = load i32*, i32** %24
%26 = getelementptr inbounds i32, i32* %25, i64 1
%27 = call i64 @strlen(i32* %26)
%28 = add i64 4, %27
%29 = add i64 %28, 1
store i64 %29, i64* %5
%30 = load i32, i32* %7
%31 = bitcast %struct.sockaddr_un* %4 to %struct.sockaddr*
%32 = load i64, i64* %5
%33 = call i32 @bind(i32 %30, %struct.sockaddr* %31, i64 %32)
store i32 %33, i32* %6
%34 = load i32, i32* %6
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %40
36: ; preds = %15
%37 = load i32, i32* %7
%38 = call i32 @close(i32 %37)
%39 = load i32, i32* %6
store i32 %39, i32* %2
br label %51
40: ; preds = %15
%41 = load i32, i32* %7
%42 = call i32 @listen(i32 %41, i32 5)
store i32 %42, i32* %6
%43 = load i32, i32* %6
%44 = icmp slt i32 %43, 0
br i1 %44, label %45, label %49
45: ; preds = %40
%46 = load i32, i32* %7
%47 = call i32 @close(i32 %46)
%48 = load i32, i32* %6
store i32 %48, i32* %2
br label %51
49: ; preds = %40
%50 = load i32, i32* %7
store i32 %50, i32* %2
br label %51
51: ; preds = %49, %45, %36, %13
%52 = load i32, i32* %2
ret i32 %52
}

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