#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 in_addr {
  int dummy;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_zero;
  struct in_addr sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  scalar_t__ h_addr;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bzero(int /*<<< orphan*/ *, int);
int connect(int, struct sockaddr *, int);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int tcpConnect(void) {
  int error, sockfd;
  struct hostent *host;
  struct sockaddr_in server;
  host = gethostbyname("www.googleapis.com");
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd == -1) {
    perror("Socket");
    sockfd = 0;
  } else {
    server.sin_family = AF_INET;
    server.sin_port = htons(443);
    server.sin_addr = *((struct in_addr *)host->h_addr);
    bzero(&(server.sin_zero), 8);
    error =
        connect(sockfd, (struct sockaddr *)&server, sizeof(struct sockaddr));
    if (error == -1) {
      perror("Connect");
      sockfd = 0;
    }
  }
  return sockfd;
}


x86 assembly (clang, -O0):
<tcpConnect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xe91(%rip),%rdi
call   1040 <gethostbyname@plt>
mov    %rax,-0x10(%rbp)
mov    0x2e69(%rip),%rax
mov    (%rax),%edi
mov    0x2e50(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11b6 <tcpConnect+0x56>
lea    0xe6e(%rip),%rdi
call   1050 <perror@plt>
movl   $0x0,-0x8(%rbp)
jmp    121f <tcpConnect+0xbf>
mov    0x2e2b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    $0x1bb,%edi
call   1030 <htons@plt>
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
lea    -0x20(%rbp),%rdi
mov    $0x8,%esi
call   1060 <bzero@plt>
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   1070 <connect@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
jne    121a <tcpConnect+0xba>
lea    0xe0c(%rip),%rdi
call   1050 <perror@plt>
movl   $0x0,-0x8(%rbp)
jmp    121f <tcpConnect+0xbf>
mov    -0x8(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcpConnect>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
lea    0xe8f(%rip),%rdi
call   1040 <gethostbyname@plt>
mov    %rax,%r14
mov    0x2e68(%rip),%r15
mov    (%r15),%edi
mov    0x2e4e(%rip),%rax
mov    (%rax),%esi
xor    %ebp,%ebp
xor    %edx,%edx
call   1080 <socket@plt>
cmp    $0xffffffff,%eax
je     11ef <tcpConnect+0x8f>
mov    %eax,%ebx
mov    (%r15),%eax
mov    %eax,0x14(%rsp)
mov    $0x1bb,%edi
call   1030 <htons@plt>
mov    %eax,0x10(%rsp)
mov    (%r14),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x8,%esi
call   1060 <bzero@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x4,%edx
call   1070 <connect@plt>
mov    %ebx,%ebp
cmp    $0xffffffff,%eax
je     11fd <tcpConnect+0x9d>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe1d(%rip),%rdi
call   1050 <perror@plt>
jmp    11e2 <tcpConnect+0x82>
lea    0xe16(%rip),%rdi
call   1050 <perror@plt>
xor    %ebp,%ebp
jmp    11e2 <tcpConnect+0x82>

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

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