#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 */
typedef int /*<<< orphan*/ val;
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  int /*<<< orphan*/ h_addr;
  int /*<<< orphan*/ h_addrtype;
};
typedef int /*<<< orphan*/ google_addr;

/* Variables and functions */
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SHUT_WR;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int /*<<< orphan*/ TCP_NODELAY;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ shutdown(int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int write(int, char *, int);
int check_connection(void) {
  int sockfd, val;
  char buffer[256];
  struct hostent *google_ent = NULL;
  struct sockaddr_in google_addr;
  sockfd = -1;
  if ((google_ent = gethostbyname("www.google.com")) != NULL) {
    if ((sockfd = socket(google_ent->h_addrtype, SOCK_STREAM, IPPROTO_TCP)) !=
        -1) {
      val = 1;
      if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (char *)&val,
                     sizeof(val)) == 0 &&
          setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, (char *)&val,
                     sizeof(val)) == 0) {
        google_addr.sin_family = google_ent->h_addrtype;
        memcpy(&(google_addr.sin_addr), google_ent->h_addr,
               google_ent->h_length);
        google_addr.sin_port = htons(80);
        if (connect(sockfd, (struct sockaddr *)&google_addr,
                    sizeof(google_addr)) == 0) {
          if (write(sockfd, "GET /index.html HTTP/1.1\r\n\r\n", 29) >= 28) {
            shutdown(sockfd, SHUT_WR);
            if (read(sockfd, buffer, 256) != -1) {
              close(sockfd);
              return 1;
            } else
              printf("read() error\n");
          } else
            printf("write() error\n");
        } else
          printf("connect() error\n");
      } else
        printf("setsockopt() error\n");
    } else
      printf("socket() error\n");
  } else
    printf("cannot resolve IP for mom Google. \n");
  if (sockfd != -1)
    close(sockfd);
  return 0;
}


x86 assembly (clang, -O0):
<check_connection>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
movq   $0x0,-0x118(%rbp)
movl   $0xffffffff,-0x8(%rbp)
lea    0xe2c(%rip),%rdi
call   10a0 <gethostbyname@plt>
mov    %rax,-0x118(%rbp)
cmp    $0x0,%rax
je     138f <check_connection+0x1df>
mov    -0x118(%rbp),%rax
mov    0x8(%rax),%edi
mov    0x2dc5(%rip),%rax
mov    (%rax),%esi
mov    0x2dcc(%rip),%rax
mov    (%rax),%edx
call   10d0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
je     137e <check_connection+0x1ce>
movl   $0x1,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2dc0(%rip),%rax
mov    (%rax),%esi
mov    0x2d87(%rip),%rax
mov    (%rax),%edx
lea    -0xc(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jne    136d <check_connection+0x1bd>
mov    -0x8(%rbp),%edi
mov    0x2d7a(%rip),%rax
mov    (%rax),%esi
mov    0x2d81(%rip),%rax
mov    (%rax),%edx
lea    -0xc(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jne    136d <check_connection+0x1bd>
mov    -0x118(%rbp),%rax
mov    0x8(%rax),%eax
mov    %eax,-0x120(%rbp)
lea    -0x128(%rbp),%rdi
add    $0x4,%rdi
mov    -0x118(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x118(%rbp),%rax
mov    (%rax),%edx
call   10b0 <memcpy@plt>
mov    $0x50,%edi
call   1060 <htons@plt>
mov    %eax,-0x128(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x128(%rbp),%rsi
mov    $0xc,%edx
call   10c0 <connect@plt>
cmp    $0x0,%rax
jne    135c <check_connection+0x1ac>
mov    -0x8(%rbp),%edi
lea    0xd2a(%rip),%rsi
mov    $0x1d,%edx
call   1040 <write@plt>
cmp    $0x1c,%eax
jl     134b <check_connection+0x19b>
mov    -0x8(%rbp),%edi
mov    0x2cd6(%rip),%rax
mov    (%rax),%esi
call   1050 <shutdown@plt>
mov    -0x8(%rbp),%edi
lea    -0x110(%rbp),%rsi
mov    $0x100,%edx
call   1090 <read@plt>
cmp    $0xffffffff,%eax
je     133a <check_connection+0x18a>
mov    -0x8(%rbp),%edi
call   1080 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    13b4 <check_connection+0x204>
lea    0xceb(%rip),%rdi
call   1070 <printf@plt>
jmp    1357 <check_connection+0x1a7>
lea    0xce8(%rip),%rdi
call   1070 <printf@plt>
jmp    1368 <check_connection+0x1b8>
lea    0xce6(%rip),%rdi
call   1070 <printf@plt>
jmp    1379 <check_connection+0x1c9>
lea    0xce6(%rip),%rdi
call   1070 <printf@plt>
jmp    138a <check_connection+0x1da>
lea    0xce9(%rip),%rdi
call   1070 <printf@plt>
jmp    139b <check_connection+0x1eb>
lea    0xce8(%rip),%rdi
call   1070 <printf@plt>
cmpl   $0xffffffff,-0x8(%rbp)
je     13ad <check_connection+0x1fd>
mov    -0x8(%rbp),%edi
call   1080 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_connection>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x128,%rsp
lea    0xe3c(%rip),%rdi
call   10a0 <gethostbyname@plt>
test   %rax,%rax
je     12a1 <check_connection+0xf1>
mov    %rax,%rbx
mov    0x8(%rax),%edi
mov    0x2de1(%rip),%rax
mov    (%rax),%esi
mov    0x2de8(%rip),%r15
mov    (%r15),%edx
call   10d0 <socket@plt>
cmp    $0xffffffff,%eax
je     12aa <check_connection+0xfa>
mov    %eax,%ebp
movl   $0x1,0xc(%rsp)
mov    0x2dde(%rip),%rax
mov    (%rax),%esi
mov    0x2da5(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
lea    0xe30(%rip),%r14
test   %rax,%rax
jne    1314 <check_connection+0x164>
mov    (%r15),%esi
mov    0x2d9f(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
test   %rax,%rax
jne    1314 <check_connection+0x164>
mov    0x8(%rbx),%eax
mov    %eax,0x18(%rsp)
lea    0x14(%rsp),%rdi
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
call   10b0 <memcpy@plt>
mov    $0x50,%edi
call   1070 <htons@plt>
mov    %eax,0x10(%rsp)
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   10c0 <connect@plt>
test   %rax,%rax
je     12b8 <check_connection+0x108>
lea    0xdd2(%rip),%r14
jmp    1314 <check_connection+0x164>
lea    0xd84(%rip),%rdi
jmp    12b1 <check_connection+0x101>
lea    0xd9e(%rip),%rdi
call   1030 <puts@plt>
jmp    1323 <check_connection+0x173>
lea    0xd50(%rip),%rsi
mov    %ebp,%edi
mov    $0x1d,%edx
call   1050 <write@plt>
cmp    $0x1c,%eax
jl     1304 <check_connection+0x154>
mov    0x2d01(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1060 <shutdown@plt>
lea    0x20(%rsp),%rsi
mov    %ebp,%edi
mov    $0x100,%edx
call   1090 <read@plt>
cmp    $0xffffffff,%eax
je     130d <check_connection+0x15d>
mov    %ebp,%edi
call   1080 <close@plt>
mov    $0x1,%eax
jmp    1325 <check_connection+0x175>
lea    0xd76(%rip),%r14
jmp    1314 <check_connection+0x164>
lea    0xd7b(%rip),%r14
mov    %r14,%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x128,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.hostent = type { i32, i32, i32 }
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [15 x i8] c"www.google.com\00"
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@TCP_NODELAY = global i32 0
@.str.1 = [29 x i8] c"GET /index.html HTTP/1.1\0D\0A\0D\0A\00"
@SHUT_WR = global i32 0
@.str.2 = [14 x i8] c"read() error\0A\00"
@.str.3 = [15 x i8] c"write() error\0A\00"
@.str.4 = [17 x i8] c"connect() error\0A\00"
@.str.5 = [20 x i8] c"setsockopt() error\0A\00"
@.str.6 = [16 x i8] c"socket() error\0A\00"
@.str.7 = [36 x i8] c"cannot resolve IP for mom Google. \0A\00"
define i32 @check_connection() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [256 x i8]
%5 = alloca %struct.hostent*
%6 = alloca %struct.sockaddr_in
store %struct.hostent* null, %struct.hostent** %5
store i32 -1, i32* %2
%7 = call %struct.hostent* @gethostbyname(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store %struct.hostent* %7, %struct.hostent** %5
%8 = icmp ne %struct.hostent* %7, null
br i1 %8, label %9, label %80
9: ; preds = %0
%10 = load %struct.hostent*, %struct.hostent** %5
%11 = getelementptr inbounds %struct.hostent, %struct.hostent* %10, i32 0, i32 2
%12 = load i32, i32* %11
%13 = load i32, i32* @SOCK_STREAM
%14 = load i32, i32* @IPPROTO_TCP
%15 = call i32 @socket(i32 %12, i32 %13, i32 %14)
store i32 %15, i32* %2
%16 = icmp ne i32 %15, -1
br i1 %16, label %17, label %77
17: ; preds = %9
store i32 1, i32* %3
%18 = load i32, i32* %2
%19 = load i32, i32* @SOL_SOCKET
%20 = load i32, i32* @SO_REUSEADDR
%21 = bitcast i32* %3 to i8*
%22 = call i64 @setsockopt(i32 %18, i32 %19, i32 %20, i8* %21, i32 4)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %74
24: ; preds = %17
%25 = load i32, i32* %2
%26 = load i32, i32* @IPPROTO_TCP
%27 = load i32, i32* @TCP_NODELAY
%28 = bitcast i32* %3 to i8*
%29 = call i64 @setsockopt(i32 %25, i32 %26, i32 %27, i8* %28, i32 4)
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %74
31: ; preds = %24
%32 = load %struct.hostent*, %struct.hostent** %5
%33 = getelementptr inbounds %struct.hostent, %struct.hostent* %32, i32 0, i32 2
%34 = load i32, i32* %33
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %34, i32* %35
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
%37 = load %struct.hostent*, %struct.hostent** %5
%38 = getelementptr inbounds %struct.hostent, %struct.hostent* %37, i32 0, i32 1
%39 = load i32, i32* %38
%40 = load %struct.hostent*, %struct.hostent** %5
%41 = getelementptr inbounds %struct.hostent, %struct.hostent* %40, i32 0, i32 0
%42 = load i32, i32* %41
%43 = call i32 @memcpy(i32* %36, i32 %39, i32 %42)
%44 = call i32 @htons(i32 80)
%45 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 %44, i32* %45
%46 = load i32, i32* %2
%47 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%48 = call i64 @connect(i32 %46, %struct.sockaddr* %47, i32 12)
%49 = icmp eq i64 %48, 0
br i1 %49, label %50, label %71
50: ; preds = %31
%51 = load i32, i32* %2
%52 = call i32 @write(i32 %51, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i32 29)
%53 = icmp sge i32 %52, 28
br i1 %53, label %54, label %68
54: ; preds = %50
%55 = load i32, i32* %2
%56 = load i32, i32* @SHUT_WR
%57 = call i32 @shutdown(i32 %55, i32 %56)
%58 = load i32, i32* %2
%59 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%60 = call i32 @read(i32 %58, i8* %59, i32 256)
%61 = icmp ne i32 %60, -1
br i1 %61, label %62, label %65
62: ; preds = %54
%63 = load i32, i32* %2
%64 = call i32 @close(i32 %63)
store i32 1, i32* %1
br label %89
65: ; preds = %54
%66 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %67
67: ; preds = %65
br label %70
68: ; preds = %50
%69 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
br label %70
70: ; preds = %68, %67
br label %73
71: ; preds = %31
%72 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
br label %73
73: ; preds = %71, %70
br label %76
74: ; preds = %24, %17
%75 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
br label %76
76: ; preds = %74, %73
br label %79
77: ; preds = %9
%78 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0))
br label %79
79: ; preds = %77, %76
br label %82
80: ; preds = %0
%81 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.7, i64 0, i64 0))
br label %82
82: ; preds = %80, %79
%83 = load i32, i32* %2
%84 = icmp ne i32 %83, -1
br i1 %84, label %85, label %88
85: ; preds = %82
%86 = load i32, i32* %2
%87 = call i32 @close(i32 %86)
br label %88
88: ; preds = %85, %82
store i32 0, i32* %1
br label %89
89: ; preds = %88, %62
%90 = load i32, i32* %1
ret i32 %90
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [15 x i8] c"www.google.com\00"
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@TCP_NODELAY = global i32 0
@.str.1 = [29 x i8] c"GET /index.html HTTP/1.1\0D\0A\0D\0A\00"
@SHUT_WR = global i32 0
@str = [35 x i8] c"cannot resolve IP for mom Google. \00"
@str.8 = [15 x i8] c"socket() error\00"
@str.9 = [19 x i8] c"setsockopt() error\00"
@str.10 = [16 x i8] c"connect() error\00"
@str.11 = [14 x i8] c"write() error\00"
@str.12 = [13 x i8] c"read() error\00"
define i32 @check_connection() {
%1 = alloca i32
%2 = alloca [256 x i8]
%3 = alloca %struct.sockaddr_in
%4 = bitcast i32* %1 to i8*
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%6 = bitcast %struct.sockaddr_in* %3 to i8*
%7 = tail call %struct.hostent* @gethostbyname(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq %struct.hostent* %7, null
br i1 %8, label %52, label %9
9: ; preds = %0
%10 = getelementptr inbounds %struct.hostent, %struct.hostent* %7, i64 0, i32 2
%11 = load i32, i32* %10
%12 = load i32, i32* @SOCK_STREAM
%13 = load i32, i32* @IPPROTO_TCP
%14 = tail call i32 @socket(i32 %11, i32 %12, i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %50, label %16
16: ; preds = %9
store i32 1, i32* %1
%17 = load i32, i32* @SOL_SOCKET
%18 = load i32, i32* @SO_REUSEADDR
%19 = call i64 @setsockopt(i32 %14, i32 %17, i32 %18, i8* %4, i32 4)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %54
21: ; preds = %16
%22 = load i32, i32* @IPPROTO_TCP
%23 = load i32, i32* @TCP_NODELAY
%24 = call i64 @setsockopt(i32 %14, i32 %22, i32 %23, i8* %4, i32 4)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %54
26: ; preds = %21
%27 = load i32, i32* %10
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %27, i32* %28
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
%30 = getelementptr inbounds %struct.hostent, %struct.hostent* %7, i64 0, i32 1
%31 = load i32, i32* %30
%32 = getelementptr inbounds %struct.hostent, %struct.hostent* %7, i64 0, i32 0
%33 = load i32, i32* %32
%34 = call i32 @memcpy(i32* %29, i32 %31, i32 %33)
%35 = call i32 @htons(i32 80)
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %35, i32* %36
%37 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%38 = call i64 @connect(i32 %14, %struct.sockaddr* %37, i32 12)
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %54
40: ; preds = %26
%41 = call i32 @write(i32 %14, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i32 29)
%42 = icmp sgt i32 %41, 27
br i1 %42, label %43, label %54
43: ; preds = %40
%44 = load i32, i32* @SHUT_WR
%45 = call i32 @shutdown(i32 %14, i32 %44)
%46 = call i32 @read(i32 %14, i8* %5, i32 256)
%47 = icmp eq i32 %46, -1
br i1 %47, label %54, label %48
48: ; preds = %43
%49 = call i32 @close(i32 %14)
br label %58
50: ; preds = %9
%51 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.8, i64 0, i64 0))
br label %58
52: ; preds = %0
%53 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str, i64 0, i64 0))
br label %58
54: ; preds = %16, %21, %26, %40, %43
%55 = phi i8* [ getelementptr inbounds ([13 x i8], [13 x i8]* @str.12, i64 0, i64 0), %43 ], [ getelementptr inbounds ([14 x i8], [14 x i8]* @str.11, i64 0, i64 0), %40 ], [ getelementptr inbounds ([16 x i8], [16 x i8]* @str.10, i64 0, i64 0), %26 ], [ getelementptr inbounds ([19 x i8], [19 x i8]* @str.9, i64 0, i64 0), %21 ], [ getelementptr inbounds ([19 x i8], [19 x i8]* @str.9, i64 0, i64 0), %16 ]
%56 = call i32 @puts(i8* dereferenceable(1) %55)
%57 = call i32 @close(i32 %14)
br label %58
58: ; preds = %52, %50, %54, %48
%59 = phi i32 [ 1, %48 ], [ 0, %54 ], [ 0, %50 ], [ 0, %52 ]
ret i32 %59
}