#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*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_aton(char *, struct in_addr *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ shutdown(int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int connect_socket(int connect_port, char *address) {
  struct sockaddr_in a;
  int s;
  if ((s = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    fprintf(stderr, "socket error\n");
    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)) {
    fprintf(stderr, "bad IP address format\n");
    close(s);
    return -1;
  }
  if (connect(s, (struct sockaddr *)&a, sizeof(a)) == -1) {
    fprintf(stderr, "connect()\n");
    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    0x2e29(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
jne    11dd <connect_socket+0x5d>
mov    0x2e15(%rip),%rax
mov    (%rax),%edi
lea    0xe3c(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    129d <connect_socket+0x11d>
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    0x2de1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1080 <inet_aton@plt>
cmp    $0x0,%eax
jne    1243 <connect_socket+0xc3>
mov    0x2daf(%rip),%rax
mov    (%rax),%edi
lea    0xde4(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    129d <connect_socket+0x11d>
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1090 <connect@plt>
cmp    $0xffffffff,%eax
jne    1297 <connect_socket+0x117>
mov    0x2d6c(%rip),%rax
mov    (%rax),%edi
lea    0xdb8(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x24(%rbp),%edi
mov    0x2d74(%rip),%rax
mov    (%rax),%esi
call   1030 <shutdown@plt>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    129d <connect_socket+0x11d>
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    0x2e25(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
cmp    $0xffffffff,%eax
je     1222 <connect_socket+0xa2>
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   1080 <inet_aton@plt>
test   %eax,%eax
je     1248 <connect_socket+0xc8>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1090 <connect@plt>
cmp    $0xffffffff,%eax
jne    1269 <connect_socket+0xe9>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
lea    0xe1a(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2dd9(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1030 <shutdown@plt>
jmp    125d <connect_socket+0xdd>
mov    0x2da7(%rip),%rax
mov    (%rax),%edi
lea    0xdce(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    $0xffffffff,%edi
call   1060 <close@plt>
jmp    1269 <connect_socket+0xe9>
mov    0x2d81(%rip),%rax
mov    (%rax),%edi
lea    0xdb6(%rip),%rsi
call   1070 <fprintf@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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
@stderr = global i32 0
@.str = [14 x i8] c"socket error\0A\00"
@.str.1 = [23 x i8] c"bad IP address format\0A\00"
@.str.2 = [11 x i8] c"connect()\0A\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 %13
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%12 = tail call i32 @close(i32 -1)
br label %36
13: ; preds = %2
%14 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%15 = call i32 @htons(i32 %0)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %15, i32* %16
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %17, i32* %18
%19 = bitcast %struct.sockaddr_in* %3 to %struct.in_addr*
%20 = call i32 @inet_aton(i8* %1, %struct.in_addr* %19)
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %13
%23 = load i32, i32* @stderr
%24 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
%25 = call i32 @close(i32 %7)
br label %36
26: ; preds = %13
%27 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%28 = call i32 @connect(i32 %7, %struct.sockaddr* %27, i32 12)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %36
30: ; preds = %26
%31 = load i32, i32* @stderr
%32 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%33 = load i32, i32* @SHUT_RDWR
%34 = call i32 @shutdown(i32 %7, i32 %33)
%35 = call i32 @close(i32 %7)
br label %36
36: ; preds = %26, %30, %22, %9
%37 = phi i32 [ -1, %9 ], [ -1, %30 ], [ -1, %22 ], [ %7, %26 ]
ret i32 %37
}