#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 wordstart(char const *, unsigned int);
unsigned int maxfit(const char *text, unsigned int count, unsigned int width) {
  unsigned int total = 0;
  unsigned int i;
  for (i = 0; i < count; i++) {
    int advance;
    if (text[i] == '\n')
      break;
    backend_font_getmetrics("default", text[i], NULL, NULL, NULL, NULL,
                            &advance);
    total += advance;
    if (total >= width)
      return wordstart(text, i);
  }
  return i;
}


x86 assembly (clang, -O0):
<maxfit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x14(%rbp),%eax
jae    11d0 <maxfit+0xb0>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%ecx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    1165 <maxfit+0x45>
jmp    11d0 <maxfit+0xb0>
mov    -0x10(%rbp),%rcx
mov    -0x20(%rbp),%eax
mov    %eax,%edx
lea    0xe8b(%rip),%rdi
xor    %eax,%eax
mov    %eax,%r9d
lea    -0x24(%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    -0x24(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x18(%rbp),%eax
jb     11bd <maxfit+0x9d>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%esi
call   1040 <wordstart@plt>
mov    %eax,-0x4(%rbp)
jmp    11d6 <maxfit+0xb6>
jmp    11c2 <maxfit+0xa2>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1140 <maxfit+0x20>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<maxfit>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
test   %esi,%esi
je     118e <maxfit+0x6e>
mov    %edx,%r12d
mov    %rdi,%r14
mov    %esi,%ebp
xor    %ebx,%ebx
lea    0xebd(%rip),%r15
xor    %r13d,%r13d
cs nopw 0x0(%rax,%rax,1)
movzbl (%r14,%rbx,1),%eax
cmp    $0xa,%al
je     119e <maxfit+0x7e>
lea    0x14(%rsp),%rcx
mov    %rcx,(%rsp)
movsbl %al,%esi
mov    %r15,%rdi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1030 <backend_font_getmetrics@plt>
add    0x14(%rsp),%r13d
cmp    %r12d,%r13d
jae    1192 <maxfit+0x72>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1150 <maxfit+0x30>
mov    %ebp,%ebx
jmp    119e <maxfit+0x7e>
xor    %ebx,%ebx
jmp    119e <maxfit+0x7e>
mov    %r14,%rdi
mov    %ebx,%esi
call   1040 <wordstart@plt>
mov    %eax,%ebx
mov    %ebx,%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 @maxfit(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 0, i32* %8
store i32 0, i32* %9
br label %11
11: ; preds = %42, %3
%12 = load i32, i32* %9
%13 = load i32, i32* %6
%14 = icmp ult i32 %12, %13
br i1 %14, label %15, label %45
15: ; preds = %11
%16 = load i8*, i8** %5
%17 = load i32, i32* %9
%18 = zext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %16, i64 %18
%20 = load i8, i8* %19
%21 = sext i8 %20 to i32
%22 = icmp eq i32 %21, 10
br i1 %22, label %23, label %24
23: ; preds = %15
br label %45
24: ; preds = %15
%25 = load i8*, i8** %5
%26 = load i32, i32* %9
%27 = zext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %25, i64 %27
%29 = load i8, i8* %28
%30 = call i32 @backend_font_getmetrics(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8 signext %29, i32* null, i32* null, i32* null, i32* null, i32* %10)
%31 = load i32, i32* %10
%32 = load i32, i32* %8
%33 = add i32 %32, %31
store i32 %33, i32* %8
%34 = load i32, i32* %8
%35 = load i32, i32* %7
%36 = icmp uge i32 %34, %35
br i1 %36, label %37, label %41
37: ; preds = %24
%38 = load i8*, i8** %5
%39 = load i32, i32* %9
%40 = call i32 @wordstart(i8* %38, i32 %39)
store i32 %40, i32* %4
br label %47
41: ; preds = %24
br label %42
42: ; preds = %41
%43 = load i32, i32* %9
%44 = add i32 %43, 1
store i32 %44, i32* %9
br label %11
45: ; preds = %23, %11
%46 = load i32, i32* %9
store i32 %46, i32* %4
br label %47
47: ; preds = %45, %37
%48 = load i32, i32* %4
ret i32 %48
}

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