#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*/ errno;
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(int /*<<< orphan*/, char const *const);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, long);
scalar_t__ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char const *const, char *);
int /*<<< orphan*/ scripted();
int /*<<< orphan*/ set_error_msg(char *);
int /*<<< orphan*/ show_strerror(char const *const, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strip_escapes(char const *const);
long write_stream(int /*<<< orphan*/ *, int const, int const);
int write_file(const char *const filename, const char *const mode,
               const int from, const int to) {
  FILE *fp;
  long size;
  if (*filename == '!')
    fp = popen(filename + 1, "w");
  else
    fp = fopen(strip_escapes(filename), mode);
  if (!fp) {
    show_strerror(filename, errno);
    set_error_msg("Cannot open output file");
    return -1;
  }
  if ((size = write_stream(fp, from, to)) < 0)
    return -1;
  if (((*filename == '!') ? pclose(fp) : fclose(fp)) < 0) {
    show_strerror(filename, errno);
    set_error_msg("Cannot close output file");
    return -1;
  }
  if (!scripted())
    fprintf(stderr, "%lu\n", size);
  return (from && from <= to) ? to - from + 1 : 0;
}


x86 assembly (clang, -O0):
<write_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x21,%eax
jne    11e3 <write_file+0x43>
mov    -0x10(%rbp),%rdi
add    $0x1,%rdi
lea    0xe2b(%rip),%rsi
call   1080 <popen@plt>
mov    %rax,-0x28(%rbp)
jmp    11fb <write_file+0x5b>
mov    -0x10(%rbp),%rdi
call   1070 <strip_escapes@plt>
mov    %eax,%edi
mov    -0x18(%rbp),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1230 <write_file+0x90>
mov    -0x10(%rbp),%rdi
mov    0x2dc7(%rip),%rax
mov    (%rax),%esi
call   10c0 <show_strerror@plt>
lea    0xde3(%rip),%rdi
call   1050 <set_error_msg@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1320 <write_file+0x180>
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x20(%rbp),%edx
call   10a0 <write_stream@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jge    1259 <write_file+0xb9>
movl   $0xffffffff,-0x4(%rbp)
jmp    1320 <write_file+0x180>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x21,%eax
jne    127b <write_file+0xdb>
mov    -0x28(%rbp),%rdi
call   1040 <pclose@plt>
mov    %rax,-0x38(%rbp)
jmp    1288 <write_file+0xe8>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    $0x0,%rax
jge    12c0 <write_file+0x120>
mov    -0x10(%rbp),%rdi
mov    0x2d37(%rip),%rax
mov    (%rax),%esi
call   10c0 <show_strerror@plt>
lea    0xd6b(%rip),%rdi
call   1050 <set_error_msg@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1320 <write_file+0x180>
mov    $0x0,%al
call   10b0 <scripted@plt>
cmp    $0x0,%eax
jne    12e9 <write_file+0x149>
mov    0x2d09(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%rdx
lea    0xd4f(%rip),%rsi
call   1060 <fprintf@plt>
cmpl   $0x0,-0x1c(%rbp)
je     1310 <write_file+0x170>
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jg     1310 <write_file+0x170>
mov    -0x20(%rbp),%eax
sub    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    131a <write_file+0x17a>
xor    %eax,%eax
mov    %eax,-0x3c(%rbp)
jmp    131a <write_file+0x17a>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%r14d
mov    %edx,%r15d
mov    %rdi,%r13
cmpb   $0x21,(%rdi)
jne    11cb <write_file+0x2b>
lea    0x1(%r13),%rdi
lea    0xe3c(%rip),%rsi
call   1080 <popen@plt>
jmp    11e0 <write_file+0x40>
mov    %rsi,%rbx
mov    %r13,%rdi
call   1070 <strip_escapes@plt>
mov    %eax,%edi
mov    %rbx,%rsi
call   1090 <fopen@plt>
mov    %rax,%rbp
test   %rax,%rax
je     1258 <write_file+0xb8>
mov    %rbp,%rdi
mov    %r15d,%esi
mov    %r14d,%edx
call   10a0 <write_stream@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
js     12a6 <write_file+0x106>
mov    %rax,%r12
cmpb   $0x21,0x0(%r13)
jne    127c <write_file+0xdc>
mov    %rbp,%rdi
call   1040 <pclose@plt>
test   %rax,%rax
js     1289 <write_file+0xe9>
xor    %ebp,%ebp
xor    %eax,%eax
call   10b0 <scripted@plt>
test   %eax,%eax
jne    1240 <write_file+0xa0>
mov    0x2db1(%rip),%rax
mov    (%rax),%edi
lea    0xdfb(%rip),%rsi
mov    %r12,%rdx
call   1060 <fprintf@plt>
mov    %r14d,%ebx
sub    %r15d,%ebx
add    $0x1,%ebx
cmp    %r15d,%r14d
cmovl  %ebp,%ebx
test   %r15d,%r15d
cmove  %r15d,%ebx
jmp    12a6 <write_file+0x106>
mov    0x2d79(%rip),%rax
mov    (%rax),%esi
mov    %r13,%rdi
call   10c0 <show_strerror@plt>
lea    0xd92(%rip),%rdi
call   1050 <set_error_msg@plt>
mov    $0xffffffff,%ebx
jmp    12a6 <write_file+0x106>
mov    %rbp,%rdi
call   1030 <fclose@plt>
test   %rax,%rax
jns    121b <write_file+0x7b>
mov    0x2d48(%rip),%rax
mov    (%rax),%esi
mov    %r13,%rdi
call   10c0 <show_strerror@plt>
lea    0xd79(%rip),%rdi
call   1050 <set_error_msg@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@errno = global i32 0
@.str.1 = [24 x i8] c"Cannot open output file\00"
@.str.2 = [25 x i8] c"Cannot close output file\00"
@stderr = global i32 0
@.str.3 = [5 x i8] c"%lu\0A\00"
define i32 @write_file(i8* %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32*
%11 = alloca i64
store i8* %0, i8** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%12 = load i8*, i8** %6
%13 = load i8, i8* %12
%14 = sext i8 %13 to i32
%15 = icmp eq i32 %14, 33
br i1 %15, label %16, label %20
16: ; preds = %4
%17 = load i8*, i8** %6
%18 = getelementptr inbounds i8, i8* %17, i64 1
%19 = call i32* @popen(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %19, i32** %10
br label %25
20: ; preds = %4
%21 = load i8*, i8** %6
%22 = call i32 @strip_escapes(i8* %21)
%23 = load i8*, i8** %7
%24 = call i32* @fopen(i32 %22, i8* %23)
store i32* %24, i32** %10
br label %25
25: ; preds = %20, %16
%26 = load i32*, i32** %10
%27 = icmp ne i32* %26, null
br i1 %27, label %33, label %28
28: ; preds = %25
%29 = load i8*, i8** %6
%30 = load i32, i32* @errno
%31 = call i32 @show_strerror(i8* %29, i32 %30)
%32 = call i32 @set_error_msg(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %5
br label %81
33: ; preds = %25
%34 = load i32*, i32** %10
%35 = load i32, i32* %8
%36 = load i32, i32* %9
%37 = call i64 @write_stream(i32* %34, i32 %35, i32 %36)
store i64 %37, i64* %11
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %33
store i32 -1, i32* %5
br label %81
40: ; preds = %33
%41 = load i8*, i8** %6
%42 = load i8, i8* %41
%43 = sext i8 %42 to i32
%44 = icmp eq i32 %43, 33
br i1 %44, label %45, label %48
45: ; preds = %40
%46 = load i32*, i32** %10
%47 = call i64 @pclose(i32* %46)
br label %51
48: ; preds = %40
%49 = load i32*, i32** %10
%50 = call i64 @fclose(i32* %49)
br label %51
51: ; preds = %48, %45
%52 = phi i64 [ %47, %45 ], [ %50, %48 ]
%53 = icmp slt i64 %52, 0
br i1 %53, label %54, label %59
54: ; preds = %51
%55 = load i8*, i8** %6
%56 = load i32, i32* @errno
%57 = call i32 @show_strerror(i8* %55, i32 %56)
%58 = call i32 @set_error_msg(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %5
br label %81
59: ; preds = %51
%60 = call i32 (...) @scripted()
%61 = icmp ne i32 %60, 0
br i1 %61, label %66, label %62
62: ; preds = %59
%63 = load i32, i32* @stderr
%64 = load i64, i64* %11
%65 = call i32 @fprintf(i32 %63, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i64 %64)
br label %66
66: ; preds = %62, %59
%67 = load i32, i32* %8
%68 = icmp ne i32 %67, 0
br i1 %68, label %69, label %78
69: ; preds = %66
%70 = load i32, i32* %8
%71 = load i32, i32* %9
%72 = icmp sle i32 %70, %71
br i1 %72, label %73, label %78
73: ; preds = %69
%74 = load i32, i32* %9
%75 = load i32, i32* %8
%76 = sub nsw i32 %74, %75
%77 = add nsw i32 %76, 1
br label %79
78: ; preds = %69, %66
br label %79
79: ; preds = %78, %73
%80 = phi i32 [ %77, %73 ], [ 0, %78 ]
store i32 %80, i32* %5
br label %81
81: ; preds = %79, %54, %39, %28
%82 = load i32, i32* %5
ret i32 %82
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@errno = global i32 0
@.str.1 = [24 x i8] c"Cannot open output file\00"
@.str.2 = [25 x i8] c"Cannot close output file\00"
@stderr = global i32 0
@.str.3 = [5 x i8] c"%lu\0A\00"
define i32 @write_file(i8* %0, i8* %1, i32 %2, i32 %3) {
%5 = load i8, i8* %0
%6 = icmp eq i8 %5, 33
br i1 %6, label %7, label %10
7: ; preds = %4
%8 = getelementptr inbounds i8, i8* %0, i64 1
%9 = tail call i32* @popen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %13
10: ; preds = %4
%11 = tail call i32 @strip_escapes(i8* %0)
%12 = tail call i32* @fopen(i32 %11, i8* %1)
br label %13
13: ; preds = %10, %7
%14 = phi i32* [ %9, %7 ], [ %12, %10 ]
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %20
16: ; preds = %13
%17 = load i32, i32* @errno
%18 = tail call i32 @show_strerror(i8* %0, i32 %17)
%19 = tail call i32 @set_error_msg(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %50
20: ; preds = %13
%21 = tail call i64 @write_stream(i32* %14, i32 %2, i32 %3)
%22 = icmp slt i64 %21, 0
br i1 %22, label %50, label %23
23: ; preds = %20
%24 = load i8, i8* %0
%25 = icmp eq i8 %24, 33
br i1 %25, label %26, label %28
26: ; preds = %23
%27 = tail call i64 @pclose(i32* %14)
br label %30
28: ; preds = %23
%29 = tail call i64 @fclose(i32* %14)
br label %30
30: ; preds = %28, %26
%31 = phi i64 [ %27, %26 ], [ %29, %28 ]
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %37
33: ; preds = %30
%34 = load i32, i32* @errno
%35 = tail call i32 @show_strerror(i8* %0, i32 %34)
%36 = tail call i32 @set_error_msg(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
br label %50
37: ; preds = %30
%38 = tail call i32 (...) @scripted()
%39 = icmp eq i32 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %37
%41 = load i32, i32* @stderr
%42 = tail call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i64 %21)
br label %43
43: ; preds = %40, %37
%44 = icmp eq i32 %2, 0
%45 = icmp slt i32 %3, %2
%46 = or i1 %44, %45
%47 = sub i32 1, %2
%48 = add i32 %47, %3
%49 = select i1 %46, i32 0, i32 %48
br label %50
50: ; preds = %20, %43, %33, %16
%51 = phi i32 [ -1, %33 ], [ %49, %43 ], [ -1, %16 ], [ -1, %20 ]
ret i32 %51
}