#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 /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int strlen(int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int);
int connect_unix_socket() {
  int s, len;
  struct sockaddr_un remote;
  if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
    perror("socket");
    return -1;
  }
  remote.sun_family = AF_UNIX;
  strncpy(remote.sun_path, "/var/run/qubes/qrexec_agent_fdpass",
          sizeof(remote.sun_path));
  len = strlen(remote.sun_path) + sizeof(remote.sun_family);
  if (connect(s, (struct sockaddr *)&remote, len) == -1) {
    perror("connect");
    exit(1);
  }
  return s;
}


x86 assembly (clang, -O0):
<connect_unix_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e79(%rip),%rax
mov    (%rax),%edi
mov    0x2e60(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    11a5 <connect_unix_socket+0x45>
lea    0xe6c(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    120a <connect_unix_socket+0xaa>
mov    0x2e3c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%edi
lea    0xe4c(%rip),%rsi
mov    $0x4,%edx
call   1030 <strncpy@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   1070 <connect@plt>
cmp    $0xffffffff,%eax
jne    1204 <connect_unix_socket+0xa4>
lea    0xe35(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_unix_socket>:
push   %r14
push   %rbx
push   %rax
mov    0x2e7d(%rip),%r14
mov    (%r14),%edi
mov    0x2e63(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     11be <connect_unix_socket+0x5e>
mov    (%r14),%eax
mov    %eax,0x4(%rsp)
lea    0xe74(%rip),%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
call   1040 <strlen@plt>
lea    0x4(%rax),%edx
mov    %rsp,%rsi
mov    %ebx,%edi
call   1070 <connect@plt>
cmp    $0xffffffff,%eax
je     11cc <connect_unix_socket+0x6c>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe3b(%rip),%rdi
call   1050 <perror@plt>
jmp    11b4 <connect_unix_socket+0x54>
lea    0xe57(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [35 x i8] c"/var/run/qubes/qrexec_agent_fdpass\00"
@.str.2 = [8 x i8] c"connect\00"
define i32 @connect_unix_socket() {
%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 %11
9: ; preds = %0
%10 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %33
11: ; preds = %0
%12 = load i32, i32* @AF_UNIX
%13 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 1
store i32 %12, i32* %13
%14 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 0
%15 = load i32, i32* %14
%16 = call i32 @strncpy(i32 %15, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i32 4)
%17 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 0
%18 = load i32, i32* %17
%19 = call i32 @strlen(i32 %18)
%20 = sext i32 %19 to i64
%21 = add i64 %20, 4
%22 = trunc i64 %21 to i32
store i32 %22, i32* %3
%23 = load i32, i32* %2
%24 = bitcast %struct.sockaddr_un* %4 to %struct.sockaddr*
%25 = load i32, i32* %3
%26 = call i32 @connect(i32 %23, %struct.sockaddr* %24, i32 %25)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %31
28: ; preds = %11
%29 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %11
%32 = load i32, i32* %2
store i32 %32, i32* %1
br label %33
33: ; preds = %31, %9
%34 = load i32, i32* %1
ret i32 %34
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [35 x i8] c"/var/run/qubes/qrexec_agent_fdpass\00"
@.str.2 = [8 x i8] c"connect\00"
define i32 @connect_unix_socket() {
%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 %7, label %9
7: ; preds = %0
%8 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %21
9: ; preds = %0
%10 = load i32, i32* @AF_UNIX
%11 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %1, i64 0, i32 1
store i32 %10, i32* %11
%12 = tail call i32 @strncpy(i32 undef, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i32 4)
%13 = tail call i32 @strlen(i32 undef)
%14 = add i32 %13, 4
%15 = bitcast %struct.sockaddr_un* %1 to %struct.sockaddr*
%16 = call i32 @connect(i32 %5, %struct.sockaddr* %15, i32 %14)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %21
18: ; preds = %9
%19 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %9, %7
ret i32 %5
}