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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(void) {
  int sockfd;
  struct sockaddr_in addr;
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  memset(&addr, 0, sizeof(addr));
  addr.sin_family = AF_INET;
  {
    struct hostent *h = gethostbyname("www.google.com");
    if (h == NULL)
      return 1;
    addr.sin_addr = *((struct in_addr *)h->h_addr);
  }
  addr.sin_port = htons(80);
  if (connect(sockfd, (struct sockaddr *)&addr, sizeof(addr)) == -1)
    return 2;
  close(sockfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e72(%rip),%rax
mov    (%rax),%edi
mov    0x2e59(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x8(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2e46(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
lea    0xe52(%rip),%rdi
call   1060 <gethostbyname@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11ce <main+0x6e>
movl   $0x1,-0x4(%rbp)
jmp    121c <main+0xbc>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    $0x50,%edi
call   1030 <htons@plt>
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1070 <connect@plt>
cmp    $0xffffffff,%eax
jne    120d <main+0xad>
movl   $0x2,-0x4(%rbp)
jmp    121c <main+0xbc>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    0x2e7b(%rip),%rbx
mov    (%rbx),%edi
mov    0x2e62(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,%ebp
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    (%rbx),%eax
mov    %eax,0x10(%rsp)
lea    0xe61(%rip),%rdi
call   1060 <gethostbyname@plt>
test   %rax,%rax
je     11e1 <main+0x81>
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    $0x50,%edi
call   1030 <htons@plt>
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   1070 <connect@plt>
cmp    $0xffffffff,%eax
je     11e8 <main+0x88>
mov    %ebp,%edi
call   1050 <close@plt>
xor    %eax,%eax
jmp    11ed <main+0x8d>
mov    $0x1,%eax
jmp    11ed <main+0x8d>
mov    $0x2,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

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

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