#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_in {
  int dummy;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ EINPROGRESS;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
scalar_t__ errno;
int fcntl(int, int /*<<< orphan*/, int);
scalar_t__ qio_wait_writable(int, int);
int qsocket_get_addr(struct sockaddr_in *, char const *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int qsocket_open(const char *hostname, int port, int timeoutms) {
  struct sockaddr_in addr;
  if (qsocket_get_addr(&addr, hostname, port) == false) {
    return -1;
  }
  int sockfd;
  if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    return -2;
  }
  int flags = fcntl(sockfd, F_GETFL, 0);
  if (timeoutms >= 0)
    fcntl(sockfd, F_SETFL, flags | O_NONBLOCK);
  int status = connect(sockfd, (struct sockaddr *)&addr, sizeof(addr));
  if (status < 0 &&
      (errno != EINPROGRESS || qio_wait_writable(sockfd, timeoutms) <= 0)) {
    close(sockfd);
    return -3;
  }
  if (timeoutms >= 0)
    fcntl(sockfd, F_SETFL, flags);
  return sockfd;
}


x86 assembly (clang, -O0):
<qsocket_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
lea    -0x20(%rbp),%rdi
call   1030 <qsocket_get_addr@plt>
cmp    $0x0,%eax
jne    1197 <qsocket_open+0x37>
movl   $0xffffffff,-0x4(%rbp)
jmp    128a <qsocket_open+0x12a>
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
mov    0x2e19(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    11c8 <qsocket_open+0x68>
movl   $0xfffffffe,-0x4(%rbp)
jmp    128a <qsocket_open+0x12a>
mov    -0x24(%rbp),%edi
mov    0x2dde(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <fcntl@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x18(%rbp)
jl     1205 <qsocket_open+0xa5>
mov    -0x24(%rbp),%edi
mov    0x2dd6(%rip),%rax
mov    (%rax),%esi
mov    -0x28(%rbp),%edx
mov    0x2de2(%rip),%rax
or     (%rax),%edx
call   1050 <fcntl@plt>
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   1070 <connect@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    1266 <qsocket_open+0x106>
mov    0x2d8e(%rip),%rax
mov    (%rax),%rax
mov    0x2da4(%rip),%rcx
cmp    (%rcx),%rax
jne    1252 <qsocket_open+0xf2>
mov    -0x24(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1040 <qio_wait_writable@plt>
cmp    $0x0,%rax
jg     1266 <qsocket_open+0x106>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
movl   $0xfffffffd,-0x4(%rbp)
jmp    128a <qsocket_open+0x12a>
cmpl   $0x0,-0x18(%rbp)
jl     1284 <qsocket_open+0x124>
mov    -0x24(%rbp),%edi
mov    0x2d4e(%rip),%rax
mov    (%rax),%esi
mov    -0x28(%rbp),%edx
call   1050 <fcntl@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<qsocket_open>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edx,%ebp
mov    %esi,%edx
mov    %rdi,%rsi
lea    0x8(%rsp),%rdi
call   1030 <qsocket_get_addr@plt>
test   %eax,%eax
je     1203 <qsocket_open+0xa3>
mov    0x2e60(%rip),%rax
mov    (%rax),%edi
mov    0x2e2f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
test   %eax,%eax
js     120a <qsocket_open+0xaa>
mov    %eax,%ebx
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1050 <fcntl@plt>
mov    %eax,%r14d
test   %ebp,%ebp
js     11d5 <qsocket_open+0x75>
mov    0x2e08(%rip),%rax
mov    (%rax),%esi
mov    0x2e17(%rip),%rax
mov    (%rax),%edx
or     %r14d,%edx
mov    %ebx,%edi
call   1050 <fcntl@plt>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   1070 <connect@plt>
test   %eax,%eax
js     1211 <qsocket_open+0xb1>
test   %ebp,%ebp
js     1241 <qsocket_open+0xe1>
mov    0x2dd3(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14d,%edx
call   1050 <fcntl@plt>
jmp    1241 <qsocket_open+0xe1>
mov    $0xffffffff,%ebx
jmp    1241 <qsocket_open+0xe1>
mov    $0xfffffffe,%ebx
jmp    1241 <qsocket_open+0xe1>
mov    0x2da0(%rip),%rax
mov    (%rax),%rax
mov    0x2db6(%rip),%rcx
cmp    (%rcx),%rax
jne    1235 <qsocket_open+0xd5>
mov    %ebx,%edi
mov    %ebp,%esi
call   1040 <qio_wait_writable@plt>
test   %rax,%rax
jg     11ea <qsocket_open+0x8a>
mov    %ebx,%edi
call   1060 <close@plt>
mov    $0xfffffffd,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@errno = global i64 0
@EINPROGRESS = global i64 0
define i32 @qsocket_open(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.sockaddr_in
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%12 = load i8*, i8** %5
%13 = load i32, i32* %6
%14 = call i32 @qsocket_get_addr(%struct.sockaddr_in* %8, i8* %12, i32 %13)
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %3
store i32 -1, i32* %4
br label %64
17: ; preds = %3
%18 = load i32, i32* @AF_INET
%19 = load i32, i32* @SOCK_STREAM
%20 = call i32 @socket(i32 %18, i32 %19, i32 0)
store i32 %20, i32* %9
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %17
store i32 -2, i32* %4
br label %64
23: ; preds = %17
%24 = load i32, i32* %9
%25 = load i32, i32* @F_GETFL
%26 = call i32 @fcntl(i32 %24, i32 %25, i32 0)
store i32 %26, i32* %10
%27 = load i32, i32* %7
%28 = icmp sge i32 %27, 0
br i1 %28, label %29, label %36
29: ; preds = %23
%30 = load i32, i32* %9
%31 = load i32, i32* @F_SETFL
%32 = load i32, i32* %10
%33 = load i32, i32* @O_NONBLOCK
%34 = or i32 %32, %33
%35 = call i32 @fcntl(i32 %30, i32 %31, i32 %34)
br label %36
36: ; preds = %29, %23
%37 = load i32, i32* %9
%38 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%39 = call i32 @connect(i32 %37, %struct.sockaddr* %38, i32 4)
store i32 %39, i32* %11
%40 = load i32, i32* %11
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %54
42: ; preds = %36
%43 = load i64, i64* @errno
%44 = load i64, i64* @EINPROGRESS
%45 = icmp ne i64 %43, %44
br i1 %45, label %51, label %46
46: ; preds = %42
%47 = load i32, i32* %9
%48 = load i32, i32* %7
%49 = call i64 @qio_wait_writable(i32 %47, i32 %48)
%50 = icmp sle i64 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %46, %42
%52 = load i32, i32* %9
%53 = call i32 @close(i32 %52)
store i32 -3, i32* %4
br label %64
54: ; preds = %46, %36
%55 = load i32, i32* %7
%56 = icmp sge i32 %55, 0
br i1 %56, label %57, label %62
57: ; preds = %54
%58 = load i32, i32* %9
%59 = load i32, i32* @F_SETFL
%60 = load i32, i32* %10
%61 = call i32 @fcntl(i32 %58, i32 %59, i32 %60)
br label %62
62: ; preds = %57, %54
%63 = load i32, i32* %9
store i32 %63, i32* %4
br label %64
64: ; preds = %62, %51, %22, %16
%65 = load i32, i32* %4
ret i32 %65
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@errno = global i64 0
@EINPROGRESS = global i64 0
define i32 @qsocket_open(i8* %0, i32 %1, i32 %2) {
%4 = alloca %struct.sockaddr_in
%5 = bitcast %struct.sockaddr_in* %4 to i8*
%6 = call i32 @qsocket_get_addr(%struct.sockaddr_in* %4, i8* %0, i32 %1)
%7 = icmp eq i32 %6, 0
br i1 %7, label %39, label %8
8: ; preds = %3
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_STREAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
%12 = icmp slt i32 %11, 0
br i1 %12, label %39, label %13
13: ; preds = %8
%14 = load i32, i32* @F_GETFL
%15 = call i32 @fcntl(i32 %11, i32 %14, i32 0)
%16 = icmp sgt i32 %2, -1
br i1 %16, label %17, label %22
17: ; preds = %13
%18 = load i32, i32* @F_SETFL
%19 = load i32, i32* @O_NONBLOCK
%20 = or i32 %19, %15
%21 = call i32 @fcntl(i32 %11, i32 %18, i32 %20)
br label %22
22: ; preds = %17, %13
%23 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%24 = call i32 @connect(i32 %11, %struct.sockaddr* %23, i32 4)
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %35
26: ; preds = %22
%27 = load i64, i64* @errno
%28 = load i64, i64* @EINPROGRESS
%29 = icmp eq i64 %27, %28
br i1 %29, label %30, label %33
30: ; preds = %26
%31 = call i64 @qio_wait_writable(i32 %11, i32 %2)
%32 = icmp slt i64 %31, 1
br i1 %32, label %33, label %35
33: ; preds = %30, %26
%34 = call i32 @close(i32 %11)
br label %39
35: ; preds = %30, %22
br i1 %16, label %36, label %39
36: ; preds = %35
%37 = load i32, i32* @F_SETFL
%38 = call i32 @fcntl(i32 %11, i32 %37, i32 %15)
br label %39
39: ; preds = %8, %35, %36, %33, %3
%40 = phi i32 [ -1, %3 ], [ -2, %8 ], [ -3, %33 ], [ %11, %36 ], [ %11, %35 ]
ret i32 %40
}