#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 */
struct _reent {
  int dummy;
};
typedef size_t ssize_t;

/* Variables and functions */
int STDERR_FILENO;
int STDOUT_FILENO;
int /*<<< orphan*/ rs232_transmitchar(char);
ssize_t _write_r(struct _reent *reent, int fd, const void *data,
                 size_t length) {
  if ((fd == STDOUT_FILENO) || (fd == STDERR_FILENO)) {
    for (size_t i = 0; i < length; i++) {
      char c = ((char *)data)[i];
      if (c == '\n') {
        rs232_transmitchar('\r');
      }
      rs232_transmitchar(c);
    }
    return length;
  }
  return 0;
}


x86 assembly (clang, -O0):
<_write_r>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x14(%rbp),%eax
mov    0x2ebf(%rip),%rcx
cmp    (%rcx),%eax
je     114b <_write_r+0x3b>
mov    -0x14(%rbp),%eax
mov    0x2e95(%rip),%rcx
cmp    (%rcx),%eax
jne    11ad <_write_r+0x9d>
movq   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x28(%rbp),%rax
jae    11a0 <_write_r+0x90>
mov    -0x20(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0x31(%rbp)
movsbl -0x31(%rbp),%eax
cmp    $0xa,%eax
jne    1186 <_write_r+0x76>
mov    $0xd,%edi
call   1030 <rs232_transmitchar@plt>
movsbl -0x31(%rbp),%edi
call   1030 <rs232_transmitchar@plt>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    1153 <_write_r+0x43>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11b5 <_write_r+0xa5>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_write_r>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2ed2(%rip),%rax
cmp    %esi,(%rax)
setne  %bl
mov    0x2eae(%rip),%rax
cmp    %esi,(%rax)
mov    %rcx,%r15
setne  %cl
xor    %eax,%eax
test   %cl,%bl
jne    117c <_write_r+0x6c>
test   %r15,%r15
je     117c <_write_r+0x6c>
mov    %rdx,%r14
xor    %ebx,%ebx
jmp    1162 <_write_r+0x52>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movsbl %bpl,%edi
call   1030 <rs232_transmitchar@plt>
add    $0x1,%rbx
cmp    %rbx,%r15
je     1179 <_write_r+0x69>
movzbl (%r14,%rbx,1),%ebp
cmp    $0xa,%bpl
jne    1150 <_write_r+0x40>
mov    $0xd,%edi
call   1030 <rs232_transmitchar@plt>
jmp    1150 <_write_r+0x40>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct._reent = type { i32 }
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
define i64 @_write_r(%struct._reent* %0, i32 %1, i8* %2, i64 %3) {
%5 = alloca i64
%6 = alloca %struct._reent*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i64
%11 = alloca i8
store %struct._reent* %0, %struct._reent** %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i64 %3, i64* %9
%12 = load i32, i32* %7
%13 = load i32, i32* @STDOUT_FILENO
%14 = icmp eq i32 %12, %13
br i1 %14, label %19, label %15
15: ; preds = %4
%16 = load i32, i32* %7
%17 = load i32, i32* @STDERR_FILENO
%18 = icmp eq i32 %16, %17
br i1 %18, label %19, label %42
19: ; preds = %15, %4
store i64 0, i64* %10
br label %20
20: ; preds = %37, %19
%21 = load i64, i64* %10
%22 = load i64, i64* %9
%23 = icmp ult i64 %21, %22
br i1 %23, label %24, label %40
24: ; preds = %20
%25 = load i8*, i8** %8
%26 = load i64, i64* %10
%27 = getelementptr inbounds i8, i8* %25, i64 %26
%28 = load i8, i8* %27
store i8 %28, i8* %11
%29 = load i8, i8* %11
%30 = sext i8 %29 to i32
%31 = icmp eq i32 %30, 10
br i1 %31, label %32, label %34
32: ; preds = %24
%33 = call i32 @rs232_transmitchar(i8 signext 13)
br label %34
34: ; preds = %32, %24
%35 = load i8, i8* %11
%36 = call i32 @rs232_transmitchar(i8 signext %35)
br label %37
37: ; preds = %34
%38 = load i64, i64* %10
%39 = add i64 %38, 1
store i64 %39, i64* %10
br label %20
40: ; preds = %20
%41 = load i64, i64* %9
store i64 %41, i64* %5
br label %43
42: ; preds = %15
store i64 0, i64* %5
br label %43
43: ; preds = %42, %40
%44 = load i64, i64* %5
ret i64 %44
}

LLVM IR (-O2):
%struct._reent = type { i32 }
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
define i64 @_write_r(%struct._reent* nocapture readnone %0, i32 %1, i8* nocapture %2, i64 %3) {
%5 = load i32, i32* @STDOUT_FILENO
%6 = icmp ne i32 %5, %1
%7 = load i32, i32* @STDERR_FILENO
%8 = icmp ne i32 %7, %1
%9 = select i1 %6, i1 %8, i1 false
%10 = icmp eq i64 %3, 0
%11 = or i1 %9, %10
br i1 %11, label %23, label %12
12: ; preds = %4, %19
%13 = phi i64 [ %21, %19 ], [ 0, %4 ]
%14 = getelementptr inbounds i8, i8* %2, i64 %13
%15 = load i8, i8* %14
%16 = icmp eq i8 %15, 10
br i1 %16, label %17, label %19
17: ; preds = %12
%18 = tail call i32 @rs232_transmitchar(i8 signext 13)
br label %19
19: ; preds = %17, %12
%20 = tail call i32 @rs232_transmitchar(i8 signext %15)
%21 = add nuw i64 %13, 1
%22 = icmp eq i64 %21, %3
br i1 %22, label %23, label %12
23: ; preds = %19, %4
%24 = phi i64 [ 0, %4 ], [ %3, %19 ]
ret i64 %24
}