#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 dummy;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int unix_socket_initialize(struct sockaddr_un *, char const *);
int unix_socket_connect(const char *path) {
  struct sockaddr_un addr;
  int fd;
  fd = unix_socket_initialize(&addr, path);
  if (fd < 0)
    return -1;
  if (connect(fd, (struct sockaddr *)&addr, sizeof(addr))) {
    close(fd);
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<unix_socket_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rsi
lea    -0x18(%rbp),%rdi
call   1030 <unix_socket_initialize@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1162 <unix_socket_connect+0x32>
movl   $0xffffffff,-0x4(%rbp)
jmp    1197 <unix_socket_connect+0x67>
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1050 <connect@plt>
cmp    $0x0,%rax
je     1191 <unix_socket_connect+0x61>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1197 <unix_socket_connect+0x67>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unix_socket_connect>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rsi
mov    %rsp,%rdi
call   1030 <unix_socket_initialize@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
js     1168 <unix_socket_connect+0x38>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x4,%edx
call   1050 <connect@plt>
test   %rax,%rax
je     1166 <unix_socket_connect+0x36>
mov    %ebp,%edi
call   1040 <close@plt>
jmp    1168 <unix_socket_connect+0x38>
mov    %ebp,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i32 }
%struct.sockaddr = type { i32 }
define i32 @unix_socket_connect(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.sockaddr_un
%5 = alloca i32
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i32 @unix_socket_initialize(%struct.sockaddr_un* %4, i8* %6)
store i32 %7, i32* %5
%8 = load i32, i32* %5
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %1
store i32 -1, i32* %2
br label %21
11: ; preds = %1
%12 = load i32, i32* %5
%13 = bitcast %struct.sockaddr_un* %4 to %struct.sockaddr*
%14 = call i64 @connect(i32 %12, %struct.sockaddr* %13, i32 4)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %11
%17 = load i32, i32* %5
%18 = call i32 @close(i32 %17)
store i32 -1, i32* %2
br label %21
19: ; preds = %11
%20 = load i32, i32* %5
store i32 %20, i32* %2
br label %21
21: ; preds = %19, %16, %10
%22 = load i32, i32* %2
ret i32 %22
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32 }
%struct.sockaddr = type { i32 }
define i32 @unix_socket_connect(i8* %0) {
%2 = alloca %struct.sockaddr_un
%3 = bitcast %struct.sockaddr_un* %2 to i8*
%4 = call i32 @unix_socket_initialize(%struct.sockaddr_un* %2, i8* %0)
%5 = icmp slt i32 %4, 0
br i1 %5, label %12, label %6
6: ; preds = %1
%7 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%8 = call i64 @connect(i32 %4, %struct.sockaddr* %7, i32 4)
%9 = icmp eq i64 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %6
%11 = call i32 @close(i32 %4)
br label %12
12: ; preds = %6, %1, %10
%13 = phi i32 [ -1, %10 ], [ -1, %1 ], [ %4, %6 ]
ret i32 %13
}