#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;
int /*<<< orphan*/ SOCK_STREAM;
int _StrToAddr(struct sockaddr_in *, int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int qSocketOpen(char *hostname, int port) {
  int sockfd;
  struct sockaddr_in addr;
  bzero((char *)&addr, sizeof(addr));
  if (_StrToAddr(&addr, AF_INET, hostname, port) != 1)
    return -1;
  if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    return -2;
  if (connect(sockfd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
    close(sockfd);
    return -3;
  }
  return sockfd;
}


x86 assembly (clang, -O0):
<qSocketOpen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
lea    -0x20(%rbp),%rdi
mov    $0x4,%esi
call   1050 <bzero@plt>
mov    0x2e74(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rdx
mov    -0x14(%rbp),%ecx
lea    -0x20(%rbp),%rdi
call   1040 <_StrToAddr@plt>
cmp    $0x1,%eax
je     119b <qSocketOpen+0x4b>
movl   $0xffffffff,-0x4(%rbp)
jmp    1201 <qSocketOpen+0xb1>
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
mov    0x2e2d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    11cc <qSocketOpen+0x7c>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1201 <qSocketOpen+0xb1>
mov    -0x18(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   1060 <connect@plt>
cmp    $0x0,%rax
jge    11fb <qSocketOpen+0xab>
mov    -0x18(%rbp),%edi
call   1030 <close@plt>
movl   $0xfffffffd,-0x4(%rbp)
jmp    1201 <qSocketOpen+0xb1>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<qSocketOpen>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x4,%esi
call   1050 <bzero@plt>
mov    0x2e75(%rip),%r15
mov    (%r15),%esi
mov    %r14,%rdi
mov    %rbx,%rdx
mov    %ebp,%ecx
call   1040 <_StrToAddr@plt>
mov    $0xffffffff,%ebx
cmp    $0x1,%eax
jne    11cd <qSocketOpen+0x7d>
mov    (%r15),%edi
mov    0x2e41(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
test   %eax,%eax
js     11c8 <qSocketOpen+0x78>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x4,%edx
call   1060 <connect@plt>
test   %rax,%rax
jns    11cd <qSocketOpen+0x7d>
mov    %ebx,%edi
call   1030 <close@plt>
mov    $0xfffffffd,%ebx
jmp    11cd <qSocketOpen+0x7d>
mov    $0xfffffffe,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
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
define i32 @qSocketOpen(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = bitcast %struct.sockaddr_in* %7 to i8*
%9 = call i32 @bzero(i8* %8, i32 4)
%10 = load i32, i32* @AF_INET
%11 = load i8*, i8** %4
%12 = load i32, i32* %5
%13 = call i32 @_StrToAddr(%struct.sockaddr_in* %7, i32 %10, i8* %11, i32 %12)
%14 = icmp ne i32 %13, 1
br i1 %14, label %15, label %16
15: ; preds = %2
store i32 -1, i32* %3
br label %32
16: ; preds = %2
%17 = load i32, i32* @AF_INET
%18 = load i32, i32* @SOCK_STREAM
%19 = call i32 @socket(i32 %17, i32 %18, i32 0)
store i32 %19, i32* %6
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %16
store i32 -2, i32* %3
br label %32
22: ; preds = %16
%23 = load i32, i32* %6
%24 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%25 = call i64 @connect(i32 %23, %struct.sockaddr* %24, i32 4)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %22
%28 = load i32, i32* %6
%29 = call i32 @close(i32 %28)
store i32 -3, i32* %3
br label %32
30: ; preds = %22
%31 = load i32, i32* %6
store i32 %31, i32* %3
br label %32
32: ; preds = %30, %27, %21, %15
%33 = load i32, i32* %3
ret i32 %33
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
define i32 @qSocketOpen(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = call i32 @bzero(i8* %4, i32 4)
%6 = load i32, i32* @AF_INET
%7 = call i32 @_StrToAddr(%struct.sockaddr_in* %3, i32 %6, i8* %0, i32 %1)
%8 = icmp eq i32 %7, 1
br i1 %8, label %9, label %20
9: ; preds = %2
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_STREAM
%12 = call i32 @socket(i32 %10, i32 %11, i32 0)
%13 = icmp slt i32 %12, 0
br i1 %13, label %20, label %14
14: ; preds = %9
%15 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%16 = call i64 @connect(i32 %12, %struct.sockaddr* %15, i32 4)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = call i32 @close(i32 %12)
br label %20
20: ; preds = %14, %9, %2, %18
%21 = phi i32 [ -3, %18 ], [ -1, %2 ], [ -2, %9 ], [ %12, %14 ]
ret i32 %21
}