#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 */
unsigned char charset_petcii_to_screencode(unsigned char const,
                                           int /*<<< orphan*/);
unsigned char *lib_malloc(size_t);
size_t strlen(char const *);
void charset_petcii_to_screencode_line(const unsigned char *line,
                                       unsigned char **buf, unsigned int *len) {
  size_t linelen, i;
  linelen = strlen((const char *)line);
  *buf = lib_malloc(linelen);
  for (i = 0; i < linelen; i++) {
    (*buf)[i] = charset_petcii_to_screencode(line[i], 0);
  }
  *len = (unsigned int)linelen;
}


x86 assembly (clang, -O0):
<charset_petcii_to_screencode_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <lib_malloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    11ae <charset_petcii_to_screencode_line+0x7e>
mov    -0x8(%rbp),%rax
mov    -0x28(%rbp),%rcx
xor    %esi,%esi
movzbl (%rax,%rcx,1),%edi
call   1050 <charset_petcii_to_screencode@plt>
mov    %al,%dl
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    -0x28(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    116c <charset_petcii_to_screencode_line+0x3c>
mov    -0x20(%rbp),%rax
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<charset_petcii_to_screencode_line>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
call   1040 <strlen@plt>
mov    %rax,%r13
mov    %rax,%rdi
call   1030 <lib_malloc@plt>
mov    %rax,(%r15)
test   %r13,%r13
je     117b <charset_petcii_to_screencode_line+0x4b>
xor    %ebx,%ebx
nopl   0x0(%rax)
movzbl (%r12,%rbx,1),%edi
xor    %esi,%esi
call   1050 <charset_petcii_to_screencode@plt>
mov    (%r15),%rcx
mov    %al,(%rcx,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%r13
jne    1160 <charset_petcii_to_screencode_line+0x30>
mov    %r13d,(%r14)
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @charset_petcii_to_screencode_line(i8* %0, i8** %1, i32* %2) {
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i64
%8 = alloca i64
store i8* %0, i8** %4
store i8** %1, i8*** %5
store i32* %2, i32** %6
%9 = load i8*, i8** %4
%10 = call i64 @strlen(i8* %9)
store i64 %10, i64* %7
%11 = load i64, i64* %7
%12 = call i8* @lib_malloc(i64 %11)
%13 = load i8**, i8*** %5
store i8* %12, i8** %13
store i64 0, i64* %8
br label %14
14: ; preds = %28, %3
%15 = load i64, i64* %8
%16 = load i64, i64* %7
%17 = icmp ult i64 %15, %16
br i1 %17, label %18, label %31
18: ; preds = %14
%19 = load i8*, i8** %4
%20 = load i64, i64* %8
%21 = getelementptr inbounds i8, i8* %19, i64 %20
%22 = load i8, i8* %21
%23 = call zeroext i8 @charset_petcii_to_screencode(i8 zeroext %22, i32 0)
%24 = load i8**, i8*** %5
%25 = load i8*, i8** %24
%26 = load i64, i64* %8
%27 = getelementptr inbounds i8, i8* %25, i64 %26
store i8 %23, i8* %27
br label %28
28: ; preds = %18
%29 = load i64, i64* %8
%30 = add i64 %29, 1
store i64 %30, i64* %8
br label %14
31: ; preds = %14
%32 = load i64, i64* %7
%33 = trunc i64 %32 to i32
%34 = load i32*, i32** %6
store i32 %33, i32* %34
ret void
}

LLVM IR (-O2):
define void @charset_petcii_to_screencode_line(i8* nocapture %0, i8** nocapture %1, i32* nocapture writeonly %2) {
%4 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%5 = tail call i8* @lib_malloc(i64 %4)
store i8* %5, i8** %1
%6 = icmp eq i64 %4, 0
br i1 %6, label %16, label %7
7: ; preds = %3, %7
%8 = phi i64 [ %14, %7 ], [ 0, %3 ]
%9 = getelementptr inbounds i8, i8* %0, i64 %8
%10 = load i8, i8* %9
%11 = tail call zeroext i8 @charset_petcii_to_screencode(i8 zeroext %10, i32 0)
%12 = load i8*, i8** %1
%13 = getelementptr inbounds i8, i8* %12, i64 %8
store i8 %11, i8* %13
%14 = add nuw i64 %8, 1
%15 = icmp eq i64 %14, %4
br i1 %15, label %16, label %7
16: ; preds = %7, %3
%17 = trunc i64 %4 to i32
store i32 %17, i32* %2
ret void
}