#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;
};
typedef int /*<<< orphan*/ sock;

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int errno;
int /*<<< orphan*/ snprintf(int /*<<< orphan*/, int, char *, char const *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int os_connect_socket(const char *name) {
  struct sockaddr_un sock;
  int fd, err;
  sock.sun_family = AF_UNIX;
  snprintf(sock.sun_path, sizeof(sock.sun_path), "%s", name);
  fd = socket(AF_UNIX, SOCK_STREAM, 0);
  if (fd < 0) {
    err = -errno;
    goto out;
  }
  err = connect(fd, (struct sockaddr *)&sock, sizeof(sock));
  if (err) {
    err = -errno;
    goto out_close;
  }
  return fd;
out_close:
  close(fd);
out:
  return err;
}


x86 assembly (clang, -O0):
<os_connect_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e95(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%rcx
mov    $0x4,%esi
lea    0xe95(%rip),%rdx
call   1030 <snprintf@plt>
mov    0x2e71(%rip),%rax
mov    (%rax),%edi
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11a9 <os_connect_socket+0x69>
xor    %eax,%eax
mov    0x2e31(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x20(%rbp)
jmp    11ed <os_connect_socket+0xad>
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x8,%edx
call   1050 <connect@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     11da <os_connect_socket+0x9a>
xor    %eax,%eax
mov    0x2e00(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x20(%rbp)
jmp    11e5 <os_connect_socket+0xa5>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11f3 <os_connect_socket+0xb3>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<os_connect_socket>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rcx
mov    0x2e9b(%rip),%rbx
mov    (%rbx),%eax
mov    %eax,0x4(%rsp)
lea    0xea6(%rip),%rdx
mov    $0x4,%esi
call   1030 <snprintf@plt>
mov    (%rbx),%edi
mov    0x2e6b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
test   %eax,%eax
js     11a3 <os_connect_socket+0x63>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x8,%edx
call   1050 <connect@plt>
test   %eax,%eax
je     11ae <os_connect_socket+0x6e>
mov    0x2e3a(%rip),%rax
mov    (%rax),%ebp
mov    %ebx,%edi
call   1040 <close@plt>
mov    %ebp,%ebx
jmp    11ac <os_connect_socket+0x6c>
mov    0x2e26(%rip),%rax
mov    (%rax),%ebx
neg    %ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@.str = [3 x i8] c"%s\00"
@SOCK_STREAM = global i32 0
@errno = global i32 0
define i32 @os_connect_socket(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.sockaddr_un
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
%7 = load i32, i32* @AF_UNIX
%8 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 1
store i32 %7, i32* %8
%9 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 0
%10 = load i32, i32* %9
%11 = load i8*, i8** %3
%12 = call i32 @snprintf(i32 %10, i32 4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %11)
%13 = load i32, i32* @AF_UNIX
%14 = load i32, i32* @SOCK_STREAM
%15 = call i32 @socket(i32 %13, i32 %14, i32 0)
store i32 %15, i32* %5
%16 = load i32, i32* %5
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %1
%19 = load i32, i32* @errno
%20 = sub nsw i32 0, %19
store i32 %20, i32* %6
br label %35
21: ; preds = %1
%22 = load i32, i32* %5
%23 = bitcast %struct.sockaddr_un* %4 to %struct.sockaddr*
%24 = call i32 @connect(i32 %22, %struct.sockaddr* %23, i32 8)
store i32 %24, i32* %6
%25 = load i32, i32* %6
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %21
%28 = load i32, i32* @errno
%29 = sub nsw i32 0, %28
store i32 %29, i32* %6
br label %32
30: ; preds = %21
%31 = load i32, i32* %5
store i32 %31, i32* %2
br label %37
32: ; preds = %27
%33 = load i32, i32* %5
%34 = call i32 @close(i32 %33)
br label %35
35: ; preds = %32, %18
%36 = load i32, i32* %6
store i32 %36, i32* %2
br label %37
37: ; preds = %35, %30
%38 = load i32, i32* %2
ret i32 %38
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@.str = [3 x i8] c"%s\00"
@SOCK_STREAM = global i32 0
@errno = global i32 0
define i32 @os_connect_socket(i8* %0) {
%2 = alloca %struct.sockaddr_un
%3 = bitcast %struct.sockaddr_un* %2 to i8*
%4 = load i32, i32* @AF_UNIX
%5 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
store i32 %4, i32* %5
%6 = tail call i32 @snprintf(i32 undef, i32 4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %0)
%7 = load i32, i32* @AF_UNIX
%8 = load i32, i32* @SOCK_STREAM
%9 = tail call i32 @socket(i32 %7, i32 %8, i32 0)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = load i32, i32* @errno
br label %20
13: ; preds = %1
%14 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%15 = call i32 @connect(i32 %9, %struct.sockaddr* %14, i32 8)
%16 = icmp eq i32 %15, 0
br i1 %16, label %23, label %17
17: ; preds = %13
%18 = load i32, i32* @errno
%19 = call i32 @close(i32 %9)
br label %20
20: ; preds = %17, %11
%21 = phi i32 [ %12, %11 ], [ %18, %17 ]
%22 = sub nsw i32 0, %21
br label %23
23: ; preds = %13, %20
%24 = phi i32 [ %22, %20 ], [ %9, %13 ]
ret i32 %24
}