#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*/ COLOR_PAIR(int);
int /*<<< orphan*/ attroff(int /*<<< orphan*/);
int /*<<< orphan*/ attron(int /*<<< orphan*/);
size_t count_bytes(char const *, size_t, size_t, unsigned int);
int /*<<< orphan*/ draw_rect(int, int, unsigned int, int);
int /*<<< orphan*/ move(int, int);
int /*<<< orphan*/ mvprintw(int, int, char *, size_t, char const *);
unsigned int string_lines(char const *, size_t, unsigned int);
void draw_text_rect(const char *str, size_t strlen, int color_id, int x, int y,
                    unsigned width) {
  unsigned line = 0;
  unsigned lines = string_lines(str, strlen, width);
  size_t str_offset = 0;
  attron(COLOR_PAIR(color_id));
  draw_rect(x, y + line, width, lines - line == 0 ? 1 : lines - line);
  move(y, x);
  for (; line < lines; ++line) {
    size_t len = count_bytes(str, strlen, str_offset, width);
    if (y + (int)line >= 0) {
      mvprintw(y + line, x, "%.*s", len, str + str_offset);
    }
    str_offset += len;
  }
  attroff(COLOR_PAIR(color_id));
}


x86 assembly (clang, -O0):
<draw_text_rect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
mov    %r9d,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   1080 <string_lines@plt>
mov    %eax,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x14(%rbp),%edi
call   1070 <COLOR_PAIR@plt>
mov    %eax,%edi
call   1050 <attron@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x1c(%rbp),%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x28(%rbp),%eax
sub    -0x24(%rbp),%eax
cmp    $0x0,%eax
jne    1200 <draw_text_rect+0x80>
mov    $0x1,%eax
mov    %eax,-0x48(%rbp)
jmp    1209 <draw_text_rect+0x89>
mov    -0x28(%rbp),%eax
sub    -0x24(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    -0x3c(%rbp),%edx
mov    -0x40(%rbp),%esi
mov    -0x44(%rbp),%edi
mov    -0x48(%rbp),%ecx
call   1060 <draw_rect@plt>
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%esi
call   10a0 <move@plt>
mov    -0x24(%rbp),%eax
cmp    -0x28(%rbp),%eax
jae    1293 <draw_text_rect+0x113>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x30(%rbp),%rdx
mov    -0x20(%rbp),%ecx
call   1040 <count_bytes@plt>
mov    %rax,-0x38(%rbp)
mov    -0x1c(%rbp),%eax
add    -0x24(%rbp),%eax
cmp    $0x0,%eax
jl     1279 <draw_text_rect+0xf9>
mov    -0x1c(%rbp),%edi
add    -0x24(%rbp),%edi
mov    -0x18(%rbp),%esi
mov    -0x38(%rbp),%rcx
mov    -0x8(%rbp),%r8
add    -0x30(%rbp),%r8
lea    0xd8c(%rip),%rdx
call   1090 <mvprintw@plt>
mov    -0x38(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1225 <draw_text_rect+0xa5>
mov    -0x14(%rbp),%edi
call   1070 <COLOR_PAIR@plt>
mov    %eax,%edi
call   1030 <attroff@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<draw_text_rect>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r9d,%r13d
mov    %r8d,%ebp
mov    %ecx,%r12d
mov    %edx,%r15d
mov    %rdi,%rbx
mov    %rsi,0x10(%rsp)
mov    %r9d,%edx
call   1080 <string_lines@plt>
mov    %eax,%r14d
mov    %r15d,0x8(%rsp)
mov    %r15d,%edi
call   1070 <COLOR_PAIR@plt>
mov    %eax,%edi
call   1050 <attron@plt>
test   %r14d,%r14d
mov    $0x1,%ecx
cmovne %r14d,%ecx
mov    %r12d,%edi
mov    %ebp,%esi
mov    %r13d,%edx
call   1060 <draw_rect@plt>
mov    %ebp,%edi
mov    %r12d,0xc(%rsp)
mov    %r12d,%esi
call   10a0 <move@plt>
test   %r14d,%r14d
je     1241 <draw_text_rect+0xc1>
xor    %r15d,%r15d
jmp    120c <draw_text_rect+0x8c>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    %r12,%r15
add    $0x1,%ebp
add    $0xffffffff,%r14d
je     1241 <draw_text_rect+0xc1>
mov    %rbx,%rdi
mov    0x10(%rsp),%rsi
mov    %r15,%rdx
mov    %r13d,%ecx
call   1040 <count_bytes@plt>
mov    %rax,%r12
test   %ebp,%ebp
js     1200 <draw_text_rect+0x80>
lea    (%rbx,%r15,1),%r8
mov    %ebp,%edi
mov    0xc(%rsp),%esi
lea    0xdc9(%rip),%rdx
mov    %r12,%rcx
call   1090 <mvprintw@plt>
jmp    1200 <draw_text_rect+0x80>
mov    0x8(%rsp),%edi
call   1070 <COLOR_PAIR@plt>
mov    %eax,%edi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <attroff@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"%.*s\00"
define void @draw_text_rect(i8* %0, i64 %1, i32 %2, i32 %3, i32 %4, i32 %5) {
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i64
%16 = alloca i64
store i8* %0, i8** %7
store i64 %1, i64* %8
store i32 %2, i32* %9
store i32 %3, i32* %10
store i32 %4, i32* %11
store i32 %5, i32* %12
store i32 0, i32* %13
%17 = load i8*, i8** %7
%18 = load i64, i64* %8
%19 = load i32, i32* %12
%20 = call i32 @string_lines(i8* %17, i64 %18, i32 %19)
store i32 %20, i32* %14
store i64 0, i64* %15
%21 = load i32, i32* %9
%22 = call i32 @COLOR_PAIR(i32 %21)
%23 = call i32 @attron(i32 %22)
%24 = load i32, i32* %10
%25 = load i32, i32* %11
%26 = load i32, i32* %13
%27 = add i32 %25, %26
%28 = load i32, i32* %12
%29 = load i32, i32* %14
%30 = load i32, i32* %13
%31 = sub i32 %29, %30
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %6
br label %38
34: ; preds = %6
%35 = load i32, i32* %14
%36 = load i32, i32* %13
%37 = sub i32 %35, %36
br label %38
38: ; preds = %34, %33
%39 = phi i32 [ 1, %33 ], [ %37, %34 ]
%40 = call i32 @draw_rect(i32 %24, i32 %27, i32 %28, i32 %39)
%41 = load i32, i32* %11
%42 = load i32, i32* %10
%43 = call i32 @move(i32 %41, i32 %42)
br label %44
44: ; preds = %72, %38
%45 = load i32, i32* %13
%46 = load i32, i32* %14
%47 = icmp ult i32 %45, %46
br i1 %47, label %48, label %75
48: ; preds = %44
%49 = load i8*, i8** %7
%50 = load i64, i64* %8
%51 = load i64, i64* %15
%52 = load i32, i32* %12
%53 = call i64 @count_bytes(i8* %49, i64 %50, i64 %51, i32 %52)
store i64 %53, i64* %16
%54 = load i32, i32* %11
%55 = load i32, i32* %13
%56 = add nsw i32 %54, %55
%57 = icmp sge i32 %56, 0
br i1 %57, label %58, label %68
58: ; preds = %48
%59 = load i32, i32* %11
%60 = load i32, i32* %13
%61 = add i32 %59, %60
%62 = load i32, i32* %10
%63 = load i64, i64* %16
%64 = load i8*, i8** %7
%65 = load i64, i64* %15
%66 = getelementptr inbounds i8, i8* %64, i64 %65
%67 = call i32 @mvprintw(i32 %61, i32 %62, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i64 %63, i8* %66)
br label %68
68: ; preds = %58, %48
%69 = load i64, i64* %16
%70 = load i64, i64* %15
%71 = add i64 %70, %69
store i64 %71, i64* %15
br label %72
72: ; preds = %68
%73 = load i32, i32* %13
%74 = add i32 %73, 1
store i32 %74, i32* %13
br label %44
75: ; preds = %44
%76 = load i32, i32* %9
%77 = call i32 @COLOR_PAIR(i32 %76)
%78 = call i32 @attroff(i32 %77)
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"%.*s\00"
define void @draw_text_rect(i8* %0, i64 %1, i32 %2, i32 %3, i32 %4, i32 %5) {
%7 = tail call i32 @string_lines(i8* %0, i64 %1, i32 %5)
%8 = tail call i32 @COLOR_PAIR(i32 %2)
%9 = tail call i32 @attron(i32 %8)
%10 = icmp eq i32 %7, 0
%11 = select i1 %10, i32 1, i32 %7
%12 = tail call i32 @draw_rect(i32 %3, i32 %4, i32 %5, i32 %11)
%13 = tail call i32 @move(i32 %4, i32 %3)
br i1 %10, label %27, label %14
14: ; preds = %6, %23
%15 = phi i32 [ %25, %23 ], [ 0, %6 ]
%16 = phi i64 [ %24, %23 ], [ 0, %6 ]
%17 = tail call i64 @count_bytes(i8* %0, i64 %1, i64 %16, i32 %5)
%18 = add i32 %15, %4
%19 = icmp sgt i32 %18, -1
br i1 %19, label %20, label %23
20: ; preds = %14
%21 = getelementptr inbounds i8, i8* %0, i64 %16
%22 = tail call i32 @mvprintw(i32 %18, i32 %3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i64 %17, i8* %21)
br label %23
23: ; preds = %20, %14
%24 = add i64 %17, %16
%25 = add nuw i32 %15, 1
%26 = icmp eq i32 %25, %7
br i1 %26, label %27, label %14
27: ; preds = %23, %6
%28 = tail call i32 @COLOR_PAIR(i32 %2)
%29 = tail call i32 @attroff(i32 %28)
ret void
}