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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_aton(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int connect_ip(char *ip, int port) {
  struct sockaddr_in addr;
  int fd;
  bzero(&addr, sizeof(addr));
  addr.sin_family = AF_INET;
  addr.sin_port = htons(port);
  inet_aton(ip, &(addr.sin_addr));
  if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    perror("Client: socket");
    return -1;
  }
  if (connect(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr)) < 0) {
    perror("Client: connect");
    close(fd);
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<connect_ip>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
lea    -0x20(%rbp),%rdi
mov    $0xc,%esi
call   1070 <bzero@plt>
mov    0x2e54(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1050 <inet_aton@plt>
mov    0x2e30(%rip),%rax
mov    (%rax),%edi
mov    0x2e17(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
jne    11ee <connect_ip+0x7e>
lea    0xe23(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    122f <connect_ip+0xbf>
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   1080 <connect@plt>
cmp    $0x0,%rax
jge    1229 <connect_ip+0xb9>
lea    0xdff(%rip),%rdi
call   1060 <perror@plt>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    122f <connect_ip+0xbf>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_ip>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebx
mov    %rdi,%r14
lea    0x8(%rsp),%r15
mov    %r15,%rdi
mov    $0xc,%esi
call   1070 <bzero@plt>
mov    0x2e4f(%rip),%r12
mov    (%r12),%eax
mov    %eax,0x10(%rsp)
mov    %ebx,%edi
call   1030 <htons@plt>
mov    %eax,0xc(%rsp)
mov    %r14,%rdi
mov    %r15,%rsi
call   1050 <inet_aton@plt>
mov    (%r12),%edi
mov    0x2e16(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
cmp    $0xffffffff,%eax
je     11f6 <connect_ip+0x86>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    $0x4,%edx
call   1080 <connect@plt>
test   %rax,%rax
js     1204 <connect_ip+0x94>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xe03(%rip),%rdi
call   1060 <perror@plt>
jmp    1217 <connect_ip+0xa7>
lea    0xe04(%rip),%rdi
call   1060 <perror@plt>
mov    %ebx,%edi
call   1040 <close@plt>
mov    $0xffffffff,%ebx
jmp    11e8 <connect_ip+0x78>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [15 x i8] c"Client: socket\00"
@.str.1 = [16 x i8] c"Client: connect\00"
define i32 @connect_ip(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = call i32 @bzero(%struct.sockaddr_in* %6, i32 12)
%9 = load i32, i32* @AF_INET
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %9, i32* %10
%11 = load i32, i32* %5
%12 = call i32 @htons(i32 %11)
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i32 %12, i32* %13
%14 = load i8*, i8** %4
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%16 = call i32 @inet_aton(i8* %14, i32* %15)
%17 = load i32, i32* @AF_INET
%18 = load i32, i32* @SOCK_STREAM
%19 = call i32 @socket(i32 %17, i32 %18, i32 0)
store i32 %19, i32* %7
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %2
%22 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %34
23: ; preds = %2
%24 = load i32, i32* %7
%25 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%26 = call i64 @connect(i32 %24, %struct.sockaddr* %25, i32 4)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %32
28: ; preds = %23
%29 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%30 = load i32, i32* %7
%31 = call i32 @close(i32 %30)
store i32 -1, i32* %3
br label %34
32: ; preds = %23
%33 = load i32, i32* %7
store i32 %33, i32* %3
br label %34
34: ; preds = %32, %28, %21
%35 = load i32, i32* %3
ret i32 %35
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [15 x i8] c"Client: socket\00"
@.str.1 = [16 x i8] c"Client: connect\00"
define i32 @connect_ip(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = call i32 @bzero(%struct.sockaddr_in* %3, i32 12)
%6 = load i32, i32* @AF_INET
%7 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %6, i32* %7
%8 = tail call i32 @htons(i32 %1)
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %8, i32* %9
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%11 = call i32 @inet_aton(i8* %0, i32* %10)
%12 = load i32, i32* @AF_INET
%13 = load i32, i32* @SOCK_STREAM
%14 = call i32 @socket(i32 %12, i32 %13, i32 0)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %25
18: ; preds = %2
%19 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%20 = call i64 @connect(i32 %14, %struct.sockaddr* %19, i32 4)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %18
%23 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%24 = call i32 @close(i32 %14)
br label %25
25: ; preds = %18, %22, %16
%26 = phi i32 [ -1, %16 ], [ -1, %22 ], [ %14, %18 ]
ret i32 %26
}