#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 hostent {
  int /*<<< orphan*/ h_addr;
};
typedef int /*<<< orphan*/ server_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr const *, int);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ socket_error(char *);
int check_connect() {
  int sock = -1;
  int ret = -1;
  struct sockaddr_in server_addr;
  struct hostent *hostp = NULL;
  int namelen = sizeof(server_addr);
  hostp = gethostbyname("www.baidu.com");
  if (!hostp) {
    socket_error("gethostbyname:");
    goto err;
  }
  sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  if (-1 == sock) {
    socket_error("socket:");
    goto err;
  }
  namelen = sizeof(server_addr);
  memset(&server_addr, 0, namelen);
  server_addr.sin_family = AF_INET;
  server_addr.sin_port = htons(80);
  memcpy(&server_addr.sin_addr, hostp->h_addr, sizeof(server_addr.sin_addr));
  if (connect(sock, (const struct sockaddr *)&server_addr, namelen) != 0) {
    goto err;
  }
  close(sock);
  sock = -1;
  return 0;
err:
  if (sock != -1) {
    close(sock);
    sock = -1;
  }
  return -1;
}


x86 assembly (clang, -O0):
<check_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0xffffffff,-0x8(%rbp)
movl   $0xffffffff,-0xc(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0xc,-0x24(%rbp)
lea    0xe54(%rip),%rdi
call   1070 <gethostbyname@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11d1 <check_connect+0x51>
lea    0xe47(%rip),%rdi
call   1030 <socket_error@plt>
jmp    128e <check_connect+0x10e>
mov    0x2df0(%rip),%rax
mov    (%rax),%edi
mov    0x2def(%rip),%rax
mov    (%rax),%esi
mov    0x2df6(%rip),%rax
mov    (%rax),%edx
call   10a0 <socket@plt>
mov    %eax,-0x8(%rbp)
mov    $0xffffffff,%eax
cmp    -0x8(%rbp),%eax
jne    1213 <check_connect+0x93>
lea    0xe14(%rip),%rdi
call   1030 <socket_error@plt>
jmp    128e <check_connect+0x10e>
movl   $0xc,-0x24(%rbp)
mov    -0x24(%rbp),%edx
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
call   1050 <memset@plt>
mov    0x2db9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    $0x50,%edi
call   1040 <htons@plt>
mov    %eax,-0x14(%rbp)
lea    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%esi
mov    $0x4,%edx
call   1080 <memcpy@plt>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1090 <connect@plt>
cmp    $0x0,%rax
je     1273 <check_connect+0xf3>
jmp    128e <check_connect+0x10e>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x8(%rbp)
movl   $0x0,-0x4(%rbp)
jmp    12ae <check_connect+0x12e>
cmpl   $0xffffffff,-0x8(%rbp)
je     12a7 <check_connect+0x127>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x8(%rbp)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_connect>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
lea    0xe71(%rip),%rdi
call   1070 <gethostbyname@plt>
test   %rax,%rax
je     1223 <check_connect+0xa3>
mov    %rax,%rbx
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
mov    0x2e20(%rip),%rax
mov    (%rax),%esi
mov    0x2e27(%rip),%rax
mov    (%rax),%edx
call   10a0 <socket@plt>
cmp    $0xffffffff,%eax
je     122c <check_connect+0xac>
mov    %eax,%ebp
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2e08(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    $0x50,%edi
call   1040 <htons@plt>
mov    %eax,0x4(%rsp)
mov    (%rbx),%esi
mov    %r14,%rdi
mov    $0x4,%edx
call   1080 <memcpy@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1090 <connect@plt>
mov    %rax,%rbx
mov    %ebp,%edi
call   1060 <close@plt>
neg    %rbx
sbb    %eax,%eax
jmp    123d <check_connect+0xbd>
lea    0xde4(%rip),%rdi
jmp    1233 <check_connect+0xb3>
lea    0xdea(%rip),%rdi
call   1030 <socket_error@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [14 x i8] c"www.baidu.com\00"
@.str.1 = [15 x i8] c"gethostbyname:\00"
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str.2 = [8 x i8] c"socket:\00"
@AF_INET = global i32 0
define i32 @check_connect() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca %struct.hostent*
%6 = alloca i32
store i32 -1, i32* %2
store i32 -1, i32* %3
store %struct.hostent* null, %struct.hostent** %5
store i32 12, i32* %6
%7 = call %struct.hostent* @gethostbyname(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store %struct.hostent* %7, %struct.hostent** %5
%8 = load %struct.hostent*, %struct.hostent** %5
%9 = icmp ne %struct.hostent* %8, null
br i1 %9, label %12, label %10
10: ; preds = %0
%11 = call i32 @socket_error(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %42
12: ; preds = %0
%13 = load i32, i32* @PF_INET
%14 = load i32, i32* @SOCK_STREAM
%15 = load i32, i32* @IPPROTO_TCP
%16 = call i32 @socket(i32 %13, i32 %14, i32 %15)
store i32 %16, i32* %2
%17 = load i32, i32* %2
%18 = icmp eq i32 -1, %17
br i1 %18, label %19, label %21
19: ; preds = %12
%20 = call i32 @socket_error(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %42
21: ; preds = %12
store i32 12, i32* %6
%22 = load i32, i32* %6
%23 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 %22)
%24 = load i32, i32* @AF_INET
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 2
store i32 %24, i32* %25
%26 = call i32 @htons(i32 80)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
store i32 %26, i32* %27
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
%29 = load %struct.hostent*, %struct.hostent** %5
%30 = getelementptr inbounds %struct.hostent, %struct.hostent* %29, i32 0, i32 0
%31 = load i32, i32* %30
%32 = call i32 @memcpy(i32* %28, i32 %31, i32 4)
%33 = load i32, i32* %2
%34 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%35 = load i32, i32* %6
%36 = call i64 @connect(i32 %33, %struct.sockaddr* %34, i32 %35)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %21
br label %42
39: ; preds = %21
%40 = load i32, i32* %2
%41 = call i32 @close(i32 %40)
store i32 -1, i32* %2
store i32 0, i32* %1
br label %49
42: ; preds = %38, %19, %10
%43 = load i32, i32* %2
%44 = icmp ne i32 %43, -1
br i1 %44, label %45, label %48
45: ; preds = %42
%46 = load i32, i32* %2
%47 = call i32 @close(i32 %46)
store i32 -1, i32* %2
br label %48
48: ; preds = %45, %42
store i32 -1, i32* %1
br label %49
49: ; preds = %48, %39
%50 = load i32, i32* %1
ret i32 %50
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [14 x i8] c"www.baidu.com\00"
@.str.1 = [15 x i8] c"gethostbyname:\00"
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str.2 = [8 x i8] c"socket:\00"
@AF_INET = global i32 0
define i32 @check_connect() {
%1 = alloca %struct.sockaddr_in
%2 = bitcast %struct.sockaddr_in* %1 to i8*
%3 = tail call %struct.hostent* @gethostbyname(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq %struct.hostent* %3, null
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = tail call i32 @socket_error(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %30
7: ; preds = %0
%8 = load i32, i32* @PF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = load i32, i32* @IPPROTO_TCP
%11 = tail call i32 @socket(i32 %8, i32 %9, i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %7
%14 = tail call i32 @socket_error(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %30
15: ; preds = %7
%16 = call i32 @memset(%struct.sockaddr_in* %1, i32 0, i32 12)
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %17, i32* %18
%19 = call i32 @htons(i32 80)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 %19, i32* %20
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
%22 = getelementptr inbounds %struct.hostent, %struct.hostent* %3, i64 0, i32 0
%23 = load i32, i32* %22
%24 = call i32 @memcpy(i32* %21, i32 %23, i32 4)
%25 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%26 = call i64 @connect(i32 %11, %struct.sockaddr* %25, i32 12)
%27 = icmp ne i64 %26, 0
%28 = call i32 @close(i32 %11)
%29 = sext i1 %27 to i32
br label %30
30: ; preds = %15, %5, %13
%31 = phi i32 [ -1, %13 ], [ -1, %5 ], [ %29, %15 ]
ret i32 %31
}