#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*/ _stdout_hook(char);
int write(int fd, char *buf, int nbytes) {
  int i;
  for (i = 0; i < nbytes; i++) {
    if (*(buf + i) == '\n') {
      _stdout_hook('\r');
    }
    _stdout_hook(*(buf + i));
  }
  return nbytes;
}


x86 assembly (clang, -O0):
<write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1173 <write+0x63>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    1154 <write+0x44>
mov    $0xd,%edi
call   1030 <_stdout_hook@plt>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1030 <_stdout_hook@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1129 <write+0x19>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
test   %edx,%edx
jle    1159 <write+0x49>
mov    %rsi,%rbx
mov    %r14d,%r15d
xor    %ebp,%ebp
jmp    1141 <write+0x31>
nopl   0x0(%rax,%rax,1)
movsbl %al,%edi
call   1030 <_stdout_hook@plt>
add    $0x1,%rbp
cmp    %rbp,%r15
je     1159 <write+0x49>
movzbl (%rbx,%rbp,1),%eax
cmp    $0xa,%al
jne    1130 <write+0x20>
mov    $0xd,%edi
call   1030 <_stdout_hook@plt>
movzbl (%rbx,%rbp,1),%eax
jmp    1130 <write+0x20>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @write(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 0, i32* %7
br label %8
8: ; preds = %29, %3
%9 = load i32, i32* %7
%10 = load i32, i32* %6
%11 = icmp slt i32 %9, %10
br i1 %11, label %12, label %32
12: ; preds = %8
%13 = load i8*, i8** %5
%14 = load i32, i32* %7
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %13, i64 %15
%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 = %12
%21 = call i32 @_stdout_hook(i8 signext 13)
br label %22
22: ; preds = %20, %12
%23 = load i8*, i8** %5
%24 = load i32, i32* %7
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %23, i64 %25
%27 = load i8, i8* %26
%28 = call i32 @_stdout_hook(i8 signext %27)
br label %29
29: ; preds = %22
%30 = load i32, i32* %7
%31 = add nsw i32 %30, 1
store i32 %31, i32* %7
br label %8
32: ; preds = %8
%33 = load i32, i32* %6
ret i32 %33
}

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