#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 {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
  int /*<<< orphan*/ sin_port;
};
struct sockaddr {
  int dummy;
};
struct in_addr {
  int dummy;
};
typedef int /*<<< orphan*/ a;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SHUT_RDWR;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_aton(char *, struct in_addr *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ shutdown(int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int connect_socket(int connect_port, char *address) {
  struct sockaddr_in a;
  int s;
  if ((s = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    perror("socket");
    close(s);
    return -1;
  }
  memset(&a, 0, sizeof(a));
  a.sin_port = htons(connect_port);
  a.sin_family = AF_INET;
  if (!inet_aton(address, (struct in_addr *)&a.sin_addr.s_addr)) {
    perror("bad IP address format");
    close(s);
    return -1;
  }
  if (connect(s, (struct sockaddr *)&a, sizeof(a)) == -1) {
    perror("connect()");
    shutdown(s, SHUT_RDWR);
    close(s);
    return -1;
  }
  return s;
}


x86 assembly (clang, -O0):
<connect_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
jne    11d4 <connect_socket+0x54>
lea    0xe45(%rip),%rdi
call   1080 <perror@plt>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1282 <connect_socket+0x102>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    -0x8(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x18(%rbp)
mov    0x2dea(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1070 <inet_aton@plt>
cmp    $0x0,%eax
jne    1231 <connect_socket+0xb1>
lea    0xdef(%rip),%rdi
call   1080 <perror@plt>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1282 <connect_socket+0x102>
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1090 <connect@plt>
cmp    $0xffffffff,%eax
jne    127c <connect_socket+0xfc>
lea    0xdcb(%rip),%rdi
call   1080 <perror@plt>
mov    -0x24(%rbp),%edi
mov    0x2d8f(%rip),%rax
mov    (%rax),%esi
call   1030 <shutdown@plt>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1282 <connect_socket+0x102>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_socket>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
mov    0x2e48(%rip),%r12
mov    (%r12),%edi
mov    0x2e2d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
cmp    $0xffffffff,%eax
je     120a <connect_socket+0x8a>
mov    %eax,%ebx
mov    %rsp,%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    %ebp,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
mov    (%r12),%eax
mov    %eax,0x4(%rsp)
mov    %r14,%rdi
mov    %r15,%rsi
call   1070 <inet_aton@plt>
test   %eax,%eax
je     1227 <connect_socket+0xa7>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1090 <connect@plt>
cmp    $0xffffffff,%eax
je     1235 <connect_socket+0xb5>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdef(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%ebx
mov    $0xffffffff,%edi
call   1060 <close@plt>
jmp    11fb <connect_socket+0x7b>
lea    0xdd9(%rip),%rdi
call   1080 <perror@plt>
jmp    1251 <connect_socket+0xd1>
lea    0xde1(%rip),%rdi
call   1080 <perror@plt>
mov    0x2da8(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1030 <shutdown@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    $0xffffffff,%ebx
jmp    11fb <connect_socket+0x7b>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.in_addr = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [22 x i8] c"bad IP address format\00"
@.str.2 = [10 x i8] c"connect()\00"
@SHUT_RDWR = global i32 0
define i32 @connect_socket(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %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* %7
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%14 = load i32, i32* %7
%15 = call i32 @close(i32 %14)
store i32 -1, i32* %3
br label %47
16: ; preds = %2
%17 = call i32 @memset(%struct.sockaddr_in* %6, i32 0, i32 12)
%18 = load i32, i32* %4
%19 = call i32 @htons(i32 %18)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %19, i32* %20
%21 = load i32, i32* @AF_INET
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i32 %21, i32* %22
%23 = load i8*, i8** %5
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%25 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %24, i32 0, i32 0
%26 = bitcast i32* %25 to %struct.in_addr*
%27 = call i32 @inet_aton(i8* %23, %struct.in_addr* %26)
%28 = icmp ne i32 %27, 0
br i1 %28, label %33, label %29
29: ; preds = %16
%30 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%31 = load i32, i32* %7
%32 = call i32 @close(i32 %31)
store i32 -1, i32* %3
br label %47
33: ; preds = %16
%34 = load i32, i32* %7
%35 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%36 = call i32 @connect(i32 %34, %struct.sockaddr* %35, i32 12)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %45
38: ; preds = %33
%39 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%40 = load i32, i32* %7
%41 = load i32, i32* @SHUT_RDWR
%42 = call i32 @shutdown(i32 %40, i32 %41)
%43 = load i32, i32* %7
%44 = call i32 @close(i32 %43)
store i32 -1, i32* %3
br label %47
45: ; preds = %33
%46 = load i32, i32* %7
store i32 %46, i32* %3
br label %47
47: ; preds = %45, %38, %29, %12
%48 = load i32, i32* %3
ret i32 %48
}

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