#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 EOF;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ fputc(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
scalar_t__ remove(char *);
scalar_t__ rename(char *, char *);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  int i;
  FILE *src_fs;
  FILE *dst_fs;
  int ch;
  int foundCr = 0;
  if (argc < 2) {
    fprintf(stderr, "Usage: 2dos file ...\n");
    exit(1);
  }
  for (i = 1; i < argc; i++) {
    if ((src_fs = fopen(argv[i], "rb")) == NULL) {
      fprintf(stderr, "Unable to open file %s\n", argv[i]);
      exit(1);
    }
    if ((dst_fs = fopen("2dos.tmp", "wb")) == NULL) {
      fprintf(stderr, "Unable to open 2dos.tmp for output\n");
      exit(1);
    }
    while ((ch = fgetc(src_fs)) != EOF) {
      if (foundCr) {
        if (ch != 0x0D) {
          fputc(0x0D, dst_fs);
          fputc(0x0A, dst_fs);
        }
        switch (ch) {
        case 0x0D: {
          break;
        }
        case 0x0A: {
          foundCr = 0;
          break;
        }
        default: {
          foundCr = 0;
          fputc(ch, dst_fs);
          break;
        }
        }
      } else {
        switch (ch) {
        case 0x0D: {
          foundCr = 1;
          break;
        }
        case 0x0A: {
          fputc(0x0D, dst_fs);
          fputc(0x0A, dst_fs);
          break;
        }
        default: {
          fputc(ch, dst_fs);
          break;
        }
        }
      }
    }
    fclose(src_fs);
    fclose(dst_fs);
    if (remove(argv[i]) != 0) {
      perror(argv[i]);
      exit(1);
    }
    if (rename("2dos.tmp", argv[i]) != 0) {
      perror(argv[i]);
      exit(1);
    }
  }
  exit(0);
  return 0;
}


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,-0x30(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11d8 <main+0x48>
mov    0x2e1a(%rip),%rax
mov    (%rax),%edi
lea    0xe39(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
movl   $0x1,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    141e <main+0x28e>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
lea    0xe18(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    123e <main+0xae>
mov    0x2dc0(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rdx
lea    0xdec(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xdec(%rip),%rdi
lea    0xdee(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    1280 <main+0xf0>
mov    0x2d72(%rip),%rax
mov    (%rax),%edi
lea    0xdce(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
jmp    1285 <main+0xf5>
mov    -0x20(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %eax,-0x2c(%rbp)
mov    0x2d50(%rip),%rcx
cmp    (%rcx),%eax
je     1386 <main+0x1f6>
cmpl   $0x0,-0x30(%rbp)
je     131e <main+0x18e>
cmpl   $0xd,-0x2c(%rbp)
je     12d0 <main+0x140>
mov    -0x28(%rbp),%rsi
mov    $0xd,%edi
call   1060 <fputc@plt>
mov    -0x28(%rbp),%rsi
mov    $0xa,%edi
call   1060 <fputc@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x34(%rbp)
sub    $0xa,%eax
je     12fa <main+0x16a>
jmp    12e4 <main+0x154>
mov    -0x34(%rbp),%eax
sub    $0xd,%eax
jne    1306 <main+0x176>
jmp    12f5 <main+0x165>
jmp    1319 <main+0x189>
movl   $0x0,-0x30(%rbp)
jmp    1319 <main+0x189>
movl   $0x0,-0x30(%rbp)
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rsi
call   1060 <fputc@plt>
jmp    1381 <main+0x1f1>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x38(%rbp)
sub    $0xa,%eax
je     134f <main+0x1bf>
jmp    1332 <main+0x1a2>
mov    -0x38(%rbp),%eax
sub    $0xd,%eax
jne    1370 <main+0x1e0>
jmp    1343 <main+0x1b3>
movl   $0x1,-0x30(%rbp)
jmp    137c <main+0x1ec>
mov    -0x28(%rbp),%rsi
mov    $0xd,%edi
call   1060 <fputc@plt>
mov    -0x28(%rbp),%rsi
mov    $0xa,%edi
call   1060 <fputc@plt>
jmp    137c <main+0x1ec>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rsi
call   1060 <fputc@plt>
jmp    1381 <main+0x1f1>
jmp    1285 <main+0xf5>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1030 <remove@plt>
cmp    $0x0,%rax
je     13ce <main+0x23e>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
lea    0xc50(%rip),%rdi
call   10a0 <rename@plt>
cmp    $0x0,%rax
je     140b <main+0x27b>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
jmp    1410 <main+0x280>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11df <main+0x4f>
xor    %edi,%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
cmp    $0x2,%edi
jge    11c4 <main+0x34>
mov    0x2e2e(%rip),%rax
mov    (%rax),%edi
lea    0xe4d(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    %rsi,%r14
mov    %edi,%eax
mov    %rax,0x8(%rsp)
xor    %r13d,%r13d
mov    $0x1,%r12d
lea    0xe53(%rip),%rbp
mov    0x2e03(%rip),%r15
mov    %rsi,0x10(%rsp)
nopw   0x0(%rax,%rax,1)
mov    (%r14,%r12,8),%rdi
lea    0xe1b(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1311 <main+0x181>
mov    %rax,%rbx
mov    %rbp,%rdi
lea    0xe24(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1336 <main+0x1a6>
mov    %rax,%rbp
jmp    123e <main+0xae>
nopl   0x0(%rax)
mov    %r14d,%edi
mov    %rbp,%rsi
call   1060 <fputc@plt>
xor    %r13d,%r13d
mov    %rbx,%rdi
call   1050 <fgetc@plt>
mov    %eax,%r14d
cmp    (%r15),%eax
je     12c0 <main+0x130>
test   %r13d,%r13d
je     1290 <main+0x100>
mov    $0x1,%r13d
cmp    $0xd,%r14d
je     123e <main+0xae>
mov    $0xd,%edi
mov    %rbp,%rsi
call   1060 <fputc@plt>
mov    $0xa,%edi
mov    %rbp,%rsi
call   1060 <fputc@plt>
xor    %r13d,%r13d
cmp    $0xa,%r14d
je     123e <main+0xae>
jmp    1230 <main+0xa0>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
cmp    $0xd,%r14d
je     12b4 <main+0x124>
cmp    $0xa,%r14d
jne    1230 <main+0xa0>
mov    $0xd,%edi
mov    %rbp,%rsi
call   1060 <fputc@plt>
mov    $0xa,%r14d
jmp    1230 <main+0xa0>
mov    $0x1,%r13d
jmp    123e <main+0xae>
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %rbp,%rdi
call   1040 <fclose@plt>
mov    0x10(%rsp),%r14
mov    (%r14,%r12,8),%rdi
call   1030 <remove@plt>
mov    (%r14,%r12,8),%rsi
test   %rax,%rax
jne    134b <main+0x1bb>
lea    0xd43(%rip),%rbp
mov    %rbp,%rdi
call   10a0 <rename@plt>
test   %rax,%rax
jne    1350 <main+0x1c0>
add    $0x1,%r12
cmp    0x8(%rsp),%r12
jne    11f0 <main+0x60>
xor    %edi,%edi
call   10b0 <exit@plt>
mov    0x2cc0(%rip),%rax
mov    (%rax),%edi
mov    (%r14,%r12,8),%rdx
lea    0xcf4(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    0x2c9b(%rip),%rax
mov    (%rax),%edi
lea    0xcf7(%rip),%rsi
jmp    11b3 <main+0x23>
mov    %rsi,%rdi
jmp    1354 <main+0x1c4>
mov    (%r14,%r12,8),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [22 x i8] c"Usage: 2dos file ...\0A\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [24 x i8] c"Unable to open file %s\0A\00"
@.str.3 = [9 x i8] c"2dos.tmp\00"
@.str.4 = [3 x i8] c"wb\00"
@.str.5 = [36 x i8] c"Unable to open 2dos.tmp for output\0A\00"
@EOF = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %10
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 2
br i1 %12, label %13, label %17
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %2
store i32 1, i32* %6
br label %18
18: ; preds = %123, %17
%19 = load i32, i32* %6
%20 = load i32, i32* %4
%21 = icmp slt i32 %19, %20
br i1 %21, label %22, label %126
22: ; preds = %18
%23 = load i8**, i8*** %5
%24 = load i32, i32* %6
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8*, i8** %23, i64 %25
%27 = load i8*, i8** %26
%28 = call i32* @fopen(i8* %27, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %28, i32** %7
%29 = icmp eq i32* %28, null
br i1 %29, label %30, label %39
30: ; preds = %22
%31 = load i32, i32* @stderr
%32 = load i8**, i8*** %5
%33 = load i32, i32* %6
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8*, i8** %32, i64 %34
%36 = load i8*, i8** %35
%37 = call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %36)
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %22
%40 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
store i32* %40, i32** %8
%41 = icmp eq i32* %40, null
br i1 %41, label %42, label %46
42: ; preds = %39
%43 = load i32, i32* @stderr
%44 = call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0))
%45 = call i32 @exit(i32 1)
unreachable
46: ; preds = %39
br label %47
47: ; preds = %85, %46
%48 = load i32*, i32** %7
%49 = call i32 @fgetc(i32* %48)
store i32 %49, i32* %9
%50 = load i32, i32* @EOF
%51 = icmp ne i32 %49, %50
br i1 %51, label %52, label %86
52: ; preds = %47
%53 = load i32, i32* %10
%54 = icmp ne i32 %53, 0
br i1 %54, label %55, label %72
55: ; preds = %52
%56 = load i32, i32* %9
%57 = icmp ne i32 %56, 13
br i1 %57, label %58, label %63
58: ; preds = %55
%59 = load i32*, i32** %8
%60 = call i32 @fputc(i32 13, i32* %59)
%61 = load i32*, i32** %8
%62 = call i32 @fputc(i32 10, i32* %61)
br label %63
63: ; preds = %58, %55
%64 = load i32, i32* %9
switch i32 %64, label %67 [
i32 13, label %65
i32 10, label %66
]
65: ; preds = %63
br label %71
66: ; preds = %63
store i32 0, i32* %10
br label %71
67: ; preds = %63
store i32 0, i32* %10
%68 = load i32, i32* %9
%69 = load i32*, i32** %8
%70 = call i32 @fputc(i32 %68, i32* %69)
br label %71
71: ; preds = %67, %66, %65
br label %85
72: ; preds = %52
%73 = load i32, i32* %9
switch i32 %73, label %80 [
i32 13, label %74
i32 10, label %75
]
74: ; preds = %72
store i32 1, i32* %10
br label %84
75: ; preds = %72
%76 = load i32*, i32** %8
%77 = call i32 @fputc(i32 13, i32* %76)
%78 = load i32*, i32** %8
%79 = call i32 @fputc(i32 10, i32* %78)
br label %84
80: ; preds = %72
%81 = load i32, i32* %9
%82 = load i32*, i32** %8
%83 = call i32 @fputc(i32 %81, i32* %82)
br label %84
84: ; preds = %80, %75, %74
br label %85
85: ; preds = %84, %71
br label %47
86: ; preds = %47
%87 = load i32*, i32** %7
%88 = call i32 @fclose(i32* %87)
%89 = load i32*, i32** %8
%90 = call i32 @fclose(i32* %89)
%91 = load i8**, i8*** %5
%92 = load i32, i32* %6
%93 = sext i32 %92 to i64
%94 = getelementptr inbounds i8*, i8** %91, i64 %93
%95 = load i8*, i8** %94
%96 = call i64 @remove(i8* %95)
%97 = icmp ne i64 %96, 0
br i1 %97, label %98, label %106
98: ; preds = %86
%99 = load i8**, i8*** %5
%100 = load i32, i32* %6
%101 = sext i32 %100 to i64
%102 = getelementptr inbounds i8*, i8** %99, i64 %101
%103 = load i8*, i8** %102
%104 = call i32 @perror(i8* %103)
%105 = call i32 @exit(i32 1)
unreachable
106: ; preds = %86
%107 = load i8**, i8*** %5
%108 = load i32, i32* %6
%109 = sext i32 %108 to i64
%110 = getelementptr inbounds i8*, i8** %107, i64 %109
%111 = load i8*, i8** %110
%112 = call i64 @rename(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* %111)
%113 = icmp ne i64 %112, 0
br i1 %113, label %114, label %122
114: ; preds = %106
%115 = load i8**, i8*** %5
%116 = load i32, i32* %6
%117 = sext i32 %116 to i64
%118 = getelementptr inbounds i8*, i8** %115, i64 %117
%119 = load i8*, i8** %118
%120 = call i32 @perror(i8* %119)
%121 = call i32 @exit(i32 1)
unreachable
122: ; preds = %106
br label %123
123: ; preds = %122
%124 = load i32, i32* %6
%125 = add nsw i32 %124, 1
store i32 %125, i32* %6
br label %18
126: ; preds = %18
%127 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [22 x i8] c"Usage: 2dos file ...\0A\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [24 x i8] c"Unable to open file %s\0A\00"
@.str.3 = [9 x i8] c"2dos.tmp\00"
@.str.4 = [3 x i8] c"wb\00"
@.str.5 = [36 x i8] c"Unable to open 2dos.tmp for output\0A\00"
@EOF = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %6, label %4
4: ; preds = %2
%5 = zext i32 %0 to i64
br label %12
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = tail call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %67
%11 = icmp eq i64 %70, %5
br i1 %11, label %75, label %12
12: ; preds = %4, %10
%13 = phi i64 [ 1, %4 ], [ %70, %10 ]
%14 = phi i32 [ 0, %4 ], [ %57, %10 ]
%15 = getelementptr inbounds i8*, i8** %1, i64 %13
%16 = load i8*, i8** %15
%17 = tail call i32* @fopen(i8* %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %24
19: ; preds = %12
%20 = load i32, i32* @stderr
%21 = load i8*, i8** %15
%22 = tail call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %21)
%23 = tail call i32 @exit(i32 1)
unreachable
24: ; preds = %12
%25 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%26 = icmp eq i32* %25, null
br i1 %26, label %31, label %27
27: ; preds = %24
%28 = tail call i32 @fgetc(i32* %17)
%29 = load i32, i32* @EOF
%30 = icmp eq i32 %28, %29
br i1 %30, label %56, label %35
31: ; preds = %24
%32 = load i32, i32* @stderr
%33 = tail call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0))
%34 = tail call i32 @exit(i32 1)
unreachable
35: ; preds = %27, %51
%36 = phi i32 [ %53, %51 ], [ %28, %27 ]
%37 = phi i32 [ %52, %51 ], [ %14, %27 ]
%38 = icmp eq i32 %37, 0
br i1 %38, label %45, label %39
39: ; preds = %35
%40 = icmp eq i32 %36, 13
br i1 %40, label %51, label %41
41: ; preds = %39
%42 = tail call i32 @fputc(i32 13, i32* %25)
%43 = tail call i32 @fputc(i32 10, i32* %25)
%44 = icmp eq i32 %36, 10
br i1 %44, label %51, label %48
45: ; preds = %35
switch i32 %36, label %48 [
i32 13, label %51
i32 10, label %46
]
46: ; preds = %45
%47 = tail call i32 @fputc(i32 13, i32* %25)
br label %48
48: ; preds = %45, %41, %46
%49 = phi i32 [ 10, %46 ], [ %36, %41 ], [ %36, %45 ]
%50 = tail call i32 @fputc(i32 %49, i32* %25)
br label %51
51: ; preds = %48, %41, %39, %45
%52 = phi i32 [ 1, %39 ], [ 1, %45 ], [ 0, %41 ], [ 0, %48 ]
%53 = tail call i32 @fgetc(i32* %17)
%54 = load i32, i32* @EOF
%55 = icmp eq i32 %53, %54
br i1 %55, label %56, label %35
56: ; preds = %51, %27
%57 = phi i32 [ %14, %27 ], [ %52, %51 ]
%58 = tail call i32 @fclose(i32* %17)
%59 = tail call i32 @fclose(i32* %25)
%60 = load i8*, i8** %15
%61 = tail call i64 @remove(i8* %60)
%62 = icmp eq i64 %61, 0
%63 = load i8*, i8** %15
br i1 %62, label %67, label %64
64: ; preds = %56
%65 = tail call i32 @perror(i8* %63)
%66 = tail call i32 @exit(i32 1)
unreachable
67: ; preds = %56
%68 = tail call i64 @rename(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* %63)
%69 = icmp eq i64 %68, 0
%70 = add nuw nsw i64 %13, 1
br i1 %69, label %10, label %71
71: ; preds = %67
%72 = load i8*, i8** %15
%73 = tail call i32 @perror(i8* %72)
%74 = tail call i32 @exit(i32 1)
unreachable
75: ; preds = %10
%76 = tail call i32 @exit(i32 0)
unreachable
}