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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ PF_UNSPEC;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fail(int, char *, ...);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int /*<<< orphan*/ gai_strerror(int);
int getaddrinfo(char *, char *, struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
char *perform_http_get(int, char *, char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
char *download_page(char *host, char *protocol, char *page) {
  struct addrinfo hints, *res, *res0;
  int error;
  int socket_ref;
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = PF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  error = getaddrinfo(host, protocol, &hints, &res0);
  fail(error, gai_strerror(error));
  socket_ref = -1;
  for (res = res0; res; res = res->ai_next) {
    socket_ref = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
    fail(socket_ref < 0, "socket: %s\n", strerror(errno));
    if (connect(socket_ref, res->ai_addr, res->ai_addrlen) < 0) {
      fprintf(stderr, "connect: %s\n", strerror(errno));
      close(socket_ref);
      exit(EXIT_FAILURE);
    }
    break;
  }
  char *page_content = NULL;
  if (socket_ref != -1) {
    page_content = perform_http_get(socket_ref, host, page);
  }
  freeaddrinfo(res0);
  return page_content;
}


x86 assembly (clang, -O0):
<download_page>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
mov    0x2dfd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2dd9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x38(%rbp),%rdx
lea    -0x48(%rbp),%rcx
call   10b0 <getaddrinfo@plt>
mov    %eax,-0x4c(%rbp)
mov    -0x4c(%rbp),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x4c(%rbp),%edi
call   1040 <gai_strerror@plt>
mov    -0x5c(%rbp),%edi
movslq %eax,%rsi
mov    $0x0,%al
call   1030 <fail@plt>
movl   $0xffffffff,-0x50(%rbp)
mov    -0x48(%rbp),%rax
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     12fd <download_page+0x13d>
mov    -0x40(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x40(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x40(%rbp),%rax
mov    0x8(%rax),%edx
call   10e0 <socket@plt>
mov    %eax,-0x50(%rbp)
cmpl   $0x0,-0x50(%rbp)
setl   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x60(%rbp)
mov    0x2d4c(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x60(%rbp),%edi
mov    %rax,%rdx
lea    0xd70(%rip),%rsi
mov    $0x0,%al
call   1030 <fail@plt>
mov    -0x50(%rbp),%edi
mov    -0x40(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x40(%rbp),%rax
mov    (%rax),%edx
call   10a0 <connect@plt>
cmp    $0x0,%rax
jge    12f8 <download_page+0x138>
mov    0x2d1b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x64(%rbp)
mov    0x2cff(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x64(%rbp),%edi
mov    %rax,%rdx
lea    0xd2f(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x50(%rbp),%edi
call   1060 <close@plt>
mov    0x2ccf(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
jmp    12fd <download_page+0x13d>
movq   $0x0,-0x58(%rbp)
cmpl   $0xffffffff,-0x50(%rbp)
je     1323 <download_page+0x163>
mov    -0x50(%rbp),%edi
mov    -0x8(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1080 <perform_http_get@plt>
mov    %rax,-0x58(%rbp)
mov    -0x48(%rbp),%rdi
call   10d0 <freeaddrinfo@plt>
mov    -0x58(%rbp),%rax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<download_page>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdi,%r15
lea    0x8(%rsp),%r13
xor    %r12d,%r12d
mov    %r13,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
mov    0x2df3(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2dce(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    %rsp,%rcx
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %r13,%rdx
call   10b0 <getaddrinfo@plt>
mov    %eax,%ebx
mov    %eax,%edi
call   1040 <gai_strerror@plt>
movslq %eax,%rsi
mov    %ebx,%edi
xor    %eax,%eax
call   1030 <fail@plt>
mov    (%rsp),%rbx
test   %rbx,%rbx
je     129e <download_page+0xde>
mov    0x10(%rbx),%edi
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
call   10e0 <socket@plt>
mov    %eax,%r12d
mov    %eax,%ebp
shr    $0x1f,%ebp
mov    0x2d74(%rip),%r13
mov    0x0(%r13),%edi
call   10c0 <strerror@plt>
lea    0xd9c(%rip),%rsi
mov    %ebp,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1030 <fail@plt>
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %r12d,%edi
call   10a0 <connect@plt>
test   %rax,%rax
js     12b9 <download_page+0xf9>
cmp    $0xffffffff,%r12d
je     129b <download_page+0xdb>
mov    %r12d,%edi
mov    %r15,%rsi
mov    %r14,%rdx
call   1080 <perform_http_get@plt>
mov    %rax,%r12
jmp    129e <download_page+0xde>
xor    %r12d,%r12d
mov    (%rsp),%rdi
call   10d0 <freeaddrinfo@plt>
mov    %r12,%rax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d18(%rip),%rax
mov    (%rax),%ebp
mov    0x0(%r13),%edi
call   10c0 <strerror@plt>
lea    0xd3a(%rip),%rsi
mov    %ebp,%edi
mov    %rax,%rdx
call   1070 <fprintf@plt>
mov    %r12d,%edi
call   1060 <close@plt>
mov    0x2cd5(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo* }
@PF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@.str = [12 x i8] c"socket: %s\0A\00"
@errno = global i32 0
@stderr = global i32 0
@.str.1 = [13 x i8] c"connect: %s\0A\00"
@EXIT_FAILURE = global i32 0
define i8* @download_page(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca %struct.addrinfo
%8 = alloca %struct.addrinfo*
%9 = alloca %struct.addrinfo*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
%13 = call i32 @memset(%struct.addrinfo* %7, i32 0, i32 32)
%14 = load i32, i32* @PF_UNSPEC
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i32 0, i32 4
store i32 %14, i32* %15
%16 = load i32, i32* @SOCK_STREAM
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i32 0, i32 3
store i32 %16, i32* %17
%18 = load i8*, i8** %4
%19 = load i8*, i8** %5
%20 = call i32 @getaddrinfo(i8* %18, i8* %19, %struct.addrinfo* %7, %struct.addrinfo** %9)
store i32 %20, i32* %10
%21 = load i32, i32* %10
%22 = load i32, i32* %10
%23 = call i32 @gai_strerror(i32 %22)
%24 = sext i32 %23 to i64
%25 = inttoptr i64 %24 to i8*
%26 = call i32 (i32, i8*, ...) @fail(i32 %21, i8* %25)
store i32 -1, i32* %11
%27 = load %struct.addrinfo*, %struct.addrinfo** %9
store %struct.addrinfo* %27, %struct.addrinfo** %8
br label %28
28: ; preds = %67, %3
%29 = load %struct.addrinfo*, %struct.addrinfo** %8
%30 = icmp ne %struct.addrinfo* %29, null
br i1 %30, label %31, label %71
31: ; preds = %28
%32 = load %struct.addrinfo*, %struct.addrinfo** %8
%33 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %32, i32 0, i32 4
%34 = load i32, i32* %33
%35 = load %struct.addrinfo*, %struct.addrinfo** %8
%36 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %35, i32 0, i32 3
%37 = load i32, i32* %36
%38 = load %struct.addrinfo*, %struct.addrinfo** %8
%39 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %38, i32 0, i32 2
%40 = load i32, i32* %39
%41 = call i32 @socket(i32 %34, i32 %37, i32 %40)
store i32 %41, i32* %11
%42 = load i32, i32* %11
%43 = icmp slt i32 %42, 0
%44 = zext i1 %43 to i32
%45 = load i32, i32* @errno
%46 = call i8* @strerror(i32 %45)
%47 = call i32 (i32, i8*, ...) @fail(i32 %44, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %46)
%48 = load i32, i32* %11
%49 = load %struct.addrinfo*, %struct.addrinfo** %8
%50 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %49, i32 0, i32 1
%51 = load i32, i32* %50
%52 = load %struct.addrinfo*, %struct.addrinfo** %8
%53 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %52, i32 0, i32 0
%54 = load i32, i32* %53
%55 = call i64 @connect(i32 %48, i32 %51, i32 %54)
%56 = icmp slt i64 %55, 0
br i1 %56, label %57, label %66
57: ; preds = %31
%58 = load i32, i32* @stderr
%59 = load i32, i32* @errno
%60 = call i8* @strerror(i32 %59)
%61 = call i32 @fprintf(i32 %58, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %60)
%62 = load i32, i32* %11
%63 = call i32 @close(i32 %62)
%64 = load i32, i32* @EXIT_FAILURE
%65 = call i32 @exit(i32 %64)
unreachable
66: ; preds = %31
br label %71
67: ; No predecessors!
%68 = load %struct.addrinfo*, %struct.addrinfo** %8
%69 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %68, i32 0, i32 5
%70 = load %struct.addrinfo*, %struct.addrinfo** %69
store %struct.addrinfo* %70, %struct.addrinfo** %8
br label %28
71: ; preds = %66, %28
store i8* null, i8** %12
%72 = load i32, i32* %11
%73 = icmp ne i32 %72, -1
br i1 %73, label %74, label %79
74: ; preds = %71
%75 = load i32, i32* %11
%76 = load i8*, i8** %4
%77 = load i8*, i8** %6
%78 = call i8* @perform_http_get(i32 %75, i8* %76, i8* %77)
store i8* %78, i8** %12
br label %79
79: ; preds = %74, %71
%80 = load %struct.addrinfo*, %struct.addrinfo** %9
%81 = call i32 @freeaddrinfo(%struct.addrinfo* %80)
%82 = load i8*, i8** %12
ret i8* %82
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo* }
@PF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@.str = [12 x i8] c"socket: %s\0A\00"
@errno = global i32 0
@stderr = global i32 0
@.str.1 = [13 x i8] c"connect: %s\0A\00"
@EXIT_FAILURE = global i32 0
define i8* @download_page(i8* %0, i8* %1, i8* %2) {
%4 = alloca %struct.addrinfo
%5 = alloca %struct.addrinfo*
%6 = bitcast %struct.addrinfo* %4 to i8*
%7 = bitcast %struct.addrinfo** %5 to i8*
%8 = call i32 @memset(%struct.addrinfo* %4, i32 0, i32 32)
%9 = load i32, i32* @PF_UNSPEC
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 4
store i32 %9, i32* %10
%11 = load i32, i32* @SOCK_STREAM
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 3
store i32 %11, i32* %12
%13 = call i32 @getaddrinfo(i8* %0, i8* %1, %struct.addrinfo* %4, %struct.addrinfo** %5)
%14 = call i32 @gai_strerror(i32 %13)
%15 = sext i32 %14 to i64
%16 = inttoptr i64 %15 to i8*
%17 = call i32 (i32, i8*, ...) @fail(i32 %13, i8* %16)
%18 = load %struct.addrinfo*, %struct.addrinfo** %5
%19 = icmp eq %struct.addrinfo* %18, null
br i1 %19, label %50, label %20
20: ; preds = %3
%21 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %18, i64 0, i32 4
%22 = load i32, i32* %21
%23 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %18, i64 0, i32 3
%24 = load i32, i32* %23
%25 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %18, i64 0, i32 2
%26 = load i32, i32* %25
%27 = call i32 @socket(i32 %22, i32 %24, i32 %26)
%28 = lshr i32 %27, 31
%29 = load i32, i32* @errno
%30 = call i8* @strerror(i32 %29)
%31 = call i32 (i32, i8*, ...) @fail(i32 %28, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %30)
%32 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %18, i64 0, i32 1
%33 = load i32, i32* %32
%34 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %18, i64 0, i32 0
%35 = load i32, i32* %34
%36 = call i64 @connect(i32 %27, i32 %33, i32 %35)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %46
38: ; preds = %20
%39 = load i32, i32* @stderr
%40 = load i32, i32* @errno
%41 = call i8* @strerror(i32 %40)
%42 = call i32 @fprintf(i32 %39, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %41)
%43 = call i32 @close(i32 %27)
%44 = load i32, i32* @EXIT_FAILURE
%45 = call i32 @exit(i32 %44)
unreachable
46: ; preds = %20
%47 = icmp eq i32 %27, -1
br i1 %47, label %50, label %48
48: ; preds = %46
%49 = call i8* @perform_http_get(i32 %27, i8* %0, i8* %2)
br label %50
50: ; preds = %3, %48, %46
%51 = phi i8* [ %49, %48 ], [ null, %46 ], [ null, %3 ]
%52 = load %struct.addrinfo*, %struct.addrinfo** %5
%53 = call i32 @freeaddrinfo(%struct.addrinfo* %52)
ret i8* %51
}