#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_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct protoent {
  int /*<<< orphan*/ p_proto;
};
typedef int /*<<< orphan*/ remote_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
struct protoent *getprotobyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_pton(int /*<<< orphan*/, char const *,
                             int /*<<< orphan*/ *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int connect_sock(const char *serv_ip) {
  int sock_fd;
  int proto;
  struct protoent *my_proto;
  struct sockaddr_in remote_addr;
  my_proto = getprotobyname("tcp");
  if ((sock_fd = socket(PF_INET, SOCK_STREAM, my_proto->p_proto)) < 0) {
    fprintf(stderr, "ERROR: couldn't allocate a socket()\n");
    exit(1);
  }
  bzero(&remote_addr, sizeof(remote_addr));
  remote_addr.sin_family = AF_INET;
  remote_addr.sin_port = htons(2222);
  inet_pton(AF_INET, serv_ip, &remote_addr.sin_addr);
  if (connect(sock_fd, (struct sockaddr *)&remote_addr, sizeof(remote_addr)) <
      0) {
    fprintf(stderr, "ERROR: connect() with remote host failed.\n");
    exit(1);
  }
  return sock_fd;
}


x86 assembly (clang, -O0):
<connect_sock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
lea    0xe6d(%rip),%rdi
call   1060 <getprotobyname@plt>
mov    %rax,-0x18(%rbp)
mov    0x2e25(%rip),%rax
mov    (%rax),%edi
mov    0x2e24(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%rax
mov    (%rax),%edx
call   10a0 <socket@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    11e4 <connect_sock+0x64>
mov    0x2e0c(%rip),%rax
mov    (%rax),%edi
lea    0xe2f(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    -0x28(%rbp),%rdi
mov    $0xc,%esi
call   1070 <bzero@plt>
mov    0x2def(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    $0x8ae,%edi
call   1030 <htons@plt>
mov    %eax,-0x24(%rbp)
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rsi
lea    -0x28(%rbp),%rdx
call   1050 <inet_pton@plt>
mov    -0xc(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1090 <connect@plt>
cmp    $0x0,%rax
jge    125b <connect_sock+0xdb>
mov    0x2d95(%rip),%rax
mov    (%rax),%edi
lea    0xddd(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0xc(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_sock>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
lea    0xe6c(%rip),%rdi
call   1060 <getprotobyname@plt>
mov    0x2e28(%rip),%rcx
mov    (%rcx),%edi
mov    0x2e27(%rip),%rcx
mov    (%rcx),%esi
mov    (%rax),%edx
call   10a0 <socket@plt>
test   %eax,%eax
js     1213 <connect_sock+0x93>
mov    %eax,%ebp
lea    0x8(%rsp),%r15
mov    %r15,%rdi
mov    $0xc,%esi
call   1070 <bzero@plt>
mov    0x2e17(%rip),%rbx
mov    (%rbx),%eax
mov    %eax,0x10(%rsp)
mov    $0x8ae,%edi
call   1030 <htons@plt>
mov    %eax,0xc(%rsp)
mov    (%rbx),%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   1050 <inet_pton@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0xc,%edx
call   1090 <connect@plt>
test   %rax,%rax
js     1225 <connect_sock+0xa5>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dbe(%rip),%rax
mov    (%rax),%edi
lea    0xde1(%rip),%rsi
jmp    1235 <connect_sock+0xb5>
mov    0x2dac(%rip),%rax
mov    (%rax),%edi
lea    0xdf4(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.protoent = type { i32 }
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [4 x i8] c"tcp\00"
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str.1 = [37 x i8] c"ERROR: couldn't allocate a socket()\0A\00"
@AF_INET = global i32 0
@.str.2 = [43 x i8] c"ERROR: connect() with remote host failed.\0A\00"
define i32 @connect_sock(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.protoent*
%6 = alloca %struct.sockaddr_in
store i8* %0, i8** %2
%7 = call %struct.protoent* @getprotobyname(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store %struct.protoent* %7, %struct.protoent** %5
%8 = load i32, i32* @PF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = load %struct.protoent*, %struct.protoent** %5
%11 = getelementptr inbounds %struct.protoent, %struct.protoent* %10, i32 0, i32 0
%12 = load i32, i32* %11
%13 = call i32 @socket(i32 %8, i32 %9, i32 %12)
store i32 %13, i32* %3
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %1
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %1
%20 = call i32 @bzero(%struct.sockaddr_in* %6, i32 12)
%21 = load i32, i32* @AF_INET
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %21, i32* %22
%23 = call i32 @htons(i32 2222)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i32 %23, i32* %24
%25 = load i32, i32* @AF_INET
%26 = load i8*, i8** %2
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%28 = call i32 @inet_pton(i32 %25, i8* %26, i32* %27)
%29 = load i32, i32* %3
%30 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%31 = call i64 @connect(i32 %29, %struct.sockaddr* %30, i32 12)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %37
33: ; preds = %19
%34 = load i32, i32* @stderr
%35 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0))
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %19
%38 = load i32, i32* %3
ret i32 %38
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.protoent = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [4 x i8] c"tcp\00"
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str.1 = [37 x i8] c"ERROR: couldn't allocate a socket()\0A\00"
@AF_INET = global i32 0
@.str.2 = [43 x i8] c"ERROR: connect() with remote host failed.\0A\00"
define i32 @connect_sock(i8* %0) {
%2 = alloca %struct.sockaddr_in
%3 = bitcast %struct.sockaddr_in* %2 to i8*
%4 = tail call %struct.protoent* @getprotobyname(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%5 = load i32, i32* @PF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = getelementptr inbounds %struct.protoent, %struct.protoent* %4, i64 0, i32 0
%8 = load i32, i32* %7
%9 = tail call i32 @socket(i32 %5, i32 %6, i32 %8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %1
%12 = load i32, i32* @stderr
%13 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %1
%16 = call i32 @bzero(%struct.sockaddr_in* %2, i32 12)
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %17, i32* %18
%19 = tail call i32 @htons(i32 2222)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i32 %19, i32* %20
%21 = load i32, i32* @AF_INET
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
%23 = call i32 @inet_pton(i32 %21, i8* %0, i32* %22)
%24 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%25 = call i64 @connect(i32 %9, %struct.sockaddr* %24, i32 12)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %15
%28 = load i32, i32* @stderr
%29 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0))
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %15
ret i32 %9
}