#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  scalar_t__ s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
  int /*<<< orphan*/ sin_port;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ assert(int);
int connect_nonb(int, struct sockaddr const *, int, int);
int errno;
int fcntl(int, int /*<<< orphan*/, ...);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_pton(int /*<<< orphan*/, char const *, TYPE_1__ *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int connectToIP(const char *ip) {
  int sockfd = socket(AF_INET, SOCK_STREAM, 0);
  assert(sockfd >= 0);
  struct sockaddr_in addr;
  memset(&addr, 0, sizeof(addr));
  addr.sin_port = htons(80);
  addr.sin_family = AF_INET;
  addr.sin_addr.s_addr = 0;
  inet_pton(AF_INET, ip, &addr.sin_addr);
  int err =
      connect_nonb(sockfd, (const struct sockaddr *)&addr, sizeof(addr), 5);
  if (err != 0) {
    fprintf(stderr, "Error with sockies: %d.\n", errno);
    return -1;
  }
  assert(err == 0);
  int flags = fcntl(sockfd, F_GETFL);
  err = fcntl(sockfd, F_SETFL, flags | O_NONBLOCK);
  assert(err == 0);
  return sockfd;
}


x86 assembly (clang, -O0):
<connectToIP>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
mov    0x2e24(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
setge  %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <memset@plt>
mov    $0x50,%edi
call   1050 <htons@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2e0b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
lea    -0x28(%rbp),%rdx
call   1090 <inet_pton@plt>
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x10,%edx
mov    $0x5,%ecx
call   1030 <connect_nonb@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     124d <connectToIP+0xcd>
mov    0x2da6(%rip),%rax
mov    (%rax),%edi
mov    0x2d85(%rip),%rax
mov    (%rax),%edx
lea    0xdc4(%rip),%rsi
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ad <connectToIP+0x12d>
cmpl   $0x0,-0x2c(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x14(%rbp),%edi
mov    0x2d48(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1040 <fcntl@plt>
mov    %eax,-0x30(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2d4a(%rip),%rax
mov    (%rax),%esi
mov    -0x30(%rbp),%edx
mov    0x2d56(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1040 <fcntl@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connectToIP>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
mov    0x2e53(%rip),%r12
mov    (%r12),%edi
mov    0x2e20(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,%ebx
mov    %eax,%edi
not    %edi
shr    $0x1f,%edi
call   1070 <assert@plt>
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <memset@plt>
mov    $0x50,%edi
call   1050 <htons@plt>
mov    %eax,0x14(%rsp)
mov    (%r12),%edi
mov    %edi,0x10(%rsp)
movq   $0x0,0x8(%rsp)
mov    %r14,%rsi
mov    %r15,%rdx
call   1090 <inet_pton@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x10,%edx
mov    $0x5,%ecx
call   1030 <connect_nonb@plt>
test   %eax,%eax
je     1232 <connectToIP+0xb2>
mov    0x2dbc(%rip),%rax
mov    (%rax),%edi
mov    0x2d9b(%rip),%rax
mov    (%rax),%edx
lea    0xdda(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0xffffffff,%ebx
jmp    1278 <connectToIP+0xf8>
mov    $0x1,%edi
call   1070 <assert@plt>
mov    0x2d6d(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %eax,%eax
call   1040 <fcntl@plt>
mov    0x2d73(%rip),%rcx
mov    (%rcx),%esi
mov    0x2d82(%rip),%rcx
or     (%rcx),%eax
mov    %ebx,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <fcntl@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1070 <assert@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i64 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str = [25 x i8] c"Error with sockies: %d.\0A\00"
@errno = global i32 0
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
define i32 @connectToIP(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %4
%11 = load i32, i32* %4
%12 = icmp sge i32 %11, 0
%13 = zext i1 %12 to i32
%14 = call i32 @assert(i32 %13)
%15 = call i32 @memset(%struct.sockaddr_in* %5, i32 0, i32 16)
%16 = call i32 @htons(i32 80)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 2
store i32 %16, i32* %17
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
store i32 %18, i32* %19
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
%21 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %20, i32 0, i32 0
store i64 0, i64* %21
%22 = load i32, i32* @AF_INET
%23 = load i8*, i8** %3
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
%25 = call i32 @inet_pton(i32 %22, i8* %23, %struct.TYPE_2__* %24)
%26 = load i32, i32* %4
%27 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%28 = call i32 @connect_nonb(i32 %26, %struct.sockaddr* %27, i32 16, i32 5)
store i32 %28, i32* %6
%29 = load i32, i32* %6
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %35
31: ; preds = %1
%32 = load i32, i32* @stderr
%33 = load i32, i32* @errno
%34 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %33)
store i32 -1, i32* %2
br label %54
35: ; preds = %1
%36 = load i32, i32* %6
%37 = icmp eq i32 %36, 0
%38 = zext i1 %37 to i32
%39 = call i32 @assert(i32 %38)
%40 = load i32, i32* %4
%41 = load i32, i32* @F_GETFL
%42 = call i32 (i32, i32, ...) @fcntl(i32 %40, i32 %41)
store i32 %42, i32* %7
%43 = load i32, i32* %4
%44 = load i32, i32* @F_SETFL
%45 = load i32, i32* %7
%46 = load i32, i32* @O_NONBLOCK
%47 = or i32 %45, %46
%48 = call i32 (i32, i32, ...) @fcntl(i32 %43, i32 %44, i32 %47)
store i32 %48, i32* %6
%49 = load i32, i32* %6
%50 = icmp eq i32 %49, 0
%51 = zext i1 %50 to i32
%52 = call i32 @assert(i32 %51)
%53 = load i32, i32* %4
store i32 %53, i32* %2
br label %54
54: ; preds = %35, %31
%55 = load i32, i32* %2
ret i32 %55
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i64 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str = [25 x i8] c"Error with sockies: %d.\0A\00"
@errno = global i32 0
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
define i32 @connectToIP(i8* %0) {
%2 = alloca %struct.sockaddr_in
%3 = load i32, i32* @AF_INET
%4 = load i32, i32* @SOCK_STREAM
%5 = tail call i32 @socket(i32 %3, i32 %4, i32 0)
%6 = xor i32 %5, -1
%7 = lshr i32 %6, 31
%8 = tail call i32 @assert(i32 %7)
%9 = bitcast %struct.sockaddr_in* %2 to i8*
%10 = call i32 @memset(%struct.sockaddr_in* %2, i32 0, i32 16)
%11 = call i32 @htons(i32 80)
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %11, i32* %12
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i32 %13, i32* %14
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0, i32 0
store i64 0, i64* %16
%17 = call i32 @inet_pton(i32 %13, i8* %0, %struct.TYPE_2__* %15)
%18 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%19 = call i32 @connect_nonb(i32 %5, %struct.sockaddr* %18, i32 16, i32 5)
%20 = icmp eq i32 %19, 0
br i1 %20, label %25, label %21
21: ; preds = %1
%22 = load i32, i32* @stderr
%23 = load i32, i32* @errno
%24 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %23)
br label %36
25: ; preds = %1
%26 = call i32 @assert(i32 1)
%27 = load i32, i32* @F_GETFL
%28 = call i32 (i32, i32, ...) @fcntl(i32 %5, i32 %27)
%29 = load i32, i32* @F_SETFL
%30 = load i32, i32* @O_NONBLOCK
%31 = or i32 %30, %28
%32 = call i32 (i32, i32, ...) @fcntl(i32 %5, i32 %29, i32 %31)
%33 = icmp eq i32 %32, 0
%34 = zext i1 %33 to i32
%35 = call i32 @assert(i32 %34)
br label %36
36: ; preds = %25, %21
%37 = phi i32 [ -1, %21 ], [ %5, %25 ]
ret i32 %37
}