#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_port;
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ sad;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int);
int inet_pton(int /*<<< orphan*/, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int c_connection(char *ip) {
  int sockFD;
  int port;
  struct sockaddr_in sad;
  int err = 0;
  sockFD = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  printf("[*]Socket created\n");
  memset(&sad, 0, sizeof(sad));
  sad.sin_family = AF_INET;
  err = inet_pton(AF_INET, ip, &sad.sin_addr);
  if (err == 0) {
    printf("[*]Error: Invalid ip\n");
    return -1;
  } else if (err == -1) {
    printf("[*]Error: Wrong address family\n");
    return -1;
  }
  printf("[*] address stored\n");
  sad.sin_port = htons(4444);
  connect(sockFD, (struct sockaddr *)&sad, sizeof(sad));
  if (sockFD != 0)
    return sockFD;
  else {
    printf("[*]Error connection failed\n");
    return -1;
  }
}


x86 assembly (clang, -O0):
<c_connection>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    0x2e4e(%rip),%rax
mov    (%rax),%edi
mov    0x2e4d(%rip),%rax
mov    (%rax),%esi
mov    0x2e54(%rip),%rax
mov    (%rax),%edx
call   1080 <socket@plt>
mov    %eax,-0x14(%rbp)
lea    0xe63(%rip),%rdi
call   1040 <printf@plt>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2e2f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
lea    -0x28(%rbp),%rdx
add    $0x4,%rdx
call   1060 <inet_pton@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jne    11fd <c_connection+0x9d>
lea    0xe27(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1276 <c_connection+0x116>
cmpl   $0xffffffff,-0x2c(%rbp)
jne    121f <c_connection+0xbf>
lea    0xe1b(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1276 <c_connection+0x116>
jmp    1224 <c_connection+0xc4>
lea    0xe1e(%rip),%rdi
call   1040 <printf@plt>
mov    $0x115c,%edi
call   1030 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1070 <connect@plt>
cmpl   $0x0,-0x14(%rbp)
je     1263 <c_connection+0x103>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1276 <c_connection+0x116>
lea    0xdf3(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<c_connection>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
mov    0x2e57(%rip),%rax
mov    (%rax),%edi
mov    0x2e56(%rip),%rax
mov    (%rax),%esi
mov    0x2e5d(%rip),%rax
mov    (%rax),%edx
call   1080 <socket@plt>
mov    %eax,%ebx
lea    0xe6d(%rip),%rdi
call   1030 <puts@plt>
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
mov    %edi,0x10(%rsp)
lea    0xc(%rsp),%rdx
mov    %r14,%rsi
call   1060 <inet_pton@plt>
test   %eax,%eax
je     11d5 <c_connection+0x75>
cmp    $0xffffffff,%eax
jne    11de <c_connection+0x7e>
lea    0xe6d(%rip),%rdi
jmp    1214 <c_connection+0xb4>
lea    0xe83(%rip),%rdi
jmp    1214 <c_connection+0xb4>
lea    0xe2d(%rip),%rdi
call   1030 <puts@plt>
mov    $0x115c,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1070 <connect@plt>
test   %ebx,%ebx
jne    121e <c_connection+0xbe>
lea    0xe11(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str = [19 x i8] c"[*]Socket created\0A\00"
@AF_INET = global i32 0
@.str.1 = [22 x i8] c"[*]Error: Invalid ip\0A\00"
@.str.2 = [32 x i8] c"[*]Error: Wrong address family\0A\00"
@.str.3 = [20 x i8] c"[*] address stored\0A\00"
@.str.4 = [28 x i8] c"[*]Error connection failed\0A\00"
define i32 @c_connection(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
store i8* %0, i8** %3
store i32 0, i32* %7
%8 = load i32, i32* @PF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = load i32, i32* @IPPROTO_TCP
%11 = call i32 @socket(i32 %8, i32 %9, i32 %10)
store i32 %11, i32* %4
%12 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @memset(%struct.sockaddr_in* %6, i32 0, i32 12)
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %14, i32* %15
%16 = load i32, i32* @AF_INET
%17 = load i8*, i8** %3
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
%19 = call i32 @inet_pton(i32 %16, i8* %17, i32* %18)
store i32 %19, i32* %7
%20 = load i32, i32* %7
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %1
%23 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %43
24: ; preds = %1
%25 = load i32, i32* %7
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %29
27: ; preds = %24
%28 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %2
br label %43
29: ; preds = %24
br label %30
30: ; preds = %29
%31 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
%32 = call i32 @htons(i32 4444)
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 %32, i32* %33
%34 = load i32, i32* %4
%35 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%36 = call i32 @connect(i32 %34, %struct.sockaddr* %35, i32 12)
%37 = load i32, i32* %4
%38 = icmp ne i32 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %30
%40 = load i32, i32* %4
store i32 %40, i32* %2
br label %43
41: ; preds = %30
%42 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %2
br label %43
43: ; preds = %41, %39, %27, %22
%44 = load i32, i32* %2
ret i32 %44
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@AF_INET = global i32 0
@str = [18 x i8] c"[*]Socket created\00"
@str.5 = [19 x i8] c"[*] address stored\00"
@str.6 = [27 x i8] c"[*]Error connection failed\00"
@str.7 = [31 x i8] c"[*]Error: Wrong address family\00"
@str.8 = [21 x i8] c"[*]Error: Invalid ip\00"
define i32 @c_connection(i8* %0) {
%2 = alloca %struct.sockaddr_in
%3 = bitcast %struct.sockaddr_in* %2 to i8*
%4 = load i32, i32* @PF_INET
%5 = load i32, i32* @SOCK_STREAM
%6 = load i32, i32* @IPPROTO_TCP
%7 = tail call i32 @socket(i32 %4, i32 %5, i32 %6)
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
%9 = call i32 @memset(%struct.sockaddr_in* %2, i32 0, i32 12)
%10 = load i32, i32* @AF_INET
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %10, i32* %11
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
%13 = call i32 @inet_pton(i32 %10, i8* %0, i32* %12)
switch i32 %13, label %15 [
i32 0, label %22
i32 -1, label %14
]
14: ; preds = %1
br label %22
15: ; preds = %1
%16 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.5, i64 0, i64 0))
%17 = call i32 @htons(i32 4444)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i32 %17, i32* %18
%19 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%20 = call i32 @connect(i32 %7, %struct.sockaddr* %19, i32 12)
%21 = icmp eq i32 %7, 0
br i1 %21, label %22, label %25
22: ; preds = %15, %1, %14
%23 = phi i8* [ getelementptr inbounds ([31 x i8], [31 x i8]* @str.7, i64 0, i64 0), %14 ], [ getelementptr inbounds ([21 x i8], [21 x i8]* @str.8, i64 0, i64 0), %1 ], [ getelementptr inbounds ([27 x i8], [27 x i8]* @str.6, i64 0, i64 0), %15 ]
%24 = call i32 @puts(i8* dereferenceable(1) %23)
br label %25
25: ; preds = %22, %15
%26 = phi i32 [ %7, %15 ], [ -1, %22 ]
ret i32 %26
}