#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 scalar_t__ socklen_t;

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, scalar_t__);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
char *strerror(int /*<<< orphan*/);
scalar_t__ strlen(int /*<<< orphan*/);
int testAPIsocket(int);
int openapi() {
  int sock;
  struct sockaddr_un rsock;
  socklen_t sockaddr_len;
  if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
    fprintf(stdout, "Failed to create socket for API: socket() - %s\n",
            strerror(errno));
    fflush(stdout);
    exit(1);
  }
  rsock.sun_family = AF_UNIX;
  strcpy(rsock.sun_path, "/tmp/qaoedsocket");
  sockaddr_len = strlen(rsock.sun_path) + sizeof(rsock.sun_family) + 1;
  if (connect(sock, (struct sockaddr *)&rsock, sockaddr_len) == -1) {
    fprintf(stdout, "Failed to connect to API: connect() - %s\n",
            strerror(errno));
    fflush(stdout);
    exit(1);
  }
  if (testAPIsocket(sock) == -1) {
    close(sock);
    return (-1);
  }
  return (sock);
}


x86 assembly (clang, -O0):
<openapi>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
mov    0x2e20(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    1211 <openapi+0x71>
mov    0x2df4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2df0(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x1c(%rbp),%edi
mov    %rax,%rdx
lea    0xe0c(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
call   1080 <fflush@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2dd0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    -0x10(%rbp),%edi
lea    0xe09(%rip),%rsi
call   1040 <strcpy@plt>
mov    -0x10(%rbp),%edi
call   1050 <strlen@plt>
add    $0x4,%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   10a0 <connect@plt>
cmp    $0xffffffff,%eax
jne    129d <openapi+0xfd>
mov    0x2d68(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2d64(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x20(%rbp),%edi
mov    %rax,%rdx
lea    0xdc1(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2d3c(%rip),%rax
mov    (%rax),%edi
call   1080 <fflush@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x8(%rbp),%edi
call   1030 <testAPIsocket@plt>
cmp    $0xffffffff,%eax
jne    12c2 <openapi+0x122>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c8 <openapi+0x128>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<openapi>:
push   %r14
push   %rbx
push   %rax
mov    0x2e3d(%rip),%r14
mov    (%r14),%edi
mov    0x2e23(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
cmp    $0xffffffff,%eax
je     1212 <openapi+0x72>
mov    %eax,%ebx
mov    (%r14),%eax
mov    %eax,0x4(%rsp)
lea    0xe5d(%rip),%rsi
call   1040 <strcpy@plt>
call   1050 <strlen@plt>
lea    0x5(%rax),%rdx
mov    %rsp,%rsi
mov    %ebx,%edi
call   10a0 <connect@plt>
cmp    $0xffffffff,%eax
je     1233 <openapi+0x93>
mov    %ebx,%edi
call   1030 <testAPIsocket@plt>
cmp    $0xffffffff,%eax
jne    1208 <openapi+0x68>
mov    %ebx,%edi
call   1060 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2daf(%rip),%r14
mov    (%r14),%ebx
mov    0x2dad(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xdcf(%rip),%rsi
jmp    1252 <openapi+0xb2>
mov    0x2d8e(%rip),%r14
mov    (%r14),%ebx
mov    0x2d8c(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xdef(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1070 <fprintf@plt>
mov    (%r14),%edi
call   1080 <fflush@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@stdout = global i32 0
@.str = [48 x i8] c"Failed to create socket for API: socket() - %s\0A\00"
@errno = global i32 0
@.str.1 = [17 x i8] c"/tmp/qaoedsocket\00"
@.str.2 = [42 x i8] c"Failed to connect to API: connect() - %s\0A\00"
define i32 @openapi() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_un
%4 = alloca i64
%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* %2
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %17
9: ; preds = %0
%10 = load i32, i32* @stdout
%11 = load i32, i32* @errno
%12 = call i8* @strerror(i32 %11)
%13 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0), i8* %12)
%14 = load i32, i32* @stdout
%15 = call i32 @fflush(i32 %14)
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %0
%18 = load i32, i32* @AF_UNIX
%19 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i32 0, i32 1
store i32 %18, i32* %19
%20 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i32 0, i32 0
%21 = load i32, i32* %20
%22 = call i32 @strcpy(i32 %21, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%23 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i32 0, i32 0
%24 = load i32, i32* %23
%25 = call i64 @strlen(i32 %24)
%26 = add i64 %25, 4
%27 = add i64 %26, 1
store i64 %27, i64* %4
%28 = load i32, i32* %2
%29 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%30 = load i64, i64* %4
%31 = call i32 @connect(i32 %28, %struct.sockaddr* %29, i64 %30)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %41
33: ; preds = %17
%34 = load i32, i32* @stdout
%35 = load i32, i32* @errno
%36 = call i8* @strerror(i32 %35)
%37 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i8* %36)
%38 = load i32, i32* @stdout
%39 = call i32 @fflush(i32 %38)
%40 = call i32 @exit(i32 1)
unreachable
41: ; preds = %17
%42 = load i32, i32* %2
%43 = call i32 @testAPIsocket(i32 %42)
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %48
45: ; preds = %41
%46 = load i32, i32* %2
%47 = call i32 @close(i32 %46)
store i32 -1, i32* %1
br label %50
48: ; preds = %41
%49 = load i32, i32* %2
store i32 %49, i32* %1
br label %50
50: ; preds = %48, %45
%51 = load i32, i32* %1
ret i32 %51
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@stdout = global i32 0
@.str = [48 x i8] c"Failed to create socket for API: socket() - %s\0A\00"
@errno = global i32 0
@.str.1 = [17 x i8] c"/tmp/qaoedsocket\00"
@.str.2 = [42 x i8] c"Failed to connect to API: connect() - %s\0A\00"
define i32 @openapi() {
%1 = alloca %struct.sockaddr_un
%2 = bitcast %struct.sockaddr_un* %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 eq i32 %5, -1
br i1 %6, label %7, label %15
7: ; preds = %0
%8 = load i32, i32* @stdout
%9 = load i32, i32* @errno
%10 = tail call i8* @strerror(i32 %9)
%11 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = load i32, i32* @stdout
%13 = tail call i32 @fflush(i32 %12)
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %0
%16 = load i32, i32* @AF_UNIX
%17 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %1, i64 0, i32 1
store i32 %16, i32* %17
%18 = tail call i32 @strcpy(i32 undef, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%19 = tail call i64 @strlen(i32 undef)
%20 = add i64 %19, 5
%21 = bitcast %struct.sockaddr_un* %1 to %struct.sockaddr*
%22 = call i32 @connect(i32 %5, %struct.sockaddr* %21, i64 %20)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %32
24: ; preds = %15
%25 = load i32, i32* @stdout
%26 = load i32, i32* @errno
%27 = call i8* @strerror(i32 %26)
%28 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i8* %27)
%29 = load i32, i32* @stdout
%30 = call i32 @fflush(i32 %29)
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %15
%33 = call i32 @testAPIsocket(i32 %5)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %37
35: ; preds = %32
%36 = call i32 @close(i32 %5)
br label %37
37: ; preds = %32, %35
%38 = phi i32 [ -1, %35 ], [ %5, %32 ]
ret i32 %38
}