#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 /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int errno;
int /*<<< orphan*/ log_debug(char *, char *, char const *);
int /*<<< orphan*/ log_warn(char *, char *, char const *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strlcpy(int /*<<< orphan*/, char const *, int);
int ctl_connect(const char *name) {
  int s;
  struct sockaddr_un su;
  int rc;
  log_debug("control", "connect to 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 (connect(s, (struct sockaddr *)&su, sizeof(struct sockaddr_un)) == -1) {
    rc = errno;
    log_warn("control", "unable to connect to socket %s", name);
    close(s);
    errno = rc;
    return -1;
  }
  return s;
}


x86 assembly (clang, -O0):
<ctl_connect>:
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_connect+0x54>
movl   $0xffffffff,-0x4(%rbp)
jmp    1234 <ctl_connect+0xd4>
mov    0x2e25(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1050 <strlcpy@plt>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
call   1070 <connect@plt>
cmp    $0xffffffff,%eax
jne    122e <ctl_connect+0xce>
mov    0x2dd6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdx
lea    0xdfe(%rip),%rdi
lea    0xe1c(%rip),%rsi
call   1060 <log_warn@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x24(%rbp),%ecx
mov    0x2da8(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1234 <ctl_connect+0xd4>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ctl_connect>:
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     120a <ctl_connect+0xaa>
mov    %eax,%ebp
mov    0x2e2e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    %rbx,%rsi
mov    $0x4,%edx
call   1050 <strlcpy@plt>
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
mov    $0x8,%edx
call   1070 <connect@plt>
cmp    $0xffffffff,%eax
je     11e0 <ctl_connect+0x80>
mov    %ebp,%r14d
jmp    120a <ctl_connect+0xaa>
mov    0x2de1(%rip),%r15
mov    (%r15),%r12d
lea    0xe0f(%rip),%rdi
lea    0xe2d(%rip),%rsi
mov    %rbx,%rdx
call   1060 <log_warn@plt>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %r12d,(%r15)
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 = [29 x i8] c"connect to 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 = [31 x i8] c"unable to connect to socket %s\00"
define i32 @ctl_connect(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 ([29 x i8], [29 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 %34
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 @connect(i32 %21, %struct.sockaddr* %22, i32 8)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %32
25: ; preds = %14
%26 = load i32, i32* @errno
store i32 %26, i32* %6
%27 = load i8*, i8** %3
%28 = call i32 @log_warn(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %27)
%29 = load i32, i32* %4
%30 = call i32 @close(i32 %29)
%31 = load i32, i32* %6
store i32 %31, i32* @errno
store i32 -1, i32* %2
br label %34
32: ; preds = %14
%33 = load i32, i32* %4
store i32 %33, i32* %2
br label %34
34: ; preds = %32, %25, %13
%35 = load i32, i32* %2
ret i32 %35
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [8 x i8] c"control\00"
@.str.1 = [29 x i8] c"connect to 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 = [31 x i8] c"unable to connect to socket %s\00"
define i32 @ctl_connect(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 ([29 x i8], [29 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 %20, 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 @connect(i32 %7, %struct.sockaddr* %13, i32 8)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %20
16: ; preds = %9
%17 = load i32, i32* @errno
%18 = call i32 @log_warn(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%19 = call i32 @close(i32 %7)
store i32 %17, i32* @errno
br label %20
20: ; preds = %9, %1, %16
%21 = phi i32 [ -1, %16 ], [ -1, %1 ], [ %7, %9 ]
ret i32 %21
}