#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 */

/* Variables and functions */
int /*<<< orphan*/ EBADF;
#define STDERR_FILENO 129
#define STDOUT_FILENO 128
int /*<<< orphan*/ errno;
int /*<<< orphan*/ uart_txChar(char);
__attribute__((__weak__)) int _write(int file, char *ptr, int len) {
  int n;
  switch (file) {
  case STDOUT_FILENO:
  case STDERR_FILENO:
    for (n = 0; n < len; n++) {
      if (*ptr == '\n') {
        uart_txChar('\r');
      }
      uart_txChar(*ptr++);
    }
    break;
  default:
    errno = EBADF;
    return -1;
  }
  return len;
}


x86 assembly (clang, -O0):
<_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%eax
add    $0xffffff80,%eax
sub    $0x1,%eax
ja     118d <_write+0x7d>
jmp    1136 <_write+0x26>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1188 <_write+0x78>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xa,%eax
jne    1163 <_write+0x53>
mov    $0xd,%edi
call   1030 <uart_txChar@plt>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movsbl (%rax),%edi
call   1030 <uart_txChar@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    113d <_write+0x2d>
jmp    11ab <_write+0x9b>
mov    0x2e44(%rip),%rax
mov    (%rax),%ecx
mov    0x2e43(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11b1 <_write+0xa1>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_write>:
push   %r15
push   %r14
push   %rbx
and    $0xfffffffe,%edi
cmp    $0x80,%edi
jne    115b <_write+0x4b>
mov    %edx,%r14d
test   %edx,%edx
jle    1173 <_write+0x63>
mov    %rsi,%r15
xor    %ebx,%ebx
jmp    1141 <_write+0x31>
xchg   %ax,%ax
movsbl %al,%edi
call   1030 <uart_txChar@plt>
add    $0x1,%rbx
cmp    %ebx,%r14d
je     1173 <_write+0x63>
movzbl (%r15,%rbx,1),%eax
cmp    $0xa,%al
jne    1130 <_write+0x20>
mov    $0xd,%edi
call   1030 <uart_txChar@plt>
movzbl (%r15,%rbx,1),%eax
jmp    1130 <_write+0x20>
mov    0x2e76(%rip),%rax
mov    (%rax),%eax
mov    0x2e75(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@EBADF = global i32 0
@errno = global i32 0
define weak i32 @_write(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%9 = load i32, i32* %5
switch i32 %9, label %31 [
i32 128, label %10
i32 129, label %10
]
10: ; preds = %3, %3
store i32 0, i32* %8
br label %11
11: ; preds = %27, %10
%12 = load i32, i32* %8
%13 = load i32, i32* %7
%14 = icmp slt i32 %12, %13
br i1 %14, label %15, label %30
15: ; preds = %11
%16 = load i8*, i8** %6
%17 = load i8, i8* %16
%18 = sext i8 %17 to i32
%19 = icmp eq i32 %18, 10
br i1 %19, label %20, label %22
20: ; preds = %15
%21 = call i32 @uart_txChar(i8 signext 13)
br label %22
22: ; preds = %20, %15
%23 = load i8*, i8** %6
%24 = getelementptr inbounds i8, i8* %23, i32 1
store i8* %24, i8** %6
%25 = load i8, i8* %23
%26 = call i32 @uart_txChar(i8 signext %25)
br label %27
27: ; preds = %22
%28 = load i32, i32* %8
%29 = add nsw i32 %28, 1
store i32 %29, i32* %8
br label %11
30: ; preds = %11
br label %33
31: ; preds = %3
%32 = load i32, i32* @EBADF
store i32 %32, i32* @errno
store i32 -1, i32* %4
br label %35
33: ; preds = %30
%34 = load i32, i32* %7
store i32 %34, i32* %4
br label %35
35: ; preds = %33, %31
%36 = load i32, i32* %4
ret i32 %36
}

LLVM IR (-O2):
@EBADF = global i32 0
@errno = global i32 0
define weak i32 @_write(i32 %0, i8* %1, i32 %2) {
%4 = and i32 %0, -2
%5 = icmp eq i32 %4, 128
br i1 %5, label %6, label %22
6: ; preds = %3
%7 = icmp sgt i32 %2, 0
br i1 %7, label %8, label %24
8: ; preds = %6, %16
%9 = phi i32 [ %20, %16 ], [ 0, %6 ]
%10 = phi i8* [ %18, %16 ], [ %1, %6 ]
%11 = load i8, i8* %10
%12 = icmp eq i8 %11, 10
br i1 %12, label %13, label %16
13: ; preds = %8
%14 = tail call i32 @uart_txChar(i8 signext 13)
%15 = load i8, i8* %10
br label %16
16: ; preds = %13, %8
%17 = phi i8 [ %15, %13 ], [ %11, %8 ]
%18 = getelementptr inbounds i8, i8* %10, i64 1
%19 = tail call i32 @uart_txChar(i8 signext %17)
%20 = add nuw nsw i32 %9, 1
%21 = icmp eq i32 %20, %2
br i1 %21, label %24, label %8
22: ; preds = %3
%23 = load i32, i32* @EBADF
store i32 %23, i32* @errno
br label %24
24: ; preds = %16, %6, %22
%25 = phi i32 [ -1, %22 ], [ %2, %6 ], [ %2, %16 ]
ret i32 %25
}