#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*/ fb_write_cell(unsigned int, char const, unsigned char,
                                 unsigned char);
int fb_write_string(unsigned int i, const char *str, unsigned int len,
                    unsigned char bg, unsigned char fg) {
  unsigned int index, fb_size = 80 * 25;
  for (index = 0; index < fb_size - i; index++) {
    if (str[index] == 0)
      break;
    if (index >= len)
      break;
    fb_write_cell(i + index, str[index], fg, bg);
  }
  return (int)index;
}


x86 assembly (clang, -O0):
<fb_write_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %r8b,%al
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %cl,-0x15(%rbp)
mov    %al,-0x16(%rbp)
movl   $0x7d0,-0x20(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    -0x20(%rbp),%ecx
sub    -0x4(%rbp),%ecx
cmp    %ecx,%eax
jae    11a4 <fb_write_string+0x94>
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
jne    1163 <fb_write_string+0x53>
jmp    11a4 <fb_write_string+0x94>
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jb     1174 <fb_write_string+0x64>
jmp    11a4 <fb_write_string+0x94>
mov    -0x4(%rbp),%edi
add    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
mov    (%rax,%rcx,1),%cl
mov    -0x16(%rbp),%al
movsbl %cl,%esi
movzbl %al,%edx
movzbl -0x15(%rbp),%ecx
call   1030 <fb_write_cell@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1139 <fb_write_string+0x29>
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fb_write_string>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,(%rsp)
xor    %r15d,%r15d
cmp    $0x7d0,%edi
je     1180 <fb_write_string+0x70>
mov    $0x7d0,%r15d
sub    %edi,%r15d
mov    %edx,%r12d
mov    %edi,%r14d
xor    %ebp,%ebp
movzbl %r8b,%r13d
movzbl %cl,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    (%rsp),%rax
movzbl (%rax,%rbp,1),%eax
test   %al,%al
je     117d <fb_write_string+0x6d>
cmp    %r12,%rbp
jae    117d <fb_write_string+0x6d>
lea    (%r14,%rbp,1),%edi
movsbl %al,%esi
mov    %r13d,%edx
mov    %ebx,%ecx
call   1030 <fb_write_cell@plt>
add    $0x1,%rbp
cmp    %rbp,%r15
jne    1150 <fb_write_string+0x40>
jmp    1180 <fb_write_string+0x70>
mov    %ebp,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @fb_write_string(i32 %0, i8* nocapture %1, i32 %2, i8 zeroext %3, i8 zeroext %4) {
%6 = icmp eq i32 %0, 2000
br i1 %6, label %26, label %7
7: ; preds = %5
%8 = sub i32 2000, %0
%9 = zext i32 %2 to i64
%10 = zext i32 %8 to i64
br label %11
11: ; preds = %7, %19
%12 = phi i64 [ 0, %7 ], [ %22, %19 ]
%13 = getelementptr inbounds i8, i8* %1, i64 %12
%14 = load i8, i8* %13
%15 = icmp ne i8 %14, 0
%16 = icmp ult i64 %12, %9
%17 = select i1 %15, i1 %16, i1 false
%18 = trunc i64 %12 to i32
br i1 %17, label %19, label %24
19: ; preds = %11
%20 = add i32 %18, %0
%21 = tail call i32 @fb_write_cell(i32 %20, i8 signext %14, i8 zeroext %4, i8 zeroext %3)
%22 = add nuw nsw i64 %12, 1
%23 = icmp eq i64 %22, %10
br i1 %23, label %26, label %11
24: ; preds = %11
%25 = trunc i64 %12 to i32
br label %26
26: ; preds = %24, %19, %5
%27 = phi i32 [ 0, %5 ], [ %25, %24 ], [ %8, %19 ]
ret i32 %27
}