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

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
scalar_t__ EINPROGRESS;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ _log_stderr(char *, int, char *, char const *,
                               int /*<<< orphan*/);
int connect(int, struct sockaddr *, int);
scalar_t__ errno;
int /*<<< orphan*/ ez_net_close_socket(int);
scalar_t__ ez_net_set_non_block(int);
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(scalar_t__);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *, int);
int ez_net_unix_connect_ex(const char *path, int flags) {
  int s;
  struct sockaddr_un sa;
  if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
    return -1;
  }
  memset(&sa, 0, sizeof(sa));
  sa.sun_family = AF_UNIX;
  strncpy(sa.sun_path, path, sizeof(sa.sun_path) - 1);
  if ((flags & 1) && ez_net_set_non_block(s) != 0) {
    ez_net_close_socket(s);
    s = -1;
    return s;
  }
  if (connect(s, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
    if (errno == EINPROGRESS && (flags & 1))
      goto end;
    else
      do {
        _log_stderr("/scratch/repos/new/home/jordi_armengol_estape/c-scraper/"
                    "outputs/2022-01-22/02-19-57/repos/ezzuodp/c_util/refs/"
                    "heads/master/src/ez_net.c",
                    312, "connect to unix:%s failed, cause:[%s]!", path,
                    strerror(errno));
      } while (0);
    if (s != -1) {
      ez_net_close_socket(s);
      s = -1;
    }
  }
end:
  return s;
}


x86 assembly (clang, -O0):
<ez_net_unix_connect_ex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    11c0 <ez_net_unix_connect_ex+0x40>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d9 <ez_net_unix_connect_ex+0x159>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    0x2e09(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x3,%edx
call   1030 <strncpy@plt>
mov    -0x14(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     1228 <ez_net_unix_connect_ex+0xa8>
mov    -0x18(%rbp),%edi
call   1040 <ez_net_set_non_block@plt>
cmp    $0x0,%rax
je     1228 <ez_net_unix_connect_ex+0xa8>
mov    -0x18(%rbp),%edi
call   1070 <ez_net_close_socket@plt>
movl   $0xffffffff,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12d9 <ez_net_unix_connect_ex+0x159>
mov    -0x18(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
call   1080 <connect@plt>
cmp    $0xffffffff,%eax
jne    12ce <ez_net_unix_connect_ex+0x14e>
mov    0x2d7f(%rip),%rax
mov    (%rax),%rax
mov    0x2d95(%rip),%rcx
cmp    (%rcx),%rax
jne    1270 <ez_net_unix_connect_ex+0xf0>
mov    -0x14(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     1270 <ez_net_unix_connect_ex+0xf0>
jmp    12d3 <ez_net_unix_connect_ex+0x153>
jmp    1275 <ez_net_unix_connect_ex+0xf5>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2d44(%rip),%rax
mov    (%rax),%rdi
call   1090 <strerror@plt>
mov    -0x28(%rbp),%rcx
mov    %eax,%r8d
lea    0xd66(%rip),%rdi
mov    $0x138,%esi
lea    0xde2(%rip),%rdx
call   1050 <_log_stderr@plt>
jmp    12b0 <ez_net_unix_connect_ex+0x130>
cmpl   $0xffffffff,-0x18(%rbp)
je     12c9 <ez_net_unix_connect_ex+0x149>
mov    -0x18(%rbp),%edi
call   1070 <ez_net_close_socket@plt>
movl   $0xffffffff,-0x18(%rbp)
jmp    12ce <ez_net_unix_connect_ex+0x14e>
jmp    12d3 <ez_net_unix_connect_ex+0x153>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ez_net_unix_connect_ex>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r15d
mov    %rdi,%r14
mov    0x2e47(%rip),%rbx
mov    (%rbx),%edi
mov    0x2e2e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    $0xffffffff,%r12d
cmp    $0xffffffff,%eax
je     1257 <ez_net_unix_connect_ex+0xd7>
mov    %eax,%ebp
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    (%rbx),%eax
mov    %eax,0xc(%rsp)
mov    0x8(%rsp),%edi
mov    %r14,%rsi
mov    $0x3,%edx
call   1030 <strncpy@plt>
test   $0x1,%r15b
je     11f6 <ez_net_unix_connect_ex+0x76>
mov    %ebp,%edi
call   1040 <ez_net_set_non_block@plt>
test   %rax,%rax
jne    1250 <ez_net_unix_connect_ex+0xd0>
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
mov    $0x8,%edx
call   1080 <connect@plt>
cmp    $0xffffffff,%eax
jne    1228 <ez_net_unix_connect_ex+0xa8>
mov    0x2db5(%rip),%rax
mov    (%rax),%rdi
test   $0x1,%r15b
je     122d <ez_net_unix_connect_ex+0xad>
mov    0x2dc5(%rip),%rax
cmp    (%rax),%rdi
jne    122d <ez_net_unix_connect_ex+0xad>
mov    %ebp,%r12d
jmp    1257 <ez_net_unix_connect_ex+0xd7>
call   1090 <strerror@plt>
lea    0xdc7(%rip),%rdi
lea    0xe48(%rip),%rdx
mov    $0x138,%esi
mov    %r14,%rcx
mov    %eax,%r8d
call   1050 <_log_stderr@plt>
mov    %ebp,%edi
call   1070 <ez_net_close_socket@plt>
mov    %r12d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
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
@errno = global i64 0
@EINPROGRESS = global i64 0
@.str = [136 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/ezzuodp/c_util/refs/heads/master/src/ez_net.c\00"
@.str.1 = [39 x i8] c"connect to unix:%s failed, cause:[%s]!\00"
define i32 @ez_net_unix_connect_ex(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.sockaddr_un
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = load i32, i32* @AF_UNIX
%9 = load i32, i32* @SOCK_STREAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %6
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %13
12: ; preds = %2
store i32 -1, i32* %3
br label %63
13: ; preds = %2
%14 = call i32 @memset(%struct.sockaddr_un* %7, i32 0, i32 8)
%15 = load i32, i32* @AF_UNIX
%16 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %7, i32 0, i32 1
store i32 %15, i32* %16
%17 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %7, i32 0, i32 0
%18 = load i32, i32* %17
%19 = load i8*, i8** %4
%20 = call i32 @strncpy(i32 %18, i8* %19, i32 3)
%21 = load i32, i32* %5
%22 = and i32 %21, 1
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %32
24: ; preds = %13
%25 = load i32, i32* %6
%26 = call i64 @ez_net_set_non_block(i32 %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %32
28: ; preds = %24
%29 = load i32, i32* %6
%30 = call i32 @ez_net_close_socket(i32 %29)
store i32 -1, i32* %6
%31 = load i32, i32* %6
store i32 %31, i32* %3
br label %63
32: ; preds = %24, %13
%33 = load i32, i32* %6
%34 = bitcast %struct.sockaddr_un* %7 to %struct.sockaddr*
%35 = call i32 @connect(i32 %33, %struct.sockaddr* %34, i32 8)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %60
37: ; preds = %32
%38 = load i64, i64* @errno
%39 = load i64, i64* @EINPROGRESS
%40 = icmp eq i64 %38, %39
br i1 %40, label %41, label %46
41: ; preds = %37
%42 = load i32, i32* %5
%43 = and i32 %42, 1
%44 = icmp ne i32 %43, 0
br i1 %44, label %45, label %46
45: ; preds = %41
br label %61
46: ; preds = %41, %37
br label %47
47: ; preds = %46
%48 = load i8*, i8** %4
%49 = load i64, i64* @errno
%50 = call i32 @strerror(i64 %49)
%51 = call i32 @_log_stderr(i8* getelementptr inbounds ([136 x i8], [136 x i8]* @.str, i64 0, i64 0), i32 312, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i8* %48, i32 %50)
br label %52
52: ; preds = %47
br label %53
53: ; preds = %52
%54 = load i32, i32* %6
%55 = icmp ne i32 %54, -1
br i1 %55, label %56, label %59
56: ; preds = %53
%57 = load i32, i32* %6
%58 = call i32 @ez_net_close_socket(i32 %57)
store i32 -1, i32* %6
br label %59
59: ; preds = %56, %53
br label %60
60: ; preds = %59, %32
br label %61
61: ; preds = %60, %45
%62 = load i32, i32* %6
store i32 %62, i32* %3
br label %63
63: ; preds = %61, %28, %12
%64 = load i32, i32* %3
ret i32 %64
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@errno = global i64 0
@EINPROGRESS = global i64 0
@.str = [136 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/ezzuodp/c_util/refs/heads/master/src/ez_net.c\00"
@.str.1 = [39 x i8] c"connect to unix:%s failed, cause:[%s]!\00"
define i32 @ez_net_unix_connect_ex(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_un
%4 = bitcast %struct.sockaddr_un* %3 to i8*
%5 = load i32, i32* @AF_UNIX
%6 = load i32, i32* @SOCK_STREAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = icmp eq i32 %7, -1
br i1 %8, label %35, label %9
9: ; preds = %2
%10 = call i32 @memset(%struct.sockaddr_un* %3, i32 0, i32 8)
%11 = load i32, i32* @AF_UNIX
%12 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i64 0, i32 1
store i32 %11, i32* %12
%13 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i64 0, i32 0
%14 = load i32, i32* %13
%15 = call i32 @strncpy(i32 %14, i8* %0, i32 3)
%16 = and i32 %1, 1
%17 = icmp eq i32 %16, 0
br i1 %17, label %21, label %18
18: ; preds = %9
%19 = call i64 @ez_net_set_non_block(i32 %7)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %33
21: ; preds = %18, %9
%22 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%23 = call i32 @connect(i32 %7, %struct.sockaddr* %22, i32 8)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %35
25: ; preds = %21
%26 = load i64, i64* @errno
%27 = load i64, i64* @EINPROGRESS
%28 = icmp ne i64 %26, %27
%29 = or i1 %17, %28
br i1 %29, label %30, label %35
30: ; preds = %25
%31 = call i32 @strerror(i64 %26)
%32 = call i32 @_log_stderr(i8* getelementptr inbounds ([136 x i8], [136 x i8]* @.str, i64 0, i64 0), i32 312, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %31)
br label %33
33: ; preds = %18, %30
%34 = call i32 @ez_net_close_socket(i32 %7)
br label %35
35: ; preds = %33, %21, %25, %2
%36 = phi i32 [ -1, %2 ], [ %7, %21 ], [ %7, %25 ], [ -1, %33 ]
ret i32 %36
}