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

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ PF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
char *get_temp_directory();
int /*<<< orphan*/ snprintf(int /*<<< orphan*/, int, char *, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int connect_socket(int pid) {
  int fd = socket(PF_UNIX, SOCK_STREAM, 0);
  if (fd == -1) {
    return -1;
  }
  struct sockaddr_un addr;
  addr.sun_family = AF_UNIX;
  snprintf(addr.sun_path, sizeof(addr.sun_path), "%s/.java_pid%d",
           get_temp_directory(), pid);
  if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
    close(fd);
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<connect_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e96(%rip),%rax
mov    (%rax),%edi
mov    0x2e65(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    118d <connect_socket+0x3d>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f5 <connect_socket+0xa5>
mov    0x2e4c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1060 <get_temp_directory@plt>
mov    -0x1c(%rbp),%edi
mov    %rax,%rcx
mov    -0x8(%rbp),%r8d
mov    $0x4,%esi
lea    0xe44(%rip),%rdx
call   1030 <snprintf@plt>
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x8,%edx
call   1050 <connect@plt>
cmp    $0xffffffff,%eax
jne    11ef <connect_socket+0x9f>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f5 <connect_socket+0xa5>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_socket>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebp
mov    0x2e9c(%rip),%rax
mov    (%rax),%edi
mov    0x2e6b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
cmp    $0xffffffff,%eax
je     11bb <connect_socket+0x6b>
mov    %eax,%ebx
mov    0x2e64(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
xor    %eax,%eax
call   1060 <get_temp_directory@plt>
lea    0xe70(%rip),%rdx
mov    $0x4,%esi
mov    %rax,%rcx
mov    %ebp,%r8d
call   1030 <snprintf@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x8,%edx
call   1050 <connect@plt>
cmp    $0xffffffff,%eax
jne    11c0 <connect_socket+0x70>
mov    %ebx,%edi
call   1040 <close@plt>
mov    $0xffffffff,%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 }
@PF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@AF_UNIX = global i32 0
@.str = [15 x i8] c"%s/.java_pid%d\00"
define i32 @connect_socket(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_un
store i32 %0, i32* %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* %4
%9 = load i32, i32* %4
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %12
11: ; preds = %1
store i32 -1, i32* %2
br label %29
12: ; preds = %1
%13 = load i32, i32* @AF_UNIX
%14 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 1
store i32 %13, i32* %14
%15 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 0
%16 = load i32, i32* %15
%17 = call i8* (...) @get_temp_directory()
%18 = load i32, i32* %3
%19 = call i32 @snprintf(i32 %16, i32 4, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %17, i32 %18)
%20 = load i32, i32* %4
%21 = bitcast %struct.sockaddr_un* %5 to %struct.sockaddr*
%22 = call i32 @connect(i32 %20, %struct.sockaddr* %21, i32 8)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %27
24: ; preds = %12
%25 = load i32, i32* %4
%26 = call i32 @close(i32 %25)
store i32 -1, i32* %2
br label %29
27: ; preds = %12
%28 = load i32, i32* %4
store i32 %28, i32* %2
br label %29
29: ; preds = %27, %24, %11
%30 = load i32, i32* %2
ret i32 %30
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@PF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@AF_UNIX = global i32 0
@.str = [15 x i8] c"%s/.java_pid%d\00"
define i32 @connect_socket(i32 %0) {
%2 = alloca %struct.sockaddr_un
%3 = load i32, i32* @PF_UNIX
%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 %20, label %7
7: ; preds = %1
%8 = bitcast %struct.sockaddr_un* %2 to i8*
%9 = load i32, i32* @AF_UNIX
%10 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
store i32 %9, i32* %10
%11 = tail call i8* (...) @get_temp_directory()
%12 = tail call i32 @snprintf(i32 undef, i32 4, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %11, i32 %0)
%13 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%14 = call i32 @connect(i32 %5, %struct.sockaddr* %13, i32 8)
%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, %16
%19 = phi i32 [ -1, %16 ], [ %5, %7 ]
br label %20
20: ; preds = %1, %18
%21 = phi i32 [ %19, %18 ], [ -1, %1 ]
ret i32 %21
}