#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;
typedef int /*<<< orphan*/ on;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ EINPROGRESS;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
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);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
char *strerror(scalar_t__);
int anetTcpGenericConnect(char *err, char *addr, int port, int flags) {
  int s, on = 1;
  struct sockaddr_in sa;
  if ((s = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    anetSetError(err, "creating socket: %s\n", strerror(errno));
    return -1;
  }
  setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
  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\n", 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\n", strerror(errno));
    close(s);
    return -1;
  }
  return s;
}


x86 assembly (clang, -O0):
<anetTcpGenericConnect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
movl   $0x1,-0x28(%rbp)
mov    0x2e0c(%rip),%rax
mov    (%rax),%edi
mov    0x2deb(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
jne    1228 <anetTcpGenericConnect+0x78>
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    0x2dbf(%rip),%rax
mov    (%rax),%rdi
call   10c0 <strerror@plt>
mov    -0x48(%rbp),%rdi
mov    %rax,%rdx
lea    0xde9(%rip),%rsi
call   1080 <anetSetError@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1396 <anetTcpGenericConnect+0x1e6>
mov    -0x24(%rbp),%edi
mov    0x2db6(%rip),%rax
mov    (%rax),%esi
mov    0x2d7d(%rip),%rax
mov    (%rax),%edx
lea    -0x28(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
mov    0x2d8d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x1c(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,-0x34(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   10a0 <inet_aton@plt>
cmp    $0x0,%rax
jne    12ce <anetTcpGenericConnect+0x11e>
mov    -0x18(%rbp),%rdi
call   1070 <gethostbyname@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    12ba <anetTcpGenericConnect+0x10a>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xd74(%rip),%rsi
call   1080 <anetSetError@plt>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1396 <anetTcpGenericConnect+0x1e6>
lea    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%rax
mov    (%rax),%esi
mov    $0x4,%edx
call   1090 <memcpy@plt>
mov    -0x20(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     1304 <anetTcpGenericConnect+0x154>
mov    -0x10(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1030 <anetNonBlock@plt>
cmp    $0x0,%rax
je     12ff <anetTcpGenericConnect+0x14f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1396 <anetTcpGenericConnect+0x1e6>
jmp    1304 <anetTcpGenericConnect+0x154>
mov    -0x24(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <connect@plt>
cmp    $0xffffffff,%eax
jne    1390 <anetTcpGenericConnect+0x1e0>
mov    0x2c9b(%rip),%rax
mov    (%rax),%rax
mov    0x2ca9(%rip),%rcx
cmp    (%rcx),%rax
jne    1352 <anetTcpGenericConnect+0x1a2>
mov    -0x20(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     1352 <anetTcpGenericConnect+0x1a2>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1396 <anetTcpGenericConnect+0x1e6>
mov    -0x10(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    0x2c5f(%rip),%rax
mov    (%rax),%rdi
call   10c0 <strerror@plt>
mov    -0x50(%rbp),%rdi
mov    %rax,%rdx
lea    0xcb1(%rip),%rsi
call   1080 <anetSetError@plt>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1396 <anetTcpGenericConnect+0x1e6>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%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,%r15d
mov    %edx,%ebx
mov    %rsi,%r12
mov    %rdi,%r14
movl   $0x1,0x4(%rsp)
mov    0x2e08(%rip),%r13
mov    0x0(%r13),%edi
mov    0x2de5(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
cmp    $0xffffffff,%eax
je     1244 <anetTcpGenericConnect+0x94>
mov    %eax,%ebp
mov    0x2dee(%rip),%rax
mov    (%rax),%esi
mov    0x2db5(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
mov    0x0(%r13),%eax
mov    %eax,0x10(%rsp)
mov    %ebx,%edi
call   1050 <htons@plt>
mov    %eax,0xc(%rsp)
lea    0x8(%rsp),%rsi
mov    %r12,%rdi
call   10a0 <inet_aton@plt>
test   %rax,%rax
je     126a <anetTcpGenericConnect+0xba>
test   $0x1,%r15b
jne    128e <anetTcpGenericConnect+0xde>
jmp    12a5 <anetTcpGenericConnect+0xf5>
mov    0x2d75(%rip),%rax
mov    (%rax),%rdi
call   10c0 <strerror@plt>
lea    0xda6(%rip),%rsi
mov    %r14,%rdi
mov    %rax,%rdx
call   1080 <anetSetError@plt>
jmp    1308 <anetTcpGenericConnect+0x158>
mov    %r12,%rdi
call   1070 <gethostbyname@plt>
test   %rax,%rax
je     12db <anetTcpGenericConnect+0x12b>
mov    (%rax),%esi
lea    0x8(%rsp),%rdi
mov    $0x4,%edx
call   1090 <memcpy@plt>
test   $0x1,%r15b
je     12a5 <anetTcpGenericConnect+0xf5>
mov    %r14,%rdi
mov    %ebp,%esi
call   1030 <anetNonBlock@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
test   %rcx,%rcx
jne    130d <anetTcpGenericConnect+0x15d>
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   10b0 <connect@plt>
cmp    $0xffffffff,%eax
jne    12d7 <anetTcpGenericConnect+0x127>
mov    0x2cfe(%rip),%rax
mov    (%rax),%rdi
test   $0x1,%r15b
je     12ea <anetTcpGenericConnect+0x13a>
mov    0x2d06(%rip),%rax
cmp    (%rax),%rdi
jne    12ea <anetTcpGenericConnect+0x13a>
mov    %ebp,%eax
jmp    130d <anetTcpGenericConnect+0x15d>
lea    0xd33(%rip),%rsi
mov    %r14,%rdi
mov    %r12,%rdx
jmp    12fc <anetTcpGenericConnect+0x14c>
call   10c0 <strerror@plt>
lea    0xd32(%rip),%rsi
mov    %r14,%rdi
mov    %rax,%rdx
call   1080 <anetSetError@plt>
mov    %ebp,%edi
call   1060 <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
@SOCK_STREAM = global i32 0
@.str = [21 x i8] c"creating socket: %s\0A\00"
@errno = global i64 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [19 x i8] c"can't resolve: %s\0A\00"
@EINPROGRESS = global i64 0
@.str.2 = [13 x i8] c"connect: %s\0A\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 i32
%12 = alloca %struct.sockaddr_in
%13 = alloca %struct.hostent*
store i8* %0, i8** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i32 1, i32* %11
%14 = load i32, i32* @AF_INET
%15 = load i32, i32* @SOCK_STREAM
%16 = call i32 @socket(i32 %14, i32 %15, i32 0)
store i32 %16, i32* %10
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %23
18: ; preds = %4
%19 = load i8*, i8** %6
%20 = load i64, i64* @errno
%21 = call i8* @strerror(i64 %20)
%22 = call i32 @anetSetError(i8* %19, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %21)
store i32 -1, i32* %5
br label %89
23: ; preds = %4
%24 = load i32, i32* %10
%25 = load i32, i32* @SOL_SOCKET
%26 = load i32, i32* @SO_REUSEADDR
%27 = call i32 @setsockopt(i32 %24, i32 %25, i32 %26, i32* %11, i32 4)
%28 = load i32, i32* @AF_INET
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 2
store i32 %28, i32* %29
%30 = load i32, i32* %8
%31 = call i32 @htons(i32 %30)
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 1
store i32 %31, i32* %32
%33 = load i8*, i8** %7
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 0
%35 = call i64 @inet_aton(i8* %33, i32* %34)
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %54
37: ; preds = %23
%38 = load i8*, i8** %7
%39 = call %struct.hostent* @gethostbyname(i8* %38)
store %struct.hostent* %39, %struct.hostent** %13
%40 = load %struct.hostent*, %struct.hostent** %13
%41 = icmp eq %struct.hostent* %40, null
br i1 %41, label %42, label %48
42: ; preds = %37
%43 = load i8*, i8** %6
%44 = load i8*, i8** %7
%45 = call i32 @anetSetError(i8* %43, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %44)
%46 = load i32, i32* %10
%47 = call i32 @close(i32 %46)
store i32 -1, i32* %5
br label %89
48: ; preds = %37
%49 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 0
%50 = load %struct.hostent*, %struct.hostent** %13
%51 = getelementptr inbounds %struct.hostent, %struct.hostent* %50, i32 0, i32 0
%52 = load i32, i32* %51
%53 = call i32 @memcpy(i32* %49, i32 %52, i32 4)
br label %54
54: ; preds = %48, %23
%55 = load i32, i32* %9
%56 = and i32 %55, 1
%57 = icmp ne i32 %56, 0
br i1 %57, label %58, label %65
58: ; preds = %54
%59 = load i8*, i8** %6
%60 = load i32, i32* %10
%61 = call i64 @anetNonBlock(i8* %59, i32 %60)
%62 = icmp ne i64 %61, 0
br i1 %62, label %63, label %64
63: ; preds = %58
store i32 -1, i32* %5
br label %89
64: ; preds = %58
br label %65
65: ; preds = %64, %54
%66 = load i32, i32* %10
%67 = bitcast %struct.sockaddr_in* %12 to %struct.sockaddr*
%68 = call i32 @connect(i32 %66, %struct.sockaddr* %67, i32 12)
%69 = icmp eq i32 %68, -1
br i1 %69, label %70, label %87
70: ; preds = %65
%71 = load i64, i64* @errno
%72 = load i64, i64* @EINPROGRESS
%73 = icmp eq i64 %71, %72
br i1 %73, label %74, label %80
74: ; preds = %70
%75 = load i32, i32* %9
%76 = and i32 %75, 1
%77 = icmp ne i32 %76, 0
br i1 %77, label %78, label %80
78: ; preds = %74
%79 = load i32, i32* %10
store i32 %79, i32* %5
br label %89
80: ; preds = %74, %70
%81 = load i8*, i8** %6
%82 = load i64, i64* @errno
%83 = call i8* @strerror(i64 %82)
%84 = call i32 @anetSetError(i8* %81, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* %83)
%85 = load i32, i32* %10
%86 = call i32 @close(i32 %85)
store i32 -1, i32* %5
br label %89
87: ; preds = %65
%88 = load i32, i32* %10
store i32 %88, i32* %5
br label %89
89: ; preds = %87, %80, %78, %63, %42, %18
%90 = load i32, i32* %5
ret i32 %90
}

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