#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 dummy;
};
typedef int ssize_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ example_connect(int, struct addrinfo *);
struct addrinfo *example_getaddrinfo(char *, char *);
int example_socket(struct addrinfo *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int recv(int, char *, int, int /*<<< orphan*/);
int send(int, char *, scalar_t__, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int /*<<< orphan*/ stdout;
scalar_t__ strlen(char *);
int main(void) {
  char *hostname = "www.mtu.edu";
  char *port = "80";
  struct addrinfo *socketaddrinfo = example_getaddrinfo(hostname, port);
  int sock = example_socket(socketaddrinfo);
  example_connect(sock, socketaddrinfo);
  freeaddrinfo(socketaddrinfo);
  char httpget[1024];
  snprintf(httpget, 1024,
           "GET / HTTP/1.1\r\n"
           "Host: %s\r\n"
           "Connection: close\r\n\r\n",
           hostname);
  ssize_t s = send(sock, httpget, strlen(httpget), 0);
  if (s == -1) {
    perror("send");
    exit(EXIT_FAILURE);
  } else if (s < (ssize_t)strlen(httpget)) {
    printf("Entire message did not get sent.");
  }
  while (1) {
    char recvbuf[1024];
    ssize_t recvdBytes = 0;
    recvdBytes = recv(sock, recvbuf, 1024, 0);
    if (recvdBytes > 0)
      fwrite(recvbuf, 1, recvdBytes, stdout);
    else if (recvdBytes == 0) {
      fflush(stdout);
      close(sock);
      exit(EXIT_SUCCESS);
    } else {
      perror("recv:");
      close(sock);
      exit(EXIT_FAILURE);
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x860,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe07(%rip),%rax
mov    %rax,-0x10(%rbp)
lea    0xe08(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   10f0 <example_getaddrinfo@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   10a0 <example_socket@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rsi
call   10e0 <example_connect@plt>
mov    -0x20(%rbp),%rdi
call   1100 <freeaddrinfo@plt>
lea    -0x430(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x400,%esi
lea    0xdbe(%rip),%rdx
call   1070 <snprintf@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x854(%rbp)
lea    -0x430(%rbp),%rax
mov    %rax,-0x850(%rbp)
lea    -0x430(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x854(%rbp),%edi
mov    -0x850(%rbp),%rsi
mov    %rax,%rdx
xor    %ecx,%ecx
call   1050 <send@plt>
mov    %eax,-0x434(%rbp)
cmpl   $0xffffffff,-0x434(%rbp)
jne    12bd <main+0xdd>
lea    0xd95(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2d2a(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    -0x434(%rbp),%eax
mov    %eax,-0x858(%rbp)
lea    -0x430(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x858(%rbp),%eax
cmp    %ecx,%eax
jge    12f2 <main+0x112>
lea    0xd57(%rip),%rdi
call   1060 <printf@plt>
jmp    12f7 <main+0x117>
jmp    12fc <main+0x11c>
movl   $0x0,-0x844(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x840(%rbp),%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x844(%rbp)
cmpl   $0x0,-0x844(%rbp)
jle    1354 <main+0x174>
lea    -0x840(%rbp),%rdi
mov    -0x844(%rbp),%edx
mov    0x2c95(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%esi
call   10d0 <fwrite@plt>
jmp    13a7 <main+0x1c7>
cmpl   $0x0,-0x844(%rbp)
jne    1385 <main+0x1a5>
mov    0x2c70(%rip),%rax
mov    (%rax),%edi
call   1090 <fflush@plt>
mov    -0x24(%rbp),%edi
call   1080 <close@plt>
mov    0x2c52(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
lea    0xcd9(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x24(%rbp),%edi
call   1080 <close@plt>
mov    0x2c40(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
jmp    12fc <main+0x11c>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x808,%rsp
lea    0xe0c(%rip),%r14
lea    0xe11(%rip),%rsi
mov    %r14,%rdi
call   10f0 <example_getaddrinfo@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   10a0 <example_socket@plt>
mov    %eax,%r15d
mov    %eax,%edi
mov    %rbx,%rsi
call   10e0 <example_connect@plt>
mov    %rbx,%rdi
call   1100 <freeaddrinfo@plt>
lea    0xde5(%rip),%rdx
lea    0x400(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x400,%esi
mov    %r14,%rcx
call   1070 <snprintf@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %rax,%rdx
xor    %ecx,%ecx
call   1050 <send@plt>
cmp    $0xffffffff,%eax
jne    1270 <main+0x90>
lea    0xdd9(%rip),%rdi
call   10b0 <perror@plt>
jmp    1314 <main+0x134>
mov    %eax,%ebx
lea    0x400(%rsp),%rdi
call   1040 <strlen@plt>
cmp    %eax,%ebx
jge    128f <main+0xaf>
lea    0xdba(%rip),%rdi
call   1060 <printf@plt>
mov    %rsp,%rsi
mov    %r15d,%edi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jle    12d8 <main+0xf8>
mov    0x2d2c(%rip),%rbp
mov    %rsp,%rbx
nop
mov    0x0(%rbp),%ecx
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %eax,%edx
call   10d0 <fwrite@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jg     12b0 <main+0xd0>
test   %eax,%eax
jne    1300 <main+0x120>
mov    0x2cf5(%rip),%rax
mov    (%rax),%edi
call   1090 <fflush@plt>
mov    %r15d,%edi
call   1080 <close@plt>
mov    0x2cd7(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
lea    0xd5e(%rip),%rdi
call   10b0 <perror@plt>
mov    %r15d,%edi
call   1080 <close@plt>
mov    0x2cc5(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.addrinfo = type { i32 }
@.str = [12 x i8] c"www.mtu.edu\00"
@.str.1 = [3 x i8] c"80\00"
@.str.2 = [48 x i8] c"GET / HTTP/1.1\0D\0AHost: %s\0D\0AConnection: close\0D\0A\0D\0A\00"
@.str.3 = [5 x i8] c"send\00"
@EXIT_FAILURE = global i32 0
@.str.4 = [33 x i8] c"Entire message did not get sent.\00"
@stdout = global i32 0
@EXIT_SUCCESS = global i32 0
@.str.5 = [6 x i8] c"recv:\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca %struct.addrinfo*
%5 = alloca i32
%6 = alloca [1024 x i8]
%7 = alloca i32
%8 = alloca [1024 x i8]
%9 = alloca i32
store i32 0, i32* %1
store i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8** %2
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8** %3
%10 = load i8*, i8** %2
%11 = load i8*, i8** %3
%12 = call %struct.addrinfo* @example_getaddrinfo(i8* %10, i8* %11)
store %struct.addrinfo* %12, %struct.addrinfo** %4
%13 = load %struct.addrinfo*, %struct.addrinfo** %4
%14 = call i32 @example_socket(%struct.addrinfo* %13)
store i32 %14, i32* %5
%15 = load i32, i32* %5
%16 = load %struct.addrinfo*, %struct.addrinfo** %4
%17 = call i32 @example_connect(i32 %15, %struct.addrinfo* %16)
%18 = load %struct.addrinfo*, %struct.addrinfo** %4
%19 = call i32 @freeaddrinfo(%struct.addrinfo* %18)
%20 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%21 = load i8*, i8** %2
%22 = call i32 @snprintf(i8* %20, i32 1024, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0), i8* %21)
%23 = load i32, i32* %5
%24 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%25 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%26 = call i64 @strlen(i8* %25)
%27 = call i32 @send(i32 %23, i8* %24, i64 %26, i32 0)
store i32 %27, i32* %7
%28 = load i32, i32* %7
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %34
30: ; preds = %0
%31 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%32 = load i32, i32* @EXIT_FAILURE
%33 = call i32 @exit(i32 %32)
unreachable
34: ; preds = %0
%35 = load i32, i32* %7
%36 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%37 = call i64 @strlen(i8* %36)
%38 = trunc i64 %37 to i32
%39 = icmp slt i32 %35, %38
br i1 %39, label %40, label %42
40: ; preds = %34
%41 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0))
br label %42
42: ; preds = %40, %34
br label %43
43: ; preds = %42
br label %44
44: ; preds = %43, %71
store i32 0, i32* %9
%45 = load i32, i32* %5
%46 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%47 = call i32 @recv(i32 %45, i8* %46, i32 1024, i32 0)
store i32 %47, i32* %9
%48 = load i32, i32* %9
%49 = icmp sgt i32 %48, 0
br i1 %49, label %50, label %55
50: ; preds = %44
%51 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%52 = load i32, i32* %9
%53 = load i32, i32* @stdout
%54 = call i32 @fwrite(i8* %51, i32 1, i32 %52, i32 %53)
br label %71
55: ; preds = %44
%56 = load i32, i32* %9
%57 = icmp eq i32 %56, 0
br i1 %57, label %58, label %65
58: ; preds = %55
%59 = load i32, i32* @stdout
%60 = call i32 @fflush(i32 %59)
%61 = load i32, i32* %5
%62 = call i32 @close(i32 %61)
%63 = load i32, i32* @EXIT_SUCCESS
%64 = call i32 @exit(i32 %63)
unreachable
65: ; preds = %55
%66 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%67 = load i32, i32* %5
%68 = call i32 @close(i32 %67)
%69 = load i32, i32* @EXIT_FAILURE
%70 = call i32 @exit(i32 %69)
unreachable
71: ; preds = %50
br label %44
}

LLVM IR (-O2):
%struct.addrinfo = type { i32 }
@.str = [12 x i8] c"www.mtu.edu\00"
@.str.1 = [3 x i8] c"80\00"
@.str.2 = [48 x i8] c"GET / HTTP/1.1\0D\0AHost: %s\0D\0AConnection: close\0D\0A\0D\0A\00"
@.str.3 = [5 x i8] c"send\00"
@EXIT_FAILURE = global i32 0
@.str.4 = [33 x i8] c"Entire message did not get sent.\00"
@stdout = global i32 0
@EXIT_SUCCESS = global i32 0
@.str.5 = [6 x i8] c"recv:\00"
define i32 @main() {
%1 = alloca [1024 x i8]
%2 = alloca [1024 x i8]
%3 = tail call %struct.addrinfo* @example_getaddrinfo(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%4 = tail call i32 @example_socket(%struct.addrinfo* %3)
%5 = tail call i32 @example_connect(i32 %4, %struct.addrinfo* %3)
%6 = tail call i32 @freeaddrinfo(%struct.addrinfo* %3)
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%8 = call i32 @snprintf(i8* %7, i32 1024, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%9 = call i64 @strlen(i8* %7)
%10 = call i32 @send(i32 %4, i8* %7, i64 %9, i32 0)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %16
12: ; preds = %0
%13 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%14 = load i32, i32* @EXIT_FAILURE
%15 = call i32 @exit(i32 %14)
unreachable
16: ; preds = %0
%17 = call i64 @strlen(i8* %7)
%18 = trunc i64 %17 to i32
%19 = icmp slt i32 %10, %18
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0))
br label %22
22: ; preds = %16, %20
%23 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%24 = call i32 @recv(i32 %4, i8* %23, i32 1024, i32 0)
%25 = icmp sgt i32 %24, 0
br i1 %25, label %26, label %32
26: ; preds = %22, %26
%27 = phi i32 [ %30, %26 ], [ %24, %22 ]
%28 = load i32, i32* @stdout
%29 = call i32 @fwrite(i8* %23, i32 1, i32 %27, i32 %28)
%30 = call i32 @recv(i32 %4, i8* %23, i32 1024, i32 0)
%31 = icmp sgt i32 %30, 0
br i1 %31, label %26, label %32
32: ; preds = %26, %22
%33 = phi i32 [ %24, %22 ], [ %30, %26 ]
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %41
35: ; preds = %32
%36 = load i32, i32* @stdout
%37 = call i32 @fflush(i32 %36)
%38 = call i32 @close(i32 %4)
%39 = load i32, i32* @EXIT_SUCCESS
%40 = call i32 @exit(i32 %39)
unreachable
41: ; preds = %32
%42 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%43 = call i32 @close(i32 %4)
%44 = load i32, i32* @EXIT_FAILURE
%45 = call i32 @exit(i32 %44)
unreachable
}