#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 {
  scalar_t__ *sun_path;
  int /*<<< orphan*/ sun_family;
};
struct sockaddr {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ AF_LOCAL;
int /*<<< orphan*/ PF_LOCAL;
int /*<<< orphan*/ SOCK_STREAM;
size_t SUN_LEN(struct sockaddr_un *);
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, size_t);
int errno;
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(scalar_t__ *, char const *, int);
int connect_to_socket(const char *filename, int *new_sock) {
  struct sockaddr_un srvr_addr;
  size_t len;
  int sock;
  int ret;
  int err;
  sock = socket(PF_LOCAL, SOCK_STREAM, 0);
  if (sock == -1) {
    err = errno;
    goto out;
  }
  memset(&srvr_addr, 0, sizeof srvr_addr);
  srvr_addr.sun_family = AF_LOCAL;
  strncpy(srvr_addr.sun_path, filename, sizeof(srvr_addr.sun_path) - 1);
  srvr_addr.sun_path[sizeof(srvr_addr.sun_path) - 1] = 0;
  len = SUN_LEN(&srvr_addr);
  ret = connect(sock, (struct sockaddr *)&srvr_addr, len);
  if (ret == -1) {
    close(sock);
    err = errno;
    goto out;
  }
  *new_sock = sock;
  err = 0;
out:
  return err;
}


x86 assembly (clang, -O0):
<connect_to_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e51(%rip),%rax
mov    (%rax),%edi
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    11a7 <connect_to_socket+0x47>
mov    0x2e33(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
jmp    1234 <connect_to_socket+0xd4>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    0x2e2a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    $0x7,%edx
call   1030 <strncpy@plt>
mov    -0x20(%rbp),%rax
movq   $0x0,0x38(%rax)
lea    -0x20(%rbp),%rdi
call   1070 <SUN_LEN@plt>
mov    %rax,-0x28(%rbp)
mov    -0x2c(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1060 <connect@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0xffffffff,-0x30(%rbp)
jne    1224 <connect_to_socket+0xc4>
mov    -0x2c(%rbp),%edi
call   1050 <close@plt>
mov    0x2db6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
jmp    1234 <connect_to_socket+0xd4>
mov    -0x2c(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_to_socket>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2e4f(%rip),%rax
mov    (%rax),%edi
mov    0x2e56(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
cmp    $0xffffffff,%eax
je     11f6 <connect_to_socket+0x96>
mov    %eax,%ebp
mov    %rsp,%r12
xor    %ebx,%ebx
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    0x2e3b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    (%rsp),%rdi
mov    %r15,%rsi
mov    $0x7,%edx
call   1030 <strncpy@plt>
mov    (%rsp),%rax
movq   $0x0,0x38(%rax)
mov    %r12,%rdi
call   1070 <SUN_LEN@plt>
mov    %ebp,%edi
mov    %r12,%rsi
mov    %rax,%rdx
call   1060 <connect@plt>
cmp    $0xffffffff,%eax
je     11ef <connect_to_socket+0x8f>
mov    %ebp,(%r14)
jmp    11ff <connect_to_socket+0x9f>
mov    %ebp,%edi
call   1050 <close@plt>
mov    0x2dd3(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i64*, i32 }
%struct.sockaddr = type { i32 }
@PF_LOCAL = global i32 0
@SOCK_STREAM = global i32 0
@errno = global i32 0
@AF_LOCAL = global i32 0
define i32 @connect_to_socket(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca %struct.sockaddr_un
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %3
store i32* %1, i32** %4
%10 = load i32, i32* @PF_LOCAL
%11 = load i32, i32* @SOCK_STREAM
%12 = call i32 @socket(i32 %10, i32 %11, i32 0)
store i32 %12, i32* %7
%13 = load i32, i32* %7
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = load i32, i32* @errno
store i32 %16, i32* %9
br label %42
17: ; preds = %2
%18 = call i32 @memset(%struct.sockaddr_un* %5, i32 0, i32 16)
%19 = load i32, i32* @AF_LOCAL
%20 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 1
store i32 %19, i32* %20
%21 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 0
%22 = load i64*, i64** %21
%23 = load i8*, i8** %3
%24 = call i32 @strncpy(i64* %22, i8* %23, i32 7)
%25 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 0
%26 = load i64*, i64** %25
%27 = getelementptr inbounds i64, i64* %26, i64 7
store i64 0, i64* %27
%28 = call i64 @SUN_LEN(%struct.sockaddr_un* %5)
store i64 %28, i64* %6
%29 = load i32, i32* %7
%30 = bitcast %struct.sockaddr_un* %5 to %struct.sockaddr*
%31 = load i64, i64* %6
%32 = call i32 @connect(i32 %29, %struct.sockaddr* %30, i64 %31)
store i32 %32, i32* %8
%33 = load i32, i32* %8
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %39
35: ; preds = %17
%36 = load i32, i32* %7
%37 = call i32 @close(i32 %36)
%38 = load i32, i32* @errno
store i32 %38, i32* %9
br label %42
39: ; preds = %17
%40 = load i32, i32* %7
%41 = load i32*, i32** %4
store i32 %40, i32* %41
store i32 0, i32* %9
br label %42
42: ; preds = %39, %35, %15
%43 = load i32, i32* %9
ret i32 %43
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i64*, i32 }
%struct.sockaddr = type { i32 }
@PF_LOCAL = global i32 0
@SOCK_STREAM = global i32 0
@errno = global i32 0
@AF_LOCAL = global i32 0
define i32 @connect_to_socket(i8* %0, i32* nocapture writeonly %1) {
%3 = alloca %struct.sockaddr_un
%4 = bitcast %struct.sockaddr_un* %3 to i8*
%5 = load i32, i32* @PF_LOCAL
%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 %9, label %11
9: ; preds = %2
%10 = load i32, i32* @errno
br label %28
11: ; preds = %2
%12 = call i32 @memset(%struct.sockaddr_un* %3, i32 0, i32 16)
%13 = load i32, i32* @AF_LOCAL
%14 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i64 0, i32 1
store i32 %13, i32* %14
%15 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i64 0, i32 0
%16 = load i64*, i64** %15
%17 = call i32 @strncpy(i64* %16, i8* %0, i32 7)
%18 = load i64*, i64** %15
%19 = getelementptr inbounds i64, i64* %18, i64 7
store i64 0, i64* %19
%20 = call i64 @SUN_LEN(%struct.sockaddr_un* %3)
%21 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%22 = call i32 @connect(i32 %7, %struct.sockaddr* %21, i64 %20)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %27
24: ; preds = %11
%25 = call i32 @close(i32 %7)
%26 = load i32, i32* @errno
br label %28
27: ; preds = %11
store i32 %7, i32* %1
br label %28
28: ; preds = %27, %24, %9
%29 = phi i32 [ %10, %9 ], [ %26, %24 ], [ 0, %27 ]
ret i32 %29
}