#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*/ PF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ uds_set_addr(struct sockaddr_un *, char const *);
int uds_client_connect(const char *sock_name) {
  struct sockaddr_un addr;
  int sock = socket(PF_UNIX, SOCK_STREAM, 0);
  if (sock < 0) {
    perror("socket failed");
    return -1;
  }
  uds_set_addr(&addr, sock_name);
  if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) != 0) {
    perror("connect failed");
    return -1;
  }
  return sock;
}


x86 assembly (clang, -O0):
<uds_client_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2ea5(%rip),%rax
mov    (%rax),%edi
mov    0x2e7c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    118a <uds_client_connect+0x4a>
lea    0xe87(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d0 <uds_client_connect+0x90>
mov    -0x10(%rbp),%rsi
lea    -0x18(%rbp),%rdi
call   1030 <uds_set_addr@plt>
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1050 <connect@plt>
cmp    $0x0,%rax
je     11ca <uds_client_connect+0x8a>
lea    0xe55(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d0 <uds_client_connect+0x90>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<uds_client_connect>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
mov    0x2ea5(%rip),%rax
mov    (%rax),%edi
mov    0x2e7c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
test   %eax,%eax
js     119b <uds_client_connect+0x5b>
mov    %eax,%ebx
lea    0x8(%rsp),%r15
mov    %r15,%rdi
mov    %r14,%rsi
call   1030 <uds_set_addr@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x4,%edx
call   1050 <connect@plt>
test   %rax,%rax
jne    11a4 <uds_client_connect+0x64>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe5e(%rip),%rdi
jmp    11ab <uds_client_connect+0x6b>
lea    0xe63(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%ebx
jmp    118f <uds_client_connect+0x4f>

LLVM IR (-O0):
%struct.sockaddr_un = type { i32 }
%struct.sockaddr = type { i32 }
@PF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [14 x i8] c"socket failed\00"
@.str.1 = [15 x i8] c"connect failed\00"
define i32 @uds_client_connect(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.sockaddr_un
%5 = alloca i32
store i8* %0, i8** %3
%6 = load i32, i32* @PF_UNIX
%7 = load i32, i32* @SOCK_STREAM
%8 = call i32 @socket(i32 %6, i32 %7, i32 0)
store i32 %8, i32* %5
%9 = load i32, i32* %5
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %24
13: ; preds = %1
%14 = load i8*, i8** %3
%15 = call i32 @uds_set_addr(%struct.sockaddr_un* %4, i8* %14)
%16 = load i32, i32* %5
%17 = bitcast %struct.sockaddr_un* %4 to %struct.sockaddr*
%18 = call i64 @connect(i32 %16, %struct.sockaddr* %17, i32 4)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %13
%21 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %24
22: ; preds = %13
%23 = load i32, i32* %5
store i32 %23, i32* %2
br label %24
24: ; preds = %22, %20, %11
%25 = load i32, i32* %2
ret i32 %25
}

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