#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*/ SOCK_STREAM;
int connect(int, struct sockaddr *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
int strlen(int /*<<< orphan*/);
int connect_to_daemon(void) {
  int s, len;
  struct sockaddr_un remote;
  if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
    return -1;
  remote.sun_family = AF_UNIX;
  strcpy(remote.sun_path, "/tmp/q.socket");
  len = strlen(remote.sun_path) + sizeof(remote.sun_family);
  if (connect(s, (struct sockaddr *)&remote, len) == -1)
    return -1;
  return s;
}


x86 assembly (clang, -O0):
<connect_to_daemon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e99(%rip),%rax
mov    (%rax),%edi
mov    0x2e80(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    1179 <connect_to_daemon+0x39>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cf <connect_to_daemon+0x8f>
mov    0x2e68(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%edi
lea    0xe71(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x18(%rbp),%edi
call   1040 <strlen@plt>
cltq
add    $0x4,%rax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1050 <connect@plt>
cmp    $0xffffffff,%eax
jne    11c9 <connect_to_daemon+0x89>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cf <connect_to_daemon+0x8f>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_to_daemon>:
push   %r14
push   %rbx
push   %rax
mov    0x2e9d(%rip),%r14
mov    (%r14),%edi
mov    0x2e83(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
cmp    $0xffffffff,%eax
je     1192 <connect_to_daemon+0x52>
mov    %eax,%ebx
mov    (%r14),%eax
mov    %eax,0x4(%rsp)
lea    0xe8d(%rip),%rsi
call   1030 <strcpy@plt>
call   1040 <strlen@plt>
lea    0x4(%rax),%edx
mov    %rsp,%rsi
mov    %ebx,%edi
call   1050 <connect@plt>
cmp    $0xffffffff,%eax
cmove  %eax,%ebx
jmp    1197 <connect_to_daemon+0x57>
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 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [14 x i8] c"/tmp/q.socket\00"
define i32 @connect_to_daemon() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_un
%5 = load i32, i32* @AF_UNIX
%6 = load i32, i32* @SOCK_STREAM
%7 = call i32 @socket(i32 %5, i32 %6, i32 0)
store i32 %7, i32* %2
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %10
9: ; preds = %0
store i32 -1, i32* %1
br label %30
10: ; preds = %0
%11 = load i32, i32* @AF_UNIX
%12 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 1
store i32 %11, i32* %12
%13 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 0
%14 = load i32, i32* %13
%15 = call i32 @strcpy(i32 %14, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%16 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 0
%17 = load i32, i32* %16
%18 = call i32 @strlen(i32 %17)
%19 = sext i32 %18 to i64
%20 = add i64 %19, 4
%21 = trunc i64 %20 to i32
store i32 %21, i32* %3
%22 = load i32, i32* %2
%23 = bitcast %struct.sockaddr_un* %4 to %struct.sockaddr*
%24 = load i32, i32* %3
%25 = call i32 @connect(i32 %22, %struct.sockaddr* %23, i32 %24)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %28
27: ; preds = %10
store i32 -1, i32* %1
br label %30
28: ; preds = %10
%29 = load i32, i32* %2
store i32 %29, i32* %1
br label %30
30: ; preds = %28, %27, %9
%31 = load i32, i32* %1
ret i32 %31
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [14 x i8] c"/tmp/q.socket\00"
define i32 @connect_to_daemon() {
%1 = alloca %struct.sockaddr_un
%2 = bitcast %struct.sockaddr_un* %1 to i8*
%3 = load i32, i32* @AF_UNIX
%4 = load i32, i32* @SOCK_STREAM
%5 = tail call i32 @socket(i32 %3, i32 %4, i32 0)
%6 = icmp eq i32 %5, -1
br i1 %6, label %17, label %7
7: ; preds = %0
%8 = load i32, i32* @AF_UNIX
%9 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %1, i64 0, i32 1
store i32 %8, i32* %9
%10 = tail call i32 @strcpy(i32 undef, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 @strlen(i32 undef)
%12 = add i32 %11, 4
%13 = bitcast %struct.sockaddr_un* %1 to %struct.sockaddr*
%14 = call i32 @connect(i32 %5, %struct.sockaddr* %13, i32 %12)
%15 = icmp eq i32 %14, -1
%16 = select i1 %15, i32 -1, i32 %5
br label %17
17: ; preds = %7, %0
%18 = phi i32 [ -1, %0 ], [ %16, %7 ]
ret i32 %18
}