#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 */
typedef int /*<<< orphan*/ uint16_t;
struct sockaddr_in {
  int dummy;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ serv_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ resolve(char *, int /*<<< orphan*/, struct sockaddr_in *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int openClientSocket(char *ip, uint16_t port) {
  int clientSocketFd = socket(AF_INET, SOCK_STREAM, 0);
  if (clientSocketFd < 0) {
    fprintf(stderr, "Error: Could not create socket!\n");
    return -1;
  }
  struct sockaddr_in serv_addr;
  if (!resolve(ip, port, &serv_addr)) {
    fprintf(stderr,
            "Error: '%s' could not be resolved and is not a valid IP (neither "
            "IPv4 nor IPv6)!\n",
            ip);
  }
  if (connect(clientSocketFd, (struct sockaddr *)&serv_addr,
              sizeof(serv_addr)) < 0) {
    fprintf(stderr, "Error: Connect Failed!\n");
    return -3;
  }
  return clientSocketFd;
}


x86 assembly (clang, -O0):
<openClientSocket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e92(%rip),%rax
mov    (%rax),%edi
mov    0x2e71(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1198 <openClientSocket+0x58>
mov    0x2e5c(%rip),%rax
mov    (%rax),%edi
lea    0xe7b(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1210 <openClientSocket+0xd0>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
lea    -0x20(%rbp),%rdx
call   1030 <resolve@plt>
cmp    $0x0,%eax
jne    11cc <openClientSocket+0x8c>
mov    0x2e20(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe5c(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x18(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   1050 <connect@plt>
cmp    $0x0,%rax
jge    120a <openClientSocket+0xca>
mov    0x2dea(%rip),%rax
mov    (%rax),%edi
lea    0xe7c(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0xfffffffd,-0x4(%rbp)
jmp    1210 <openClientSocket+0xd0>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<openClientSocket>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebp
mov    %rdi,%r14
mov    0x2e94(%rip),%rax
mov    (%rax),%edi
mov    0x2e73(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
test   %eax,%eax
js     11cd <openClientSocket+0x8d>
mov    %eax,%ebx
lea    0x8(%rsp),%rdx
mov    %r14,%rdi
mov    %ebp,%esi
call   1030 <resolve@plt>
test   %eax,%eax
jne    1199 <openClientSocket+0x59>
mov    0x2e52(%rip),%rax
mov    (%rax),%edi
lea    0xe92(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   1050 <connect@plt>
test   %rax,%rax
jns    11e9 <openClientSocket+0xa9>
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
lea    0xeb4(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0xfffffffd,%ebx
jmp    11e9 <openClientSocket+0xa9>
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
lea    0xe23(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str = [33 x i8] c"Error: Could not create socket!\0A\00"
@.str.1 = [82 x i8] c"Error: '%s' could not be resolved and is not a valid IP (neither IPv4 nor IPv6)!\0A\00"
@.str.2 = [24 x i8] c"Error: Connect Failed!\0A\00"
define i32 @openClientSocket(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
store i8* %0, i8** %4
store i32 %1, i32* %5
%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* %6
%11 = load i32, i32* %6
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %35
16: ; preds = %2
%17 = load i8*, i8** %4
%18 = load i32, i32* %5
%19 = call i32 @resolve(i8* %17, i32 %18, %struct.sockaddr_in* %7)
%20 = icmp ne i32 %19, 0
br i1 %20, label %25, label %21
21: ; preds = %16
%22 = load i32, i32* @stderr
%23 = load i8*, i8** %4
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([82 x i8], [82 x i8]* @.str.1, i64 0, i64 0), i8* %23)
br label %25
25: ; preds = %21, %16
%26 = load i32, i32* %6
%27 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%28 = call i64 @connect(i32 %26, %struct.sockaddr* %27, i32 4)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %25
%31 = load i32, i32* @stderr
%32 = call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
store i32 -3, i32* %3
br label %35
33: ; preds = %25
%34 = load i32, i32* %6
store i32 %34, i32* %3
br label %35
35: ; preds = %33, %30, %13
%36 = load i32, i32* %3
ret i32 %36
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str = [33 x i8] c"Error: Could not create socket!\0A\00"
@.str.1 = [82 x i8] c"Error: '%s' could not be resolved and is not a valid IP (neither IPv4 nor IPv6)!\0A\00"
@.str.2 = [24 x i8] c"Error: Connect Failed!\0A\00"
define i32 @openClientSocket(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = load i32, i32* @AF_INET
%5 = load i32, i32* @SOCK_STREAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = tail call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
br label %27
11: ; preds = %2
%12 = bitcast %struct.sockaddr_in* %3 to i8*
%13 = call i32 @resolve(i8* %0, i32 %1, %struct.sockaddr_in* %3)
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %11
%16 = load i32, i32* @stderr
%17 = call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([82 x i8], [82 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %18
18: ; preds = %15, %11
%19 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%20 = call i64 @connect(i32 %6, %struct.sockaddr* %19, i32 4)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %18
%23 = load i32, i32* @stderr
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
br label %25
25: ; preds = %18, %22
%26 = phi i32 [ -3, %22 ], [ %6, %18 ]
br label %27
27: ; preds = %25, %8
%28 = phi i32 [ -1, %8 ], [ %26, %25 ]
ret i32 %28
}