#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_putc(int);
void console_write(const char *s, int n) {
  char c;
  while (((c = *s++) != (char)0) && (n-- != 0)) {
    (void)console_putc((int)c);
  }
}


x86 assembly (clang, -O0):
<console_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
mov    (%rax),%al
mov    %al,-0xd(%rbp)
movsbl %al,%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0xe(%rbp)
je     1158 <console_write+0x48>
mov    -0xc(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0xc(%rbp)
cmp    $0x0,%eax
setne  %al
mov    %al,-0xe(%rbp)
mov    -0xe(%rbp),%al
test   $0x1,%al
jne    1168 <console_write+0x58>
jmp    1176 <console_write+0x66>
movsbl -0xd(%rbp),%edi
call   1030 <console_putc@plt>
jmp    111f <console_write+0xf>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<console_write>:
push   %r15
push   %r14
push   %rbx
mov    (%rdi),%al
test   %al,%al
je     114d <console_write+0x3d>
test   %esi,%esi
je     114d <console_write+0x3d>
mov    %rdi,%r14
mov    %esi,%r15d
mov    $0x1,%ebx
nopw   0x0(%rax,%rax,1)
movsbl %al,%edi
call   1030 <console_putc@plt>
movzbl (%r14,%rbx,1),%eax
test   %al,%al
je     114d <console_write+0x3d>
lea    0x1(%rbx),%rcx
cmp    %ebx,%r15d
mov    %rcx,%rbx
jne    1130 <console_write+0x20>
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @console_write(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8
store i8* %0, i8** %3
store i32 %1, i32* %4
br label %6
6: ; preds = %18, %2
%7 = load i8*, i8** %3
%8 = getelementptr inbounds i8, i8* %7, i32 1
store i8* %8, i8** %3
%9 = load i8, i8* %7
store i8 %9, i8* %5
%10 = sext i8 %9 to i32
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %6
%13 = load i32, i32* %4
%14 = add nsw i32 %13, -1
store i32 %14, i32* %4
%15 = icmp ne i32 %13, 0
br label %16
16: ; preds = %12, %6
%17 = phi i1 [ false, %6 ], [ %15, %12 ]
br i1 %17, label %18, label %22
18: ; preds = %16
%19 = load i8, i8* %5
%20 = sext i8 %19 to i32
%21 = call i32 @console_putc(i32 %20)
br label %6
22: ; preds = %16
ret void
}

LLVM IR (-O2):
define void @console_write(i8* nocapture %0, i32 %1) {
%3 = load i8, i8* %0
%4 = icmp eq i8 %3, 0
%5 = icmp eq i32 %1, 0
%6 = or i1 %4, %5
br i1 %6, label %19, label %7
7: ; preds = %2, %7
%8 = phi i8 [ %15, %7 ], [ %3, %2 ]
%9 = phi i8* [ %12, %7 ], [ %0, %2 ]
%10 = phi i32 [ %13, %7 ], [ %1, %2 ]
%11 = sext i8 %8 to i32
%12 = getelementptr inbounds i8, i8* %9, i64 1
%13 = add nsw i32 %10, -1
%14 = tail call i32 @console_putc(i32 %11)
%15 = load i8, i8* %12
%16 = icmp eq i8 %15, 0
%17 = icmp eq i32 %13, 0
%18 = select i1 %16, i1 true, i1 %17
br i1 %18, label %19, label %7
19: ; preds = %7, %2
ret void
}