#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 gui_chat_char_size_screen(char const *);
char *gui_chat_string_next_char(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                                unsigned char *, int /*<<< orphan*/,
                                int /*<<< orphan*/, int /*<<< orphan*/);
char *utf8_next_char(char const *);
char *gui_chat_string_add_offset_screen(const char *string, int offset_screen) {
  int size_on_screen;
  while (string && string[0] && (offset_screen > 0)) {
    string =
        gui_chat_string_next_char(NULL, NULL, (unsigned char *)string, 0, 0, 0);
    if (string) {
      size_on_screen = gui_chat_char_size_screen(string);
      offset_screen -= size_on_screen;
      string = utf8_next_char(string);
    }
  }
  return (char *)string;
}


x86 assembly (clang, -O0):
<gui_chat_string_add_offset_screen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x8(%rbp)
mov    %al,-0x11(%rbp)
je     116e <gui_chat_string_add_offset_screen+0x3e>
mov    -0x8(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x11(%rbp)
je     116e <gui_chat_string_add_offset_screen+0x3e>
cmpl   $0x0,-0xc(%rbp)
setg   %al
mov    %al,-0x11(%rbp)
mov    -0x11(%rbp),%al
test   $0x1,%al
jne    117e <gui_chat_string_add_offset_screen+0x4e>
jmp    11cf <gui_chat_string_add_offset_screen+0x9f>
mov    -0x8(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%esi
xor    %r9d,%r9d
mov    %rsi,%rdi
mov    %r9d,%ecx
mov    %r9d,%r8d
call   1050 <gui_chat_string_next_char@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     11ca <gui_chat_string_add_offset_screen+0x9a>
mov    -0x8(%rbp),%rdi
call   1030 <gui_chat_char_size_screen@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%ecx
mov    -0xc(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <utf8_next_char@plt>
mov    %rax,-0x8(%rbp)
jmp    113f <gui_chat_string_add_offset_screen+0xf>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gui_chat_string_add_offset_screen>:
push   %rbp
push   %r14
push   %rbx
test   %rdi,%rdi
je     118e <gui_chat_string_add_offset_screen+0x5e>
mov    %esi,%ebp
mov    %rdi,%rax
xor    %r14d,%r14d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
cmpb   $0x0,(%rax)
je     1193 <gui_chat_string_add_offset_screen+0x63>
test   %ebp,%ebp
jle    1193 <gui_chat_string_add_offset_screen+0x63>
xor    %edi,%edi
xor    %esi,%esi
mov    %rax,%rdx
xor    %ecx,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1050 <gui_chat_string_next_char@plt>
test   %rax,%rax
je     1196 <gui_chat_string_add_offset_screen+0x66>
mov    %rax,%rbx
mov    %rax,%rdi
call   1030 <gui_chat_char_size_screen@plt>
sub    %eax,%ebp
mov    %rbx,%rdi
call   1040 <utf8_next_char@plt>
test   %rax,%rax
jne    1150 <gui_chat_string_add_offset_screen+0x20>
jmp    1196 <gui_chat_string_add_offset_screen+0x66>
xor    %r14d,%r14d
jmp    1196 <gui_chat_string_add_offset_screen+0x66>
mov    %rax,%r14
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i8* @gui_chat_string_add_offset_screen(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
br label %6
6: ; preds = %33, %2
%7 = load i8*, i8** %3
%8 = icmp ne i8* %7, null
br i1 %8, label %9, label %18
9: ; preds = %6
%10 = load i8*, i8** %3
%11 = getelementptr inbounds i8, i8* %10, i64 0
%12 = load i8, i8* %11
%13 = sext i8 %12 to i32
%14 = icmp ne i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %9
%16 = load i32, i32* %4
%17 = icmp sgt i32 %16, 0
br label %18
18: ; preds = %15, %9, %6
%19 = phi i1 [ false, %9 ], [ false, %6 ], [ %17, %15 ]
br i1 %19, label %20, label %34
20: ; preds = %18
%21 = load i8*, i8** %3
%22 = call i8* @gui_chat_string_next_char(i32* null, i32* null, i8* %21, i32 0, i32 0, i32 0)
store i8* %22, i8** %3
%23 = load i8*, i8** %3
%24 = icmp ne i8* %23, null
br i1 %24, label %25, label %33
25: ; preds = %20
%26 = load i8*, i8** %3
%27 = call i32 @gui_chat_char_size_screen(i8* %26)
store i32 %27, i32* %5
%28 = load i32, i32* %5
%29 = load i32, i32* %4
%30 = sub nsw i32 %29, %28
store i32 %30, i32* %4
%31 = load i8*, i8** %3
%32 = call i8* @utf8_next_char(i8* %31)
store i8* %32, i8** %3
br label %33
33: ; preds = %25, %20
br label %6
34: ; preds = %18
%35 = load i8*, i8** %3
ret i8* %35
}

LLVM IR (-O2):
define i8* @gui_chat_string_add_offset_screen(i8* %0, i32 %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %19, label %4
4: ; preds = %2, %14
%5 = phi i8* [ %17, %14 ], [ %0, %2 ]
%6 = phi i32 [ %16, %14 ], [ %1, %2 ]
%7 = load i8, i8* %5
%8 = icmp ne i8 %7, 0
%9 = icmp sgt i32 %6, 0
%10 = select i1 %8, i1 %9, i1 false
br i1 %10, label %11, label %19
11: ; preds = %4
%12 = tail call i8* @gui_chat_string_next_char(i32* null, i32* null, i8* %5, i32 0, i32 0, i32 0)
%13 = icmp eq i8* %12, null
br i1 %13, label %19, label %14
14: ; preds = %11
%15 = tail call i32 @gui_chat_char_size_screen(i8* %12)
%16 = sub nsw i32 %6, %15
%17 = tail call i8* @utf8_next_char(i8* %12)
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %4
19: ; preds = %11, %4, %14, %2
%20 = phi i8* [ null, %2 ], [ null, %11 ], [ null, %14 ], [ %5, %4 ]
ret i8* %20
}