#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 pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char const *const, char *);
int /*<<< orphan*/ printf(char *, long);
int /*<<< orphan*/ scripted();
int /*<<< orphan*/ set_error_msg(char *);
int /*<<< orphan*/ show_strerror(char const *const, int /*<<< orphan*/);
int /*<<< orphan*/ strip_escapes(char const *const);
long write_stream(char const *const, 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(filename, fp, from, to);
  if (*filename == '!')
    ret = pclose(fp);
  else
    ret = fclose(fp);
  if (size < 0)
    return -1;
  if (ret != 0) {
    show_strerror(filename, errno);
    set_error_msg("Cannot close output file");
    return -1;
  }
  if (!scripted())
    printf("%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    0x2dcf(%rip),%rax
mov    (%rax),%esi
call   10c0 <show_strerror@plt>
lea    0xde3(%rip),%rdi
call   1060 <set_error_msg@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1316 <write_file+0x176>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x1c(%rbp),%edx
mov    -0x20(%rbp),%ecx
call   10a0 <write_stream@plt>
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x21,%eax
jne    1268 <write_file+0xc8>
mov    -0x28(%rbp),%rdi
call   1050 <pclose@plt>
mov    %eax,-0x34(%rbp)
jmp    1274 <write_file+0xd4>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
mov    %eax,-0x34(%rbp)
cmpq   $0x0,-0x30(%rbp)
jge    128b <write_file+0xeb>
movl   $0xffffffff,-0x4(%rbp)
jmp    1316 <write_file+0x176>
cmpl   $0x0,-0x34(%rbp)
je     12bf <write_file+0x11f>
mov    -0x10(%rbp),%rdi
mov    0x2d40(%rip),%rax
mov    (%rax),%esi
call   10c0 <show_strerror@plt>
lea    0xd6c(%rip),%rdi
call   1060 <set_error_msg@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1316 <write_file+0x176>
mov    $0x0,%al
call   10b0 <scripted@plt>
cmp    $0x0,%eax
jne    12df <write_file+0x13f>
mov    -0x30(%rbp),%rsi
lea    0xd59(%rip),%rdi
call   1040 <printf@plt>
cmpl   $0x0,-0x1c(%rbp)
je     1306 <write_file+0x166>
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jg     1306 <write_file+0x166>
mov    -0x20(%rbp),%eax
sub    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    1310 <write_file+0x170>
xor    %eax,%eax
mov    %eax,-0x38(%rbp)
jmp    1310 <write_file+0x170>
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   %r12
push   %rbx
mov    %ecx,%r14d
mov    %edx,%r15d
mov    %rdi,%rbp
cmpb   $0x21,(%rdi)
jne    11c8 <write_file+0x28>
lea    0x1(%rbp),%rdi
lea    0xe3f(%rip),%rsi
call   1080 <popen@plt>
jmp    11dd <write_file+0x3d>
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     1209 <write_file+0x69>
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %r15d,%edx
mov    %r14d,%ecx
call   10a0 <write_stream@plt>
mov    %rax,%r12
cmpb   $0x21,0x0(%rbp)
jne    122d <write_file+0x8d>
mov    %rbx,%rdi
call   1050 <pclose@plt>
jmp    1235 <write_file+0x95>
mov    0x2dd0(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   10c0 <show_strerror@plt>
lea    0xde1(%rip),%rdi
call   1060 <set_error_msg@plt>
mov    $0xffffffff,%ebx
jmp    1294 <write_file+0xf4>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    $0xffffffff,%ebx
test   %r12,%r12
js     1294 <write_file+0xf4>
test   %eax,%eax
je     1262 <write_file+0xc2>
mov    0x2d96(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   10c0 <show_strerror@plt>
lea    0xdbf(%rip),%rdi
call   1060 <set_error_msg@plt>
jmp    1294 <write_file+0xf4>
xor    %ebp,%ebp
xor    %eax,%eax
call   10b0 <scripted@plt>
test   %eax,%eax
jne    127e <write_file+0xde>
lea    0xdbd(%rip),%rdi
mov    %r12,%rsi
call   1040 <printf@plt>
mov    %r14d,%ebx
sub    %r15d,%ebx
add    $0x1,%ebx
cmp    %r15d,%r14d
cmovl  %ebp,%ebx
test   %r15d,%r15d
cmove  %r15d,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r12
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"
@.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 i8*, i8** %6
%36 = load i32*, i32** %10
%37 = load i32, i32* %8
%38 = load i32, i32* %9
%39 = call i64 @write_stream(i8* %35, i32* %36, i32 %37, i32 %38)
store i64 %39, i64* %11
%40 = load i8*, i8** %6
%41 = load i8, i8* %40
%42 = sext i8 %41 to i32
%43 = icmp eq i32 %42, 33
br i1 %43, label %44, label %47
44: ; preds = %34
%45 = load i32*, i32** %10
%46 = call i32 @pclose(i32* %45)
store i32 %46, i32* %12
br label %50
47: ; preds = %34
%48 = load i32*, i32** %10
%49 = call i32 @fclose(i32* %48)
store i32 %49, i32* %12
br label %50
50: ; preds = %47, %44
%51 = load i64, i64* %11
%52 = icmp slt i64 %51, 0
br i1 %52, label %53, label %54
53: ; preds = %50
store i32 -1, i32* %5
br label %83
54: ; preds = %50
%55 = load i32, i32* %12
%56 = icmp ne i32 %55, 0
br i1 %56, label %57, label %62
57: ; preds = %54
%58 = load i8*, i8** %6
%59 = load i32, i32* @errno
%60 = call i32 @show_strerror(i8* %58, i32 %59)
%61 = 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
62: ; preds = %54
%63 = call i32 (...) @scripted()
%64 = icmp ne i32 %63, 0
br i1 %64, label %68, label %65
65: ; preds = %62
%66 = load i64, i64* %11
%67 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i64 %66)
br label %68
68: ; preds = %65, %62
%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, %57, %53, %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"
@.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 %49
20: ; preds = %13
%21 = tail call i64 @write_stream(i8* %0, i32* %14, i32 %2, i32 %3)
%22 = load i8, i8* %0
%23 = icmp eq i8 %22, 33
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = tail call i32 @pclose(i32* %14)
br label %28
26: ; preds = %20
%27 = tail call i32 @fclose(i32* %14)
br label %28
28: ; preds = %26, %24
%29 = phi i32 [ %25, %24 ], [ %27, %26 ]
%30 = icmp slt i64 %21, 0
br i1 %30, label %49, label %31
31: ; preds = %28
%32 = icmp eq i32 %29, 0
br i1 %32, label %37, label %33
33: ; preds = %31
%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 %49
37: ; preds = %31
%38 = tail call i32 (...) @scripted()
%39 = icmp eq i32 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %37
%41 = tail call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i64 %21)
br label %42
42: ; preds = %40, %37
%43 = icmp eq i32 %2, 0
%44 = icmp slt i32 %3, %2
%45 = or i1 %43, %44
%46 = sub i32 1, %2
%47 = add i32 %46, %3
%48 = select i1 %45, i32 0, i32 %47
br label %49
49: ; preds = %28, %42, %33, %16
%50 = phi i32 [ -1, %33 ], [ %48, %42 ], [ -1, %16 ], [ -1, %28 ]
ret i32 %50
}