#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 lf_to_crlf(char *line, ssize_t linelen, FILE *output_stream) {
  if (linelen == 1) {
    if (line[0] == '\n') {
      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] == '\n') {
      line[linelen - 1] = '\r';
      fputs(line, output_stream);
      fputc('\n', output_stream);
    } else {
      fputs(line, output_stream);
    }
  }
}


x86 assembly (clang, -O0):
<lf_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 <lf_to_crlf+0x65>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xa,%eax
jne    116e <lf_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 <lf_to_crlf+0x60>
mov    -0x8(%rbp),%rax
mov    (%rax),%al
mov    -0x18(%rbp),%rsi
movsbl %al,%edi
call   1040 <fputc@plt>
jmp    122d <lf_to_crlf+0x10d>
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
sub    $0x2,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xd,%eax
jne    11cb <lf_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 <lf_to_crlf+0xab>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <fputs@plt>
jmp    1228 <lf_to_crlf+0x108>
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    1216 <lf_to_crlf+0xf6>
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0xd,(%rax,%rcx,1)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <fputs@plt>
mov    -0x18(%rbp),%rsi
mov    $0xa,%edi
call   1040 <fputc@plt>
jmp    1223 <lf_to_crlf+0x103>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <fputs@plt>
jmp    1228 <lf_to_crlf+0x108>
jmp    122d <lf_to_crlf+0x10d>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lf_to_crlf>:
push   %rbx
mov    %rdx,%rbx
cmp    $0x1,%esi
jne    113e <lf_to_crlf+0x1e>
mov    (%rdi),%al
cmp    $0xa,%al
jne    1173 <lf_to_crlf+0x53>
mov    $0xd,%edi
mov    %rbx,%rsi
call   1040 <fputc@plt>
jmp    1163 <lf_to_crlf+0x43>
movslq %esi,%rax
cmpb   $0xd,-0x2(%rax,%rdi,1)
jne    114f <lf_to_crlf+0x2f>
cmpb   $0xa,-0x1(%rdi,%rax,1)
je     116a <lf_to_crlf+0x4a>
cmpb   $0xa,-0x1(%rdi,%rax,1)
jne    116a <lf_to_crlf+0x4a>
movb   $0xd,-0x1(%rdi,%rax,1)
mov    %rbx,%rsi
call   1030 <fputs@plt>
mov    $0xa,%edi
jmp    1176 <lf_to_crlf+0x56>
mov    %rbx,%rsi
pop    %rbx
jmp    1030 <fputs@plt>
movsbl %al,%edi
mov    %rbx,%rsi
pop    %rbx
jmp    1040 <fputc@plt>

LLVM IR (-O0):
define void @lf_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, 10
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 %75
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 %74
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, 10
br i1 %57, label %58, label %69
58: ; preds = %49
%59 = load i8*, i8** %4
%60 = load i32, i32* %5
%61 = sub nsw i32 %60, 1
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds i8, i8* %59, i64 %62
store i8 13, i8* %63
%64 = load i8*, i8** %4
%65 = load i32*, i32** %6
%66 = call i32 @fputs(i8* %64, i32* %65)
%67 = load i32*, i32** %6
%68 = call i32 @fputc(i8 signext 10, i32* %67)
br label %73
69: ; preds = %49
%70 = load i8*, i8** %4
%71 = load i32*, i32** %6
%72 = call i32 @fputs(i8* %70, i32* %71)
br label %73
73: ; preds = %69, %58
br label %74
74: ; preds = %73, %45
br label %75
75: ; preds = %74, %26
ret void
}

LLVM IR (-O2):
define void @lf_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, 10
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 %36
11: ; preds = %5
%12 = tail call i32 @fputc(i8 signext %6, i32* %2)
br label %36
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 %36
27: ; preds = %13, %21
%28 = getelementptr inbounds i8, i8* %0, i64 %20
%29 = load i8, i8* %28
%30 = icmp eq i8 %29, 10
br i1 %30, label %31, label %34
31: ; preds = %27
store i8 13, i8* %28
%32 = tail call i32 @fputs(i8* %0, i32* %2)
%33 = tail call i32 @fputc(i8 signext 10, i32* %2)
br label %36
34: ; preds = %27
%35 = tail call i32 @fputs(i8* %0, i32* %2)
br label %36
36: ; preds = %25, %34, %31, %8, %11
ret void
}