#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*/ console_write(char *, int);
char *lib_strchr(char const *, char);
int lib_strlen(char const *);
int console_xprint(const char *str) {
  int count = 0;
  int len;
  const char *p;
  while ((p = lib_strchr(str, '\n'))) {
    console_write((char *)str, p - str);
    console_write("\r\n", 2);
    count += (p - str);
    str = p + 1;
  }
  len = lib_strlen(str);
  console_write((char *)str, len);
  count += len;
  return count;
}


x86 assembly (clang, -O0):
<console_xprint>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0xa,%esi
call   1030 <lib_strchr@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     11ac <console_xprint+0x7c>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%esi
call   1040 <console_write@plt>
lea    0xe84(%rip),%rdi
mov    $0x2,%esi
call   1040 <console_write@plt>
mov    -0x18(%rbp),%rcx
mov    -0x8(%rbp),%rax
sub    %rax,%rcx
movslq -0xc(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0xc(%rbp)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    1143 <console_xprint+0x13>
mov    -0x8(%rbp),%rdi
call   1050 <lib_strlen@plt>
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
call   1040 <console_write@plt>
mov    -0x10(%rbp),%eax
add    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<console_xprint>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%rbx
mov    $0xa,%esi
call   1030 <lib_strchr@plt>
test   %rax,%rax
je     119d <console_xprint+0x6d>
mov    %rax,%r12
xor    %r15d,%r15d
lea    0xea9(%rip),%r14
nopw   0x0(%rax,%rax,1)
mov    %r12d,%ebp
sub    %ebx,%ebp
mov    %rbx,%rdi
mov    %ebp,%esi
call   1040 <console_write@plt>
mov    %r14,%rdi
mov    $0x2,%esi
call   1040 <console_write@plt>
add    %ebp,%r15d
mov    %r12,%rbx
add    $0x1,%rbx
mov    %rbx,%rdi
mov    $0xa,%esi
call   1030 <lib_strchr@plt>
mov    %rax,%r12
test   %rax,%rax
jne    1160 <console_xprint+0x30>
jmp    11a0 <console_xprint+0x70>
xor    %r15d,%r15d
mov    %rbx,%rdi
call   1050 <lib_strlen@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
mov    %eax,%esi
call   1040 <console_write@plt>
add    %ebp,%r15d
mov    %r15d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"\0D\0A\00"
define i32 @console_xprint(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
store i8* %0, i8** %2
store i32 0, i32* %3
br label %6
6: ; preds = %10, %1
%7 = load i8*, i8** %2
%8 = call i8* @lib_strchr(i8* %7, i8 signext 10)
store i8* %8, i8** %5
%9 = icmp ne i8* %8, null
br i1 %9, label %10, label %31
10: ; preds = %6
%11 = load i8*, i8** %2
%12 = load i8*, i8** %5
%13 = load i8*, i8** %2
%14 = ptrtoint i8* %12 to i64
%15 = ptrtoint i8* %13 to i64
%16 = sub i64 %14, %15
%17 = trunc i64 %16 to i32
%18 = call i32 @console_write(i8* %11, i32 %17)
%19 = call i32 @console_write(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2)
%20 = load i8*, i8** %5
%21 = load i8*, i8** %2
%22 = ptrtoint i8* %20 to i64
%23 = ptrtoint i8* %21 to i64
%24 = sub i64 %22, %23
%25 = load i32, i32* %3
%26 = sext i32 %25 to i64
%27 = add nsw i64 %26, %24
%28 = trunc i64 %27 to i32
store i32 %28, i32* %3
%29 = load i8*, i8** %5
%30 = getelementptr inbounds i8, i8* %29, i64 1
store i8* %30, i8** %2
br label %6
31: ; preds = %6
%32 = load i8*, i8** %2
%33 = call i32 @lib_strlen(i8* %32)
store i32 %33, i32* %4
%34 = load i8*, i8** %2
%35 = load i32, i32* %4
%36 = call i32 @console_write(i8* %34, i32 %35)
%37 = load i32, i32* %4
%38 = load i32, i32* %3
%39 = add nsw i32 %38, %37
store i32 %39, i32* %3
%40 = load i32, i32* %3
ret i32 %40
}

LLVM IR (-O2):
@.str = [3 x i8] c"\0D\0A\00"
define i32 @console_xprint(i8* %0) {
%2 = tail call i8* @lib_strchr(i8* %0, i8 signext 10)
%3 = icmp eq i8* %2, null
br i1 %3, label %18, label %4
4: ; preds = %1, %4
%5 = phi i8* [ %16, %4 ], [ %2, %1 ]
%6 = phi i8* [ %15, %4 ], [ %0, %1 ]
%7 = phi i32 [ %14, %4 ], [ 0, %1 ]
%8 = ptrtoint i8* %5 to i64
%9 = ptrtoint i8* %6 to i64
%10 = sub i64 %8, %9
%11 = trunc i64 %10 to i32
%12 = tail call i32 @console_write(i8* %6, i32 %11)
%13 = tail call i32 @console_write(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2)
%14 = add i32 %7, %11
%15 = getelementptr inbounds i8, i8* %5, i64 1
%16 = tail call i8* @lib_strchr(i8* %15, i8 signext 10)
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %4
18: ; preds = %4, %1
%19 = phi i32 [ 0, %1 ], [ %14, %4 ]
%20 = phi i8* [ %0, %1 ], [ %15, %4 ]
%21 = tail call i32 @lib_strlen(i8* %20)
%22 = tail call i32 @console_write(i8* %20, i32 %21)
%23 = add nsw i32 %21, %19
ret i32 %23
}