#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 {
  int dummy;
};
typedef int /*<<< orphan*/ i;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ TCP_NODELAY;
int /*<<< orphan*/ *conn_get_sockaddr(char const *, unsigned short,
                                      struct sockaddr *);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              void *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
char *strerror(int /*<<< orphan*/);
int conn_active_open(const char *host, unsigned short port) {
  int sock = -1;
  struct sockaddr dst;
  printf("Opening socket");
  printf("\n");
  ;
  sock = socket(AF_INET, SOCK_STREAM, 0);
  if (sock < 0) {
    printf("Could not get socket: %s", strerror(errno));
    printf("\n");
    ;
    return -1;
  }
  int i = 1;
  setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (void *)&i, sizeof(i));
  if (NULL == conn_get_sockaddr(host, port, &dst)) {
    printf("Could not get sockaddr");
    printf("\n");
    ;
    return -1;
  }
  if (connect(sock, &dst, sizeof(struct sockaddr)) < 0) {
    printf("Could not connect to %s:%d:  %s", host, port, strerror(errno));
    printf("\n");
    ;
    return -1;
  }
  printf("Opened socket");
  printf("\n");
  ;
  return sock;
}


x86 assembly (clang, -O0):
<conn_active_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %si,%ax
mov    %rdi,-0x10(%rbp)
mov    %ax,-0x12(%rbp)
movl   $0xffffffff,-0x18(%rbp)
lea    0xe7f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe80(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
mov    0x2e22(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11f5 <conn_active_open+0x95>
mov    0x2dfd(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %rax,%rsi
lea    0xe3d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe2d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f3 <conn_active_open+0x193>
movl   $0x1,-0x24(%rbp)
mov    -0x18(%rbp),%edi
mov    0x2dd2(%rip),%rax
mov    (%rax),%esi
mov    0x2dd9(%rip),%rax
mov    (%rax),%edx
lea    -0x24(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rdx
movzwl -0x12(%rbp),%esi
call   1040 <conn_get_sockaddr@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    1267 <conn_active_open+0x107>
lea    0xde4(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xdbb(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f3 <conn_active_open+0x193>
mov    -0x18(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   1060 <connect@plt>
cmp    $0x0,%rax
jge    12d1 <conn_active_open+0x171>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
movzwl -0x12(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2d28(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%edx
mov    %rax,%rcx
lea    0xd91(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xd51(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f3 <conn_active_open+0x193>
lea    0xd89(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xd29(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<conn_active_open>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebx
mov    %rdi,%r14
lea    0xe7c(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
mov    0x2e23(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
test   %eax,%eax
js     121d <conn_active_open+0xad>
mov    %eax,%ebp
movl   $0x1,0xc(%rsp)
mov    0x2e15(%rip),%rax
mov    (%rax),%esi
mov    0x2e1c(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
movzwl %bx,%ebx
lea    0x8(%rsp),%rdx
mov    %r14,%rdi
mov    %ebx,%esi
call   1050 <conn_get_sockaddr@plt>
test   %rax,%rax
je     124d <conn_active_open+0xdd>
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
mov    $0x4,%edx
call   1070 <connect@plt>
test   %rax,%rax
js     125d <conn_active_open+0xed>
lea    0xe4b(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1286 <conn_active_open+0x116>
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xddd(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    $0xffffffff,%ebp
jmp    1290 <conn_active_open+0x120>
lea    0xdd4(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1281 <conn_active_open+0x111>
mov    0x2d5c(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xdcd(%rip),%rdi
mov    %r14,%rsi
mov    %ebx,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xffffffff,%ebp
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
@.str = [15 x i8] c"Opening socket\00"
@.str.1 = [2 x i8] c"\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [25 x i8] c"Could not get socket: %s\00"
@errno = global i32 0
@IPPROTO_TCP = global i32 0
@TCP_NODELAY = global i32 0
@.str.3 = [23 x i8] c"Could not get sockaddr\00"
@.str.4 = [32 x i8] c"Could not connect to %s:%d: %s\00"
@.str.5 = [14 x i8] c"Opened socket\00"
define i32 @conn_active_open(i8* %0, i16 zeroext %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i16
%6 = alloca i32
%7 = alloca %struct.sockaddr
%8 = alloca i32
store i8* %0, i8** %4
store i16 %1, i16* %5
store i32 -1, i32* %6
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%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* %6
%14 = load i32, i32* %6
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %21
16: ; preds = %2
%17 = load i32, i32* @errno
%18 = call i8* @strerror(i32 %17)
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %18)
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %50
21: ; preds = %2
store i32 1, i32* %8
%22 = load i32, i32* %6
%23 = load i32, i32* @IPPROTO_TCP
%24 = load i32, i32* @TCP_NODELAY
%25 = bitcast i32* %8 to i8*
%26 = call i32 @setsockopt(i32 %22, i32 %23, i32 %24, i8* %25, i32 4)
%27 = load i8*, i8** %4
%28 = load i16, i16* %5
%29 = call i32* @conn_get_sockaddr(i8* %27, i16 zeroext %28, %struct.sockaddr* %7)
%30 = icmp eq i32* null, %29
br i1 %30, label %31, label %34
31: ; preds = %21
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %50
34: ; preds = %21
%35 = load i32, i32* %6
%36 = call i64 @connect(i32 %35, %struct.sockaddr* %7, i32 4)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %46
38: ; preds = %34
%39 = load i8*, i8** %4
%40 = load i16, i16* %5
%41 = zext i16 %40 to i32
%42 = load i32, i32* @errno
%43 = call i8* @strerror(i32 %42)
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i8* %39, i32 %41, i8* %43)
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %50
46: ; preds = %34
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%49 = load i32, i32* %6
store i32 %49, i32* %3
br label %50
50: ; preds = %46, %38, %31, %16
%51 = load i32, i32* %3
ret i32 %51
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@.str = [15 x i8] c"Opening socket\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [25 x i8] c"Could not get socket: %s\00"
@errno = global i32 0
@IPPROTO_TCP = global i32 0
@TCP_NODELAY = global i32 0
@.str.3 = [23 x i8] c"Could not get sockaddr\00"
@.str.4 = [32 x i8] c"Could not connect to %s:%d: %s\00"
@.str.5 = [14 x i8] c"Opened socket\00"
define i32 @conn_active_open(i8* %0, i16 zeroext %1) {
%3 = alloca %struct.sockaddr
%4 = alloca i32
%5 = bitcast %struct.sockaddr* %3 to i8*
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 @putchar(i32 10)
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = tail call i32 @socket(i32 %8, i32 %9, i32 0)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %17
12: ; preds = %2
%13 = load i32, i32* @errno
%14 = tail call i8* @strerror(i32 %13)
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %14)
%16 = tail call i32 @putchar(i32 10)
br label %39
17: ; preds = %2
%18 = bitcast i32* %4 to i8*
store i32 1, i32* %4
%19 = load i32, i32* @IPPROTO_TCP
%20 = load i32, i32* @TCP_NODELAY
%21 = call i32 @setsockopt(i32 %10, i32 %19, i32 %20, i8* %18, i32 4)
%22 = call i32* @conn_get_sockaddr(i8* %0, i16 zeroext %1, %struct.sockaddr* %3)
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %26
24: ; preds = %17
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
br label %36
26: ; preds = %17
%27 = call i64 @connect(i32 %10, %struct.sockaddr* %3, i32 4)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %34
29: ; preds = %26
%30 = zext i16 %1 to i32
%31 = load i32, i32* @errno
%32 = call i8* @strerror(i32 %31)
%33 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i8* %0, i32 %30, i8* %32)
br label %36
34: ; preds = %26
%35 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
br label %36
36: ; preds = %34, %29, %24
%37 = phi i32 [ -1, %24 ], [ -1, %29 ], [ %10, %34 ]
%38 = call i32 @putchar(i32 10)
br label %39
39: ; preds = %36, %12
%40 = phi i32 [ -1, %12 ], [ %37, %36 ]
ret i32 %40
}