#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;
  }
  size = write_stream(fp, from, to);
  if (size < 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    1321 <write_file+0x181>
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    1321 <write_file+0x181>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x21,%eax
jne    127c <write_file+0xdc>
mov    -0x28(%rbp),%rdi
call   1040 <pclose@plt>
mov    %rax,-0x38(%rbp)
jmp    1289 <write_file+0xe9>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    $0x0,%rax
jge    12c1 <write_file+0x121>
mov    -0x10(%rbp),%rdi
mov    0x2d36(%rip),%rax
mov    (%rax),%esi
call   10c0 <show_strerror@plt>
lea    0xd6a(%rip),%rdi
call   1050 <set_error_msg@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1321 <write_file+0x181>
mov    $0x0,%al
call   10b0 <scripted@plt>
cmp    $0x0,%eax
jne    12ea <write_file+0x14a>
mov    0x2d08(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%rdx
lea    0xd4e(%rip),%rsi
call   1060 <fprintf@plt>
cmpl   $0x0,-0x1c(%rbp)
je     1311 <write_file+0x171>
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jg     1311 <write_file+0x171>
mov    -0x20(%rbp),%eax
sub    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    131b <write_file+0x17b>
xor    %eax,%eax
mov    %eax,-0x3c(%rbp)
jmp    131b <write_file+0x17b>
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 %82
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 = load i64, i64* %11
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %41
40: ; preds = %33
store i32 -1, i32* %5
br label %82
41: ; preds = %33
%42 = load i8*, i8** %6
%43 = load i8, i8* %42
%44 = sext i8 %43 to i32
%45 = icmp eq i32 %44, 33
br i1 %45, label %46, label %49
46: ; preds = %41
%47 = load i32*, i32** %10
%48 = call i64 @pclose(i32* %47)
br label %52
49: ; preds = %41
%50 = load i32*, i32** %10
%51 = call i64 @fclose(i32* %50)
br label %52
52: ; preds = %49, %46
%53 = phi i64 [ %48, %46 ], [ %51, %49 ]
%54 = icmp slt i64 %53, 0
br i1 %54, label %55, label %60
55: ; preds = %52
%56 = load i8*, i8** %6
%57 = load i32, i32* @errno
%58 = call i32 @show_strerror(i8* %56, i32 %57)
%59 = 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 %82
60: ; preds = %52
%61 = call i32 (...) @scripted()
%62 = icmp ne i32 %61, 0
br i1 %62, label %67, label %63
63: ; preds = %60
%64 = load i32, i32* @stderr
%65 = load i64, i64* %11
%66 = call i32 @fprintf(i32 %64, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i64 %65)
br label %67
67: ; preds = %63, %60
%68 = load i32, i32* %8
%69 = icmp ne i32 %68, 0
br i1 %69, label %70, label %79
70: ; preds = %67
%71 = load i32, i32* %8
%72 = load i32, i32* %9
%73 = icmp sle i32 %71, %72
br i1 %73, label %74, label %79
74: ; preds = %70
%75 = load i32, i32* %9
%76 = load i32, i32* %8
%77 = sub nsw i32 %75, %76
%78 = add nsw i32 %77, 1
br label %80
79: ; preds = %70, %67
br label %80
80: ; preds = %79, %74
%81 = phi i32 [ %78, %74 ], [ 0, %79 ]
store i32 %81, i32* %5
br label %82
82: ; preds = %80, %55, %40, %28
%83 = load i32, i32* %5
ret i32 %83
}

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
}