#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 */
typedef int /*<<< orphan*/ BIO;

/* Variables and functions */
scalar_t__ BIO_do_connect(int /*<<< orphan*/ *);
int /*<<< orphan*/ BIO_free_all(int /*<<< orphan*/ *);
int /*<<< orphan*/ *BIO_new_connect(char *);
int BIO_read(int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ BIO_write(int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ strcat(char *, char *);
int strlen(char *);
int main(void) {
  BIO *bio = NULL;
  char request_str[1024] = {0};
  int request_len = 0;
  char response_buf[1024] = {0};
  int response_len = 0;
  bio = BIO_new_connect("bgstation0.com:80");
  if (bio == NULL) {
    printf("BIO_new_connect error!\n");
    return -1;
  }
  if (BIO_do_connect(bio) <= 0) {
    printf("BIO_do_connect error!\n");
    BIO_free_all(bio);
    return -2;
  }
  strcat(request_str, "GET /index.html HTTP/1.1\r\n");
  strcat(request_str, "Host: bgstation0.com\r\n");
  strcat(request_str, "Connection: Close\r\n\r\n");
  request_len = strlen(request_str);
  BIO_write(bio, request_str, request_len);
  while ((response_len = BIO_read(bio, response_buf, 1024 - 1)) > 0) {
    printf("%s", response_buf);
    memset(response_buf, 0, sizeof(char) * 1024);
  }
  BIO_free_all(bio);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x830,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x0,-0x10(%rbp)
lea    -0x410(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
movl   $0x0,-0x414(%rbp)
lea    -0x820(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
movl   $0x0,-0x824(%rbp)
lea    0xe15(%rip),%rdi
call   1080 <BIO_new_connect@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1219 <main+0x89>
lea    0xe0c(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1311 <main+0x181>
mov    -0x10(%rbp),%rdi
call   1030 <BIO_do_connect@plt>
cmp    $0x0,%rax
jg     124f <main+0xbf>
lea    0xdf7(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1090 <BIO_free_all@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1311 <main+0x181>
lea    -0x410(%rbp),%rdi
lea    0xde4(%rip),%rsi
call   10a0 <strcat@plt>
lea    -0x410(%rbp),%rdi
lea    0xdec(%rip),%rsi
call   10a0 <strcat@plt>
lea    -0x410(%rbp),%rdi
lea    0xdf0(%rip),%rsi
call   10a0 <strcat@plt>
lea    -0x410(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x414(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x410(%rbp),%rsi
mov    -0x414(%rbp),%edx
call   10b0 <BIO_write@plt>
mov    -0x10(%rbp),%rdi
lea    -0x820(%rbp),%rsi
mov    $0x3ff,%edx
call   1040 <BIO_read@plt>
mov    %eax,-0x824(%rbp)
cmp    $0x0,%eax
jle    1301 <main+0x171>
lea    -0x820(%rbp),%rsi
lea    0xda7(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x820(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
jmp    12b0 <main+0x120>
mov    -0x10(%rbp),%rdi
call   1090 <BIO_free_all@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x830,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x800,%rsp
lea    0x400(%rsp),%rdi
mov    $0x400,%edx
xor    %esi,%esi
call   1080 <memset@plt>
mov    %rsp,%rdi
mov    $0x400,%edx
xor    %esi,%esi
call   1080 <memset@plt>
lea    0xe2a(%rip),%rdi
call   1090 <BIO_new_connect@plt>
test   %rax,%rax
je     129c <main+0xfc>
mov    %rax,%r15
mov    %rax,%rdi
call   1030 <BIO_do_connect@plt>
test   %rax,%rax
jle    12af <main+0x10f>
lea    0xe13(%rip),%rsi
lea    0x400(%rsp),%rbx
mov    %rbx,%rdi
call   10b0 <strcat@plt>
lea    0xe17(%rip),%rsi
mov    %rbx,%rdi
call   10b0 <strcat@plt>
lea    0xe1f(%rip),%rsi
mov    %rbx,%rdi
call   10b0 <strcat@plt>
mov    %rbx,%rdi
call   1060 <strlen@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   10c0 <BIO_write@plt>
mov    %rsp,%rsi
mov    %r15,%rdi
mov    $0x3ff,%edx
call   1050 <BIO_read@plt>
test   %eax,%eax
jle    1290 <main+0xf0>
lea    0xdfd(%rip),%r14
mov    %rsp,%rbx
mov    %r14,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1080 <memset@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    $0x3ff,%edx
call   1050 <BIO_read@plt>
test   %eax,%eax
jg     1260 <main+0xc0>
mov    %r15,%rdi
call   10a0 <BIO_free_all@plt>
xor    %eax,%eax
jmp    12c8 <main+0x128>
lea    0xdd0(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%eax
jmp    12c8 <main+0x128>
lea    0xda7(%rip),%rdi
call   1040 <puts@plt>
mov    %r15,%rdi
call   10a0 <BIO_free_all@plt>
mov    $0xfffffffe,%eax
add    $0x800,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [18 x i8] c"bgstation0.com:80\00"
@.str.1 = [24 x i8] c"BIO_new_connect error!\0A\00"
@.str.2 = [23 x i8] c"BIO_do_connect error!\0A\00"
@.str.3 = [27 x i8] c"GET /index.html HTTP/1.1\0D\0A\00"
@.str.4 = [23 x i8] c"Host: bgstation0.com\0D\0A\00"
@.str.5 = [22 x i8] c"Connection: Close\0D\0A\0D\0A\00"
@.str.6 = [3 x i8] c"%s\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [1024 x i8]
%4 = alloca i32
%5 = alloca [1024 x i8]
%6 = alloca i32
store i32 0, i32* %1
store i32* null, i32** %2
%7 = bitcast [1024 x i8]* %3 to i8*
store i32 0, i32* %4
%8 = bitcast [1024 x i8]* %5 to i8*
store i32 0, i32* %6
%9 = call i32* @BIO_new_connect(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %2
%10 = load i32*, i32** %2
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %14
12: ; preds = %0
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %48
14: ; preds = %0
%15 = load i32*, i32** %2
%16 = call i64 @BIO_do_connect(i32* %15)
%17 = icmp sle i64 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %14
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
%20 = load i32*, i32** %2
%21 = call i32 @BIO_free_all(i32* %20)
store i32 -2, i32* %1
br label %48
22: ; preds = %14
%23 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%24 = call i32 @strcat(i8* %23, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
%25 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%26 = call i32 @strcat(i8* %25, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
%27 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%28 = call i32 @strcat(i8* %27, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0))
%29 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%30 = call i32 @strlen(i8* %29)
store i32 %30, i32* %4
%31 = load i32*, i32** %2
%32 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%33 = load i32, i32* %4
%34 = call i32 @BIO_write(i32* %31, i8* %32, i32 %33)
br label %35
35: ; preds = %40, %22
%36 = load i32*, i32** %2
%37 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%38 = call i32 @BIO_read(i32* %36, i8* %37, i32 1023)
store i32 %38, i32* %6
%39 = icmp sgt i32 %38, 0
br i1 %39, label %40, label %45
40: ; preds = %35
%41 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i8* %41)
%43 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%44 = call i32 @memset(i8* %43, i32 0, i32 1024)
br label %35
45: ; preds = %35
%46 = load i32*, i32** %2
%47 = call i32 @BIO_free_all(i32* %46)
store i32 0, i32* %1
br label %48
48: ; preds = %45, %18, %12
%49 = load i32, i32* %1
ret i32 %49
}

LLVM IR (-O2):
@.str = [18 x i8] c"bgstation0.com:80\00"
@.str.3 = [27 x i8] c"GET /index.html HTTP/1.1\0D\0A\00"
@.str.4 = [23 x i8] c"Host: bgstation0.com\0D\0A\00"
@.str.5 = [22 x i8] c"Connection: Close\0D\0A\0D\0A\00"
@.str.6 = [3 x i8] c"%s\00"
@str = [22 x i8] c"BIO_do_connect error!\00"
@str.7 = [23 x i8] c"BIO_new_connect error!\00"
define i32 @main() {
%1 = alloca [1024 x i8]
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%5 = tail call i32* @BIO_new_connect(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.7, i64 0, i64 0))
br label %30
9: ; preds = %0
%10 = tail call i64 @BIO_do_connect(i32* %5)
%11 = icmp slt i64 %10, 1
br i1 %11, label %12, label %15
12: ; preds = %9
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
%14 = tail call i32 @BIO_free_all(i32* %5)
br label %30
15: ; preds = %9
%16 = call i32 @strcat(i8* %3, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
%17 = call i32 @strcat(i8* %3, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
%18 = call i32 @strcat(i8* %3, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0))
%19 = call i32 @strlen(i8* %3)
%20 = call i32 @BIO_write(i32* %5, i8* %3, i32 %19)
%21 = call i32 @BIO_read(i32* %5, i8* %4, i32 1023)
%22 = icmp sgt i32 %21, 0
br i1 %22, label %23, label %28
23: ; preds = %15, %23
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i8* %4)
%25 = call i32 @memset(i8* %4, i32 0, i32 1024)
%26 = call i32 @BIO_read(i32* %5, i8* %4, i32 1023)
%27 = icmp sgt i32 %26, 0
br i1 %27, label %23, label %28
28: ; preds = %23, %15
%29 = call i32 @BIO_free_all(i32* %5)
br label %30
30: ; preds = %28, %12, %7
%31 = phi i32 [ -1, %7 ], [ -2, %12 ], [ 0, %28 ]
ret i32 %31
}