#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*/ PF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int errno;
int listen(int, int);
int /*<<< orphan*/ log_debug(char *, char *, char const *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strlcpy(int /*<<< orphan*/, char const *, int);
int ctl_create(const char *name) {
  int s;
  struct sockaddr_un su;
  int rc;
  log_debug("control", "create control socket %s", name);
  if ((s = socket(PF_UNIX, SOCK_STREAM, 0)) == -1)
    return -1;
  su.sun_family = AF_UNIX;
  strlcpy(su.sun_path, name, sizeof(su.sun_path));
  if (bind(s, (struct sockaddr *)&su, sizeof(struct sockaddr_un)) == -1) {
    rc = errno;
    close(s);
    errno = rc;
    return -1;
  }
  log_debug("control", "listen to control socket %s", name);
  if (listen(s, 5) == -1) {
    rc = errno;
    close(s);
    errno = rc;
    log_debug("control", "cannot listen to control socket %s", name);
    return -1;
  }
  return s;
}


x86 assembly (clang, -O0):
<ctl_create>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdx
lea    0xe89(%rip),%rdi
lea    0xe8a(%rip),%rsi
call   1030 <log_debug@plt>
mov    0x2e6e(%rip),%rax
mov    (%rax),%edi
mov    0x2e3d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    11b4 <ctl_create+0x54>
movl   $0xffffffff,-0x4(%rbp)
jmp    128d <ctl_create+0x12d>
mov    0x2e25(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1060 <strlcpy@plt>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
call   1070 <bind@plt>
cmp    $0xffffffff,%eax
jne    1217 <ctl_create+0xb7>
mov    0x2dd6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x24(%rbp),%ecx
mov    0x2dbf(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    128d <ctl_create+0x12d>
mov    -0x10(%rbp),%rdx
lea    0xdde(%rip),%rdi
lea    0xdff(%rip),%rsi
call   1030 <log_debug@plt>
mov    -0x14(%rbp),%edi
mov    $0x5,%esi
call   1050 <listen@plt>
cmp    $0xffffffff,%eax
jne    1287 <ctl_create+0x127>
mov    0x2d7d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x24(%rbp),%ecx
mov    0x2d66(%rip),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rdx
lea    0xd91(%rip),%rdi
lea    0xdab(%rip),%rsi
call   1030 <log_debug@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    128d <ctl_create+0x12d>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ctl_create>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
lea    0xe8a(%rip),%rdi
lea    0xe8b(%rip),%rsi
mov    %rbx,%rdx
call   1030 <log_debug@plt>
mov    0x2e6c(%rip),%rax
mov    (%rax),%edi
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    $0xffffffff,%r14d
cmp    $0xffffffff,%eax
je     124b <ctl_create+0xeb>
mov    %eax,%ebp
mov    0x2e2a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    %rbx,%rsi
mov    $0x4,%edx
call   1060 <strlcpy@plt>
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
mov    $0x8,%edx
call   1070 <bind@plt>
cmp    $0xffffffff,%eax
je     120b <ctl_create+0xab>
lea    0xe1a(%rip),%rdi
lea    0xe3b(%rip),%rsi
mov    %rbx,%rdx
call   1030 <log_debug@plt>
mov    %ebp,%edi
mov    $0x5,%esi
call   1050 <listen@plt>
cmp    $0xffffffff,%eax
je     1221 <ctl_create+0xc1>
mov    %ebp,%r14d
jmp    124b <ctl_create+0xeb>
mov    0x2db6(%rip),%r15
mov    (%r15),%ebx
mov    %ebp,%edi
call   1040 <close@plt>
mov    %ebx,(%r15)
jmp    124b <ctl_create+0xeb>
mov    0x2da0(%rip),%r15
mov    (%r15),%r12d
mov    %ebp,%edi
call   1040 <close@plt>
mov    %r12d,(%r15)
lea    0xdc4(%rip),%rdi
lea    0xdde(%rip),%rsi
mov    %rbx,%rdx
call   1030 <log_debug@plt>
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [8 x i8] c"control\00"
@.str.1 = [25 x i8] c"create control socket %s\00"
@PF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@AF_UNIX = global i32 0
@errno = global i32 0
@.str.2 = [28 x i8] c"listen to control socket %s\00"
@.str.3 = [35 x i8] c"cannot listen to control socket %s\00"
define i32 @ctl_create(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.sockaddr_un
%6 = alloca i32
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = call i32 @log_debug(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %7)
%9 = load i32, i32* @PF_UNIX
%10 = load i32, i32* @SOCK_STREAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %4
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %14
13: ; preds = %1
store i32 -1, i32* %2
br label %45
14: ; preds = %1
%15 = load i32, i32* @AF_UNIX
%16 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 1
store i32 %15, i32* %16
%17 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 0
%18 = load i32, i32* %17
%19 = load i8*, i8** %3
%20 = call i32 @strlcpy(i32 %18, i8* %19, i32 4)
%21 = load i32, i32* %4
%22 = bitcast %struct.sockaddr_un* %5 to %struct.sockaddr*
%23 = call i32 @bind(i32 %21, %struct.sockaddr* %22, i32 8)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %30
25: ; preds = %14
%26 = load i32, i32* @errno
store i32 %26, i32* %6
%27 = load i32, i32* %4
%28 = call i32 @close(i32 %27)
%29 = load i32, i32* %6
store i32 %29, i32* @errno
store i32 -1, i32* %2
br label %45
30: ; preds = %14
%31 = load i8*, i8** %3
%32 = call i32 @log_debug(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %31)
%33 = load i32, i32* %4
%34 = call i32 @listen(i32 %33, i32 5)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %43
36: ; preds = %30
%37 = load i32, i32* @errno
store i32 %37, i32* %6
%38 = load i32, i32* %4
%39 = call i32 @close(i32 %38)
%40 = load i32, i32* %6
store i32 %40, i32* @errno
%41 = load i8*, i8** %3
%42 = call i32 @log_debug(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i8* %41)
store i32 -1, i32* %2
br label %45
43: ; preds = %30
%44 = load i32, i32* %4
store i32 %44, i32* %2
br label %45
45: ; preds = %43, %36, %25, %13
%46 = load i32, i32* %2
ret i32 %46
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [8 x i8] c"control\00"
@.str.1 = [25 x i8] c"create control socket %s\00"
@PF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@AF_UNIX = global i32 0
@errno = global i32 0
@.str.2 = [28 x i8] c"listen to control socket %s\00"
@.str.3 = [35 x i8] c"cannot listen to control socket %s\00"
define i32 @ctl_create(i8* %0) {
%2 = alloca %struct.sockaddr_un
%3 = bitcast %struct.sockaddr_un* %2 to i8*
%4 = tail call i32 @log_debug(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%5 = load i32, i32* @PF_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 %27, label %9
9: ; preds = %1
%10 = load i32, i32* @AF_UNIX
%11 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
store i32 %10, i32* %11
%12 = tail call i32 @strlcpy(i32 undef, i8* %0, i32 4)
%13 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%14 = call i32 @bind(i32 %7, %struct.sockaddr* %13, i32 8)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %19
16: ; preds = %9
%17 = load i32, i32* @errno
%18 = call i32 @close(i32 %7)
store i32 %17, i32* @errno
br label %27
19: ; preds = %9
%20 = call i32 @log_debug(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%21 = call i32 @listen(i32 %7, i32 5)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %27
23: ; preds = %19
%24 = load i32, i32* @errno
%25 = call i32 @close(i32 %7)
store i32 %24, i32* @errno
%26 = call i32 @log_debug(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br label %27
27: ; preds = %19, %1, %23, %16
%28 = phi i32 [ -1, %16 ], [ -1, %23 ], [ -1, %1 ], [ %7, %19 ]
ret i32 %28
}