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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, char *);
int open_tcp(char *, int);
int /*<<< orphan*/ printf(char *, char *);
char *strerror(int /*<<< orphan*/);
int strlen(char *);
FILE *fopen_http(char *server, char *path) {
  int fd;
  FILE *stream;
  char buffer[80];
  fd = open_tcp(server, 80);
  stream = fdopen(fd, "r+");
  if (!stream) {
    printf("FAILURE: fdopen(): %s\n", strerror(errno));
    close(fd);
    exit(EXIT_FAILURE);
  }
  fprintf(stream, "GET %s HTTP/1.0\n\n", path);
  fflush(stream);
  while (fgets(buffer, 80, stream)) {
    if (strlen(buffer) == 2) {
      return stream;
    }
  }
  fclose(stream);
  return NULL;
}


x86 assembly (clang, -O0):
<fopen_http>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x50,%esi
call   1030 <open_tcp@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
lea    0xe22(%rip),%rsi
call   10b0 <fdopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1225 <fopen_http+0x75>
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    %rax,%rsi
lea    0xdf9(%rip),%rdi
call   1060 <printf@plt>
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
mov    0x2dba(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xde6(%rip),%rsi
call   1090 <fprintf@plt>
mov    -0x28(%rbp),%rdi
call   10a0 <fflush@plt>
lea    -0x80(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    $0x50,%esi
call   1080 <fgets@plt>
cmp    $0x0,%rax
je     1282 <fopen_http+0xd2>
lea    -0x80(%rbp),%rdi
call   1050 <strlen@plt>
cmp    $0x2,%eax
jne    127d <fopen_http+0xcd>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1293 <fopen_http+0xe3>
jmp    1242 <fopen_http+0x92>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fopen_http>:
push   %rbp
push   %r14
push   %rbx
sub    $0x50,%rsp
mov    %rsi,%rbp
mov    $0x50,%esi
call   1030 <open_tcp@plt>
mov    %eax,%r14d
lea    0xe31(%rip),%rsi
mov    %eax,%edi
call   10b0 <fdopen@plt>
test   %rax,%rax
je     123a <fopen_http+0x8a>
mov    %rax,%rbx
lea    0xe35(%rip),%rsi
mov    %rax,%rdi
mov    %rbp,%rdx
call   1090 <fprintf@plt>
mov    %rbx,%rdi
call   10a0 <fflush@plt>
mov    %rsp,%rbp
nopl   0x0(%rax,%rax,1)
mov    %rbp,%rdi
mov    $0x50,%esi
mov    %rbx,%rdx
call   1080 <fgets@plt>
test   %rax,%rax
je     1224 <fopen_http+0x74>
mov    %rbp,%rdi
call   1050 <strlen@plt>
cmp    $0x2,%eax
jne    1200 <fopen_http+0x50>
jmp    122e <fopen_http+0x7e>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x50,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2d9f(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xdb4(%rip),%rdi
mov    %rax,%rsi
call   1060 <printf@plt>
mov    %r14d,%edi
call   1070 <close@plt>
mov    0x2d72(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"r+\00"
@.str.1 = [23 x i8] c"FAILURE: fdopen(): %s\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.2 = [18 x i8] c"GET %s HTTP/1.0\0A\0A\00"
define i32* @fopen_http(i8* %0, i8* %1) {
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca [80 x i8]
store i8* %0, i8** %4
store i8* %1, i8** %5
%9 = load i8*, i8** %4
%10 = call i32 @open_tcp(i8* %9, i32 80)
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = call i32* @fdopen(i32 %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %7
%13 = load i32*, i32** %7
%14 = icmp ne i32* %13, null
br i1 %14, label %23, label %15
15: ; preds = %2
%16 = load i32, i32* @errno
%17 = call i8* @strerror(i32 %16)
%18 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %17)
%19 = load i32, i32* %6
%20 = call i32 @close(i32 %19)
%21 = load i32, i32* @EXIT_FAILURE
%22 = call i32 @exit(i32 %21)
unreachable
23: ; preds = %2
%24 = load i32*, i32** %7
%25 = load i8*, i8** %5
%26 = call i32 @fprintf(i32* %24, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %25)
%27 = load i32*, i32** %7
%28 = call i32 @fflush(i32* %27)
br label %29
29: ; preds = %40, %23
%30 = getelementptr inbounds [80 x i8], [80 x i8]* %8, i64 0, i64 0
%31 = load i32*, i32** %7
%32 = call i64 @fgets(i8* %30, i32 80, i32* %31)
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %41
34: ; preds = %29
%35 = getelementptr inbounds [80 x i8], [80 x i8]* %8, i64 0, i64 0
%36 = call i32 @strlen(i8* %35)
%37 = icmp eq i32 %36, 2
br i1 %37, label %38, label %40
38: ; preds = %34
%39 = load i32*, i32** %7
store i32* %39, i32** %3
br label %44
40: ; preds = %34
br label %29
41: ; preds = %29
%42 = load i32*, i32** %7
%43 = call i32 @fclose(i32* %42)
store i32* null, i32** %3
br label %44
44: ; preds = %41, %38
%45 = load i32*, i32** %3
ret i32* %45
}

LLVM IR (-O2):
@.str = [3 x i8] c"r+\00"
@.str.1 = [23 x i8] c"FAILURE: fdopen(): %s\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.2 = [18 x i8] c"GET %s HTTP/1.0\0A\0A\00"
define noalias i32* @fopen_http(i8* %0, i8* %1) {
%3 = alloca [80 x i8]
%4 = getelementptr inbounds [80 x i8], [80 x i8]* %3, i64 0, i64 0
%5 = tail call i32 @open_tcp(i8* %0, i32 80)
%6 = tail call i32* @fdopen(i32 %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %15
8: ; preds = %2
%9 = load i32, i32* @errno
%10 = tail call i8* @strerror(i32 %9)
%11 = tail call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %10)
%12 = tail call i32 @close(i32 %5)
%13 = load i32, i32* @EXIT_FAILURE
%14 = tail call i32 @exit(i32 %13)
unreachable
15: ; preds = %2
%16 = tail call i32 @fprintf(i32* %6, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %1)
%17 = tail call i32 @fflush(i32* %6)
br label %18
18: ; preds = %21, %15
%19 = call i64 @fgets(i8* %4, i32 80, i32* %6)
%20 = icmp eq i64 %19, 0
br i1 %20, label %24, label %21
21: ; preds = %18
%22 = call i32 @strlen(i8* %4)
%23 = icmp eq i32 %22, 2
br i1 %23, label %26, label %18
24: ; preds = %18
%25 = call i32 @fclose(i32* %6)
br label %26
26: ; preds = %21, %24
%27 = phi i32* [ null, %24 ], [ %6, %21 ]
ret i32* %27
}