#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 /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(char *);
char *generate_header(char *, char *);
int get_socket(int, char *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int /*<<< orphan*/ shutdown(int, int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char *);
int write(int, char *, int /*<<< orphan*/);
int url_to_memory(char *buffer, int buf_size, char *url, char *host, char *ip) {
  int n = 0;
  memset(buffer, 0, buf_size);
  int sockfd = get_socket(80, ip);
  if (sockfd < 0) {
    perror("error, could not open socket");
    return 0;
  }
  char *header = generate_header(url, host);
  n = write(sockfd, header, strlen(header));
  if (n < 0) {
    perror("error, could not write to socket");
    shutdown(sockfd, 0);
    close(sockfd);
    return 0;
  }
  do {
    n = read(sockfd, buffer, buf_size - 1);
    buffer[n] = 0;
  } while (n == buf_size);
  free(header);
  shutdown(sockfd, 0);
  close(sockfd);
  return 1;
}


x86 assembly (clang, -O0):
<url_to_memory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
xor    %esi,%esi
call   1090 <memset@plt>
mov    -0x30(%rbp),%rsi
mov    $0x50,%edi
call   1040 <get_socket@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
jge    1203 <url_to_memory+0x63>
lea    0xe0e(%rip),%rdi
call   10c0 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    12c4 <url_to_memory+0x124>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1050 <generate_header@plt>
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x40(%rbp),%rdi
call   1080 <strlen@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rsi
mov    %eax,%edx
call   1060 <write@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    1270 <url_to_memory+0xd0>
lea    0xdd0(%rip),%rdi
call   10c0 <perror@plt>
mov    -0x38(%rbp),%edi
xor    %esi,%esi
call   1070 <shutdown@plt>
mov    -0x38(%rbp),%edi
call   10a0 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    12c4 <url_to_memory+0x124>
jmp    1275 <url_to_memory+0xd5>
mov    -0x38(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
sub    $0x1,%edx
call   10b0 <read@plt>
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x34(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x34(%rbp),%eax
cmp    -0x14(%rbp),%eax
je     1275 <url_to_memory+0xd5>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%edi
xor    %esi,%esi
call   1070 <shutdown@plt>
mov    -0x38(%rbp),%edi
call   10a0 <close@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<url_to_memory>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8,%rbx
mov    %rcx,(%rsp)
mov    %rdx,%r13
mov    %esi,%r12d
mov    %rdi,%rbp
xor    %r15d,%r15d
xor    %esi,%esi
mov    %r12d,%edx
call   1090 <memset@plt>
mov    $0x50,%edi
mov    %rbx,%rsi
call   1040 <get_socket@plt>
test   %eax,%eax
js     125b <url_to_memory+0xbb>
mov    %eax,%r14d
mov    %r13,%rdi
mov    (%rsp),%rsi
call   1050 <generate_header@plt>
mov    %rax,%r15
mov    %rax,%rdi
call   1080 <strlen@plt>
mov    %r14d,%edi
mov    %r15,%rsi
mov    %eax,%edx
call   1060 <write@plt>
test   %eax,%eax
js     1269 <url_to_memory+0xc9>
lea    -0x1(%r12),%ebx
nopl   (%rax)
mov    %r14d,%edi
mov    %rbp,%rsi
mov    %ebx,%edx
call   10b0 <read@plt>
cltq
movb   $0x0,0x0(%rbp,%rax,1)
cmp    %r12d,%eax
je     1210 <url_to_memory+0x70>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %r14d,%edi
xor    %esi,%esi
call   1070 <shutdown@plt>
mov    %r14d,%edi
call   10a0 <close@plt>
mov    $0x1,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd9e(%rip),%rdi
call   10c0 <perror@plt>
jmp    1249 <url_to_memory+0xa9>
lea    0xdad(%rip),%rdi
call   10c0 <perror@plt>
xor    %r15d,%r15d
mov    %r14d,%edi
xor    %esi,%esi
call   1070 <shutdown@plt>
mov    %r14d,%edi
call   10a0 <close@plt>
jmp    1249 <url_to_memory+0xa9>

LLVM IR (-O0):
@.str = [29 x i8] c"error, could not open socket\00"
@.str.1 = [33 x i8] c"error, could not write to socket\00"
define i32 @url_to_memory(i8* %0, i32 %1, i8* %2, i8* %3, i8* %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i32
%13 = alloca i32
%14 = alloca i8*
store i8* %0, i8** %7
store i32 %1, i32* %8
store i8* %2, i8** %9
store i8* %3, i8** %10
store i8* %4, i8** %11
store i32 0, i32* %12
%15 = load i8*, i8** %7
%16 = load i32, i32* %8
%17 = call i32 @memset(i8* %15, i32 0, i32 %16)
%18 = load i8*, i8** %11
%19 = call i32 @get_socket(i32 80, i8* %18)
store i32 %19, i32* %13
%20 = load i32, i32* %13
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %5
%23 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %6
br label %63
24: ; preds = %5
%25 = load i8*, i8** %9
%26 = load i8*, i8** %10
%27 = call i8* @generate_header(i8* %25, i8* %26)
store i8* %27, i8** %14
%28 = load i32, i32* %13
%29 = load i8*, i8** %14
%30 = load i8*, i8** %14
%31 = call i32 @strlen(i8* %30)
%32 = call i32 @write(i32 %28, i8* %29, i32 %31)
store i32 %32, i32* %12
%33 = load i32, i32* %12
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %41
35: ; preds = %24
%36 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
%37 = load i32, i32* %13
%38 = call i32 @shutdown(i32 %37, i32 0)
%39 = load i32, i32* %13
%40 = call i32 @close(i32 %39)
store i32 0, i32* %6
br label %63
41: ; preds = %24
br label %42
42: ; preds = %52, %41
%43 = load i32, i32* %13
%44 = load i8*, i8** %7
%45 = load i32, i32* %8
%46 = sub nsw i32 %45, 1
%47 = call i32 @read(i32 %43, i8* %44, i32 %46)
store i32 %47, i32* %12
%48 = load i8*, i8** %7
%49 = load i32, i32* %12
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i8, i8* %48, i64 %50
store i8 0, i8* %51
br label %52
52: ; preds = %42
%53 = load i32, i32* %12
%54 = load i32, i32* %8
%55 = icmp eq i32 %53, %54
br i1 %55, label %42, label %56
56: ; preds = %52
%57 = load i8*, i8** %14
%58 = call i32 @free(i8* %57)
%59 = load i32, i32* %13
%60 = call i32 @shutdown(i32 %59, i32 0)
%61 = load i32, i32* %13
%62 = call i32 @close(i32 %61)
store i32 1, i32* %6
br label %63
63: ; preds = %56, %35, %22
%64 = load i32, i32* %6
ret i32 %64
}

LLVM IR (-O2):
@.str = [29 x i8] c"error, could not open socket\00"
@.str.1 = [33 x i8] c"error, could not write to socket\00"
define i32 @url_to_memory(i8* %0, i32 %1, i8* %2, i8* %3, i8* %4) {
%6 = tail call i32 @memset(i8* %0, i32 0, i32 %1)
%7 = tail call i32 @get_socket(i32 80, i8* %4)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %5
%10 = tail call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
br label %31
11: ; preds = %5
%12 = tail call i8* @generate_header(i8* %2, i8* %3)
%13 = tail call i32 @strlen(i8* %12)
%14 = tail call i32 @write(i32 %7, i8* %12, i32 %13)
%15 = icmp slt i32 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %11
%17 = add nsw i32 %1, -1
br label %22
18: ; preds = %11
%19 = tail call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
%20 = tail call i32 @shutdown(i32 %7, i32 0)
%21 = tail call i32 @close(i32 %7)
br label %31
22: ; preds = %16, %22
%23 = tail call i32 @read(i32 %7, i8* %0, i32 %17)
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %0, i64 %24
store i8 0, i8* %25
%26 = icmp eq i32 %23, %1
br i1 %26, label %22, label %27
27: ; preds = %22
%28 = tail call i32 @free(i8* %12)
%29 = tail call i32 @shutdown(i32 %7, i32 0)
%30 = tail call i32 @close(i32 %7)
br label %31
31: ; preds = %18, %27, %9
%32 = phi i32 [ 0, %9 ], [ 0, %18 ], [ 1, %27 ]
ret i32 %32
}