#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*/ ch;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ SEEK_CUR;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
void dos2unix(const char *file_name) {
  FILE *fp = NULL;
  fp = fopen(file_name, "rb+");
  if (fp != NULL) {
    char ch;
    while (fread(&ch, sizeof(ch), 1, fp) == 1) {
      if (0x0D == ch) {
        ch = 0x20;
        fseek(fp, -1L, SEEK_CUR);
        fwrite(&ch, sizeof(ch), 1, fp);
        fseek(fp, 1L, SEEK_CUR);
      }
    }
  }
  if (fp != NULL) {
    fclose(fp);
    fp = NULL;
  }
}


x86 assembly (clang, -O0):
<dos2unix>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe91(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1208 <dos2unix+0xb8>
jmp    1188 <dos2unix+0x38>
mov    -0x10(%rbp),%rcx
lea    -0x11(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1030 <fread@plt>
cmp    $0x1,%eax
jne    1203 <dos2unix+0xb3>
movsbl -0x11(%rbp),%ecx
mov    $0xd,%eax
cmp    %ecx,%eax
jne    11fe <dos2unix+0xae>
movb   $0x20,-0x11(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e23(%rip),%rax
mov    (%rax),%edx
mov    $0xffffffffffffffff,%rsi
call   1050 <fseek@plt>
mov    -0x10(%rbp),%rcx
lea    -0x11(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1070 <fwrite@plt>
mov    -0x10(%rbp),%rdi
mov    0x2df6(%rip),%rax
mov    (%rax),%edx
mov    $0x1,%esi
call   1050 <fseek@plt>
jmp    1188 <dos2unix+0x38>
jmp    1208 <dos2unix+0xb8>
cmpq   $0x0,-0x10(%rbp)
je     1224 <dos2unix+0xd4>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
movq   $0x0,-0x10(%rbp)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dos2unix>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
lea    0xea0(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     1208 <dos2unix+0xb8>
mov    %rax,%rbx
lea    0xf(%rsp),%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
cmp    $0x1,%eax
jne    11f7 <dos2unix+0xa7>
mov    0x2e54(%rip),%r15
lea    0xf(%rsp),%r14
jmp    11ba <dos2unix+0x6a>
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
cmp    $0x1,%eax
jne    11f7 <dos2unix+0xa7>
cmpb   $0xd,0xf(%rsp)
jne    11a0 <dos2unix+0x50>
movb   $0x20,0xf(%rsp)
mov    (%r15),%edx
mov    %rbx,%rdi
mov    $0xffffffffffffffff,%rsi
call   1060 <fseek@plt>
mov    $0x20,%edi
mov    %rbx,%rsi
call   1050 <fputc@plt>
mov    (%r15),%edx
mov    $0x1,%esi
mov    %rbx,%rdi
call   1060 <fseek@plt>
jmp    11a0 <dos2unix+0x50>
mov    %rbx,%rdi
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
jmp    1040 <fclose@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [4 x i8] c"rb+\00"
@SEEK_CUR = global i32 0
define void @dos2unix(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i8
store i8* %0, i8** %2
store i32* null, i32** %3
%5 = load i8*, i8** %2
%6 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i32* %6, i32** %3
%7 = load i32*, i32** %3
%8 = icmp ne i32* %7, null
br i1 %8, label %9, label %29
9: ; preds = %1
br label %10
10: ; preds = %27, %9
%11 = load i32*, i32** %3
%12 = call i32 @fread(i8* %4, i32 1, i32 1, i32* %11)
%13 = icmp eq i32 %12, 1
br i1 %13, label %14, label %28
14: ; preds = %10
%15 = load i8, i8* %4
%16 = sext i8 %15 to i32
%17 = icmp eq i32 13, %16
br i1 %17, label %18, label %27
18: ; preds = %14
store i8 32, i8* %4
%19 = load i32*, i32** %3
%20 = load i32, i32* @SEEK_CUR
%21 = call i32 @fseek(i32* %19, i64 -1, i32 %20)
%22 = load i32*, i32** %3
%23 = call i32 @fwrite(i8* %4, i32 1, i32 1, i32* %22)
%24 = load i32*, i32** %3
%25 = load i32, i32* @SEEK_CUR
%26 = call i32 @fseek(i32* %24, i64 1, i32 %25)
br label %27
27: ; preds = %18, %14
br label %10
28: ; preds = %10
br label %29
29: ; preds = %28, %1
%30 = load i32*, i32** %3
%31 = icmp ne i32* %30, null
br i1 %31, label %32, label %35
32: ; preds = %29
%33 = load i32*, i32** %3
%34 = call i32 @fclose(i32* %33)
store i32* null, i32** %3
br label %35
35: ; preds = %32, %29
ret void
}

LLVM IR (-O2):
@.str = [4 x i8] c"rb+\00"
@SEEK_CUR = global i32 0
define void @dos2unix(i8* nocapture %0) {
%2 = alloca i8
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %22, label %5
5: ; preds = %1
%6 = call i32 @fread(i8* %2, i32 1, i32 1, i32* %3)
%7 = icmp eq i32 %6, 1
br i1 %7, label %8, label %20
8: ; preds = %5, %17
%9 = load i8, i8* %2
%10 = icmp eq i8 %9, 13
br i1 %10, label %11, label %17
11: ; preds = %8
store i8 32, i8* %2
%12 = load i32, i32* @SEEK_CUR
%13 = tail call i32 @fseek(i32* %3, i64 -1, i32 %12)
%14 = tail call i32 @fputc(i32 32, i32* %3)
%15 = load i32, i32* @SEEK_CUR
%16 = tail call i32 @fseek(i32* %3, i64 1, i32 %15)
br label %17
17: ; preds = %11, %8
%18 = call i32 @fread(i8* %2, i32 1, i32 1, i32* %3)
%19 = icmp eq i32 %18, 1
br i1 %19, label %8, label %20
20: ; preds = %17, %5
%21 = tail call i32 @fclose(i32* %3)
br label %22
22: ; preds = %1, %20
ret void
}