#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 /*<<< 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 wheatCreateSocket(char *, int /*<<< orphan*/);
scalar_t__ wheatNonBlock(char *, int);
int /*<<< orphan*/ wheatSetError(char *, char *, char *);
int wheatTcpGenericConnect(char *err, char *addr, int port, int flags) {
  int s;
  struct sockaddr_in sa;
  if ((s = wheatCreateSocket(err, AF_INET)) == -1)
    return -1;
  sa.sin_family = AF_INET;
  sa.sin_port = htons(port);
  if (!addr)
    addr = "127.0.0.1";
  if (inet_aton(addr, &sa.sin_addr) == 0) {
    struct hostent *he;
    he = gethostbyname(addr);
    if (he == NULL) {
      wheatSetError(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 (wheatNonBlock(err, s) != 0)
      return -1;
  }
  if (connect(s, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
    if (errno == EINPROGRESS && flags & 1)
      return s;
    wheatSetError(err, "connect: %s", strerror(errno));
    close(s);
    return -1;
  }
  return s;
}


x86 assembly (clang, -O0):
<wheatTcpGenericConnect>:
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   1050 <wheatCreateSocket@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
jne    11e0 <wheatTcpGenericConnect+0x40>
movl   $0xffffffff,-0x4(%rbp)
jmp    1340 <wheatTcpGenericConnect+0x1a0>
mov    0x2e01(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x1c(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x2c(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    120d <wheatTcpGenericConnect+0x6d>
lea    0xdf7(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1090 <inet_aton@plt>
cmp    $0x0,%rax
jne    1278 <wheatTcpGenericConnect+0xd8>
mov    -0x18(%rbp),%rdi
call   1060 <gethostbyname@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1264 <wheatTcpGenericConnect+0xc4>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xdbf(%rip),%rsi
call   10b0 <wheatSetError@plt>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1340 <wheatTcpGenericConnect+0x1a0>
lea    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rax
mov    (%rax),%esi
mov    $0x4,%edx
call   1070 <memcpy@plt>
mov    -0x20(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     12ae <wheatTcpGenericConnect+0x10e>
mov    -0x10(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1080 <wheatNonBlock@plt>
cmp    $0x0,%rax
je     12a9 <wheatTcpGenericConnect+0x109>
movl   $0xffffffff,-0x4(%rbp)
jmp    1340 <wheatTcpGenericConnect+0x1a0>
jmp    12ae <wheatTcpGenericConnect+0x10e>
mov    -0x24(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <connect@plt>
cmp    $0xffffffff,%eax
jne    133a <wheatTcpGenericConnect+0x19a>
mov    0x2d01(%rip),%rax
mov    (%rax),%rax
mov    0x2d07(%rip),%rcx
cmp    (%rcx),%rax
jne    12fc <wheatTcpGenericConnect+0x15c>
mov    -0x20(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     12fc <wheatTcpGenericConnect+0x15c>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1340 <wheatTcpGenericConnect+0x1a0>
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2cc5(%rip),%rax
mov    (%rax),%rdi
call   10c0 <strerror@plt>
mov    -0x40(%rbp),%rdi
mov    %rax,%rdx
lea    0xcfb(%rip),%rsi
call   10b0 <wheatSetError@plt>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1340 <wheatTcpGenericConnect+0x1a0>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wheatTcpGenericConnect>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%r14d
mov    %edx,%ebx
mov    %rsi,%rbp
mov    %rdi,%r12
mov    0x2e28(%rip),%r15
mov    (%r15),%esi
call   1050 <wheatCreateSocket@plt>
mov    %eax,%r13d
mov    $0xffffffff,%eax
cmp    $0xffffffff,%r13d
je     12b3 <wheatTcpGenericConnect+0x113>
mov    (%r15),%eax
mov    %eax,0x10(%rsp)
mov    %ebx,%edi
call   1030 <htons@plt>
mov    %eax,0xc(%rsp)
test   %rbp,%rbp
lea    0xe0a(%rip),%rbx
cmovne %rbp,%rbx
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1090 <inet_aton@plt>
test   %rax,%rax
je     1214 <wheatTcpGenericConnect+0x74>
test   $0x1,%r14b
jne    1238 <wheatTcpGenericConnect+0x98>
jmp    1248 <wheatTcpGenericConnect+0xa8>
mov    %rbx,%rdi
call   1060 <gethostbyname@plt>
test   %rax,%rax
je     1280 <wheatTcpGenericConnect+0xe0>
mov    (%rax),%esi
lea    0x8(%rsp),%rdi
mov    $0x4,%edx
call   1070 <memcpy@plt>
test   $0x1,%r14b
je     1248 <wheatTcpGenericConnect+0xa8>
mov    %r12,%rdi
mov    %r13d,%esi
call   1080 <wheatNonBlock@plt>
test   %rax,%rax
jne    12ae <wheatTcpGenericConnect+0x10e>
lea    0x8(%rsp),%rsi
mov    %r13d,%edi
mov    $0xc,%edx
call   10a0 <connect@plt>
cmp    $0xffffffff,%eax
jne    127b <wheatTcpGenericConnect+0xdb>
mov    0x2d6a(%rip),%rax
mov    (%rax),%rdi
test   $0x1,%r14b
je     128f <wheatTcpGenericConnect+0xef>
mov    0x2d6a(%rip),%rax
cmp    (%rax),%rdi
jne    128f <wheatTcpGenericConnect+0xef>
mov    %r13d,%eax
jmp    12b3 <wheatTcpGenericConnect+0x113>
lea    0xd83(%rip),%rsi
mov    %r12,%rdi
mov    %rbx,%rdx
jmp    12a1 <wheatTcpGenericConnect+0x101>
call   10c0 <strerror@plt>
lea    0xd81(%rip),%rsi
mov    %r12,%rdi
mov    %rax,%rdx
call   10b0 <wheatSetError@plt>
mov    %r13d,%edi
call   1040 <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 = [10 x i8] c"127.0.0.1\00"
@.str.1 = [18 x i8] c"can't resolve: %s\00"
@errno = global i64 0
@EINPROGRESS = global i64 0
@.str.2 = [12 x i8] c"connect: %s\00"
define i32 @wheatTcpGenericConnect(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 @wheatCreateSocket(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 %84
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 = icmp ne i8* %24, null
br i1 %25, label %27, label %26
26: ; preds = %18
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8** %7
br label %27
27: ; preds = %26, %18
%28 = load i8*, i8** %7
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
%30 = call i64 @inet_aton(i8* %28, i32* %29)
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %49
32: ; preds = %27
%33 = load i8*, i8** %7
%34 = call %struct.hostent* @gethostbyname(i8* %33)
store %struct.hostent* %34, %struct.hostent** %12
%35 = load %struct.hostent*, %struct.hostent** %12
%36 = icmp eq %struct.hostent* %35, null
br i1 %36, label %37, label %43
37: ; preds = %32
%38 = load i8*, i8** %6
%39 = load i8*, i8** %7
%40 = call i32 @wheatSetError(i8* %38, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %39)
%41 = load i32, i32* %10
%42 = call i32 @close(i32 %41)
store i32 -1, i32* %5
br label %84
43: ; preds = %32
%44 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
%45 = load %struct.hostent*, %struct.hostent** %12
%46 = getelementptr inbounds %struct.hostent, %struct.hostent* %45, i32 0, i32 0
%47 = load i32, i32* %46
%48 = call i32 @memcpy(i32* %44, i32 %47, i32 4)
br label %49
49: ; preds = %43, %27
%50 = load i32, i32* %9
%51 = and i32 %50, 1
%52 = icmp ne i32 %51, 0
br i1 %52, label %53, label %60
53: ; preds = %49
%54 = load i8*, i8** %6
%55 = load i32, i32* %10
%56 = call i64 @wheatNonBlock(i8* %54, i32 %55)
%57 = icmp ne i64 %56, 0
br i1 %57, label %58, label %59
58: ; preds = %53
store i32 -1, i32* %5
br label %84
59: ; preds = %53
br label %60
60: ; preds = %59, %49
%61 = load i32, i32* %10
%62 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%63 = call i32 @connect(i32 %61, %struct.sockaddr* %62, i32 12)
%64 = icmp eq i32 %63, -1
br i1 %64, label %65, label %82
65: ; preds = %60
%66 = load i64, i64* @errno
%67 = load i64, i64* @EINPROGRESS
%68 = icmp eq i64 %66, %67
br i1 %68, label %69, label %75
69: ; preds = %65
%70 = load i32, i32* %9
%71 = and i32 %70, 1
%72 = icmp ne i32 %71, 0
br i1 %72, label %73, label %75
73: ; preds = %69
%74 = load i32, i32* %10
store i32 %74, i32* %5
br label %84
75: ; preds = %69, %65
%76 = load i8*, i8** %6
%77 = load i64, i64* @errno
%78 = call i8* @strerror(i64 %77)
%79 = call i32 @wheatSetError(i8* %76, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* %78)
%80 = load i32, i32* %10
%81 = call i32 @close(i32 %80)
store i32 -1, i32* %5
br label %84
82: ; preds = %60
%83 = load i32, i32* %10
store i32 %83, i32* %5
br label %84
84: ; preds = %82, %75, %73, %58, %37, %17
%85 = load i32, i32* %5
ret i32 %85
}

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