#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 */
char *calloc(int, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(char *);
char *generate_header(char *, char *);
int get_socket(int, char *);
int /*<<< orphan*/ hostname_to_ip(char *, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ shutdown(int, int /*<<< orphan*/);
int socket_gets(int, char *, int);
int /*<<< orphan*/ strlen(char *);
scalar_t__ write(int, char *, int /*<<< orphan*/);
int stream(char *url, void (*each_line)(char *, int, int)) {
  char ip[17] = {0};
  char *bufferline, *header;
  int sockfd, ctr;
  if (!hostname_to_ip("www.ncbi.nlm.nih.gov", ip)) {
    return 400;
  }
  bufferline = calloc(sizeof(char), 2048);
  sockfd = get_socket(80, ip);
  if (sockfd < 0) {
    perror("error, could not open socket");
    free(bufferline);
    return 0;
  }
  header = generate_header(url, "www.ncbi.nlm.nih.gov");
  if (write(sockfd, header, strlen(header)) < 0) {
    shutdown(sockfd, 0);
    free(bufferline);
    free(header);
    close(sockfd);
    return 400;
  }
  while ((ctr = socket_gets(sockfd, bufferline, 2048))) {
    each_line(bufferline, ctr, 2048);
  }
  free(header);
  shutdown(sockfd, 0);
  close(sockfd);
  free(bufferline);
  return 1;
}


x86 assembly (clang, -O0):
<stream>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x11,%edx
call   1090 <memset@plt>
lea    -0x30(%rbp),%rsi
lea    0xe15(%rip),%rdi
call   10c0 <hostname_to_ip@plt>
cmp    $0x0,%eax
jne    1205 <stream+0x45>
movl   $0x190,-0x4(%rbp)
jmp    132b <stream+0x16b>
mov    $0x1,%edi
mov    $0x800,%esi
call   10b0 <calloc@plt>
mov    %rax,-0x38(%rbp)
lea    -0x30(%rbp),%rsi
mov    $0x50,%edi
call   1040 <get_socket@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jge    1254 <stream+0x94>
lea    0xddb(%rip),%rdi
call   10d0 <perror@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
jmp    132b <stream+0x16b>
mov    -0x10(%rbp),%rdi
lea    0xda1(%rip),%rsi
call   1050 <generate_header@plt>
mov    %rax,-0x40(%rbp)
mov    -0x44(%rbp),%eax
mov    %eax,-0x54(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x40(%rbp),%rdi
call   1080 <strlen@plt>
mov    -0x54(%rbp),%edi
mov    -0x50(%rbp),%rsi
mov    %eax,%edx
call   1060 <write@plt>
cmp    $0x0,%rax
jge    12c7 <stream+0x107>
mov    -0x44(%rbp),%edi
xor    %esi,%esi
call   1070 <shutdown@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
mov    -0x44(%rbp),%edi
call   10a0 <close@plt>
movl   $0x190,-0x4(%rbp)
jmp    132b <stream+0x16b>
jmp    12cc <stream+0x10c>
mov    -0x44(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    $0x800,%edx
call   10e0 <socket_gets@plt>
mov    %eax,-0x48(%rbp)
cmp    $0x0,%eax
je     1300 <stream+0x140>
mov    -0x18(%rbp),%rax
mov    -0x38(%rbp),%rdi
mov    -0x48(%rbp),%esi
mov    $0x800,%edx
call   *%rax
jmp    12cc <stream+0x10c>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
mov    -0x44(%rbp),%edi
xor    %esi,%esi
call   1070 <shutdown@plt>
mov    -0x44(%rbp),%edi
call   10a0 <close@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<stream>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%r14
mov    %rdi,%r12
xorps  %xmm0,%xmm0
movaps %xmm0,(%rsp)
movb   $0x0,0x10(%rsp)
lea    0xe2b(%rip),%rdi
mov    %rsp,%rsi
call   10b0 <hostname_to_ip@plt>
mov    $0x190,%r15d
test   %eax,%eax
je     12b8 <stream+0x108>
mov    $0x1,%edi
mov    $0x800,%esi
call   10a0 <calloc@plt>
mov    %rax,%rbx
mov    %rsp,%rsi
mov    $0x50,%edi
call   1040 <get_socket@plt>
test   %eax,%eax
js     12c8 <stream+0x118>
mov    %eax,%ebp
lea    0xde5(%rip),%rsi
mov    %r12,%rdi
call   1050 <generate_header@plt>
mov    %rax,%r12
mov    %rax,%rdi
call   1080 <strlen@plt>
mov    %ebp,%edi
mov    %r12,%rsi
mov    %eax,%edx
call   1060 <write@plt>
mov    %ebp,%edi
test   %rax,%rax
js     129a <stream+0xea>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rsi
mov    $0x800,%edx
call   10d0 <socket_gets@plt>
test   %eax,%eax
je     1272 <stream+0xc2>
mov    %rbx,%rdi
mov    %eax,%esi
mov    $0x800,%edx
call   *%r14
mov    %ebp,%edi
jmp    1250 <stream+0xa0>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %ebp,%edi
xor    %esi,%esi
call   1070 <shutdown@plt>
mov    %ebp,%edi
call   1090 <close@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    $0x1,%r15d
jmp    12b8 <stream+0x108>
xor    %esi,%esi
call   1070 <shutdown@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %ebp,%edi
call   1090 <close@plt>
mov    %r15d,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd46(%rip),%rdi
call   10c0 <perror@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %r15d,%r15d
jmp    12b8 <stream+0x108>

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

LLVM IR (-O2):
@.str = [21 x i8] c"www.ncbi.nlm.nih.gov\00"
@.str.1 = [29 x i8] c"error, could not open socket\00"
define i32 @stream(i8* %0, void (i8*, i32, i32)* nocapture %1) {
%3 = alloca [17 x i8]
%4 = getelementptr inbounds [17 x i8], [17 x i8]* %3, i64 0, i64 0
%5 = call i32 @hostname_to_ip(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %4)
%6 = icmp eq i32 %5, 0
br i1 %6, label %36, label %7
7: ; preds = %2
%8 = call dereferenceable_or_null(2048) i8* @calloc(i32 1, i32 2048)
%9 = call i32 @get_socket(i32 80, i8* %4)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %7
%12 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%13 = call i32 @free(i8* %8)
br label %36
14: ; preds = %7
%15 = call i8* @generate_header(i8* %0, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @strlen(i8* %15)
%17 = call i64 @write(i32 %9, i8* %15, i32 %16)
%18 = icmp slt i64 %17, 0
br i1 %18, label %22, label %19
19: ; preds = %14
%20 = call i32 @socket_gets(i32 %9, i8* %8, i32 2048)
%21 = icmp eq i32 %20, 0
br i1 %21, label %31, label %27
22: ; preds = %14
%23 = call i32 @shutdown(i32 %9, i32 0)
%24 = call i32 @free(i8* %8)
%25 = call i32 @free(i8* %15)
%26 = call i32 @close(i32 %9)
br label %36
27: ; preds = %19, %27
%28 = phi i32 [ %29, %27 ], [ %20, %19 ]
%29 = call i32 @socket_gets(i32 %9, i8* %8, i32 2048)
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %27
31: ; preds = %27, %19
%32 = call i32 @free(i8* %15)
%33 = call i32 @shutdown(i32 %9, i32 0)
%34 = call i32 @close(i32 %9)
%35 = call i32 @free(i8* %8)
br label %36
36: ; preds = %2, %31, %22, %11
%37 = phi i32 [ 0, %11 ], [ 400, %22 ], [ 1, %31 ], [ 400, %2 ]
ret i32 %37
}