#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 */
char EOF;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
char fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, char *);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ remove(char *);
int sfd_filesize(int /*<<< orphan*/ *);
int sfd_fix_line_endings(char *fname) {
  FILE *f = fopen(fname, "rb");
  if (f != NULL) {
    char c, *buffer;
    int i = 0, size = sfd_filesize(f);
    buffer = malloc(size + 1);
    while ((c = fgetc(f)) != EOF)
      if (c != '\r')
        buffer[i++] = c;
    buffer[i] = '\0';
    fclose(f);
    remove(fname);
    if ((f = fopen(fname, "w+")) != NULL) {
      fprintf(f, "%s", buffer);
      free(buffer);
      fclose(f);
      return 0;
    }
  }
  return -1;
}


x86 assembly (clang, -O0):
<sfd_fix_line_endings>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe69(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     128b <sfd_fix_line_endings+0x10b>
movl   $0x0,-0x2c(%rbp)
mov    -0x18(%rbp),%rdi
call   10a0 <sfd_filesize@plt>
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%edi
add    $0x1,%edi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <fgetc@plt>
mov    %al,-0x19(%rbp)
movsbl %al,%eax
mov    0x2e05(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
je     1218 <sfd_fix_line_endings+0x98>
movsbl -0x19(%rbp),%eax
cmp    $0xd,%eax
je     1213 <sfd_fix_line_endings+0x93>
mov    -0x19(%rbp),%dl
mov    -0x28(%rbp),%rax
mov    -0x2c(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x2c(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
jmp    11cd <sfd_fix_line_endings+0x4d>
mov    -0x28(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x10(%rbp),%rdi
call   1040 <remove@plt>
mov    -0x10(%rbp),%rdi
lea    0xdc2(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     1286 <sfd_fix_line_endings+0x106>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rdx
lea    0xda3(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    1292 <sfd_fix_line_endings+0x112>
jmp    128b <sfd_fix_line_endings+0x10b>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sfd_fix_line_endings>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r15
lea    0xe6b(%rip),%rsi
call   1090 <fopen@plt>
mov    $0xffffffff,%r14d
test   %rax,%rax
je     121f <sfd_fix_line_endings+0x9f>
mov    %rax,%rbx
mov    %rax,%rdi
call   10a0 <sfd_filesize@plt>
lea    0x1(%rax),%edi
call   1080 <malloc@plt>
mov    %rax,%r12
mov    %rbx,%rdi
call   1070 <fgetc@plt>
mov    0x2e1e(%rip),%r13
xor    %ebp,%ebp
cmp    0x0(%r13),%al
jne    1231 <sfd_fix_line_endings+0xb1>
movslq %ebp,%rax
movb   $0x0,(%r12,%rax,1)
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %r15,%rdi
call   1040 <remove@plt>
lea    0xe12(%rip),%rsi
mov    %r15,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     121f <sfd_fix_line_endings+0x9f>
mov    %rax,%rbx
mov    %r12,%rdi
mov    %rax,%rsi
call   1060 <fputs@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %ebp,%ebp
jmp    124e <sfd_fix_line_endings+0xce>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
call   1070 <fgetc@plt>
cmp    0x0(%r13),%al
je     11d2 <sfd_fix_line_endings+0x52>
cmp    $0xd,%al
je     1240 <sfd_fix_line_endings+0xc0>
movslq %ebp,%rcx
add    $0x1,%ebp
mov    %al,(%r12,%rcx,1)
jmp    1240 <sfd_fix_line_endings+0xc0>

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@EOF = global i8 0
@.str.1 = [3 x i8] c"w+\00"
@.str.2 = [3 x i8] c"%s\00"
define i32 @sfd_fix_line_endings(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i8
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %3
%9 = load i8*, i8** %3
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %4
%11 = load i32*, i32** %4
%12 = icmp ne i32* %11, null
br i1 %12, label %13, label %59
13: ; preds = %1
store i32 0, i32* %7
%14 = load i32*, i32** %4
%15 = call i32 @sfd_filesize(i32* %14)
store i32 %15, i32* %8
%16 = load i32, i32* %8
%17 = add nsw i32 %16, 1
%18 = call i8* @malloc(i32 %17)
store i8* %18, i8** %6
br label %19
19: ; preds = %37, %13
%20 = load i32*, i32** %4
%21 = call signext i8 @fgetc(i32* %20)
store i8 %21, i8* %5
%22 = sext i8 %21 to i32
%23 = load i8, i8* @EOF
%24 = sext i8 %23 to i32
%25 = icmp ne i32 %22, %24
br i1 %25, label %26, label %38
26: ; preds = %19
%27 = load i8, i8* %5
%28 = sext i8 %27 to i32
%29 = icmp ne i32 %28, 13
br i1 %29, label %30, label %37
30: ; preds = %26
%31 = load i8, i8* %5
%32 = load i8*, i8** %6
%33 = load i32, i32* %7
%34 = add nsw i32 %33, 1
store i32 %34, i32* %7
%35 = sext i32 %33 to i64
%36 = getelementptr inbounds i8, i8* %32, i64 %35
store i8 %31, i8* %36
br label %37
37: ; preds = %30, %26
br label %19
38: ; preds = %19
%39 = load i8*, i8** %6
%40 = load i32, i32* %7
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %39, i64 %41
store i8 0, i8* %42
%43 = load i32*, i32** %4
%44 = call i32 @fclose(i32* %43)
%45 = load i8*, i8** %3
%46 = call i32 @remove(i8* %45)
%47 = load i8*, i8** %3
%48 = call i32* @fopen(i8* %47, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %48, i32** %4
%49 = icmp ne i32* %48, null
br i1 %49, label %50, label %58
50: ; preds = %38
%51 = load i32*, i32** %4
%52 = load i8*, i8** %6
%53 = call i32 @fprintf(i32* %51, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %52)
%54 = load i8*, i8** %6
%55 = call i32 @free(i8* %54)
%56 = load i32*, i32** %4
%57 = call i32 @fclose(i32* %56)
store i32 0, i32* %2
br label %60
58: ; preds = %38
br label %59
59: ; preds = %58, %1
store i32 -1, i32* %2
br label %60
60: ; preds = %59, %50
%61 = load i32, i32* %2
ret i32 %61
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@EOF = global i8 0
@.str.1 = [3 x i8] c"w+\00"
define i32 @sfd_fix_line_endings(i8* nocapture %0) {
%2 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %36, label %4
4: ; preds = %1
%5 = tail call i32 @sfd_filesize(i32* %2)
%6 = add nsw i32 %5, 1
%7 = tail call i8* @malloc(i32 %6)
%8 = tail call signext i8 @fgetc(i32* %2)
%9 = load i8, i8* @EOF
%10 = icmp eq i8 %8, %9
br i1 %10, label %24, label %11
11: ; preds = %4, %19
%12 = phi i8 [ %21, %19 ], [ %8, %4 ]
%13 = phi i32 [ %20, %19 ], [ 0, %4 ]
%14 = icmp eq i8 %12, 13
br i1 %14, label %19, label %15
15: ; preds = %11
%16 = add nsw i32 %13, 1
%17 = sext i32 %13 to i64
%18 = getelementptr inbounds i8, i8* %7, i64 %17
store i8 %12, i8* %18
br label %19
19: ; preds = %15, %11
%20 = phi i32 [ %16, %15 ], [ %13, %11 ]
%21 = tail call signext i8 @fgetc(i32* %2)
%22 = load i8, i8* @EOF
%23 = icmp eq i8 %21, %22
br i1 %23, label %24, label %11
24: ; preds = %19, %4
%25 = phi i32 [ 0, %4 ], [ %20, %19 ]
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %7, i64 %26
store i8 0, i8* %27
%28 = tail call i32 @fclose(i32* %2)
%29 = tail call i32 @remove(i8* %0)
%30 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%31 = icmp eq i32* %30, null
br i1 %31, label %36, label %32
32: ; preds = %24
%33 = tail call i32 @fputs(i8* %7, i32* %30)
%34 = tail call i32 @free(i8* %7)
%35 = tail call i32 @fclose(i32* %30)
br label %36
36: ; preds = %1, %24, %32
%37 = phi i32 [ 0, %32 ], [ -1, %24 ], [ -1, %1 ]
ret i32 %37
}