#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;
int fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(int /*<<< orphan*/, char const *const);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, long);
int 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;
  int ret;
  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;
  }
  size = write_stream(fp, from, to);
  if (size < 0)
    return -1;
  if (*filename == '!')
    ret = pclose(fp);
  else
    ret = fclose(fp);
  if (ret != 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    131b <write_file+0x17b>
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x20(%rbp),%edx
call   10a0 <write_stream@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jge    125a <write_file+0xba>
movl   $0xffffffff,-0x4(%rbp)
jmp    131b <write_file+0x17b>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x21,%eax
jne    127b <write_file+0xdb>
mov    -0x28(%rbp),%rdi
call   1040 <pclose@plt>
mov    %eax,-0x34(%rbp)
jmp    1287 <write_file+0xe7>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     12bb <write_file+0x11b>
mov    -0x10(%rbp),%rdi
mov    0x2d3c(%rip),%rax
mov    (%rax),%esi
call   10c0 <show_strerror@plt>
lea    0xd70(%rip),%rdi
call   1050 <set_error_msg@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    131b <write_file+0x17b>
mov    $0x0,%al
call   10b0 <scripted@plt>
cmp    $0x0,%eax
jne    12e4 <write_file+0x144>
mov    0x2d0e(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%rdx
lea    0xd54(%rip),%rsi
call   1060 <fprintf@plt>
cmpl   $0x0,-0x1c(%rbp)
je     130b <write_file+0x16b>
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jg     130b <write_file+0x16b>
mov    -0x20(%rbp),%eax
sub    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    1315 <write_file+0x175>
xor    %eax,%eax
mov    %eax,-0x38(%rbp)
jmp    1315 <write_file+0x175>
mov    -0x38(%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,%rbp
cmpb   $0x21,(%rdi)
jne    11cb <write_file+0x2b>
lea    0x1(%rbp),%rdi
lea    0xe3c(%rip),%rsi
call   1080 <popen@plt>
jmp    11e0 <write_file+0x40>
mov    %rsi,%rbx
mov    %rbp,%rdi
call   1070 <strip_escapes@plt>
mov    %eax,%edi
mov    %rbx,%rsi
call   1090 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1239 <write_file+0x99>
mov    %rbx,%rdi
mov    %r15d,%esi
mov    %r14d,%edx
call   10a0 <write_stream@plt>
mov    $0xffffffff,%r13d
test   %rax,%rax
js     12a7 <write_file+0x107>
mov    %rax,%r12
cmpb   $0x21,0x0(%rbp)
jne    125e <write_file+0xbe>
mov    %rbx,%rdi
call   1040 <pclose@plt>
test   %eax,%eax
je     126a <write_file+0xca>
mov    0x2db7(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   10c0 <show_strerror@plt>
lea    0xde8(%rip),%rdi
call   1050 <set_error_msg@plt>
jmp    12a7 <write_file+0x107>
mov    0x2d98(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   10c0 <show_strerror@plt>
lea    0xdb1(%rip),%rdi
call   1050 <set_error_msg@plt>
mov    $0xffffffff,%r13d
jmp    12a7 <write_file+0x107>
mov    %rbx,%rdi
call   1030 <fclose@plt>
test   %eax,%eax
jne    121a <write_file+0x7a>
xor    %ebx,%ebx
xor    %eax,%eax
call   10b0 <scripted@plt>
test   %eax,%eax
jne    128f <write_file+0xef>
mov    0x2d62(%rip),%rax
mov    (%rax),%edi
lea    0xdac(%rip),%rsi
mov    %r12,%rdx
call   1060 <fprintf@plt>
mov    %r14d,%r13d
sub    %r15d,%r13d
add    $0x1,%r13d
cmp    %r15d,%r14d
cmovl  %ebx,%r13d
test   %r15d,%r15d
cmove  %r15d,%r13d
mov    %r13d,%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
%12 = alloca i32
store i8* %0, i8** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%13 = load i8*, i8** %6
%14 = load i8, i8* %13
%15 = sext i8 %14 to i32
%16 = icmp eq i32 %15, 33
br i1 %16, label %17, label %21
17: ; preds = %4
%18 = load i8*, i8** %6
%19 = getelementptr inbounds i8, i8* %18, i64 1
%20 = call i32* @popen(i8* %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %20, i32** %10
br label %26
21: ; preds = %4
%22 = load i8*, i8** %6
%23 = call i32 @strip_escapes(i8* %22)
%24 = load i8*, i8** %7
%25 = call i32* @fopen(i32 %23, i8* %24)
store i32* %25, i32** %10
br label %26
26: ; preds = %21, %17
%27 = load i32*, i32** %10
%28 = icmp ne i32* %27, null
br i1 %28, label %34, label %29
29: ; preds = %26
%30 = load i8*, i8** %6
%31 = load i32, i32* @errno
%32 = call i32 @show_strerror(i8* %30, i32 %31)
%33 = 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 %83
34: ; preds = %26
%35 = load i32*, i32** %10
%36 = load i32, i32* %8
%37 = load i32, i32* %9
%38 = call i64 @write_stream(i32* %35, i32 %36, i32 %37)
store i64 %38, i64* %11
%39 = load i64, i64* %11
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %42
41: ; preds = %34
store i32 -1, i32* %5
br label %83
42: ; preds = %34
%43 = load i8*, i8** %6
%44 = load i8, i8* %43
%45 = sext i8 %44 to i32
%46 = icmp eq i32 %45, 33
br i1 %46, label %47, label %50
47: ; preds = %42
%48 = load i32*, i32** %10
%49 = call i32 @pclose(i32* %48)
store i32 %49, i32* %12
br label %53
50: ; preds = %42
%51 = load i32*, i32** %10
%52 = call i32 @fclose(i32* %51)
store i32 %52, i32* %12
br label %53
53: ; preds = %50, %47
%54 = load i32, i32* %12
%55 = icmp ne i32 %54, 0
br i1 %55, label %56, label %61
56: ; preds = %53
%57 = load i8*, i8** %6
%58 = load i32, i32* @errno
%59 = call i32 @show_strerror(i8* %57, i32 %58)
%60 = 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 %83
61: ; preds = %53
%62 = call i32 (...) @scripted()
%63 = icmp ne i32 %62, 0
br i1 %63, label %68, label %64
64: ; preds = %61
%65 = load i32, i32* @stderr
%66 = load i64, i64* %11
%67 = call i32 @fprintf(i32 %65, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i64 %66)
br label %68
68: ; preds = %64, %61
%69 = load i32, i32* %8
%70 = icmp ne i32 %69, 0
br i1 %70, label %71, label %80
71: ; preds = %68
%72 = load i32, i32* %8
%73 = load i32, i32* %9
%74 = icmp sle i32 %72, %73
br i1 %74, label %75, label %80
75: ; preds = %71
%76 = load i32, i32* %9
%77 = load i32, i32* %8
%78 = sub nsw i32 %76, %77
%79 = add nsw i32 %78, 1
br label %81
80: ; preds = %71, %68
br label %81
81: ; preds = %80, %75
%82 = phi i32 [ %79, %75 ], [ 0, %80 ]
store i32 %82, i32* %5
br label %83
83: ; preds = %81, %56, %41, %29
%84 = load i32, i32* %5
ret i32 %84
}

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 i32 @pclose(i32* %14)
br label %30
28: ; preds = %23
%29 = tail call i32 @fclose(i32* %14)
br label %30
30: ; preds = %28, %26
%31 = phi i32 [ %27, %26 ], [ %29, %28 ]
%32 = icmp eq i32 %31, 0
br i1 %32, label %37, label %33
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
}