#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*/ socklen_t;

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int /*<<< orphan*/);
int /*<<< orphan*/ init_envoy_socket(struct sockaddr_un *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int envoy_connect(void) {
  socklen_t sa_len;
  union {
    struct sockaddr sa;
    struct sockaddr_un un;
  } sa;
  int fd = socket(AF_UNIX, SOCK_STREAM, 0);
  if (fd < 0)
    return -1;
  sa_len = init_envoy_socket(&sa.un);
  if (connect(fd, &sa.sa, sa_len) < 0) {
    close(fd);
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<envoy_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e99(%rip),%rax
mov    (%rax),%edi
mov    0x2e80(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    117a <envoy_connect+0x3a>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b9 <envoy_connect+0x79>
lea    -0x10(%rbp),%rdi
call   1030 <init_envoy_socket@plt>
mov    %eax,-0x8(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    -0x8(%rbp),%edx
call   1050 <connect@plt>
cmp    $0x0,%rax
jge    11b3 <envoy_connect+0x73>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b9 <envoy_connect+0x79>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<envoy_connect>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e99(%rip),%rax
mov    (%rax),%edi
mov    0x2e80(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
js     1195 <envoy_connect+0x55>
mov    %eax,%ebp
lea    0x8(%rsp),%r14
mov    %r14,%rdi
call   1030 <init_envoy_socket@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1050 <connect@plt>
test   %rax,%rax
js     118e <envoy_connect+0x4e>
mov    %ebp,%ebx
jmp    1195 <envoy_connect+0x55>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
%union.anon = type { %struct.sockaddr }
%struct.sockaddr = type { i32 }
%struct.sockaddr_un = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
define i32 @envoy_connect() {
%1 = alloca %union.anon
%2 = bitcast %union.anon* %1 to i8*
%3 = load i32, i32* @AF_UNIX
%4 = load i32, i32* @SOCK_STREAM
%5 = tail call i32 @socket(i32 %3, i32 %4, i32 0)
%6 = icmp slt i32 %5, 0
br i1 %6, label %15, label %7
7: ; preds = %0
%8 = bitcast %union.anon* %1 to %struct.sockaddr_un*
%9 = call i32 @init_envoy_socket(%struct.sockaddr_un* %8)
%10 = getelementptr inbounds %union.anon, %union.anon* %1, i64 0, i32 0
%11 = call i64 @connect(i32 %5, %struct.sockaddr* %10, i32 %9)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %7
%14 = call i32 @close(i32 %5)
br label %15
15: ; preds = %7, %0, %13
%16 = phi i32 [ -1, %13 ], [ -1, %0 ], [ %5, %7 ]
ret i32 %16
}