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

/* Variables and functions */
int /*<<< orphan*/ fputc(char, int /*<<< orphan*/ *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
void cr_to_crlf(char *line, ssize_t linelen, FILE *output_stream) {
  if (linelen == 1) {
    if (line[0] == '\r') {
      fputc('\r', output_stream);
      fputc('\n', output_stream);
    } else {
      fputc(line[0], output_stream);
    }
  } else {
    if (line[linelen - 2] == '\r' && line[linelen - 1] == '\n') {
      fputs(line, output_stream);
    } else if (line[linelen - 1] == '\r') {
      fputs(line, output_stream);
      fputc('\n', output_stream);
    } else {
      fputs(line, output_stream);
    }
  }
}


x86 assembly (clang, -O0):
<cr_to_crlf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
cmpl   $0x1,-0xc(%rbp)
jne    1185 <cr_to_crlf+0x65>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xd,%eax
jne    116e <cr_to_crlf+0x4e>
mov    -0x18(%rbp),%rsi
mov    $0xd,%edi
call   1040 <fputc@plt>
mov    -0x18(%rbp),%rsi
mov    $0xa,%edi
call   1040 <fputc@plt>
jmp    1180 <cr_to_crlf+0x60>
mov    -0x8(%rbp),%rax
mov    (%rax),%al
mov    -0x18(%rbp),%rsi
movsbl %al,%edi
call   1040 <fputc@plt>
jmp    121c <cr_to_crlf+0xfc>
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
sub    $0x2,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xd,%eax
jne    11cb <cr_to_crlf+0xab>
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    11cb <cr_to_crlf+0xab>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <fputs@plt>
jmp    1217 <cr_to_crlf+0xf7>
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xd,%eax
jne    1205 <cr_to_crlf+0xe5>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <fputs@plt>
mov    -0x18(%rbp),%rsi
mov    $0xa,%edi
call   1040 <fputc@plt>
jmp    1212 <cr_to_crlf+0xf2>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <fputs@plt>
jmp    1217 <cr_to_crlf+0xf7>
jmp    121c <cr_to_crlf+0xfc>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cr_to_crlf>:
push   %rbp
push   %rbx
push   %rax
mov    %rdx,%rbx
cmp    $0x1,%esi
jne    1145 <cr_to_crlf+0x25>
mov    (%rdi),%al
cmp    $0xd,%al
jne    117e <cr_to_crlf+0x5e>
mov    $0xd,%edi
mov    %rbx,%rsi
call   1040 <fputc@plt>
mov    $0xa,%edi
jmp    1181 <cr_to_crlf+0x61>
movslq %esi,%rax
cmpb   $0xd,-0x2(%rax,%rdi,1)
jne    1164 <cr_to_crlf+0x44>
cmpb   $0xa,-0x1(%rdi,%rax,1)
jne    1164 <cr_to_crlf+0x44>
mov    %rbx,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1030 <fputs@plt>
mov    -0x1(%rdi,%rax,1),%bpl
mov    %rbx,%rsi
call   1030 <fputs@plt>
cmp    $0xd,%bpl
jne    118f <cr_to_crlf+0x6f>
mov    $0xa,%edi
jmp    1181 <cr_to_crlf+0x61>
movsbl %al,%edi
mov    %rbx,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1040 <fputc@plt>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define void @cr_to_crlf(i8* %0, i32 %1, i32* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32*
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32* %2, i32** %6
%7 = load i32, i32* %5
%8 = icmp eq i32 %7, 1
br i1 %8, label %9, label %27
9: ; preds = %3
%10 = load i8*, i8** %4
%11 = getelementptr inbounds i8, i8* %10, i64 0
%12 = load i8, i8* %11
%13 = sext i8 %12 to i32
%14 = icmp eq i32 %13, 13
br i1 %14, label %15, label %20
15: ; preds = %9
%16 = load i32*, i32** %6
%17 = call i32 @fputc(i8 signext 13, i32* %16)
%18 = load i32*, i32** %6
%19 = call i32 @fputc(i8 signext 10, i32* %18)
br label %26
20: ; preds = %9
%21 = load i8*, i8** %4
%22 = getelementptr inbounds i8, i8* %21, i64 0
%23 = load i8, i8* %22
%24 = load i32*, i32** %6
%25 = call i32 @fputc(i8 signext %23, i32* %24)
br label %26
26: ; preds = %20, %15
br label %70
27: ; preds = %3
%28 = load i8*, i8** %4
%29 = load i32, i32* %5
%30 = sub nsw i32 %29, 2
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %28, i64 %31
%33 = load i8, i8* %32
%34 = sext i8 %33 to i32
%35 = icmp eq i32 %34, 13
br i1 %35, label %36, label %49
36: ; preds = %27
%37 = load i8*, i8** %4
%38 = load i32, i32* %5
%39 = sub nsw i32 %38, 1
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %37, i64 %40
%42 = load i8, i8* %41
%43 = sext i8 %42 to i32
%44 = icmp eq i32 %43, 10
br i1 %44, label %45, label %49
45: ; preds = %36
%46 = load i8*, i8** %4
%47 = load i32*, i32** %6
%48 = call i32 @fputs(i8* %46, i32* %47)
br label %69
49: ; preds = %36, %27
%50 = load i8*, i8** %4
%51 = load i32, i32* %5
%52 = sub nsw i32 %51, 1
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i8, i8* %50, i64 %53
%55 = load i8, i8* %54
%56 = sext i8 %55 to i32
%57 = icmp eq i32 %56, 13
br i1 %57, label %58, label %64
58: ; preds = %49
%59 = load i8*, i8** %4
%60 = load i32*, i32** %6
%61 = call i32 @fputs(i8* %59, i32* %60)
%62 = load i32*, i32** %6
%63 = call i32 @fputc(i8 signext 10, i32* %62)
br label %68
64: ; preds = %49
%65 = load i8*, i8** %4
%66 = load i32*, i32** %6
%67 = call i32 @fputs(i8* %65, i32* %66)
br label %68
68: ; preds = %64, %58
br label %69
69: ; preds = %68, %45
br label %70
70: ; preds = %69, %26
ret void
}

LLVM IR (-O2):
define void @cr_to_crlf(i8* nocapture %0, i32 %1, i32* nocapture %2) {
%4 = icmp eq i32 %1, 1
br i1 %4, label %5, label %13
5: ; preds = %3
%6 = load i8, i8* %0
%7 = icmp eq i8 %6, 13
br i1 %7, label %8, label %11
8: ; preds = %5
%9 = tail call i32 @fputc(i8 signext 13, i32* %2)
%10 = tail call i32 @fputc(i8 signext 10, i32* %2)
br label %34
11: ; preds = %5
%12 = tail call i32 @fputc(i8 signext %6, i32* %2)
br label %34
13: ; preds = %3
%14 = add nsw i32 %1, -2
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %0, i64 %15
%17 = load i8, i8* %16
%18 = icmp eq i8 %17, 13
%19 = add nsw i32 %1, -1
%20 = sext i32 %19 to i64
br i1 %18, label %21, label %27
21: ; preds = %13
%22 = getelementptr inbounds i8, i8* %0, i64 %20
%23 = load i8, i8* %22
%24 = icmp eq i8 %23, 10
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = tail call i32 @fputs(i8* %0, i32* %2)
br label %34
27: ; preds = %13, %21
%28 = getelementptr inbounds i8, i8* %0, i64 %20
%29 = load i8, i8* %28
%30 = icmp eq i8 %29, 13
%31 = tail call i32 @fputs(i8* %0, i32* %2)
br i1 %30, label %32, label %34
32: ; preds = %27
%33 = tail call i32 @fputc(i8 signext 10, i32* %2)
br label %34
34: ; preds = %27, %25, %32, %8, %11
ret void
}