#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;
  struct addrinfo *ai_next;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
};
typedef int /*<<< orphan*/ host;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ NI_NUMERICHOST;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int getaddrinfo(char *, char *, struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ getnameinfo(int /*<<< orphan*/, int /*<<< orphan*/, char *,
                               int, int /*<<< orphan*/ *, int /*<<< orphan*/,
                               int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
int main(int argc, char **argv) {
  int s;
  struct addrinfo hints, *result;
  memset(&hints, 0, sizeof(struct addrinfo));
  hints.ai_family = AF_INET;
  hints.ai_socktype = SOCK_STREAM;
  s = getaddrinfo("www.google.com", "80", &hints, &result);
  if (s != 0) {
    printf("error to get addrinfo, return value %d\n", s);
    exit(1);
  }
  struct addrinfo *p;
  char host[256];
  for (p = result; p != NULL; p = p->ai_next) {
    getnameinfo(p->ai_addr, p->ai_addrlen, host, sizeof(host), NULL, 0,
                NI_NUMERICHOST);
    printf("hostip: (%s)\n", host);
  }
  int sock_fd = socket(AF_INET, SOCK_STREAM, 0);
  if (connect(sock_fd, result->ai_addr, result->ai_addrlen) == -1) {
    perror("connect error");
    exit(2);
  }
  char *buffer = "GET / HTTP/1.0\r\n\r\n";
  printf("SENDING: %s", buffer);
  printf("===\n");
  write(sock_fd, buffer, strlen(buffer));
  char resp[10000];
  int len;
  while (1) {
    len = read(sock_fd, resp, 999);
    if (len <= 0) {
      break;
    }
    printf("%s\n", resp);
  }
  close(sock_fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2880,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1060 <memset@plt>
mov    0x2df8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2ddc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
lea    0xdf8(%rip),%rdi
lea    0xe00(%rip),%rsi
lea    -0x30(%rbp),%rdx
lea    -0x38(%rbp),%rcx
call   10d0 <getaddrinfo@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1244 <main+0x84>
mov    -0x14(%rbp),%esi
lea    0xddf(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     12ad <main+0xed>
mov    -0x40(%rbp),%rax
mov    0x4(%rax),%edi
mov    -0x40(%rbp),%rax
mov    (%rax),%esi
lea    -0x140(%rbp),%rdx
mov    0x2d5e(%rip),%rax
mov    (%rax),%eax
mov    $0x100,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
mov    %eax,(%rsp)
call   1080 <getnameinfo@plt>
lea    -0x140(%rbp),%rsi
lea    0xda5(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x40(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x40(%rbp)
jmp    124c <main+0x8c>
mov    0x2d34(%rip),%rax
mov    (%rax),%edi
mov    0x2d1b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,-0x144(%rbp)
mov    -0x144(%rbp),%edi
mov    -0x38(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x38(%rbp),%rax
mov    (%rax),%edx
call   10c0 <connect@plt>
cmp    $0xffffffff,%eax
jne    1303 <main+0x143>
lea    0xd54(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x2,%edi
call   10b0 <exit@plt>
lea    0xd4c(%rip),%rax
mov    %rax,-0x150(%rbp)
mov    -0x150(%rbp),%rsi
lea    0xd4a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xd48(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x144(%rbp),%eax
mov    %eax,-0x2874(%rbp)
mov    -0x150(%rbp),%rax
mov    %rax,-0x2870(%rbp)
mov    -0x150(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x2874(%rbp),%edi
mov    -0x2870(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    -0x144(%rbp),%edi
lea    -0x2860(%rbp),%rsi
mov    $0x3e7,%edx
call   1090 <read@plt>
mov    %eax,-0x2864(%rbp)
cmpl   $0x0,-0x2864(%rbp)
jg     139d <main+0x1dd>
jmp    13b7 <main+0x1f7>
lea    -0x2860(%rbp),%rsi
lea    0xccf(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    136e <main+0x1ae>
mov    -0x144(%rbp),%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x2880,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x2848,%rsp
lea    0x18(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1070 <memset@plt>
mov    0x2dec(%rip),%r15
mov    (%r15),%eax
mov    %eax,0x2c(%rsp)
mov    0x2dce(%rip),%r12
mov    (%r12),%eax
mov    %eax,0x28(%rsp)
lea    0xde7(%rip),%rdi
lea    0xdef(%rip),%rsi
lea    0x10(%rsp),%rcx
mov    %rbx,%rdx
call   10e0 <getaddrinfo@plt>
test   %eax,%eax
jne    1356 <main+0x186>
mov    0x10(%rsp),%rbp
test   %rbp,%rbp
je     1296 <main+0xc6>
mov    0x2d8a(%rip),%r13
lea    0x30(%rsp),%rbx
lea    0xde8(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    0x0(%rbp),%esi
mov    0x4(%rbp),%edi
mov    0x0(%r13),%eax
mov    %eax,(%rsp)
mov    %rbx,%rdx
mov    $0x100,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1090 <getnameinfo@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x8(%rbp),%rbp
test   %rbp,%rbp
jne    1260 <main+0x90>
mov    (%r15),%edi
mov    (%r12),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,%ebp
mov    0x10(%rsp),%rax
mov    (%rax),%edx
mov    0x4(%rax),%esi
mov    %ebp,%edi
call   10d0 <connect@plt>
cmp    $0xffffffff,%eax
je     1370 <main+0x1a0>
lea    0xda2(%rip),%rdi
lea    0xd88(%rip),%rbx
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xd96(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
lea    0x130(%rsp),%rsi
mov    %ebp,%edi
mov    $0x3e7,%edx
call   10a0 <read@plt>
test   %eax,%eax
jle    133b <main+0x16b>
lea    0x130(%rsp),%rbx
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x3e7,%edx
call   10a0 <read@plt>
test   %eax,%eax
jg     1320 <main+0x150>
mov    %ebp,%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x2848,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcb5(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    0xcd1(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x2,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, %struct.addrinfo*, i32, i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [15 x i8] c"www.google.com\00"
@.str.1 = [3 x i8] c"80\00"
@.str.2 = [40 x i8] c"error to get addrinfo, return value %d\0A\00"
@NI_NUMERICHOST = global i32 0
@.str.3 = [14 x i8] c"hostip: (%s)\0A\00"
@.str.4 = [14 x i8] c"connect error\00"
@.str.5 = [19 x i8] c"GET / HTTP/1.0\0D\0A\0D\0A\00"
@.str.6 = [12 x i8] c"SENDING: %s\00"
@.str.7 = [5 x i8] c"===\0A\00"
@.str.8 = [4 x i8] c"%s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.addrinfo
%8 = alloca %struct.addrinfo*
%9 = alloca %struct.addrinfo*
%10 = alloca [256 x i8]
%11 = alloca i32
%12 = alloca i8*
%13 = alloca [10000 x i8]
%14 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%15 = call i32 @memset(%struct.addrinfo* %7, i32 0, i32 24)
%16 = load i32, i32* @AF_INET
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i32 0, i32 4
store i32 %16, i32* %17
%18 = load i32, i32* @SOCK_STREAM
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i32 0, i32 3
store i32 %18, i32* %19
%20 = call i32 @getaddrinfo(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), %struct.addrinfo* %7, %struct.addrinfo** %8)
store i32 %20, i32* %6
%21 = load i32, i32* %6
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %2
%24 = load i32, i32* %6
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i32 %24)
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %2
%28 = load %struct.addrinfo*, %struct.addrinfo** %8
store %struct.addrinfo* %28, %struct.addrinfo** %9
br label %29
29: ; preds = %44, %27
%30 = load %struct.addrinfo*, %struct.addrinfo** %9
%31 = icmp ne %struct.addrinfo* %30, null
br i1 %31, label %32, label %48
32: ; preds = %29
%33 = load %struct.addrinfo*, %struct.addrinfo** %9
%34 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %33, i32 0, i32 1
%35 = load i32, i32* %34
%36 = load %struct.addrinfo*, %struct.addrinfo** %9
%37 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %36, i32 0, i32 0
%38 = load i32, i32* %37
%39 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%40 = load i32, i32* @NI_NUMERICHOST
%41 = call i32 @getnameinfo(i32 %35, i32 %38, i8* %39, i32 256, i32* null, i32 0, i32 %40)
%42 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i8* %42)
br label %44
44: ; preds = %32
%45 = load %struct.addrinfo*, %struct.addrinfo** %9
%46 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %45, i32 0, i32 2
%47 = load %struct.addrinfo*, %struct.addrinfo** %46
store %struct.addrinfo* %47, %struct.addrinfo** %9
br label %29
48: ; preds = %29
%49 = load i32, i32* @AF_INET
%50 = load i32, i32* @SOCK_STREAM
%51 = call i32 @socket(i32 %49, i32 %50, i32 0)
store i32 %51, i32* %11
%52 = load i32, i32* %11
%53 = load %struct.addrinfo*, %struct.addrinfo** %8
%54 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %53, i32 0, i32 1
%55 = load i32, i32* %54
%56 = load %struct.addrinfo*, %struct.addrinfo** %8
%57 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %56, i32 0, i32 0
%58 = load i32, i32* %57
%59 = call i32 @connect(i32 %52, i32 %55, i32 %58)
%60 = icmp eq i32 %59, -1
br i1 %60, label %61, label %64
61: ; preds = %48
%62 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%63 = call i32 @exit(i32 2)
unreachable
64: ; preds = %48
store i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i8** %12
%65 = load i8*, i8** %12
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0), i8* %65)
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0))
%68 = load i32, i32* %11
%69 = load i8*, i8** %12
%70 = load i8*, i8** %12
%71 = call i32 @strlen(i8* %70)
%72 = call i32 @write(i32 %68, i8* %69, i32 %71)
br label %73
73: ; preds = %64, %80
%74 = load i32, i32* %11
%75 = getelementptr inbounds [10000 x i8], [10000 x i8]* %13, i64 0, i64 0
%76 = call i32 @read(i32 %74, i8* %75, i32 999)
store i32 %76, i32* %14
%77 = load i32, i32* %14
%78 = icmp sle i32 %77, 0
br i1 %78, label %79, label %80
79: ; preds = %73
br label %83
80: ; preds = %73
%81 = getelementptr inbounds [10000 x i8], [10000 x i8]* %13, i64 0, i64 0
%82 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.8, i64 0, i64 0), i8* %81)
br label %73
83: ; preds = %79
%84 = load i32, i32* %11
%85 = call i32 @close(i32 %84)
ret i32 0
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, %struct.addrinfo*, i32, i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [15 x i8] c"www.google.com\00"
@.str.1 = [3 x i8] c"80\00"
@.str.2 = [40 x i8] c"error to get addrinfo, return value %d\0A\00"
@NI_NUMERICHOST = global i32 0
@.str.3 = [14 x i8] c"hostip: (%s)\0A\00"
@.str.4 = [14 x i8] c"connect error\00"
@.str.5 = [19 x i8] c"GET / HTTP/1.0\0D\0A\0D\0A\00"
@.str.6 = [12 x i8] c"SENDING: %s\00"
@str = [4 x i8] c"===\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.addrinfo
%4 = alloca %struct.addrinfo*
%5 = alloca [256 x i8]
%6 = alloca [10000 x i8]
%7 = bitcast %struct.addrinfo* %3 to i8*
%8 = bitcast %struct.addrinfo** %4 to i8*
%9 = call i32 @memset(%struct.addrinfo* %3, i32 0, i32 24)
%10 = load i32, i32* @AF_INET
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 4
store i32 %10, i32* %11
%12 = load i32, i32* @SOCK_STREAM
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 3
store i32 %12, i32* %13
%14 = call i32 @getaddrinfo(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), %struct.addrinfo* %3, %struct.addrinfo** %4)
%15 = icmp eq i32 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %2
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i32 %14)
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %2
%20 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%21 = load %struct.addrinfo*, %struct.addrinfo** %4
%22 = icmp eq %struct.addrinfo* %21, null
br i1 %22, label %35, label %23
23: ; preds = %19, %23
%24 = phi %struct.addrinfo* [ %33, %23 ], [ %21, %19 ]
%25 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %24, i64 0, i32 1
%26 = load i32, i32* %25
%27 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %24, i64 0, i32 0
%28 = load i32, i32* %27
%29 = load i32, i32* @NI_NUMERICHOST
%30 = call i32 @getnameinfo(i32 %26, i32 %28, i8* %20, i32 256, i32* null, i32 0, i32 %29)
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i8* %20)
%32 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %24, i64 0, i32 2
%33 = load %struct.addrinfo*, %struct.addrinfo** %32
%34 = icmp eq %struct.addrinfo* %33, null
br i1 %34, label %35, label %23
35: ; preds = %23, %19
%36 = load i32, i32* @AF_INET
%37 = load i32, i32* @SOCK_STREAM
%38 = call i32 @socket(i32 %36, i32 %37, i32 0)
%39 = load %struct.addrinfo*, %struct.addrinfo** %4
%40 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %39, i64 0, i32 1
%41 = load i32, i32* %40
%42 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %39, i64 0, i32 0
%43 = load i32, i32* %42
%44 = call i32 @connect(i32 %38, i32 %41, i32 %43)
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %49
46: ; preds = %35
%47 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%48 = call i32 @exit(i32 2)
unreachable
49: ; preds = %35
%50 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0))
%51 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @str, i64 0, i64 0))
%52 = call i32 @strlen(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0))
%53 = call i32 @write(i32 %38, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i32 %52)
%54 = getelementptr inbounds [10000 x i8], [10000 x i8]* %6, i64 0, i64 0
%55 = call i32 @read(i32 %38, i8* %54, i32 999)
%56 = icmp slt i32 %55, 1
br i1 %56, label %61, label %57
57: ; preds = %49, %57
%58 = call i32 @puts(i8* %54)
%59 = call i32 @read(i32 %38, i8* %54, i32 999)
%60 = icmp slt i32 %59, 1
br i1 %60, label %61, label %57
61: ; preds = %57, %49
%62 = call i32 @close(i32 %38)
ret i32 0
}