#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*/ time_t;
struct link {
  int dummy;
};
typedef int int64_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
struct link *http_query_size(char const *, char *, int *, int /*<<< orphan*/,
                             int);
int /*<<< orphan*/ link_close(struct link *);
int link_stream_to_file(struct link *, int /*<<< orphan*/ *, int,
                        int /*<<< orphan*/);
int /*<<< orphan*/ unlink(char const *);
int64_t http_fetch_to_file(const char *url, const char *filename,
                           time_t stoptime) {
  FILE *file;
  int64_t size;
  int64_t actual;
  struct link *link;
  file = fopen(filename, "w");
  if (file) {
    link = http_query_size(url, "GET", &size, stoptime, 1);
    if (link) {
      actual = link_stream_to_file(link, file, size, stoptime);
      link_close(link);
      fclose(file);
      if (actual == size) {
        return actual;
      } else {
        unlink(filename);
        return -1;
      }
    } else {
      fclose(file);
      return -1;
    }
  } else {
    return -1;
  }
}


x86 assembly (clang, -O0):
<http_fetch_to_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe82(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1227 <http_fetch_to_file+0xc7>
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%ecx
lea    0xe62(%rip),%rsi
lea    -0x2c(%rbp),%rdx
mov    $0x1,%r8d
call   1060 <http_query_size@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     1212 <http_fetch_to_file+0xb2>
mov    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x2c(%rbp),%edx
mov    -0x1c(%rbp),%ecx
call   1050 <link_stream_to_file@plt>
mov    %eax,-0x30(%rbp)
mov    -0x38(%rbp),%rdi
call   1070 <link_close@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x30(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jne    11fd <http_fetch_to_file+0x9d>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    122e <http_fetch_to_file+0xce>
mov    -0x18(%rbp),%rdi
call   1030 <unlink@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    122e <http_fetch_to_file+0xce>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    122e <http_fetch_to_file+0xce>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<http_fetch_to_file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edx,%r15d
mov    %rsi,%r14
mov    %rdi,%rbp
lea    0xe84(%rip),%rsi
mov    %r14,%rdi
call   1080 <fopen@plt>
mov    $0xffffffff,%r12d
test   %rax,%rax
je     11fb <http_fetch_to_file+0x9b>
mov    %rax,%rbx
lea    0xe69(%rip),%rsi
lea    0xc(%rsp),%rdx
mov    %rbp,%rdi
mov    %r15d,%ecx
mov    $0x1,%r8d
call   1060 <http_query_size@plt>
test   %rax,%rax
je     11f3 <http_fetch_to_file+0x93>
mov    %rax,%rbp
mov    0xc(%rsp),%edx
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %r15d,%ecx
call   1050 <link_stream_to_file@plt>
mov    %eax,%r12d
mov    %rbp,%rdi
call   1070 <link_close@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
cmp    0xc(%rsp),%r12d
je     11fb <http_fetch_to_file+0x9b>
mov    %r14,%rdi
call   1030 <unlink@plt>
mov    $0xffffffff,%r12d
jmp    11fb <http_fetch_to_file+0x9b>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r12d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.link = type { i32 }
@.str = [2 x i8] c"w\00"
@.str.1 = [4 x i8] c"GET\00"
define i32 @http_fetch_to_file(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
%11 = alloca %struct.link*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%12 = load i8*, i8** %6
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %13, i32** %8
%14 = load i32*, i32** %8
%15 = icmp ne i32* %14, null
br i1 %15, label %16, label %43
16: ; preds = %3
%17 = load i8*, i8** %5
%18 = load i32, i32* %7
%19 = call %struct.link* @http_query_size(i8* %17, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32* %9, i32 %18, i32 1)
store %struct.link* %19, %struct.link** %11
%20 = load %struct.link*, %struct.link** %11
%21 = icmp ne %struct.link* %20, null
br i1 %21, label %22, label %40
22: ; preds = %16
%23 = load %struct.link*, %struct.link** %11
%24 = load i32*, i32** %8
%25 = load i32, i32* %9
%26 = load i32, i32* %7
%27 = call i32 @link_stream_to_file(%struct.link* %23, i32* %24, i32 %25, i32 %26)
store i32 %27, i32* %10
%28 = load %struct.link*, %struct.link** %11
%29 = call i32 @link_close(%struct.link* %28)
%30 = load i32*, i32** %8
%31 = call i32 @fclose(i32* %30)
%32 = load i32, i32* %10
%33 = load i32, i32* %9
%34 = icmp eq i32 %32, %33
br i1 %34, label %35, label %37
35: ; preds = %22
%36 = load i32, i32* %10
store i32 %36, i32* %4
br label %44
37: ; preds = %22
%38 = load i8*, i8** %6
%39 = call i32 @unlink(i8* %38)
store i32 -1, i32* %4
br label %44
40: ; preds = %16
%41 = load i32*, i32** %8
%42 = call i32 @fclose(i32* %41)
store i32 -1, i32* %4
br label %44
43: ; preds = %3
store i32 -1, i32* %4
br label %44
44: ; preds = %43, %40, %37, %35
%45 = load i32, i32* %4
ret i32 %45
}

LLVM IR (-O2):
%struct.link = type { i32 }
@.str = [2 x i8] c"w\00"
@.str.1 = [4 x i8] c"GET\00"
define i32 @http_fetch_to_file(i8* %0, i8* nocapture %1, i32 %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
%6 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %22, label %8
8: ; preds = %3
%9 = call %struct.link* @http_query_size(i8* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32* %4, i32 %2, i32 1)
%10 = icmp eq %struct.link* %9, null
br i1 %10, label %20, label %11
11: ; preds = %8
%12 = load i32, i32* %4
%13 = call i32 @link_stream_to_file(%struct.link* %9, i32* %6, i32 %12, i32 %2)
%14 = call i32 @link_close(%struct.link* %9)
%15 = call i32 @fclose(i32* %6)
%16 = load i32, i32* %4
%17 = icmp eq i32 %13, %16
br i1 %17, label %22, label %18
18: ; preds = %11
%19 = call i32 @unlink(i8* %1)
br label %22
20: ; preds = %8
%21 = call i32 @fclose(i32* %6)
br label %22
22: ; preds = %3, %11, %20, %18
%23 = phi i32 [ -1, %18 ], [ -1, %20 ], [ %13, %11 ], [ -1, %3 ]
ret i32 %23
}