#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 */

/* Variables and functions */
int asprintf(char **, char const *, char const *, char const *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fail(int, char *, ...);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ printf(char *, char *);
int recvfrom(int, char *, int, int /*<<< orphan*/, int /*<<< orphan*/,
             int /*<<< orphan*/);
int send(int, char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char *);
void get_page(int fd, const char *host, const char *page) {
  char *msg;
  const char *format =
      "GET %s HTTP/1.0\r\nHost: %s\r\nUser-Agent: fetch.c\r\n\r\n";
  int status;
  status = asprintf(&msg, format, page, host);
  printf("%s\n", msg);
  fail(status == -1 || !msg, "asprintf failed.\n");
  status = send(fd, msg, strlen(msg), 0);
  fail(status == -1, "send failed: %s\n", strerror(errno));
  while (true) {
    int bytes;
    char buf[0x1000 + 10];
    bytes = recvfrom(fd, buf, 0x1000, 0, 0, 0);
    if (bytes == 0) {
      break;
    }
    fail(bytes == -1, "%s\n", strerror(errno));
    buf[bytes] = '\0';
    printf("%s", buf);
  }
  free(msg);
}


x86 assembly (clang, -O0):
<get_page>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1060,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    0xe63(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    -0x20(%rbp),%rdi
call   1090 <asprintf@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x20(%rbp),%rsi
lea    0xe8e(%rip),%rdi
call   1070 <printf@plt>
mov    $0x1,%al
cmpl   $0xffffffff,-0x2c(%rbp)
mov    %al,-0x1041(%rbp)
je     11eb <get_page+0x6b>
cmpq   $0x0,-0x20(%rbp)
setne  %al
xor    $0xff,%al
mov    %al,-0x1041(%rbp)
mov    -0x1041(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
lea    0xe36(%rip),%rsi
mov    $0x0,%al
call   1040 <fail@plt>
mov    -0x4(%rbp),%eax
mov    %eax,-0x1054(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x1050(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x1054(%rbp),%edi
mov    -0x1050(%rbp),%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x1048(%rbp)
mov    0x2d8d(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x1048(%rbp),%edi
mov    %eax,%edx
lea    0xddc(%rip),%rsi
mov    $0x0,%al
call   1040 <fail@plt>
mov    -0x4(%rbp),%edi
lea    -0x1040(%rbp),%rsi
mov    $0x1000,%edx
xor    %r9d,%r9d
mov    %r9d,%ecx
mov    %r9d,%r8d
call   1080 <recvfrom@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jne    129f <get_page+0x11f>
jmp    12f9 <get_page+0x179>
cmpl   $0xffffffff,-0x30(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x1058(%rbp)
mov    0x2d28(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x1058(%rbp),%edi
mov    %eax,%edx
lea    0xd84(%rip),%rsi
mov    $0x0,%al
call   1040 <fail@plt>
movslq -0x30(%rbp),%rax
movb   $0x0,-0x1040(%rbp,%rax,1)
lea    -0x1040(%rbp),%rsi
lea    0xd67(%rip),%rdi
call   1070 <printf@plt>
jmp    1270 <get_page+0xf0>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
add    $0x1060,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_page>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1028,%rsp
mov    %rsi,%rcx
mov    %edi,%r12d
lea    0xe62(%rip),%rsi
lea    0x8(%rsp),%rdi
call   1090 <asprintf@plt>
mov    %eax,%ebx
mov    0x8(%rsp),%rsi
lea    0xe9c(%rip),%rdi
call   1070 <printf@plt>
cmp    $0xffffffff,%ebx
sete   %al
cmpq   $0x0,0x8(%rsp)
sete   %cl
or     %al,%cl
movzbl %cl,%edi
lea    0xe5d(%rip),%rsi
xor    %eax,%eax
call   1040 <fail@plt>
mov    0x8(%rsp),%rbx
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
xor    %ebx,%ebx
cmp    $0xffffffff,%eax
sete   %bl
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xe2f(%rip),%rsi
mov    %ebx,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <fail@plt>
lea    0x10(%rsp),%rsi
mov    %r12d,%edi
mov    $0x1000,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1080 <recvfrom@plt>
test   %eax,%eax
je     12b5 <get_page+0x135>
mov    %eax,%ebp
lea    0xe0a(%rip),%r14
lea    0xe07(%rip),%r15
lea    0x10(%rsp),%r13
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
xor    %ebx,%ebx
cmp    $0xffffffff,%ebp
sete   %bl
mov    0x2d71(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <fail@plt>
movslq %ebp,%rax
movb   $0x0,0x10(%rsp,%rax,1)
mov    %r15,%rdi
mov    %r13,%rsi
call   1070 <printf@plt>
mov    %r12d,%edi
mov    %r13,%rsi
mov    $0x1000,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1080 <recvfrom@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    1260 <get_page+0xe0>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
add    $0x1028,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [51 x i8] c"GET %s HTTP/1.0\0D\0AHost: %s\0D\0AUser-Agent: fetch.c\0D\0A\0D\0A\00"
@.str.1 = [4 x i8] c"%s\0A\00"
@.str.2 = [18 x i8] c"asprintf failed.\0A\00"
@.str.3 = [17 x i8] c"send failed: %s\0A\00"
@errno = global i32 0
@.str.4 = [3 x i8] c"%s\00"
define void @get_page(i32 %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca [4106 x i8]
store i32 %0, i32* %4
store i8* %1, i8** %5
store i8* %2, i8** %6
store i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0), i8** %8
%12 = load i8*, i8** %8
%13 = load i8*, i8** %6
%14 = load i8*, i8** %5
%15 = call i32 @asprintf(i8** %7, i8* %12, i8* %13, i8* %14)
store i32 %15, i32* %9
%16 = load i8*, i8** %7
%17 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %16)
%18 = load i32, i32* %9
%19 = icmp eq i32 %18, -1
br i1 %19, label %24, label %20
20: ; preds = %3
%21 = load i8*, i8** %7
%22 = icmp ne i8* %21, null
%23 = xor i1 %22, true
br label %24
24: ; preds = %20, %3
%25 = phi i1 [ true, %3 ], [ %23, %20 ]
%26 = zext i1 %25 to i32
%27 = call i32 (i32, i8*, ...) @fail(i32 %26, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%28 = load i32, i32* %4
%29 = load i8*, i8** %7
%30 = load i8*, i8** %7
%31 = call i32 @strlen(i8* %30)
%32 = call i32 @send(i32 %28, i8* %29, i32 %31, i32 0)
store i32 %32, i32* %9
%33 = load i32, i32* %9
%34 = icmp eq i32 %33, -1
%35 = zext i1 %34 to i32
%36 = load i32, i32* @errno
%37 = call i32 @strerror(i32 %36)
%38 = call i32 (i32, i8*, ...) @fail(i32 %35, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %37)
br label %39
39: ; preds = %24, %46
%40 = load i32, i32* %4
%41 = getelementptr inbounds [4106 x i8], [4106 x i8]* %11, i64 0, i64 0
%42 = call i32 @recvfrom(i32 %40, i8* %41, i32 4096, i32 0, i32 0, i32 0)
store i32 %42, i32* %10
%43 = load i32, i32* %10
%44 = icmp eq i32 %43, 0
br i1 %44, label %45, label %46
45: ; preds = %39
br label %58
46: ; preds = %39
%47 = load i32, i32* %10
%48 = icmp eq i32 %47, -1
%49 = zext i1 %48 to i32
%50 = load i32, i32* @errno
%51 = call i32 @strerror(i32 %50)
%52 = call i32 (i32, i8*, ...) @fail(i32 %49, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %51)
%53 = load i32, i32* %10
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds [4106 x i8], [4106 x i8]* %11, i64 0, i64 %54
store i8 0, i8* %55
%56 = getelementptr inbounds [4106 x i8], [4106 x i8]* %11, i64 0, i64 0
%57 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %56)
br label %39
58: ; preds = %45
%59 = load i8*, i8** %7
%60 = call i32 @free(i8* %59)
ret void
}

LLVM IR (-O2):
@.str = [51 x i8] c"GET %s HTTP/1.0\0D\0AHost: %s\0D\0AUser-Agent: fetch.c\0D\0A\0D\0A\00"
@.str.1 = [4 x i8] c"%s\0A\00"
@.str.2 = [18 x i8] c"asprintf failed.\0A\00"
@.str.3 = [17 x i8] c"send failed: %s\0A\00"
@errno = global i32 0
@.str.4 = [3 x i8] c"%s\00"
define void @get_page(i32 %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca [4106 x i8]
%6 = bitcast i8** %4 to i8*
%7 = call i32 @asprintf(i8** %4, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0), i8* %2, i8* %1)
%8 = load i8*, i8** %4
%9 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %8)
%10 = icmp eq i32 %7, -1
%11 = load i8*, i8** %4
%12 = icmp eq i8* %11, null
%13 = select i1 %10, i1 true, i1 %12
%14 = zext i1 %13 to i32
%15 = call i32 (i32, i8*, ...) @fail(i32 %14, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%16 = load i8*, i8** %4
%17 = call i32 @strlen(i8* %16)
%18 = call i32 @send(i32 %0, i8* %16, i32 %17, i32 0)
%19 = icmp eq i32 %18, -1
%20 = zext i1 %19 to i32
%21 = load i32, i32* @errno
%22 = call i32 @strerror(i32 %21)
%23 = call i32 (i32, i8*, ...) @fail(i32 %20, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %22)
%24 = getelementptr inbounds [4106 x i8], [4106 x i8]* %5, i64 0, i64 0
%25 = call i32 @recvfrom(i32 %0, i8* %24, i32 4096, i32 0, i32 0, i32 0)
%26 = icmp eq i32 %25, 0
br i1 %26, label %39, label %27
27: ; preds = %3, %27
%28 = phi i32 [ %37, %27 ], [ %25, %3 ]
%29 = icmp eq i32 %28, -1
%30 = zext i1 %29 to i32
%31 = load i32, i32* @errno
%32 = call i32 @strerror(i32 %31)
%33 = call i32 (i32, i8*, ...) @fail(i32 %30, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %32)
%34 = sext i32 %28 to i64
%35 = getelementptr inbounds [4106 x i8], [4106 x i8]* %5, i64 0, i64 %34
store i8 0, i8* %35
%36 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %24)
%37 = call i32 @recvfrom(i32 %0, i8* %24, i32 4096, i32 0, i32 0, i32 0)
%38 = icmp eq i32 %37, 0
br i1 %38, label %39, label %27
39: ; preds = %27, %3
%40 = load i8*, i8** %4
%41 = call i32 @free(i8* %40)
ret void
}