#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 addrinfo {
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_addr;
  int /*<<< orphan*/ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
};

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int getaddrinfo(char *, char *, struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int CanSeeTheInternet(void) {
  struct addrinfo hints, *res, *p;
  int status, sockfd, FoundInternet;
  memset(&hints, 0, sizeof hints);
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  if ((status = getaddrinfo("google.com", "80", &hints, &res)) != 0) {
    return 0;
  }
  sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
  FoundInternet = 1;
  if (connect(sockfd, res->ai_addr, res->ai_addrlen) == -1) {
    FoundInternet = 0;
  }
  close(sockfd);
  freeaddrinfo(res);
  return FoundInternet;
}


x86 assembly (clang, -O0):
<CanSeeTheInternet>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1030 <memset@plt>
mov    0x2e69(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
mov    0x2e4d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
lea    0xe69(%rip),%rdi
lea    0xe6d(%rip),%rsi
lea    -0x18(%rbp),%rdx
lea    -0x20(%rbp),%rcx
call   1060 <getaddrinfo@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
je     11c3 <CanSeeTheInternet+0x63>
movl   $0x0,-0x4(%rbp)
jmp    1223 <CanSeeTheInternet+0xc3>
mov    -0x20(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x20(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%edx
call   1080 <socket@plt>
mov    %eax,-0x30(%rbp)
movl   $0x1,-0x34(%rbp)
mov    -0x30(%rbp),%edi
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
call   1050 <connect@plt>
cmp    $0xffffffff,%eax
jne    120c <CanSeeTheInternet+0xac>
movl   $0x0,-0x34(%rbp)
mov    -0x30(%rbp),%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%rdi
call   1070 <freeaddrinfo@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<CanSeeTheInternet>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
lea    0x8(%rsp),%r14
xor    %ebx,%ebx
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1030 <memset@plt>
mov    0x2e63(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2e46(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
lea    0xe61(%rip),%rdi
lea    0xe65(%rip),%rsi
mov    %rsp,%rcx
mov    %r14,%rdx
call   1060 <getaddrinfo@plt>
test   %eax,%eax
jne    11f1 <CanSeeTheInternet+0x91>
mov    (%rsp),%rax
mov    0x10(%rax),%edi
mov    0x8(%rax),%edx
mov    0xc(%rax),%esi
call   1080 <socket@plt>
mov    %eax,%ebp
mov    (%rsp),%rax
mov    (%rax),%edx
mov    0x4(%rax),%esi
mov    %ebp,%edi
call   1050 <connect@plt>
xor    %ebx,%ebx
cmp    $0xffffffff,%eax
setne  %bl
mov    %ebp,%edi
call   1040 <close@plt>
mov    (%rsp),%rdi
call   1070 <freeaddrinfo@plt>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32, i32 }
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@.str = [11 x i8] c"google.com\00"
@.str.1 = [3 x i8] c"80\00"
define i32 @CanSeeTheInternet() {
%1 = alloca i32
%2 = alloca %struct.addrinfo
%3 = alloca %struct.addrinfo*
%4 = alloca %struct.addrinfo*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = call i32 @memset(%struct.addrinfo* %2, i32 0, i32 20)
%9 = load i32, i32* @AF_UNSPEC
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i32 0, i32 4
store i32 %9, i32* %10
%11 = load i32, i32* @SOCK_STREAM
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i32 0, i32 3
store i32 %11, i32* %12
%13 = call i32 @getaddrinfo(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), %struct.addrinfo* %2, %struct.addrinfo** %3)
store i32 %13, i32* %5
%14 = icmp ne i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %0
store i32 0, i32* %1
br label %43
16: ; preds = %0
%17 = load %struct.addrinfo*, %struct.addrinfo** %3
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %17, i32 0, i32 4
%19 = load i32, i32* %18
%20 = load %struct.addrinfo*, %struct.addrinfo** %3
%21 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %20, i32 0, i32 3
%22 = load i32, i32* %21
%23 = load %struct.addrinfo*, %struct.addrinfo** %3
%24 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i32 0, i32 2
%25 = load i32, i32* %24
%26 = call i32 @socket(i32 %19, i32 %22, i32 %25)
store i32 %26, i32* %6
store i32 1, i32* %7
%27 = load i32, i32* %6
%28 = load %struct.addrinfo*, %struct.addrinfo** %3
%29 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i32 0, i32 1
%30 = load i32, i32* %29
%31 = load %struct.addrinfo*, %struct.addrinfo** %3
%32 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %31, i32 0, i32 0
%33 = load i32, i32* %32
%34 = call i32 @connect(i32 %27, i32 %30, i32 %33)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %37
36: ; preds = %16
store i32 0, i32* %7
br label %37
37: ; preds = %36, %16
%38 = load i32, i32* %6
%39 = call i32 @close(i32 %38)
%40 = load %struct.addrinfo*, %struct.addrinfo** %3
%41 = call i32 @freeaddrinfo(%struct.addrinfo* %40)
%42 = load i32, i32* %7
store i32 %42, i32* %1
br label %43
43: ; preds = %37, %15
%44 = load i32, i32* %1
ret i32 %44
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32 }
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@.str = [11 x i8] c"google.com\00"
@.str.1 = [3 x i8] c"80\00"
define i32 @CanSeeTheInternet() {
%1 = alloca %struct.addrinfo
%2 = alloca %struct.addrinfo*
%3 = bitcast %struct.addrinfo* %1 to i8*
%4 = bitcast %struct.addrinfo** %2 to i8*
%5 = call i32 @memset(%struct.addrinfo* %1, i32 0, i32 20)
%6 = load i32, i32* @AF_UNSPEC
%7 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %1, i64 0, i32 4
store i32 %6, i32* %7
%8 = load i32, i32* @SOCK_STREAM
%9 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %1, i64 0, i32 3
store i32 %8, i32* %9
%10 = call i32 @getaddrinfo(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), %struct.addrinfo* %1, %struct.addrinfo** %2)
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %32
12: ; preds = %0
%13 = load %struct.addrinfo*, %struct.addrinfo** %2
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %13, i64 0, i32 4
%15 = load i32, i32* %14
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %13, i64 0, i32 3
%17 = load i32, i32* %16
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %13, i64 0, i32 2
%19 = load i32, i32* %18
%20 = call i32 @socket(i32 %15, i32 %17, i32 %19)
%21 = load %struct.addrinfo*, %struct.addrinfo** %2
%22 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %21, i64 0, i32 1
%23 = load i32, i32* %22
%24 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %21, i64 0, i32 0
%25 = load i32, i32* %24
%26 = call i32 @connect(i32 %20, i32 %23, i32 %25)
%27 = icmp ne i32 %26, -1
%28 = zext i1 %27 to i32
%29 = call i32 @close(i32 %20)
%30 = load %struct.addrinfo*, %struct.addrinfo** %2
%31 = call i32 @freeaddrinfo(%struct.addrinfo* %30)
br label %32
32: ; preds = %0, %12
%33 = phi i32 [ %28, %12 ], [ 0, %0 ]
ret i32 %33
}