#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__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
  TYPE_1__ sin_addr;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  int dummy;
};
typedef int /*<<< orphan*/ srv_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
struct hostent *gethostbyname(char const *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char const *);
int /*<<< orphan*/ printf(char *, char const *, int);
int /*<<< orphan*/ puts(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int init_sender(const char *ip, int port) {
  int fd = socket(AF_INET, SOCK_STREAM, 0);
  if (fd < 0) {
    puts("Error Creating Socket.");
    return -1;
  }
  puts("socket created...");
  struct sockaddr_in srv_addr;
  struct hostent *server = gethostbyname(ip);
  if (server == NULL) {
    puts("Error, Invalid host.");
    return -1;
  }
  puts("host extracted");
  srv_addr.sin_addr.s_addr = inet_addr(ip);
  srv_addr.sin_family = AF_INET;
  srv_addr.sin_port = htons(port);
  if (connect(fd, (struct sockaddr *)&srv_addr, sizeof(srv_addr)) < 0) {
    printf("Error connecting to %s:%d\n", ip, port);
    exit(1);
  }
  printf("successfully connected to %s:%d\n", ip, port);
  return fd;
}


x86 assembly (clang, -O0):
<init_sender>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e52(%rip),%rax
mov    (%rax),%edi
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11cd <init_sender+0x4d>
lea    0xe44(%rip),%rdi
call   1030 <puts@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1289 <init_sender+0x109>
lea    0xe43(%rip),%rdi
call   1030 <puts@plt>
mov    -0x10(%rbp),%rdi
call   1070 <gethostbyname@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1209 <init_sender+0x89>
lea    0xe31(%rip),%rdi
call   1030 <puts@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1289 <init_sender+0x109>
lea    0xe2e(%rip),%rdi
call   1030 <puts@plt>
mov    -0x10(%rbp),%rdi
call   1060 <inet_addr@plt>
mov    %eax,-0x20(%rbp)
mov    0x2dc0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1090 <connect@plt>
cmp    $0x0,%rax
jge    1270 <init_sender+0xf0>
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
lea    0xdec(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
lea    0xdea(%rip),%rdi
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):
<init_sender>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r14d
mov    %rdi,%rbx
mov    0x2e51(%rip),%r15
mov    (%r15),%edi
mov    0x2e37(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
test   %eax,%eax
js     121e <init_sender+0x9e>
mov    %eax,%ebp
lea    0xe60(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1070 <gethostbyname@plt>
test   %rax,%rax
je     1227 <init_sender+0xa7>
lea    0xe6e(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1060 <inet_addr@plt>
mov    %eax,0x10(%rsp)
mov    (%r15),%eax
mov    %eax,0xc(%rsp)
mov    %r14d,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   1090 <connect@plt>
test   %rax,%rax
js     1245 <init_sender+0xc5>
lea    0xe57(%rip),%rdi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1050 <printf@plt>
jmp    1238 <init_sender+0xb8>
lea    0xddb(%rip),%rdi
jmp    122e <init_sender+0xae>
lea    0xdfb(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe01(%rip),%rdi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1050 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [23 x i8] c"Error Creating Socket.\00"
@.str.1 = [18 x i8] c"socket created...\00"
@.str.2 = [21 x i8] c"Error, Invalid host.\00"
@.str.3 = [15 x i8] c"host extracted\00"
@.str.4 = [27 x i8] c"Error connecting to %s:%d\0A\00"
@.str.5 = [33 x i8] c"successfully connected to %s:%d\0A\00"
define i32 @init_sender(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
%8 = alloca %struct.hostent*
store i8* %0, i8** %4
store i32 %1, i32* %5
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_STREAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 @puts(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %49
16: ; preds = %2
%17 = call i32 @puts(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%18 = load i8*, i8** %4
%19 = call %struct.hostent* @gethostbyname(i8* %18)
store %struct.hostent* %19, %struct.hostent** %8
%20 = load %struct.hostent*, %struct.hostent** %8
%21 = icmp eq %struct.hostent* %20, null
br i1 %21, label %22, label %24
22: ; preds = %16
%23 = call i32 @puts(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %49
24: ; preds = %16
%25 = call i32 @puts(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%26 = load i8*, i8** %4
%27 = call i32 @inet_addr(i8* %26)
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
%29 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %28, i32 0, i32 0
store i32 %27, i32* %29
%30 = load i32, i32* @AF_INET
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
store i32 %30, i32* %31
%32 = load i32, i32* %5
%33 = call i32 @htons(i32 %32)
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
store i32 %33, i32* %34
%35 = load i32, i32* %6
%36 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%37 = call i64 @connect(i32 %35, %struct.sockaddr* %36, i32 12)
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %44
39: ; preds = %24
%40 = load i8*, i8** %4
%41 = load i32, i32* %5
%42 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0), i8* %40, i32 %41)
%43 = call i32 @exit(i32 1)
unreachable
44: ; preds = %24
%45 = load i8*, i8** %4
%46 = load i32, i32* %5
%47 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), i8* %45, i32 %46)
%48 = load i32, i32* %6
store i32 %48, i32* %3
br label %49
49: ; preds = %44, %22, %14
%50 = load i32, i32* %3
ret i32 %50
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [23 x i8] c"Error Creating Socket.\00"
@.str.1 = [18 x i8] c"socket created...\00"
@.str.2 = [21 x i8] c"Error, Invalid host.\00"
@.str.3 = [15 x i8] c"host extracted\00"
@.str.4 = [27 x i8] c"Error connecting to %s:%d\0A\00"
@.str.5 = [33 x i8] c"successfully connected to %s:%d\0A\00"
define i32 @init_sender(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 %10
8: ; preds = %2
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %35
10: ; preds = %2
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%12 = bitcast %struct.sockaddr_in* %3 to i8*
%13 = tail call %struct.hostent* @gethostbyname(i8* %0)
%14 = icmp eq %struct.hostent* %13, null
br i1 %14, label %15, label %17
15: ; preds = %10
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %33
17: ; preds = %10
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%19 = tail call i32 @inet_addr(i8* %0)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2, i32 0
store i32 %19, i32* %20
%21 = load i32, i32* @AF_INET
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %21, i32* %22
%23 = tail call i32 @htons(i32 %1)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %23, i32* %24
%25 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%26 = call i64 @connect(i32 %6, %struct.sockaddr* %25, i32 12)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %17
%29 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0), i8* %0, i32 %1)
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %17
%32 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), i8* %0, i32 %1)
br label %33
33: ; preds = %31, %15
%34 = phi i32 [ -1, %15 ], [ %6, %31 ]
br label %35
35: ; preds = %33, %8
%36 = phi i32 [ -1, %8 ], [ %34, %33 ]
ret i32 %36
}