#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*/ backend_font_getmetrics(char *, char const,
                                           int /*<<< orphan*/ *,
                                           int /*<<< orphan*/ *,
                                           int /*<<< orphan*/ *,
                                           int /*<<< orphan*/ *, int *);
unsigned int textw(const char *text, unsigned int count) {
  unsigned int total = 0;
  unsigned int i;
  for (i = 0; i < count; i++) {
    int advance;
    backend_font_getmetrics("default", text[i], NULL, NULL, NULL, NULL,
                            &advance);
    total += advance;
  }
  return total;
}


x86 assembly (clang, -O0):
<textw>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0xc(%rbp),%eax
jae    117f <textw+0x6f>
mov    -0x8(%rbp),%rcx
mov    -0x14(%rbp),%eax
mov    %eax,%edx
lea    0xeb7(%rip),%rdi
xor    %eax,%eax
mov    %eax,%r9d
lea    -0x18(%rbp),%rax
movsbl (%rcx,%rdx,1),%esi
mov    %r9,%rdx
mov    %r9,%rcx
mov    %r9,%r8
mov    %rax,(%rsp)
call   1030 <backend_font_getmetrics@plt>
mov    -0x18(%rbp),%eax
add    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    112d <textw+0x1d>
mov    -0x10(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<textw>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
test   %esi,%esi
je     116a <textw+0x5a>
mov    %rdi,%r15
mov    %esi,%r12d
xor    %ebx,%ebx
lea    0x14(%rsp),%r13
lea    0xeca(%rip),%r14
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
movsbl (%r15,%rbx,1),%esi
mov    %r13,(%rsp)
mov    %r14,%rdi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1030 <backend_font_getmetrics@plt>
add    0x14(%rsp),%ebp
add    $0x1,%rbx
cmp    %rbx,%r12
jne    1140 <textw+0x30>
jmp    116c <textw+0x5c>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [8 x i8] c"default\00"
define i32 @textw(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
store i32 0, i32* %5
store i32 0, i32* %6
br label %8
8: ; preds = %22, %2
%9 = load i32, i32* %6
%10 = load i32, i32* %4
%11 = icmp ult i32 %9, %10
br i1 %11, label %12, label %25
12: ; preds = %8
%13 = load i8*, i8** %3
%14 = load i32, i32* %6
%15 = zext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %13, i64 %15
%17 = load i8, i8* %16
%18 = call i32 @backend_font_getmetrics(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8 signext %17, i32* null, i32* null, i32* null, i32* null, i32* %7)
%19 = load i32, i32* %7
%20 = load i32, i32* %5
%21 = add i32 %20, %19
store i32 %21, i32* %5
br label %22
22: ; preds = %12
%23 = load i32, i32* %6
%24 = add i32 %23, 1
store i32 %24, i32* %6
br label %8
25: ; preds = %8
%26 = load i32, i32* %5
ret i32 %26
}

LLVM IR (-O2):
@.str = [8 x i8] c"default\00"
define i32 @textw(i8* nocapture %0, i32 %1) {
%3 = alloca i32
%4 = icmp eq i32 %1, 0
br i1 %4, label %18, label %5
5: ; preds = %2
%6 = bitcast i32* %3 to i8*
%7 = zext i32 %1 to i64
br label %8
8: ; preds = %5, %8
%9 = phi i64 [ 0, %5 ], [ %16, %8 ]
%10 = phi i32 [ 0, %5 ], [ %15, %8 ]
%11 = getelementptr inbounds i8, i8* %0, i64 %9
%12 = load i8, i8* %11
%13 = call i32 @backend_font_getmetrics(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8 signext %12, i32* null, i32* null, i32* null, i32* null, i32* %3)
%14 = load i32, i32* %3
%15 = add i32 %14, %10
%16 = add nuw nsw i64 %9, 1
%17 = icmp eq i64 %16, %7
br i1 %17, label %18, label %8
18: ; preds = %8, %2
%19 = phi i32 [ 0, %2 ], [ %15, %8 ]
ret i32 %19
}