#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 /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct in_addr {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ h_addr;
};
typedef int /*<<< orphan*/ sa;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ EINPROGRESS;
int anetCreateSocket(char *, int /*<<< orphan*/);
scalar_t__ anetNonBlock(char *, int);
int /*<<< orphan*/ anetSetError(char *, char *, char *);
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
scalar_t__ errno;
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
scalar_t__ inet_aton(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/, int);
char *strerror(scalar_t__);
int anetTcpGenericConnect(char *err, char *addr, int port, int flags) {
  int s;
  struct sockaddr_in sa;
  if ((s = anetCreateSocket(err, AF_INET)) == -1)
    return -1;
  sa.sin_family = AF_INET;
  sa.sin_port = htons(port);
  if (inet_aton(addr, &sa.sin_addr) == 0) {
    struct hostent *he;
    he = gethostbyname(addr);
    if (he == NULL) {
      anetSetError(err, "can't resolve: %s", addr);
      close(s);
      return -1;
    }
    memcpy(&sa.sin_addr, he->h_addr, sizeof(struct in_addr));
  }
  if (flags & 1) {
    if (anetNonBlock(err, s) != 0)
      return -1;
  }
  if (connect(s, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
    if (errno == EINPROGRESS && flags & 1)
      return s;
    anetSetError(err, "connect: %s", strerror(errno));
    close(s);
    return -1;
  }
  return s;
}


x86 assembly (clang, -O0):
<anetTcpGenericConnect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e27(%rip),%rax
mov    (%rax),%esi
call   1070 <anetCreateSocket@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
jne    11e0 <anetTcpGenericConnect+0x40>
movl   $0xffffffff,-0x4(%rbp)
jmp    132a <anetTcpGenericConnect+0x18a>
mov    0x2e01(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x1c(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   10a0 <inet_aton@plt>
cmp    $0x0,%rax
jne    1262 <anetTcpGenericConnect+0xc2>
mov    -0x18(%rbp),%rdi
call   1060 <gethostbyname@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    124e <anetTcpGenericConnect+0xae>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xdcb(%rip),%rsi
call   1080 <anetSetError@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    132a <anetTcpGenericConnect+0x18a>
lea    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rax
mov    (%rax),%esi
mov    $0x4,%edx
call   1090 <memcpy@plt>
mov    -0x20(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     1298 <anetTcpGenericConnect+0xf8>
mov    -0x10(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1030 <anetNonBlock@plt>
cmp    $0x0,%rax
je     1293 <anetTcpGenericConnect+0xf3>
movl   $0xffffffff,-0x4(%rbp)
jmp    132a <anetTcpGenericConnect+0x18a>
jmp    1298 <anetTcpGenericConnect+0xf8>
mov    -0x24(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <connect@plt>
cmp    $0xffffffff,%eax
jne    1324 <anetTcpGenericConnect+0x184>
mov    0x2d17(%rip),%rax
mov    (%rax),%rax
mov    0x2d1d(%rip),%rcx
cmp    (%rcx),%rax
jne    12e6 <anetTcpGenericConnect+0x146>
mov    -0x20(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     12e6 <anetTcpGenericConnect+0x146>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    132a <anetTcpGenericConnect+0x18a>
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2cdb(%rip),%rax
mov    (%rax),%rdi
call   10c0 <strerror@plt>
mov    -0x40(%rbp),%rdi
mov    %rax,%rdx
lea    0xd07(%rip),%rsi
call   1080 <anetSetError@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    132a <anetTcpGenericConnect+0x18a>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<anetTcpGenericConnect>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%r14d
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %rdi,%r12
mov    0x2e28(%rip),%r15
mov    (%r15),%esi
call   1070 <anetCreateSocket@plt>
mov    %eax,%r13d
mov    $0xffffffff,%eax
cmp    $0xffffffff,%r13d
je     12a5 <anetTcpGenericConnect+0x105>
mov    (%r15),%eax
mov    %eax,0x10(%rsp)
mov    %ebp,%edi
call   1040 <htons@plt>
mov    %eax,0xc(%rsp)
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   10a0 <inet_aton@plt>
test   %rax,%rax
je     1206 <anetTcpGenericConnect+0x66>
test   $0x1,%r14b
jne    122a <anetTcpGenericConnect+0x8a>
jmp    123a <anetTcpGenericConnect+0x9a>
mov    %rbx,%rdi
call   1060 <gethostbyname@plt>
test   %rax,%rax
je     1272 <anetTcpGenericConnect+0xd2>
mov    (%rax),%esi
lea    0x8(%rsp),%rdi
mov    $0x4,%edx
call   1090 <memcpy@plt>
test   $0x1,%r14b
je     123a <anetTcpGenericConnect+0x9a>
mov    %r12,%rdi
mov    %r13d,%esi
call   1030 <anetNonBlock@plt>
test   %rax,%rax
jne    12a0 <anetTcpGenericConnect+0x100>
lea    0x8(%rsp),%rsi
mov    %r13d,%edi
mov    $0xc,%edx
call   10b0 <connect@plt>
cmp    $0xffffffff,%eax
jne    126d <anetTcpGenericConnect+0xcd>
mov    0x2d78(%rip),%rax
mov    (%rax),%rdi
test   $0x1,%r14b
je     1281 <anetTcpGenericConnect+0xe1>
mov    0x2d78(%rip),%rax
cmp    (%rax),%rdi
jne    1281 <anetTcpGenericConnect+0xe1>
mov    %r13d,%eax
jmp    12a5 <anetTcpGenericConnect+0x105>
lea    0xd87(%rip),%rsi
mov    %r12,%rdi
mov    %rbx,%rdx
jmp    1293 <anetTcpGenericConnect+0xf3>
call   10c0 <strerror@plt>
lea    0xd85(%rip),%rsi
mov    %r12,%rdi
mov    %rax,%rdx
call   1080 <anetSetError@plt>
mov    %r13d,%edi
call   1050 <close@plt>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@.str = [18 x i8] c"can't resolve: %s\00"
@errno = global i64 0
@EINPROGRESS = global i64 0
@.str.1 = [12 x i8] c"connect: %s\00"
define i32 @anetTcpGenericConnect(i8* %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca %struct.sockaddr_in
%12 = alloca %struct.hostent*
store i8* %0, i8** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%13 = load i8*, i8** %6
%14 = load i32, i32* @AF_INET
%15 = call i32 @anetCreateSocket(i8* %13, i32 %14)
store i32 %15, i32* %10
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %18
17: ; preds = %4
store i32 -1, i32* %5
br label %80
18: ; preds = %4
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 2
store i32 %19, i32* %20
%21 = load i32, i32* %8
%22 = call i32 @htons(i32 %21)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 1
store i32 %22, i32* %23
%24 = load i8*, i8** %7
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
%26 = call i64 @inet_aton(i8* %24, i32* %25)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %45
28: ; preds = %18
%29 = load i8*, i8** %7
%30 = call %struct.hostent* @gethostbyname(i8* %29)
store %struct.hostent* %30, %struct.hostent** %12
%31 = load %struct.hostent*, %struct.hostent** %12
%32 = icmp eq %struct.hostent* %31, null
br i1 %32, label %33, label %39
33: ; preds = %28
%34 = load i8*, i8** %6
%35 = load i8*, i8** %7
%36 = call i32 @anetSetError(i8* %34, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %35)
%37 = load i32, i32* %10
%38 = call i32 @close(i32 %37)
store i32 -1, i32* %5
br label %80
39: ; preds = %28
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
%41 = load %struct.hostent*, %struct.hostent** %12
%42 = getelementptr inbounds %struct.hostent, %struct.hostent* %41, i32 0, i32 0
%43 = load i32, i32* %42
%44 = call i32 @memcpy(i32* %40, i32 %43, i32 4)
br label %45
45: ; preds = %39, %18
%46 = load i32, i32* %9
%47 = and i32 %46, 1
%48 = icmp ne i32 %47, 0
br i1 %48, label %49, label %56
49: ; preds = %45
%50 = load i8*, i8** %6
%51 = load i32, i32* %10
%52 = call i64 @anetNonBlock(i8* %50, i32 %51)
%53 = icmp ne i64 %52, 0
br i1 %53, label %54, label %55
54: ; preds = %49
store i32 -1, i32* %5
br label %80
55: ; preds = %49
br label %56
56: ; preds = %55, %45
%57 = load i32, i32* %10
%58 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%59 = call i32 @connect(i32 %57, %struct.sockaddr* %58, i32 12)
%60 = icmp eq i32 %59, -1
br i1 %60, label %61, label %78
61: ; preds = %56
%62 = load i64, i64* @errno
%63 = load i64, i64* @EINPROGRESS
%64 = icmp eq i64 %62, %63
br i1 %64, label %65, label %71
65: ; preds = %61
%66 = load i32, i32* %9
%67 = and i32 %66, 1
%68 = icmp ne i32 %67, 0
br i1 %68, label %69, label %71
69: ; preds = %65
%70 = load i32, i32* %10
store i32 %70, i32* %5
br label %80
71: ; preds = %65, %61
%72 = load i8*, i8** %6
%73 = load i64, i64* @errno
%74 = call i8* @strerror(i64 %73)
%75 = call i32 @anetSetError(i8* %72, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %74)
%76 = load i32, i32* %10
%77 = call i32 @close(i32 %76)
store i32 -1, i32* %5
br label %80
78: ; preds = %56
%79 = load i32, i32* %10
store i32 %79, i32* %5
br label %80
80: ; preds = %78, %71, %69, %54, %33, %17
%81 = load i32, i32* %5
ret i32 %81
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@.str = [18 x i8] c"can't resolve: %s\00"
@errno = global i64 0
@EINPROGRESS = global i64 0
@.str.1 = [12 x i8] c"connect: %s\00"
define i32 @anetTcpGenericConnect(i8* %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca %struct.sockaddr_in
%6 = bitcast %struct.sockaddr_in* %5 to i8*
%7 = load i32, i32* @AF_INET
%8 = tail call i32 @anetCreateSocket(i8* %0, i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %47, label %10
10: ; preds = %4
%11 = load i32, i32* @AF_INET
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 2
store i32 %11, i32* %12
%13 = tail call i32 @htons(i32 %2)
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1
store i32 %13, i32* %14
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0
%16 = call i64 @inet_aton(i8* %1, i32* %15)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %27
18: ; preds = %10
%19 = call %struct.hostent* @gethostbyname(i8* %1)
%20 = icmp eq %struct.hostent* %19, null
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = call i32 @anetSetError(i8* %0, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %1)
br label %45
23: ; preds = %18
%24 = getelementptr inbounds %struct.hostent, %struct.hostent* %19, i64 0, i32 0
%25 = load i32, i32* %24
%26 = call i32 @memcpy(i32* %15, i32 %25, i32 4)
br label %27
27: ; preds = %23, %10
%28 = and i32 %3, 1
%29 = icmp eq i32 %28, 0
br i1 %29, label %33, label %30
30: ; preds = %27
%31 = call i64 @anetNonBlock(i8* %0, i32 %8)
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %47
33: ; preds = %30, %27
%34 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%35 = call i32 @connect(i32 %8, %struct.sockaddr* %34, i32 12)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %47
37: ; preds = %33
%38 = load i64, i64* @errno
%39 = load i64, i64* @EINPROGRESS
%40 = icmp ne i64 %38, %39
%41 = or i1 %29, %40
br i1 %41, label %42, label %47
42: ; preds = %37
%43 = call i8* @strerror(i64 %38)
%44 = call i32 @anetSetError(i8* %0, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %43)
br label %45
45: ; preds = %42, %21
%46 = call i32 @close(i32 %8)
br label %47
47: ; preds = %45, %33, %37, %30, %4
%48 = phi i32 [ -1, %4 ], [ -1, %30 ], [ %8, %37 ], [ %8, %33 ], [ -1, %45 ]
ret i32 %48
}