#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;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ anetSetError(char *, char *, char *);
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ 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 socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
char *strerror(int /*<<< orphan*/);
int anetTcpConnect(char *err, char *addr, int port) {
  int s;
  struct sockaddr_in sa;
  if ((s = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    anetSetError(err, "creating socket: %s\n", strerror(errno));
    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\n", addr);
      close(s);
      return -1;
    }
    memcpy(&sa.sin_addr, he->h_addr, sizeof(struct in_addr));
  }
  if (connect(s, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
    anetSetError(err, "connect: %s\n", strerror(errno));
    close(s);
    return -1;
  }
  return s;
}


x86 assembly (clang, -O0):
<anetTcpConnect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    0x2e3e(%rip),%rax
mov    (%rax),%edi
mov    0x2e25(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x20(%rbp)
cmp    $0xffffffff,%eax
jne    11fd <anetTcpConnect+0x6d>
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x40(%rbp),%rdi
mov    %rax,%rdx
lea    0xe14(%rip),%rsi
call   1060 <anetSetError@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12dc <anetTcpConnect+0x14c>
mov    0x2de4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x1c(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1080 <inet_aton@plt>
cmp    $0x0,%rax
jne    127f <anetTcpConnect+0xef>
mov    -0x18(%rbp),%rdi
call   1050 <gethostbyname@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    126b <anetTcpConnect+0xdb>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xdc3(%rip),%rsi
call   1060 <anetSetError@plt>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12dc <anetTcpConnect+0x14c>
lea    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rax
mov    (%rax),%esi
mov    $0x4,%edx
call   1070 <memcpy@plt>
mov    -0x20(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   1090 <connect@plt>
cmp    $0xffffffff,%eax
jne    12d6 <anetTcpConnect+0x146>
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    0x2d28(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x48(%rbp),%rdi
mov    %rax,%rdx
lea    0xd6b(%rip),%rsi
call   1060 <anetSetError@plt>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12dc <anetTcpConnect+0x14c>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<anetTcpConnect>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edx,%ebp
mov    %rsi,%r15
mov    %rdi,%r14
mov    0x2e3d(%rip),%r12
mov    (%r12),%edi
mov    0x2e22(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
cmp    $0xffffffff,%eax
je     1236 <anetTcpConnect+0xa6>
mov    %eax,%ebx
mov    (%r12),%eax
mov    %eax,0x8(%rsp)
mov    %ebp,%edi
call   1030 <htons@plt>
mov    %eax,0x4(%rsp)
mov    %rsp,%rsi
mov    %r15,%rdi
call   1080 <inet_aton@plt>
test   %rax,%rax
jne    1205 <anetTcpConnect+0x75>
mov    %r15,%rdi
call   1050 <gethostbyname@plt>
test   %rax,%rax
je     1258 <anetTcpConnect+0xc8>
mov    (%rax),%esi
mov    %rsp,%rdi
mov    $0x4,%edx
call   1070 <memcpy@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1090 <connect@plt>
cmp    $0xffffffff,%eax
jne    1276 <anetTcpConnect+0xe6>
mov    0x2db0(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xdfa(%rip),%rsi
mov    %r14,%rdi
mov    %rax,%rdx
jmp    1265 <anetTcpConnect+0xd5>
mov    0x2d93(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xdb5(%rip),%rsi
mov    %r14,%rdi
mov    %rax,%rdx
call   1060 <anetSetError@plt>
jmp    1271 <anetTcpConnect+0xe1>
lea    0xdb6(%rip),%rsi
mov    %r14,%rdi
mov    %r15,%rdx
call   1060 <anetSetError@plt>
mov    %ebx,%edi
call   1040 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
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 i32 0
@.str.1 = [19 x i8] c"can't resolve: %s\0A\00"
@.str.2 = [13 x i8] c"connect: %s\0A\00"
define i32 @anetTcpConnect(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.sockaddr_in
%10 = alloca %struct.hostent*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_STREAM
%13 = call i32 @socket(i32 %11, i32 %12, i32 0)
store i32 %13, i32* %8
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %20
15: ; preds = %3
%16 = load i8*, i8** %5
%17 = load i32, i32* @errno
%18 = call i8* @strerror(i32 %17)
%19 = call i32 @anetSetError(i8* %16, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %18)
store i32 -1, i32* %4
br label %61
20: ; preds = %3
%21 = load i32, i32* @AF_INET
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i32 %21, i32* %22
%23 = load i32, i32* %7
%24 = call i32 @htons(i32 %23)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
store i32 %24, i32* %25
%26 = load i8*, i8** %6
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
%28 = call i64 @inet_aton(i8* %26, i32* %27)
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %47
30: ; preds = %20
%31 = load i8*, i8** %6
%32 = call %struct.hostent* @gethostbyname(i8* %31)
store %struct.hostent* %32, %struct.hostent** %10
%33 = load %struct.hostent*, %struct.hostent** %10
%34 = icmp eq %struct.hostent* %33, null
br i1 %34, label %35, label %41
35: ; preds = %30
%36 = load i8*, i8** %5
%37 = load i8*, i8** %6
%38 = call i32 @anetSetError(i8* %36, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %37)
%39 = load i32, i32* %8
%40 = call i32 @close(i32 %39)
store i32 -1, i32* %4
br label %61
41: ; preds = %30
%42 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
%43 = load %struct.hostent*, %struct.hostent** %10
%44 = getelementptr inbounds %struct.hostent, %struct.hostent* %43, i32 0, i32 0
%45 = load i32, i32* %44
%46 = call i32 @memcpy(i32* %42, i32 %45, i32 4)
br label %47
47: ; preds = %41, %20
%48 = load i32, i32* %8
%49 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%50 = call i32 @connect(i32 %48, %struct.sockaddr* %49, i32 12)
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %59
52: ; preds = %47
%53 = load i8*, i8** %5
%54 = load i32, i32* @errno
%55 = call i8* @strerror(i32 %54)
%56 = call i32 @anetSetError(i8* %53, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* %55)
%57 = load i32, i32* %8
%58 = call i32 @close(i32 %57)
store i32 -1, i32* %4
br label %61
59: ; preds = %47
%60 = load i32, i32* %8
store i32 %60, i32* %4
br label %61
61: ; preds = %59, %52, %35, %15
%62 = load i32, i32* %4
ret i32 %62
}

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 i32 0
@.str.1 = [19 x i8] c"can't resolve: %s\0A\00"
@.str.2 = [13 x i8] c"connect: %s\0A\00"
define i32 @anetTcpConnect(i8* %0, i8* %1, i32 %2) {
%4 = alloca %struct.sockaddr_in
%5 = bitcast %struct.sockaddr_in* %4 to i8*
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_STREAM
%8 = tail call i32 @socket(i32 %6, i32 %7, i32 0)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %14
10: ; preds = %3
%11 = load i32, i32* @errno
%12 = tail call i8* @strerror(i32 %11)
%13 = tail call i32 @anetSetError(i8* %0, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %12)
br label %41
14: ; preds = %3
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %15, i32* %16
%17 = tail call i32 @htons(i32 %2)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %17, i32* %18
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
%20 = call i64 @inet_aton(i8* %1, i32* %19)
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %32
22: ; preds = %14
%23 = call %struct.hostent* @gethostbyname(i8* %1)
%24 = icmp eq %struct.hostent* %23, null
br i1 %24, label %25, label %28
25: ; preds = %22
%26 = call i32 @anetSetError(i8* %0, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %1)
%27 = call i32 @close(i32 %8)
br label %41
28: ; preds = %22
%29 = getelementptr inbounds %struct.hostent, %struct.hostent* %23, i64 0, i32 0
%30 = load i32, i32* %29
%31 = call i32 @memcpy(i32* %19, i32 %30, i32 4)
br label %32
32: ; preds = %28, %14
%33 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%34 = call i32 @connect(i32 %8, %struct.sockaddr* %33, i32 12)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %41
36: ; preds = %32
%37 = load i32, i32* @errno
%38 = call i8* @strerror(i32 %37)
%39 = call i32 @anetSetError(i8* %0, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* %38)
%40 = call i32 @close(i32 %8)
br label %41
41: ; preds = %25, %32, %36, %10
%42 = phi i32 [ -1, %10 ], [ -1, %36 ], [ %8, %32 ], [ -1, %25 ]
ret i32 %42
}