#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_LOCAL;
int /*<<< orphan*/ PF_LOCAL;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SUN_LEN(struct sockaddr_un *);
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ socket_name();
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, int /*<<< orphan*/);
int server_connect() {
  struct sockaddr_un sock_name;
  int sock;
  if ((sock = socket(PF_LOCAL, SOCK_STREAM, 0)) == -1)
    return -1;
  sock_name.sun_family = AF_LOCAL;
  strcpy(sock_name.sun_path, socket_name());
  if (connect(sock, (struct sockaddr *)&sock_name, SUN_LEN(&sock_name)) == -1) {
    close(sock);
    return -1;
  }
  return sock;
}


x86 assembly (clang, -O0):
<server_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    0x2e61(%rip),%rax
mov    (%rax),%edi
mov    0x2e60(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    1199 <server_connect+0x39>
movl   $0xffffffff,-0x4(%rbp)
jmp    1204 <server_connect+0xa4>
mov    0x2e48(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    $0x0,%al
call   1040 <socket_name@plt>
mov    -0x28(%rbp),%edi
mov    %eax,%esi
call   1030 <strcpy@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x24(%rbp)
lea    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
lea    -0x10(%rbp),%rdi
call   1070 <SUN_LEN@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    %eax,%edx
call   1060 <connect@plt>
cmp    $0xffffffff,%eax
jne    11fe <server_connect+0x9e>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1204 <server_connect+0xa4>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<server_connect>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e61(%rip),%rax
mov    (%rax),%edi
mov    0x2e60(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    $0xffffffff,%ebx
cmp    $0xffffffff,%eax
je     11d1 <server_connect+0x71>
mov    %eax,%ebp
mov    0x2e54(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
xor    %eax,%eax
call   1040 <socket_name@plt>
mov    %eax,%esi
call   1030 <strcpy@plt>
lea    0x8(%rsp),%r14
mov    %r14,%rdi
call   1070 <SUN_LEN@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1060 <connect@plt>
cmp    $0xffffffff,%eax
je     11ca <server_connect+0x6a>
mov    %ebp,%ebx
jmp    11d1 <server_connect+0x71>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@PF_LOCAL = global i32 0
@SOCK_STREAM = global i32 0
@AF_LOCAL = global i32 0
define i32 @server_connect() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_un
%3 = alloca i32
%4 = load i32, i32* @PF_LOCAL
%5 = load i32, i32* @SOCK_STREAM
%6 = call i32 @socket(i32 %4, i32 %5, i32 0)
store i32 %6, i32* %3
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %9
8: ; preds = %0
store i32 -1, i32* %1
br label %26
9: ; preds = %0
%10 = load i32, i32* @AF_LOCAL
%11 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i32 0, i32 1
store i32 %10, i32* %11
%12 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i32 0, i32 0
%13 = load i32, i32* %12
%14 = call i32 (...) @socket_name()
%15 = call i32 @strcpy(i32 %13, i32 %14)
%16 = load i32, i32* %3
%17 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%18 = call i32 @SUN_LEN(%struct.sockaddr_un* %2)
%19 = call i32 @connect(i32 %16, %struct.sockaddr* %17, i32 %18)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %24
21: ; preds = %9
%22 = load i32, i32* %3
%23 = call i32 @close(i32 %22)
store i32 -1, i32* %1
br label %26
24: ; preds = %9
%25 = load i32, i32* %3
store i32 %25, i32* %1
br label %26
26: ; preds = %24, %21, %8
%27 = load i32, i32* %1
ret i32 %27
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@PF_LOCAL = global i32 0
@SOCK_STREAM = global i32 0
@AF_LOCAL = global i32 0
define i32 @server_connect() {
%1 = alloca %struct.sockaddr_un
%2 = bitcast %struct.sockaddr_un* %1 to i8*
%3 = load i32, i32* @PF_LOCAL
%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 %18, label %7
7: ; preds = %0
%8 = load i32, i32* @AF_LOCAL
%9 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %1, i64 0, i32 1
store i32 %8, i32* %9
%10 = tail call i32 (...) @socket_name()
%11 = tail call i32 @strcpy(i32 undef, i32 %10)
%12 = bitcast %struct.sockaddr_un* %1 to %struct.sockaddr*
%13 = call i32 @SUN_LEN(%struct.sockaddr_un* %1)
%14 = call i32 @connect(i32 %5, %struct.sockaddr* %12, i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %7
%17 = call i32 @close(i32 %5)
br label %18
18: ; preds = %7, %0, %16
%19 = phi i32 [ -1, %16 ], [ -1, %0 ], [ %5, %7 ]
ret i32 %19
}