#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*/ draw_textn(unsigned char *, int, int, int, char const *, int,
                              unsigned short);
int strlen(char const *);
char *strstr(char const *, char *);
int draw_text(unsigned char *image, int startx, int starty, int width,
              const char *text, unsigned short int factor) {
  int num_nl = 0;
  const char *end, *begin;
  const int line_space = (factor + 1) * 9;
  end = text;
  while ((end = strstr(end, "\\n"))) {
    num_nl++;
    end += sizeof("\\n") - 1;
  }
  starty -= line_space * num_nl;
  begin = end = text;
  while ((end = strstr(end, "\\n"))) {
    int len = end - begin;
    draw_textn(image, startx, starty, width, begin, len, factor);
    end += sizeof("\\n") - 1;
    begin = end;
    starty += line_space;
  }
  draw_textn(image, startx, starty, width, begin, strlen(begin), factor);
  return 0;
}


x86 assembly (clang, -O0):
<draw_text>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %r9w,%ax
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    %r8,-0x20(%rbp)
mov    %ax,-0x22(%rbp)
movl   $0x0,-0x28(%rbp)
movzwl -0x22(%rbp),%eax
add    $0x1,%eax
imul   $0x9,%eax,%eax
mov    %eax,-0x3c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
lea    0xe88(%rip),%rsi
call   1050 <strstr@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     11a5 <draw_text+0x75>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
mov    -0x30(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x30(%rbp)
jmp    116d <draw_text+0x3d>
mov    -0x3c(%rbp),%ecx
imul   -0x28(%rbp),%ecx
mov    -0x10(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x10(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
lea    0xe35(%rip),%rsi
call   1050 <strstr@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     122f <draw_text+0xff>
mov    -0x30(%rbp),%rax
mov    -0x38(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x40(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
mov    -0x14(%rbp),%ecx
mov    -0x38(%rbp),%r8
mov    -0x40(%rbp),%r9d
movzwl -0x22(%rbp),%eax
mov    %eax,(%rsp)
call   1040 <draw_textn@plt>
mov    -0x30(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x3c(%rbp),%eax
add    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    11c0 <draw_text+0x90>
mov    -0x8(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x54(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x50(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x38(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x60(%rbp),%rdi
mov    -0x54(%rbp),%esi
mov    -0x50(%rbp),%edx
mov    -0x4c(%rbp),%ecx
mov    -0x48(%rbp),%r8
mov    %eax,%r9d
movzwl -0x22(%rbp),%eax
mov    %eax,(%rsp)
call   1040 <draw_textn@plt>
xor    %eax,%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<draw_text>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r9d,%r14d
mov    %r8,%rbx
mov    %ecx,0x10(%rsp)
mov    %edx,%ebp
mov    %esi,0xc(%rsp)
mov    %rdi,%r12
lea    0xea8(%rip),%rsi
mov    %r8,%rdi
call   1050 <strstr@plt>
movzwl %r14w,%r15d
test   %rax,%rax
je     1217 <draw_text+0xe7>
mov    %rax,%r13
mov    %r15d,0x8(%rsp)
lea    (%r14,%r14,8),%eax
lea    (%r14,%r14,8),%ecx
add    $0x9,%ecx
mov    %ecx,0x14(%rsp)
mov    $0xfffffff7,%r15d
sub    %eax,%r15d
lea    0xe6c(%rip),%r14
mov    %r13,%rax
nopw   0x0(%rax,%rax,1)
add    %r15d,%ebp
add    $0x2,%rax
mov    %rax,%rdi
mov    %r14,%rsi
call   1050 <strstr@plt>
test   %rax,%rax
jne    11a0 <draw_text+0x70>
test   %r13,%r13
je     1212 <draw_text+0xe2>
mov    0x8(%rsp),%r15d
mov    0x14(%rsp),%r14d
cs nopw 0x0(%rax,%rax,1)
mov    %r13d,%r9d
sub    %ebx,%r9d
mov    %r15d,(%rsp)
mov    %r12,%rdi
mov    0xc(%rsp),%esi
mov    %ebp,%edx
mov    0x10(%rsp),%ecx
mov    %rbx,%r8
call   1040 <draw_textn@plt>
mov    %r13,%rbx
add    $0x2,%rbx
add    %r14d,%ebp
mov    %rbx,%rdi
lea    0xdfd(%rip),%rsi
call   1050 <strstr@plt>
mov    %rax,%r13
test   %rax,%rax
jne    11d0 <draw_text+0xa0>
jmp    1217 <draw_text+0xe7>
mov    0x8(%rsp),%r15d
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %r15d,(%rsp)
mov    %r12,%rdi
mov    0xc(%rsp),%esi
mov    %ebp,%edx
mov    0x10(%rsp),%ecx
mov    %rbx,%r8
mov    %eax,%r9d
call   1040 <draw_textn@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"\\n\00"
define i32 @draw_text(i8* %0, i32 %1, i32 %2, i32 %3, i8* %4, i16 zeroext %5) {
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i16
%13 = alloca i32
%14 = alloca i8*
%15 = alloca i8*
%16 = alloca i32
%17 = alloca i32
store i8* %0, i8** %7
store i32 %1, i32* %8
store i32 %2, i32* %9
store i32 %3, i32* %10
store i8* %4, i8** %11
store i16 %5, i16* %12
store i32 0, i32* %13
%18 = load i16, i16* %12
%19 = zext i16 %18 to i32
%20 = add nsw i32 %19, 1
%21 = mul nsw i32 %20, 9
store i32 %21, i32* %16
%22 = load i8*, i8** %11
store i8* %22, i8** %14
br label %23
23: ; preds = %27, %6
%24 = load i8*, i8** %14
%25 = call i8* @strstr(i8* %24, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i8* %25, i8** %14
%26 = icmp ne i8* %25, null
br i1 %26, label %27, label %32
27: ; preds = %23
%28 = load i32, i32* %13
%29 = add nsw i32 %28, 1
store i32 %29, i32* %13
%30 = load i8*, i8** %14
%31 = getelementptr inbounds i8, i8* %30, i64 2
store i8* %31, i8** %14
br label %23
32: ; preds = %23
%33 = load i32, i32* %16
%34 = load i32, i32* %13
%35 = mul nsw i32 %33, %34
%36 = load i32, i32* %9
%37 = sub nsw i32 %36, %35
store i32 %37, i32* %9
%38 = load i8*, i8** %11
store i8* %38, i8** %14
store i8* %38, i8** %15
br label %39
39: ; preds = %43, %32
%40 = load i8*, i8** %14
%41 = call i8* @strstr(i8* %40, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i8* %41, i8** %14
%42 = icmp ne i8* %41, null
br i1 %42, label %43, label %64
43: ; preds = %39
%44 = load i8*, i8** %14
%45 = load i8*, i8** %15
%46 = ptrtoint i8* %44 to i64
%47 = ptrtoint i8* %45 to i64
%48 = sub i64 %46, %47
%49 = trunc i64 %48 to i32
store i32 %49, i32* %17
%50 = load i8*, i8** %7
%51 = load i32, i32* %8
%52 = load i32, i32* %9
%53 = load i32, i32* %10
%54 = load i8*, i8** %15
%55 = load i32, i32* %17
%56 = load i16, i16* %12
%57 = call i32 @draw_textn(i8* %50, i32 %51, i32 %52, i32 %53, i8* %54, i32 %55, i16 zeroext %56)
%58 = load i8*, i8** %14
%59 = getelementptr inbounds i8, i8* %58, i64 2
store i8* %59, i8** %14
%60 = load i8*, i8** %14
store i8* %60, i8** %15
%61 = load i32, i32* %16
%62 = load i32, i32* %9
%63 = add nsw i32 %62, %61
store i32 %63, i32* %9
br label %39
64: ; preds = %39
%65 = load i8*, i8** %7
%66 = load i32, i32* %8
%67 = load i32, i32* %9
%68 = load i32, i32* %10
%69 = load i8*, i8** %15
%70 = load i8*, i8** %15
%71 = call i32 @strlen(i8* %70)
%72 = load i16, i16* %12
%73 = call i32 @draw_textn(i8* %65, i32 %66, i32 %67, i32 %68, i8* %69, i32 %71, i16 zeroext %72)
ret i32 0
}

LLVM IR (-O2):
@.str = [3 x i8] c"\\n\00"
define i32 @draw_text(i8* %0, i32 %1, i32 %2, i32 %3, i8* %4, i16 zeroext %5) {
%7 = zext i16 %5 to i32
%8 = mul nuw nsw i32 %7, 9
%9 = add nuw nsw i32 %8, 9
%10 = tail call i8* @strstr(i8* dereferenceable(1) %4, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%11 = icmp eq i8* %10, null
br i1 %11, label %35, label %12
12: ; preds = %6, %12
%13 = phi i8* [ %17, %12 ], [ %10, %6 ]
%14 = phi i32 [ %15, %12 ], [ 0, %6 ]
%15 = add nuw nsw i32 %14, 1
%16 = getelementptr inbounds i8, i8* %13, i64 2
%17 = tail call i8* @strstr(i8* dereferenceable(1) %16, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %12
19: ; preds = %12
%20 = mul nsw i32 %15, %9
%21 = sub nsw i32 %2, %20
br i1 %11, label %35, label %22
22: ; preds = %19, %22
%23 = phi i8* [ %33, %22 ], [ %10, %19 ]
%24 = phi i32 [ %32, %22 ], [ %21, %19 ]
%25 = phi i8* [ %31, %22 ], [ %4, %19 ]
%26 = ptrtoint i8* %23 to i64
%27 = ptrtoint i8* %25 to i64
%28 = sub i64 %26, %27
%29 = trunc i64 %28 to i32
%30 = tail call i32 @draw_textn(i8* %0, i32 %1, i32 %24, i32 %3, i8* %25, i32 %29, i16 zeroext %5)
%31 = getelementptr inbounds i8, i8* %23, i64 2
%32 = add nsw i32 %24, %9
%33 = tail call i8* @strstr(i8* dereferenceable(1) %31, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%34 = icmp eq i8* %33, null
br i1 %34, label %35, label %22
35: ; preds = %22, %6, %19
%36 = phi i8* [ %4, %19 ], [ %4, %6 ], [ %31, %22 ]
%37 = phi i32 [ %21, %19 ], [ %2, %6 ], [ %32, %22 ]
%38 = tail call i32 @strlen(i8* %36)
%39 = tail call i32 @draw_textn(i8* %0, i32 %1, i32 %37, i32 %3, i8* %36, i32 %38, i16 zeroext %5)
ret i32 0
}