#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 */
typedef int /*<<< orphan*/ uint16_t;

/* Variables and functions */
int /*<<< orphan*/ drawtext(int, int, char const *, int /*<<< orphan*/);
int /*<<< orphan*/ drawtextrange(int, int, int, char const *,
                                 int /*<<< orphan*/);
int textwidth(char const *, int /*<<< orphan*/);
void drawtextwidth_right(int x, int width, int y, const char *str,
                         uint16_t length) {
  const int w = textwidth(str, length);
  if (w <= width) {
    drawtext(x + width - w, y, str, length);
  } else {
    drawtextrange(x, x + width, y, str, length);
  }
}


x86 assembly (clang, -O0):
<drawtextwidth_right>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %rcx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1040 <textwidth@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x8(%rbp),%eax
jg     1181 <drawtextwidth_right+0x51>
mov    -0x4(%rbp),%edi
add    -0x8(%rbp),%edi
sub    -0x20(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
call   1030 <drawtext@plt>
jmp    119a <drawtextwidth_right+0x6a>
mov    -0x4(%rbp),%edi
mov    -0x4(%rbp),%esi
add    -0x8(%rbp),%esi
mov    -0xc(%rbp),%edx
mov    -0x18(%rbp),%rcx
mov    -0x1c(%rbp),%r8d
call   1050 <drawtextrange@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<drawtextwidth_right>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %r8d,%r15d
mov    %rcx,%r12
mov    %edx,%r14d
mov    %esi,%ebx
mov    %edi,%ebp
mov    %rcx,%rdi
mov    %r8d,%esi
call   1040 <textwidth@plt>
lea    (%rbx,%rbp,1),%esi
cmp    %ebx,%eax
jle    116f <drawtextwidth_right+0x3f>
mov    %ebp,%edi
mov    %r14d,%edx
mov    %r12,%rcx
mov    %r15d,%r8d
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <drawtextrange@plt>
sub    %eax,%esi
mov    %esi,%edi
mov    %r14d,%esi
mov    %r12,%rdx
mov    %r15d,%ecx
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <drawtext@plt>

LLVM IR (-O0):
define void @drawtextwidth_right(i32 %0, i32 %1, i32 %2, i8* %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i8* %3, i8** %9
store i32 %4, i32* %10
%12 = load i8*, i8** %9
%13 = load i32, i32* %10
%14 = call i32 @textwidth(i8* %12, i32 %13)
store i32 %14, i32* %11
%15 = load i32, i32* %11
%16 = load i32, i32* %7
%17 = icmp sle i32 %15, %16
br i1 %17, label %18, label %28
18: ; preds = %5
%19 = load i32, i32* %6
%20 = load i32, i32* %7
%21 = add nsw i32 %19, %20
%22 = load i32, i32* %11
%23 = sub nsw i32 %21, %22
%24 = load i32, i32* %8
%25 = load i8*, i8** %9
%26 = load i32, i32* %10
%27 = call i32 @drawtext(i32 %23, i32 %24, i8* %25, i32 %26)
br label %37
28: ; preds = %5
%29 = load i32, i32* %6
%30 = load i32, i32* %6
%31 = load i32, i32* %7
%32 = add nsw i32 %30, %31
%33 = load i32, i32* %8
%34 = load i8*, i8** %9
%35 = load i32, i32* %10
%36 = call i32 @drawtextrange(i32 %29, i32 %32, i32 %33, i8* %34, i32 %35)
br label %37
37: ; preds = %28, %18
ret void
}

LLVM IR (-O2):
define void @drawtextwidth_right(i32 %0, i32 %1, i32 %2, i8* %3, i32 %4) {
%6 = tail call i32 @textwidth(i8* %3, i32 %4)
%7 = icmp sgt i32 %6, %1
%8 = add nsw i32 %1, %0
br i1 %7, label %12, label %9
9: ; preds = %5
%10 = sub i32 %8, %6
%11 = tail call i32 @drawtext(i32 %10, i32 %2, i8* %3, i32 %4)
br label %14
12: ; preds = %5
%13 = tail call i32 @drawtextrange(i32 %0, i32 %8, i32 %2, i8* %3, i32 %4)
br label %14
14: ; preds = %12, %9
ret void
}