#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct timeval {
  unsigned short tv_sec;
  scalar_t__ tv_usec;
};
struct TYPE_2__ {
  scalar_t__ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
  TYPE_1__ sin_addr;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ fd_set;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ EINPROGRESS;
scalar_t__ EINTR;
scalar_t__ EWOULDBLOCK;
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ assert(int);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
scalar_t__ errno;
scalar_t__ get_host_address(char const *, struct sockaddr_in *,
                            int /*<<< orphan*/ *);
int /*<<< orphan*/ htons(unsigned short const);
int /*<<< orphan*/ log_msg(char *, int, int, scalar_t__, char *, ...);
int /*<<< orphan*/ memcpy(struct sockaddr_in *, struct sockaddr_in *, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int select(int, int /*<<< orphan*/, int /*<<< orphan*/ *, int /*<<< orphan*/,
           struct timeval *);
int /*<<< orphan*/ set_linger(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ set_non_block(int, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ socket_is_usable(int);
int connect_to_host(const char *host, const unsigned short port,
                    struct sockaddr_in *fromaddr, struct sockaddr_in *toaddr,
                    unsigned short timeout) {
  int sfd = -1;
  int ready = -1;
  struct sockaddr_in addr;
  struct timeval delay;
  fd_set testset;
  assert(host != NULL && port != 0);
  if ((sfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    log_msg(
        "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
        "2022-01-22/02-19-57/repos/bierdaci/egui/refs/heads/master/msg/share.c",
        202, 0, errno, "socket creation failed");
    errno = 0;
    return -1;
  }
  if (fromaddr && fromaddr->sin_addr.s_addr) {
    memset(&addr, 0, sizeof(addr));
    addr.sin_addr.s_addr = fromaddr->sin_addr.s_addr;
    if (bind(sfd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
      log_msg("/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
              "2022-01-22/02-19-57/repos/bierdaci/egui/refs/heads/master/msg/"
              "share.c",
              211, 1, errno,
              "WARNING: failed to set connection source address -- ignored");
  }
  memset(&addr, 0, sizeof(addr));
  if (get_host_address(host, &addr, NULL) < 0) {
    log_msg(
        "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
        "2022-01-22/02-19-57/repos/bierdaci/egui/refs/heads/master/msg/share.c",
        216, 0, 0, "can't resolve host or address '%s'", host);
    close(sfd);
    return -1;
  }
  addr.sin_family = AF_INET;
  addr.sin_port = htons(port);
  set_linger(sfd, 0, 0);
  set_non_block(sfd, 1);
  if (connect(sfd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
    if (errno != EWOULDBLOCK && errno != EINPROGRESS && errno != EINTR) {
      close(sfd);
      return -1;
    }
  }
  delay.tv_sec = timeout;
  delay.tv_usec = 0;
  FD_ZERO(&testset);
  FD_SET(sfd, &testset);
  ready = select(sfd + 1, 0, &testset, 0, &delay);
  if (ready <= 0) {
    close(sfd);
    return -1;
  }
  set_non_block(sfd, 0);
  if (socket_is_usable(sfd) < 0) {
    close(sfd);
    return -1;
  }
  if (toaddr)
    memcpy(toaddr, &addr, sizeof(addr));
  return sfd;
}


x86 assembly (clang, -O0):
<connect_to_host>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %r8w,%ax
mov    %rdi,-0x10(%rbp)
mov    %si,-0x12(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %ax,-0x2a(%rbp)
movl   $0xffffffff,-0x30(%rbp)
movl   $0xffffffff,-0x34(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x10(%rbp)
mov    %al,-0x5d(%rbp)
je     124b <connect_to_host+0x4b>
movzwl -0x12(%rbp),%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0x5d(%rbp)
mov    -0x5d(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    0x2d89(%rip),%rax
mov    (%rax),%edi
mov    0x2d68(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1120 <socket@plt>
mov    %eax,-0x30(%rbp)
cmp    $0x0,%eax
jge    12bd <connect_to_host+0xbd>
mov    0x2d44(%rip),%rax
mov    (%rax),%rcx
lea    0xd72(%rip),%rdi
mov    $0xca,%esi
xor    %edx,%edx
lea    0xdea(%rip),%r8
mov    $0x0,%al
call   10b0 <log_msg@plt>
mov    0x2d1e(%rip),%rax
movq   $0x0,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1502 <connect_to_host+0x302>
cmpq   $0x0,-0x20(%rbp)
je     133c <connect_to_host+0x13c>
mov    -0x20(%rbp),%rax
cmpq   $0x0,0x8(%rax)
je     133c <connect_to_host+0x13c>
lea    -0x48(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%edi
lea    -0x48(%rbp),%rsi
mov    $0x10,%edx
call   10f0 <bind@plt>
cmp    $0x0,%rax
jge    1337 <connect_to_host+0x137>
mov    0x2cb3(%rip),%rax
mov    (%rax),%rcx
lea    0xce1(%rip),%rdi
mov    $0xd3,%esi
mov    $0x1,%edx
lea    0xd6d(%rip),%r8
mov    $0x0,%al
call   10b0 <log_msg@plt>
jmp    133c <connect_to_host+0x13c>
lea    -0x48(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    -0x10(%rbp),%rdi
lea    -0x48(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   10d0 <get_host_address@plt>
cmp    $0x0,%rax
jge    139f <connect_to_host+0x19f>
mov    -0x10(%rbp),%r9
lea    0xc8e(%rip),%rdi
mov    $0xd8,%esi
xor    %edx,%edx
xor    %eax,%eax
mov    %eax,%ecx
lea    0xd55(%rip),%r8
mov    $0x0,%al
call   10b0 <log_msg@plt>
mov    -0x30(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1502 <connect_to_host+0x302>
mov    0x2c42(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
movzwl -0x12(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x48(%rbp)
mov    -0x30(%rbp),%edi
xor    %edx,%edx
mov    %edx,%esi
call   1060 <set_linger@plt>
mov    -0x30(%rbp),%edi
mov    $0x1,%esi
call   10e0 <set_non_block@plt>
mov    -0x30(%rbp),%edi
lea    -0x48(%rbp),%rsi
mov    $0x10,%edx
call   1100 <connect@plt>
cmp    $0x0,%rax
jge    1452 <connect_to_host+0x252>
mov    0x2bd6(%rip),%rax
mov    (%rax),%rax
mov    0x2bc4(%rip),%rcx
cmp    (%rcx),%rax
je     144d <connect_to_host+0x24d>
mov    0x2bbc(%rip),%rax
mov    (%rax),%rax
mov    0x2bca(%rip),%rcx
cmp    (%rcx),%rax
je     144d <connect_to_host+0x24d>
mov    0x2ba2(%rip),%rax
mov    (%rax),%rax
mov    0x2b88(%rip),%rcx
cmp    (%rcx),%rax
je     144d <connect_to_host+0x24d>
mov    -0x30(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1502 <connect_to_host+0x302>
jmp    1452 <connect_to_host+0x252>
mov    -0x2a(%rbp),%ax
mov    %ax,-0x58(%rbp)
movq   $0x0,-0x50(%rbp)
lea    -0x5c(%rbp),%rdi
call   1090 <FD_ZERO@plt>
mov    -0x30(%rbp),%edi
lea    -0x5c(%rbp),%rsi
call   1110 <FD_SET@plt>
mov    -0x30(%rbp),%edi
add    $0x1,%edi
xor    %ecx,%ecx
lea    -0x5c(%rbp),%rdx
lea    -0x58(%rbp),%r8
mov    %ecx,%esi
call   10a0 <select@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jg     14af <connect_to_host+0x2af>
mov    -0x30(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1502 <connect_to_host+0x302>
mov    -0x30(%rbp),%edi
xor    %esi,%esi
call   10e0 <set_non_block@plt>
mov    -0x30(%rbp),%edi
call   10c0 <socket_is_usable@plt>
cmp    $0x0,%rax
jge    14df <connect_to_host+0x2df>
mov    -0x30(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1502 <connect_to_host+0x302>
cmpq   $0x0,-0x28(%rbp)
je     14fc <connect_to_host+0x2fc>
mov    -0x28(%rbp),%rdi
lea    -0x48(%rbp),%rsi
mov    $0x10,%edx
call   1080 <memcpy@plt>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_to_host>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r8d,0x10(%rsp)
mov    %rcx,%r14
mov    %rdx,%rbp
mov    %esi,%r12d
mov    %rdi,%r13
test   %rdi,%rdi
setne  %al
test   %esi,%esi
setne  %cl
and    %al,%cl
movzbl %cl,%edi
call   1070 <assert@plt>
mov    0x2dad(%rip),%r15
mov    (%r15),%edi
mov    0x2d8b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1120 <socket@plt>
test   %eax,%eax
js     13a1 <connect_to_host+0x1a1>
mov    %eax,%ebx
test   %rbp,%rbp
je     12b9 <connect_to_host+0xb9>
cmpq   $0x0,0x8(%rbp)
je     12b9 <connect_to_host+0xb9>
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    0x8(%rbp),%rax
mov    %rax,0x8(%rsp)
mov    %ebx,%edi
mov    %rsp,%rsi
mov    $0x10,%edx
call   10f0 <bind@plt>
test   %rax,%rax
jns    12b9 <connect_to_host+0xb9>
mov    0x2d31(%rip),%rax
mov    (%rax),%rcx
lea    0xd5f(%rip),%rdi
lea    0xdf5(%rip),%r8
mov    $0xd3,%esi
mov    $0x1,%edx
xor    %eax,%eax
call   10b0 <log_msg@plt>
mov    %rsp,%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    %r13,%rdi
mov    %rbp,%rsi
xor    %edx,%edx
call   10d0 <get_host_address@plt>
test   %rax,%rax
js     13d0 <connect_to_host+0x1d0>
mov    (%r15),%eax
mov    %eax,0x4(%rsp)
movzwl %r12w,%edi
call   1030 <htons@plt>
mov    %eax,(%rsp)
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <set_linger@plt>
mov    %ebx,%edi
mov    $0x1,%esi
call   10e0 <set_non_block@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x10,%edx
call   1100 <connect@plt>
test   %rax,%rax
js     13f3 <connect_to_host+0x1f3>
mov    0x10(%rsp),%eax
mov    %ax,0x18(%rsp)
movq   $0x0,0x20(%rsp)
lea    0x14(%rsp),%rbp
mov    %rbp,%rdi
call   1090 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1110 <FD_SET@plt>
mov    %ebx,%edi
add    $0x1,%edi
lea    0x18(%rsp),%r8
xor    %esi,%esi
mov    %rbp,%rdx
xor    %ecx,%ecx
call   10a0 <select@plt>
test   %eax,%eax
jle    142d <connect_to_host+0x22d>
mov    %ebx,%edi
xor    %esi,%esi
call   10e0 <set_non_block@plt>
mov    %ebx,%edi
call   10c0 <socket_is_usable@plt>
test   %rax,%rax
js     142d <connect_to_host+0x22d>
test   %r14,%r14
je     1439 <connect_to_host+0x239>
mov    %rsp,%rsi
mov    %r14,%rdi
mov    $0x10,%edx
call   1080 <memcpy@plt>
jmp    1439 <connect_to_host+0x239>
mov    0x2c20(%rip),%rbx
mov    (%rbx),%rcx
lea    0xc4e(%rip),%rdi
lea    0xccd(%rip),%r8
mov    $0xca,%esi
xor    %edx,%edx
xor    %eax,%eax
call   10b0 <log_msg@plt>
movq   $0x0,(%rbx)
jmp    1434 <connect_to_host+0x234>
lea    0xc29(%rip),%rdi
lea    0xcfb(%rip),%r8
mov    $0xd8,%esi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r13,%r9
xor    %eax,%eax
call   10b0 <log_msg@plt>
jmp    142d <connect_to_host+0x22d>
mov    0x2bce(%rip),%rax
mov    (%rax),%rax
mov    0x2bbc(%rip),%rcx
cmp    (%rcx),%rax
je     1323 <connect_to_host+0x123>
mov    0x2bcc(%rip),%rcx
cmp    (%rcx),%rax
je     1323 <connect_to_host+0x123>
mov    0x2b94(%rip),%rcx
cmp    (%rcx),%rax
je     1323 <connect_to_host+0x123>
mov    %ebx,%edi
call   1050 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i64 }
%struct.timeval = type { i16, i64 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [134 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/bierdaci/egui/refs/heads/master/msg/share.c\00"
@errno = global i64 0
@.str.1 = [23 x i8] c"socket creation failed\00"
@.str.2 = [60 x i8] c"WARNING: failed to set connection source address -- ignored\00"
@.str.3 = [35 x i8] c"can't resolve host or address '%s'\00"
@EWOULDBLOCK = global i64 0
@EINPROGRESS = global i64 0
@EINTR = global i64 0
define i32 @connect_to_host(i8* %0, i16 zeroext %1, %struct.sockaddr_in* %2, %struct.sockaddr_in* %3, i16 zeroext %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i16
%9 = alloca %struct.sockaddr_in*
%10 = alloca %struct.sockaddr_in*
%11 = alloca i16
%12 = alloca i32
%13 = alloca i32
%14 = alloca %struct.sockaddr_in
%15 = alloca %struct.timeval
%16 = alloca i32
store i8* %0, i8** %7
store i16 %1, i16* %8
store %struct.sockaddr_in* %2, %struct.sockaddr_in** %9
store %struct.sockaddr_in* %3, %struct.sockaddr_in** %10
store i16 %4, i16* %11
store i32 -1, i32* %12
store i32 -1, i32* %13
%17 = load i8*, i8** %7
%18 = icmp ne i8* %17, null
br i1 %18, label %19, label %23
19: ; preds = %5
%20 = load i16, i16* %8
%21 = zext i16 %20 to i32
%22 = icmp ne i32 %21, 0
br label %23
23: ; preds = %19, %5
%24 = phi i1 [ false, %5 ], [ %22, %19 ]
%25 = zext i1 %24 to i32
%26 = call i32 @assert(i32 %25)
%27 = load i32, i32* @AF_INET
%28 = load i32, i32* @SOCK_STREAM
%29 = call i32 @socket(i32 %27, i32 %28, i32 0)
store i32 %29, i32* %12
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %23
%32 = load i64, i64* @errno
%33 = call i32 (i8*, i32, i32, i64, i8*, ...) @log_msg(i8* getelementptr inbounds ([134 x i8], [134 x i8]* @.str, i64 0, i64 0), i32 202, i32 0, i64 %32, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
store i64 0, i64* @errno
store i32 -1, i32* %6
br label %131
34: ; preds = %23
%35 = load %struct.sockaddr_in*, %struct.sockaddr_in** %9
%36 = icmp ne %struct.sockaddr_in* %35, null
br i1 %36, label %37, label %59
37: ; preds = %34
%38 = load %struct.sockaddr_in*, %struct.sockaddr_in** %9
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %38, i32 0, i32 2
%40 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %39, i32 0, i32 0
%41 = load i64, i64* %40
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %59
43: ; preds = %37
%44 = call i32 @memset(%struct.sockaddr_in* %14, i32 0, i32 16)
%45 = load %struct.sockaddr_in*, %struct.sockaddr_in** %9
%46 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %45, i32 0, i32 2
%47 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %46, i32 0, i32 0
%48 = load i64, i64* %47
%49 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %14, i32 0, i32 2
%50 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %49, i32 0, i32 0
store i64 %48, i64* %50
%51 = load i32, i32* %12
%52 = bitcast %struct.sockaddr_in* %14 to %struct.sockaddr*
%53 = call i64 @bind(i32 %51, %struct.sockaddr* %52, i32 16)
%54 = icmp slt i64 %53, 0
br i1 %54, label %55, label %58
55: ; preds = %43
%56 = load i64, i64* @errno
%57 = call i32 (i8*, i32, i32, i64, i8*, ...) @log_msg(i8* getelementptr inbounds ([134 x i8], [134 x i8]* @.str, i64 0, i64 0), i32 211, i32 1, i64 %56, i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.2, i64 0, i64 0))
br label %58
58: ; preds = %55, %43
br label %59
59: ; preds = %58, %37, %34
%60 = call i32 @memset(%struct.sockaddr_in* %14, i32 0, i32 16)
%61 = load i8*, i8** %7
%62 = call i64 @get_host_address(i8* %61, %struct.sockaddr_in* %14, i32* null)
%63 = icmp slt i64 %62, 0
br i1 %63, label %64, label %69
64: ; preds = %59
%65 = load i8*, i8** %7
%66 = call i32 (i8*, i32, i32, i64, i8*, ...) @log_msg(i8* getelementptr inbounds ([134 x i8], [134 x i8]* @.str, i64 0, i64 0), i32 216, i32 0, i64 0, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i8* %65)
%67 = load i32, i32* %12
%68 = call i32 @close(i32 %67)
store i32 -1, i32* %6
br label %131
69: ; preds = %59
%70 = load i32, i32* @AF_INET
%71 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %14, i32 0, i32 1
store i32 %70, i32* %71
%72 = load i16, i16* %8
%73 = call i32 @htons(i16 zeroext %72)
%74 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %14, i32 0, i32 0
store i32 %73, i32* %74
%75 = load i32, i32* %12
%76 = call i32 @set_linger(i32 %75, i32 0, i32 0)
%77 = load i32, i32* %12
%78 = call i32 @set_non_block(i32 %77, i32 1)
%79 = load i32, i32* %12
%80 = bitcast %struct.sockaddr_in* %14 to %struct.sockaddr*
%81 = call i64 @connect(i32 %79, %struct.sockaddr* %80, i32 16)
%82 = icmp slt i64 %81, 0
br i1 %82, label %83, label %99
83: ; preds = %69
%84 = load i64, i64* @errno
%85 = load i64, i64* @EWOULDBLOCK
%86 = icmp ne i64 %84, %85
br i1 %86, label %87, label %98
87: ; preds = %83
%88 = load i64, i64* @errno
%89 = load i64, i64* @EINPROGRESS
%90 = icmp ne i64 %88, %89
br i1 %90, label %91, label %98
91: ; preds = %87
%92 = load i64, i64* @errno
%93 = load i64, i64* @EINTR
%94 = icmp ne i64 %92, %93
br i1 %94, label %95, label %98
95: ; preds = %91
%96 = load i32, i32* %12
%97 = call i32 @close(i32 %96)
store i32 -1, i32* %6
br label %131
98: ; preds = %91, %87, %83
br label %99
99: ; preds = %98, %69
%100 = load i16, i16* %11
%101 = getelementptr inbounds %struct.timeval, %struct.timeval* %15, i32 0, i32 0
store i16 %100, i16* %101
%102 = getelementptr inbounds %struct.timeval, %struct.timeval* %15, i32 0, i32 1
store i64 0, i64* %102
%103 = call i32 @FD_ZERO(i32* %16)
%104 = load i32, i32* %12
%105 = call i32 @FD_SET(i32 %104, i32* %16)
%106 = load i32, i32* %12
%107 = add nsw i32 %106, 1
%108 = call i32 @select(i32 %107, i32 0, i32* %16, i32 0, %struct.timeval* %15)
store i32 %108, i32* %13
%109 = load i32, i32* %13
%110 = icmp sle i32 %109, 0
br i1 %110, label %111, label %114
111: ; preds = %99
%112 = load i32, i32* %12
%113 = call i32 @close(i32 %112)
store i32 -1, i32* %6
br label %131
114: ; preds = %99
%115 = load i32, i32* %12
%116 = call i32 @set_non_block(i32 %115, i32 0)
%117 = load i32, i32* %12
%118 = call i64 @socket_is_usable(i32 %117)
%119 = icmp slt i64 %118, 0
br i1 %119, label %120, label %123
120: ; preds = %114
%121 = load i32, i32* %12
%122 = call i32 @close(i32 %121)
store i32 -1, i32* %6
br label %131
123: ; preds = %114
%124 = load %struct.sockaddr_in*, %struct.sockaddr_in** %10
%125 = icmp ne %struct.sockaddr_in* %124, null
br i1 %125, label %126, label %129
126: ; preds = %123
%127 = load %struct.sockaddr_in*, %struct.sockaddr_in** %10
%128 = call i32 @memcpy(%struct.sockaddr_in* %127, %struct.sockaddr_in* %14, i32 16)
br label %129
129: ; preds = %126, %123
%130 = load i32, i32* %12
store i32 %130, i32* %6
br label %131
131: ; preds = %129, %120, %111, %95, %64, %31
%132 = load i32, i32* %6
ret i32 %132
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i64 }
%struct.timeval = type { i16, i64 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [134 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/bierdaci/egui/refs/heads/master/msg/share.c\00"
@errno = global i64 0
@.str.1 = [23 x i8] c"socket creation failed\00"
@.str.2 = [60 x i8] c"WARNING: failed to set connection source address -- ignored\00"
@.str.3 = [35 x i8] c"can't resolve host or address '%s'\00"
@EWOULDBLOCK = global i64 0
@EINPROGRESS = global i64 0
@EINTR = global i64 0
define i32 @connect_to_host(i8* %0, i16 zeroext %1, %struct.sockaddr_in* %2, %struct.sockaddr_in* %3, i16 zeroext %4) {
%6 = alloca %struct.sockaddr_in
%7 = alloca %struct.timeval
%8 = alloca i32
%9 = bitcast %struct.sockaddr_in* %6 to i8*
%10 = bitcast %struct.timeval* %7 to i8*
%11 = bitcast i32* %8 to i8*
%12 = icmp ne i8* %0, null
%13 = icmp ne i16 %1, 0
%14 = and i1 %12, %13
%15 = zext i1 %14 to i32
%16 = tail call i32 @assert(i32 %15)
%17 = load i32, i32* @AF_INET
%18 = load i32, i32* @SOCK_STREAM
%19 = tail call i32 @socket(i32 %17, i32 %18, i32 0)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %5
%22 = load i64, i64* @errno
%23 = tail call i32 (i8*, i32, i32, i64, i8*, ...) @log_msg(i8* getelementptr inbounds ([134 x i8], [134 x i8]* @.str, i64 0, i64 0), i32 202, i32 0, i64 %22, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
store i64 0, i64* @errno
br label %89
24: ; preds = %5
%25 = icmp eq %struct.sockaddr_in* %2, null
br i1 %25, label %40, label %26
26: ; preds = %24
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2, i32 0
%28 = load i64, i64* %27
%29 = icmp eq i64 %28, 0
br i1 %29, label %40, label %30
30: ; preds = %26
%31 = call i32 @memset(%struct.sockaddr_in* %6, i32 0, i32 16)
%32 = load i64, i64* %27
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 2, i32 0
store i64 %32, i64* %33
%34 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%35 = call i64 @bind(i32 %19, %struct.sockaddr* %34, i32 16)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %30
%38 = load i64, i64* @errno
%39 = call i32 (i8*, i32, i32, i64, i8*, ...) @log_msg(i8* getelementptr inbounds ([134 x i8], [134 x i8]* @.str, i64 0, i64 0), i32 211, i32 1, i64 %38, i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.2, i64 0, i64 0))
br label %40
40: ; preds = %30, %37, %26, %24
%41 = call i32 @memset(%struct.sockaddr_in* %6, i32 0, i32 16)
%42 = call i64 @get_host_address(i8* %0, %struct.sockaddr_in* %6, i32* null)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %47
44: ; preds = %40
%45 = call i32 (i8*, i32, i32, i64, i8*, ...) @log_msg(i8* getelementptr inbounds ([134 x i8], [134 x i8]* @.str, i64 0, i64 0), i32 216, i32 0, i64 0, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i8* %0)
%46 = call i32 @close(i32 %19)
br label %89
47: ; preds = %40
%48 = load i32, i32* @AF_INET
%49 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 1
store i32 %48, i32* %49
%50 = call i32 @htons(i16 zeroext %1)
%51 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 0
store i32 %50, i32* %51
%52 = call i32 @set_linger(i32 %19, i32 0, i32 0)
%53 = call i32 @set_non_block(i32 %19, i32 1)
%54 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%55 = call i64 @connect(i32 %19, %struct.sockaddr* %54, i32 16)
%56 = icmp slt i64 %55, 0
br i1 %56, label %57, label %69
57: ; preds = %47
%58 = load i64, i64* @errno
%59 = load i64, i64* @EWOULDBLOCK
%60 = icmp eq i64 %58, %59
%61 = load i64, i64* @EINPROGRESS
%62 = icmp eq i64 %58, %61
%63 = select i1 %60, i1 true, i1 %62
%64 = load i64, i64* @EINTR
%65 = icmp eq i64 %58, %64
%66 = select i1 %63, i1 true, i1 %65
br i1 %66, label %69, label %67
67: ; preds = %57
%68 = call i32 @close(i32 %19)
br label %89
69: ; preds = %57, %47
%70 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i64 0, i32 0
store i16 %4, i16* %70
%71 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i64 0, i32 1
store i64 0, i64* %71
%72 = call i32 @FD_ZERO(i32* %8)
%73 = call i32 @FD_SET(i32 %19, i32* %8)
%74 = add nuw nsw i32 %19, 1
%75 = call i32 @select(i32 %74, i32 0, i32* %8, i32 0, %struct.timeval* %7)
%76 = icmp slt i32 %75, 1
br i1 %76, label %77, label %79
77: ; preds = %69
%78 = call i32 @close(i32 %19)
br label %89
79: ; preds = %69
%80 = call i32 @set_non_block(i32 %19, i32 0)
%81 = call i64 @socket_is_usable(i32 %19)
%82 = icmp slt i64 %81, 0
br i1 %82, label %83, label %85
83: ; preds = %79
%84 = call i32 @close(i32 %19)
br label %89
85: ; preds = %79
%86 = icmp eq %struct.sockaddr_in* %3, null
br i1 %86, label %89, label %87
87: ; preds = %85
%88 = call i32 @memcpy(%struct.sockaddr_in* %3, %struct.sockaddr_in* %6, i32 16)
br label %89
89: ; preds = %85, %87, %83, %77, %67, %44, %21
%90 = phi i32 [ -1, %21 ], [ -1, %44 ], [ -1, %67 ], [ -1, %77 ], [ -1, %83 ], [ %19, %87 ], [ %19, %85 ]
ret i32 %90
}