#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 size_t ssize_t;

/* Variables and functions */
int /*<<< orphan*/ uart_send_byte(char);
ssize_t write(int fd, const void *buf, size_t count) {
  size_t i;
  char *ptr = (char *)buf;
  if (fd != 1 && fd != 2)
    return 0;
  for (i = 0; i < count; i++) {
    if (*ptr == '\n')
      uart_send_byte('\r');
    uart_send_byte(*ptr++);
  }
  return i;
}


x86 assembly (clang, -O0):
<write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpl   $0x1,-0xc(%rbp)
je     114c <write+0x3c>
cmpl   $0x2,-0xc(%rbp)
je     114c <write+0x3c>
movq   $0x0,-0x8(%rbp)
jmp    11ac <write+0x9c>
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    11a4 <write+0x94>
mov    -0x30(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xa,%eax
jne    117c <write+0x6c>
mov    $0xd,%edi
call   1030 <uart_send_byte@plt>
mov    -0x30(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x30(%rbp)
movsbl (%rax),%edi
call   1030 <uart_send_byte@plt>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    1154 <write+0x44>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write>:
push   %r15
push   %r14
push   %rbx
add    $0xffffffff,%edi
xor    %eax,%eax
cmp    $0x1,%edi
ja     115e <write+0x4e>
mov    %rdx,%r14
test   %rdx,%rdx
je     115e <write+0x4e>
mov    %rsi,%r15
xor    %ebx,%ebx
jmp    1141 <write+0x31>
xchg   %ax,%ax
movsbl %al,%edi
call   1030 <uart_send_byte@plt>
add    $0x1,%rbx
cmp    %rbx,%r14
je     115b <write+0x4b>
movzbl (%r15,%rbx,1),%eax
cmp    $0xa,%al
jne    1130 <write+0x20>
mov    $0xd,%edi
call   1030 <uart_send_byte@plt>
movzbl (%r15,%rbx,1),%eax
jmp    1130 <write+0x20>
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i64 @write(i32 %0, i8* nocapture %1, i64 %2) {
%4 = add i32 %0, -1
%5 = icmp ult i32 %4, 2
%6 = icmp ne i64 %2, 0
%7 = and i1 %5, %6
br i1 %7, label %8, label %22
8: ; preds = %3, %16
%9 = phi i8* [ %18, %16 ], [ %1, %3 ]
%10 = phi i64 [ %20, %16 ], [ 0, %3 ]
%11 = load i8, i8* %9
%12 = icmp eq i8 %11, 10
br i1 %12, label %13, label %16
13: ; preds = %8
%14 = tail call i32 @uart_send_byte(i8 signext 13)
%15 = load i8, i8* %9
br label %16
16: ; preds = %13, %8
%17 = phi i8 [ %15, %13 ], [ %11, %8 ]
%18 = getelementptr inbounds i8, i8* %9, i64 1
%19 = tail call i32 @uart_send_byte(i8 signext %17)
%20 = add nuw i64 %10, 1
%21 = icmp eq i64 %20, %2
br i1 %21, label %22, label %8
22: ; preds = %16, %3
%23 = phi i64 [ 0, %3 ], [ %2, %16 ]
ret i64 %23
}