#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 */
unsigned int EOF;
int /*<<< orphan*/ exit(unsigned int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, unsigned int);
unsigned int getc(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int /*<<< orphan*/ *tmpfile();
int /*<<< orphan*/ usage();
int main(int argc, char **argv) {
  FILE *input_file;
  FILE *tmp_file;
  char *filename;
  unsigned int c, file_idx, retval;
  retval = 0;
  if (argc <= 1) {
    usage();
  }
  for (file_idx = 1; file_idx < (unsigned)argc; file_idx++) {
    filename = argv[file_idx];
    printf("%s: ", filename);
    if (!(input_file = fopen(filename, "r"))) {
      printf("Cannot open file\n");
      retval = 1;
    } else if (!(tmp_file = tmpfile())) {
      printf("Cannot open temporary file\n");
      fclose(input_file);
      retval = 1;
    } else {
      while ((c = getc(input_file)) != EOF) {
        if (c != 0x0d) {
          fprintf(tmp_file, "%c", c);
        }
      }
      fflush(tmp_file);
      rewind(tmp_file);
      fclose(input_file);
      if (!(input_file = fopen(filename, "w"))) {
        printf("No write access to %s\n", filename);
        fclose(tmp_file);
        retval = 1;
      } else {
        while ((c = getc(tmp_file)) != EOF) {
          if (c != 0x0d) {
            fprintf(input_file, "%c", c);
          }
        }
        fclose(input_file);
        fclose(tmp_file);
        printf("OK\n");
      }
    }
  }
  exit(retval);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x34(%rbp)
cmpl   $0x1,-0x8(%rbp)
jg     11ce <main+0x2e>
mov    $0x0,%al
call   1090 <usage@plt>
movl   $0x1,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x8(%rbp),%eax
jae    1393 <main+0x1f3>
mov    -0x10(%rbp),%rax
mov    -0x30(%rbp),%ecx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rsi
lea    0xe05(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%rdi
lea    0xdf8(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    123a <main+0x9a>
lea    0xde0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x34(%rbp)
jmp    1380 <main+0x1e0>
mov    $0x0,%al
call   1060 <tmpfile@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1272 <main+0xd2>
lea    0xdc3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x1,-0x34(%rbp)
jmp    137b <main+0x1db>
jmp    1277 <main+0xd7>
mov    -0x18(%rbp),%rdi
call   10c0 <getc@plt>
mov    %eax,-0x2c(%rbp)
mov    0x2d5e(%rip),%rcx
cmp    (%rcx),%eax
je     12b4 <main+0x114>
cmpl   $0xd,-0x2c(%rbp)
je     12af <main+0x10f>
mov    -0x20(%rbp),%rdi
mov    -0x2c(%rbp),%edx
lea    0xd8b(%rip),%rsi
call   1070 <fprintf@plt>
jmp    1277 <main+0xd7>
mov    -0x20(%rbp),%rdi
call   1080 <fflush@plt>
mov    -0x20(%rbp),%rdi
call   1050 <rewind@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x28(%rbp),%rdi
lea    0xd5e(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1314 <main+0x174>
mov    -0x28(%rbp),%rsi
lea    0xd42(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x1,-0x34(%rbp)
jmp    1376 <main+0x1d6>
jmp    1319 <main+0x179>
mov    -0x20(%rbp),%rdi
call   10c0 <getc@plt>
mov    %eax,-0x2c(%rbp)
mov    0x2cbc(%rip),%rcx
cmp    (%rcx),%eax
je     1356 <main+0x1b6>
cmpl   $0xd,-0x2c(%rbp)
je     1351 <main+0x1b1>
mov    -0x18(%rbp),%rdi
mov    -0x2c(%rbp),%edx
lea    0xce9(%rip),%rsi
call   1070 <fprintf@plt>
jmp    1319 <main+0x179>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
lea    0xce2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    137b <main+0x1db>
jmp    1380 <main+0x1e0>
jmp    1385 <main+0x1e5>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    11d5 <main+0x35>
mov    -0x34(%rbp),%edi
call   10b0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,0x10(%rsp)
mov    %edi,%ebx
cmp    $0x1,%edi
jg     11e2 <main+0x32>
movl   $0x0,0x4(%rsp)
xor    %eax,%eax
call   10a0 <usage@plt>
cmp    $0x2,%ebx
jb     1344 <main+0x194>
mov    %ebx,%ebx
mov    $0x1,%r15d
mov    0x2df7(%rip),%r13
movl   $0x0,0x4(%rsp)
mov    %rbx,0x8(%rsp)
jmp    1221 <main+0x71>
lea    0xe19(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,0x4(%rsp)
add    $0x1,%r15
cmp    %rbx,%r15
je     1344 <main+0x194>
mov    0x10(%rsp),%rax
mov    (%rax,%r15,8),%rbp
lea    0xdcf(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbp,%rdi
lea    0xdc0(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     1200 <main+0x50>
mov    %rax,%r14
xor    %eax,%eax
call   1080 <tmpfile@plt>
test   %rax,%rax
je     1300 <main+0x150>
mov    %rax,%r12
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r14,%rdi
call   10d0 <getc@plt>
cmp    0x0(%r13),%eax
je     1290 <main+0xe0>
cmp    $0xd,%eax
je     1270 <main+0xc0>
mov    %eax,%edi
mov    %r12,%rsi
call   1070 <fputc@plt>
jmp    1270 <main+0xc0>
nop
mov    %r12,%rdi
call   1090 <fflush@plt>
mov    %r12,%rdi
call   1060 <rewind@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    %rbp,%rdi
lea    0xd55(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     1319 <main+0x169>
mov    %rax,%rbx
nop
mov    %r12,%rdi
call   10d0 <getc@plt>
cmp    0x0(%r13),%eax
je     12e0 <main+0x130>
cmp    $0xd,%eax
je     12c0 <main+0x110>
mov    %eax,%edi
mov    %rbx,%rsi
call   1070 <fputc@plt>
jmp    12c0 <main+0x110>
nop
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r12,%rdi
call   1040 <fclose@plt>
lea    0xd55(%rip),%rdi
call   1030 <puts@plt>
jmp    133a <main+0x18a>
xchg   %ax,%ax
lea    0xd2a(%rip),%rdi
call   1030 <puts@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
jmp    120c <main+0x5c>
lea    0xce9(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r12,%rdi
call   1040 <fclose@plt>
movl   $0x1,0x4(%rsp)
mov    0x8(%rsp),%rbx
jmp    1214 <main+0x64>
mov    0x4(%rsp),%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"%s: \00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [18 x i8] c"Cannot open file\0A\00"
@.str.3 = [28 x i8] c"Cannot open temporary file\0A\00"
@EOF = global i32 0
@.str.4 = [3 x i8] c"%c\00"
@.str.5 = [2 x i8] c"w\00"
@.str.6 = [23 x i8] c"No write access to %s\0A\00"
@.str.7 = [4 x i8] c"OK\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %11
%12 = load i32, i32* %4
%13 = icmp sle i32 %12, 1
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 (...) @usage()
br label %16
16: ; preds = %14, %2
store i32 1, i32* %10
br label %17
17: ; preds = %93, %16
%18 = load i32, i32* %10
%19 = load i32, i32* %4
%20 = icmp ult i32 %18, %19
br i1 %20, label %21, label %96
21: ; preds = %17
%22 = load i8**, i8*** %5
%23 = load i32, i32* %10
%24 = zext i32 %23 to i64
%25 = getelementptr inbounds i8*, i8** %22, i64 %24
%26 = load i8*, i8** %25
store i8* %26, i8** %8
%27 = load i8*, i8** %8
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %27)
%29 = load i8*, i8** %8
%30 = call i32* @fopen(i8* %29, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %30, i32** %6
%31 = icmp ne i32* %30, null
br i1 %31, label %34, label %32
32: ; preds = %21
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %11
br label %92
34: ; preds = %21
%35 = call i32* (...) @tmpfile()
store i32* %35, i32** %7
%36 = icmp ne i32* %35, null
br i1 %36, label %41, label %37
37: ; preds = %34
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
%39 = load i32*, i32** %6
%40 = call i32 @fclose(i32* %39)
store i32 1, i32* %11
br label %91
41: ; preds = %34
br label %42
42: ; preds = %54, %41
%43 = load i32*, i32** %6
%44 = call i32 @getc(i32* %43)
store i32 %44, i32* %9
%45 = load i32, i32* @EOF
%46 = icmp ne i32 %44, %45
br i1 %46, label %47, label %55
47: ; preds = %42
%48 = load i32, i32* %9
%49 = icmp ne i32 %48, 13
br i1 %49, label %50, label %54
50: ; preds = %47
%51 = load i32*, i32** %7
%52 = load i32, i32* %9
%53 = call i32 @fprintf(i32* %51, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 %52)
br label %54
54: ; preds = %50, %47
br label %42
55: ; preds = %42
%56 = load i32*, i32** %7
%57 = call i32 @fflush(i32* %56)
%58 = load i32*, i32** %7
%59 = call i32 @rewind(i32* %58)
%60 = load i32*, i32** %6
%61 = call i32 @fclose(i32* %60)
%62 = load i8*, i8** %8
%63 = call i32* @fopen(i8* %62, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i32* %63, i32** %6
%64 = icmp ne i32* %63, null
br i1 %64, label %70, label %65
65: ; preds = %55
%66 = load i8*, i8** %8
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0), i8* %66)
%68 = load i32*, i32** %7
%69 = call i32 @fclose(i32* %68)
store i32 1, i32* %11
br label %90
70: ; preds = %55
br label %71
71: ; preds = %83, %70
%72 = load i32*, i32** %7
%73 = call i32 @getc(i32* %72)
store i32 %73, i32* %9
%74 = load i32, i32* @EOF
%75 = icmp ne i32 %73, %74
br i1 %75, label %76, label %84
76: ; preds = %71
%77 = load i32, i32* %9
%78 = icmp ne i32 %77, 13
br i1 %78, label %79, label %83
79: ; preds = %76
%80 = load i32*, i32** %6
%81 = load i32, i32* %9
%82 = call i32 @fprintf(i32* %80, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 %81)
br label %83
83: ; preds = %79, %76
br label %71
84: ; preds = %71
%85 = load i32*, i32** %6
%86 = call i32 @fclose(i32* %85)
%87 = load i32*, i32** %7
%88 = call i32 @fclose(i32* %87)
%89 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0))
br label %90
90: ; preds = %84, %65
br label %91
91: ; preds = %90, %37
br label %92
92: ; preds = %91, %32
br label %93
93: ; preds = %92
%94 = load i32, i32* %10
%95 = add i32 %94, 1
store i32 %95, i32* %10
br label %17
96: ; preds = %17
%97 = load i32, i32* %11
%98 = call i32 @exit(i32 %97)
unreachable
}

LLVM IR (-O2):
@.str = [5 x i8] c"%s: \00"
@.str.1 = [2 x i8] c"r\00"
@EOF = global i32 0
@.str.5 = [2 x i8] c"w\00"
@.str.6 = [23 x i8] c"No write access to %s\0A\00"
@str = [17 x i8] c"Cannot open file\00"
@str.8 = [27 x i8] c"Cannot open temporary file\00"
@str.9 = [3 x i8] c"OK\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = tail call i32 (...) @usage()
%6 = icmp ugt i32 %0, 1
br i1 %6, label %7, label %68
7: ; preds = %2, %4
%8 = zext i32 %0 to i64
br label %9
9: ; preds = %7, %64
%10 = phi i64 [ 1, %7 ], [ %66, %64 ]
%11 = phi i32 [ 0, %7 ], [ %65, %64 ]
%12 = getelementptr inbounds i8*, i8** %1, i64 %10
%13 = load i8*, i8** %12
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %13)
%15 = tail call i32* @fopen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %19
17: ; preds = %9
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
br label %64
19: ; preds = %9
%20 = tail call i32* (...) @tmpfile()
%21 = icmp eq i32* %20, null
br i1 %21, label %26, label %22
22: ; preds = %19
%23 = tail call i32 @getc(i32* %15)
%24 = load i32, i32* @EOF
%25 = icmp eq i32 %23, %24
br i1 %25, label %38, label %29
26: ; preds = %19
%27 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.8, i64 0, i64 0))
%28 = tail call i32 @fclose(i32* %15)
br label %64
29: ; preds = %22, %34
%30 = phi i32 [ %35, %34 ], [ %23, %22 ]
%31 = icmp eq i32 %30, 13
br i1 %31, label %34, label %32
32: ; preds = %29
%33 = tail call i32 @fputc(i32 %30, i32* %20)
br label %34
34: ; preds = %32, %29
%35 = tail call i32 @getc(i32* %15)
%36 = load i32, i32* @EOF
%37 = icmp eq i32 %35, %36
br i1 %37, label %38, label %29
38: ; preds = %34, %22
%39 = tail call i32 @fflush(i32* %20)
%40 = tail call i32 @rewind(i32* %20)
%41 = tail call i32 @fclose(i32* %15)
%42 = tail call i32* @fopen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%43 = icmp eq i32* %42, null
br i1 %43, label %48, label %44
44: ; preds = %38
%45 = tail call i32 @getc(i32* %20)
%46 = load i32, i32* @EOF
%47 = icmp eq i32 %45, %46
br i1 %47, label %60, label %51
48: ; preds = %38
%49 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0), i8* %13)
%50 = tail call i32 @fclose(i32* %20)
br label %64
51: ; preds = %44, %56
%52 = phi i32 [ %57, %56 ], [ %45, %44 ]
%53 = icmp eq i32 %52, 13
br i1 %53, label %56, label %54
54: ; preds = %51
%55 = tail call i32 @fputc(i32 %52, i32* %42)
br label %56
56: ; preds = %54, %51
%57 = tail call i32 @getc(i32* %20)
%58 = load i32, i32* @EOF
%59 = icmp eq i32 %57, %58
br i1 %59, label %60, label %51
60: ; preds = %56, %44
%61 = tail call i32 @fclose(i32* %42)
%62 = tail call i32 @fclose(i32* %20)
%63 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @str.9, i64 0, i64 0))
br label %64
64: ; preds = %17, %48, %60, %26
%65 = phi i32 [ %11, %60 ], [ 1, %48 ], [ 1, %26 ], [ 1, %17 ]
%66 = add nuw nsw i64 %10, 1
%67 = icmp eq i64 %66, %8
br i1 %67, label %68, label %9
68: ; preds = %64, %4
%69 = phi i32 [ 0, %4 ], [ %65, %64 ]
%70 = tail call i32 @exit(i32 %69)
unreachable
}