#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_LOCAL;
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bzero(struct sockaddr_un *, int);
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ mvprintw(int /*<<< orphan*/, int /*<<< orphan*/, char *,
                            int /*<<< orphan*/, char *);
int /*<<< orphan*/ refresh();
int /*<<< orphan*/ sleep(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int connect_to_unix_socket(char *filepath) {
  struct sockaddr_un addr;
  int sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
  if (sockfd == -1)
    return -1;
  bzero(&addr, sizeof(addr));
  addr.sun_family = AF_LOCAL;
  strcpy(addr.sun_path, filepath);
  if (connect(sockfd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
    mvprintw(0, 0, "socket could not connect : %s : %s!", strerror(errno),
             filepath);
    refresh();
    sleep(1);
    close(sockfd);
    return -1;
  }
  return sockfd;
}


x86 assembly (clang, -O0):
<connect_to_unix_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
mov    0x2e24(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11ce <connect_to_unix_socket+0x3e>
movl   $0xffffffff,-0x4(%rbp)
jmp    125d <connect_to_unix_socket+0xcd>
lea    -0x18(%rbp),%rdi
mov    $0x8,%esi
call   1060 <bzero@plt>
mov    0x2dfd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x8,%edx
call   1070 <connect@plt>
cmp    $0xffffffff,%eax
jne    1257 <connect_to_unix_socket+0xc7>
mov    0x2db3(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%r8
xor    %esi,%esi
lea    0xdd5(%rip),%rdx
mov    %esi,%edi
call   10a0 <mvprintw@plt>
mov    $0x0,%al
call   1050 <refresh@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    125d <connect_to_unix_socket+0xcd>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_to_unix_socket>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2e47(%rip),%rax
mov    (%rax),%edi
mov    0x2e26(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    $0xffffffff,%ebx
cmp    $0xffffffff,%eax
je     123a <connect_to_unix_socket+0xaa>
mov    %eax,%ebp
mov    %rsp,%r15
mov    %r15,%rdi
mov    $0x8,%esi
call   1060 <bzero@plt>
mov    0x2e0a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
mov    (%rsp),%edi
mov    %r14,%rsi
call   1030 <strcpy@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x8,%edx
call   1070 <connect@plt>
cmp    $0xffffffff,%eax
je     11ff <connect_to_unix_socket+0x6f>
mov    %ebp,%ebx
jmp    123a <connect_to_unix_socket+0xaa>
mov    0x2dc2(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xdec(%rip),%rdx
xor    %edi,%edi
xor    %esi,%esi
mov    %eax,%ecx
mov    %r14,%r8
call   10a0 <mvprintw@plt>
xor    %eax,%eax
call   1050 <refresh@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@AF_LOCAL = global i32 0
@.str = [36 x i8] c"socket could not connect : %s : %s!\00"
@errno = global i32 0
define i32 @connect_to_unix_socket(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* @AF_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 eq i32 %9, -1
br i1 %10, label %11, label %12
11: ; preds = %1
store i32 -1, i32* %2
br label %35
12: ; preds = %1
%13 = call i32 @bzero(%struct.sockaddr_un* %4, i32 8)
%14 = load i32, i32* @AF_LOCAL
%15 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 1
store i32 %14, i32* %15
%16 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 0
%17 = load i32, i32* %16
%18 = load i8*, i8** %3
%19 = call i32 @strcpy(i32 %17, i8* %18)
%20 = load i32, i32* %5
%21 = bitcast %struct.sockaddr_un* %4 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 %33
24: ; preds = %12
%25 = load i32, i32* @errno
%26 = call i32 @strerror(i32 %25)
%27 = load i8*, i8** %3
%28 = call i32 @mvprintw(i32 0, i32 0, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 %26, i8* %27)
%29 = call i32 (...) @refresh()
%30 = call i32 @sleep(i32 1)
%31 = load i32, i32* %5
%32 = call i32 @close(i32 %31)
store i32 -1, i32* %2
br label %35
33: ; preds = %12
%34 = load i32, i32* %5
store i32 %34, i32* %2
br label %35
35: ; preds = %33, %24, %11
%36 = load i32, i32* %2
ret i32 %36
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@AF_LOCAL = global i32 0
@.str = [36 x i8] c"socket could not connect : %s : %s!\00"
@errno = global i32 0
define i32 @connect_to_unix_socket(i8* %0) {
%2 = alloca %struct.sockaddr_un
%3 = bitcast %struct.sockaddr_un* %2 to i8*
%4 = load i32, i32* @AF_UNIX
%5 = load i32, i32* @SOCK_STREAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = icmp eq i32 %6, -1
br i1 %7, label %25, label %8
8: ; preds = %1
%9 = call i32 @bzero(%struct.sockaddr_un* %2, i32 8)
%10 = load i32, i32* @AF_LOCAL
%11 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
store i32 %10, i32* %11
%12 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 0
%13 = load i32, i32* %12
%14 = tail call i32 @strcpy(i32 %13, i8* %0)
%15 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%16 = call i32 @connect(i32 %6, %struct.sockaddr* %15, i32 8)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %25
18: ; preds = %8
%19 = load i32, i32* @errno
%20 = call i32 @strerror(i32 %19)
%21 = call i32 @mvprintw(i32 0, i32 0, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 %20, i8* %0)
%22 = call i32 (...) @refresh()
%23 = call i32 @sleep(i32 1)
%24 = call i32 @close(i32 %6)
br label %25
25: ; preds = %8, %1, %18
%26 = phi i32 [ -1, %18 ], [ -1, %1 ], [ %6, %8 ]
ret i32 %26
}