#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 */
typedef int uint16_t;

/* Variables and functions */
int *ascii_to_r40(char *, unsigned int *, int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(int *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stderr;
int encode(int argc, char **argv, int indx) {
  unsigned len;
  uint16_t *out;
  while (indx < argc) {
    out = ascii_to_r40(argv[indx], &len, NULL);
    if (!out) {
      fprintf(stderr, "Input contains characters invalid in R40.\n");
      return 1;
    }
    for (int i = 0; i < len; i++) {
      printf("0x%04x ", out[i]);
    }
    printf("\n");
    indx++;
  }
  free(out);
  return 0;
}


x86 assembly (clang, -O0):
<encode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    11fd <encode+0xbd>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
lea    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1060 <ascii_to_r40@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11a7 <encode+0x67>
mov    0x2e53(%rip),%rax
mov    (%rax),%edi
lea    0xe6a(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    120d <encode+0xcd>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x18(%rbp),%eax
jae    11e1 <encode+0xa1>
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,4),%esi
lea    0xe5f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11ae <encode+0x6e>
lea    0xe41(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1152 <encode+0x12>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<encode>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
movslq %edx,%r13
movslq %edi,%rbp
lea    0xec0(%rip),%r12
jmp    1183 <encode+0x33>
nopl   (%rax)
mov    $0xa,%edi
call   1040 <putchar@plt>
add    $0x1,%r13
cmp    %rbp,%r13
jge    11cd <encode+0x7d>
mov    (%r15,%r13,8),%rdi
lea    0x4(%rsp),%rsi
xor    %edx,%edx
call   1070 <ascii_to_r40@plt>
test   %rax,%rax
je     11d9 <encode+0x89>
mov    %rax,%rbx
cmpl   $0x0,0x4(%rsp)
je     1170 <encode+0x20>
xor    %r14d,%r14d
cs nopw 0x0(%rax,%rax,1)
nop
mov    (%rbx,%r14,4),%esi
mov    %r12,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x1,%r14
mov    0x4(%rsp),%eax
cmp    %rax,%r14
jb     11b0 <encode+0x60>
jmp    1170 <encode+0x20>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
jmp    11f3 <encode+0xa3>
mov    0x2e00(%rip),%rax
mov    (%rax),%edi
lea    0xe17(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [43 x i8] c"Input contains characters invalid in R40.\0A\00"
@.str.1 = [8 x i8] c"0x%04x \00"
@.str.2 = [2 x i8] c"\0A\00"
define i32 @encode(i32 %0, i8** %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32
store i32 %0, i32* %5
store i8** %1, i8*** %6
store i32 %2, i32* %7
br label %11
11: ; preds = %42, %3
%12 = load i32, i32* %7
%13 = load i32, i32* %5
%14 = icmp slt i32 %12, %13
br i1 %14, label %15, label %46
15: ; preds = %11
%16 = load i8**, i8*** %6
%17 = load i32, i32* %7
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8*, i8** %16, i64 %18
%20 = load i8*, i8** %19
%21 = call i32* @ascii_to_r40(i8* %20, i32* %8, i32* null)
store i32* %21, i32** %9
%22 = load i32*, i32** %9
%23 = icmp ne i32* %22, null
br i1 %23, label %27, label %24
24: ; preds = %15
%25 = load i32, i32* @stderr
%26 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %4
br label %49
27: ; preds = %15
store i32 0, i32* %10
br label %28
28: ; preds = %39, %27
%29 = load i32, i32* %10
%30 = load i32, i32* %8
%31 = icmp ult i32 %29, %30
br i1 %31, label %32, label %42
32: ; preds = %28
%33 = load i32*, i32** %9
%34 = load i32, i32* %10
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i32, i32* %33, i64 %35
%37 = load i32, i32* %36
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %37)
br label %39
39: ; preds = %32
%40 = load i32, i32* %10
%41 = add nsw i32 %40, 1
store i32 %41, i32* %10
br label %28
42: ; preds = %28
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%44 = load i32, i32* %7
%45 = add nsw i32 %44, 1
store i32 %45, i32* %7
br label %11
46: ; preds = %11
%47 = load i32*, i32** %9
%48 = call i32 @free(i32* %47)
store i32 0, i32* %4
br label %49
49: ; preds = %46, %24
%50 = load i32, i32* %4
ret i32 %50
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [43 x i8] c"Input contains characters invalid in R40.\0A\00"
@.str.1 = [8 x i8] c"0x%04x \00"
define i32 @encode(i32 %0, i8** nocapture %1, i32 %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
%6 = icmp slt i32 %2, %0
%7 = sext i32 %2 to i64
%8 = sext i32 %0 to i64
br label %9
9: ; preds = %3, %21
%10 = phi i64 [ %7, %3 ], [ %23, %21 ]
%11 = getelementptr inbounds i8*, i8** %1, i64 %10
%12 = load i8*, i8** %11
%13 = call i32* @ascii_to_r40(i8* %12, i32* %4, i32* null)
%14 = icmp eq i32* %13, null
br i1 %14, label %18, label %15
15: ; preds = %9
%16 = load i32, i32* %4
%17 = icmp eq i32 %16, 0
br i1 %17, label %21, label %25
18: ; preds = %9
%19 = load i32, i32* @stderr
%20 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0))
br label %36
21: ; preds = %25, %15
%22 = call i32 @putchar(i32 10)
%23 = add nsw i64 %10, 1
%24 = icmp slt i64 %23, %8
br i1 %24, label %9, label %34
25: ; preds = %15, %25
%26 = phi i64 [ %30, %25 ], [ 0, %15 ]
%27 = getelementptr inbounds i32, i32* %13, i64 %26
%28 = load i32, i32* %27
%29 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %28)
%30 = add nuw nsw i64 %26, 1
%31 = load i32, i32* %4
%32 = zext i32 %31 to i64
%33 = icmp ult i64 %30, %32
br i1 %33, label %25, label %21
34: ; preds = %21
%35 = call i32 @free(i32* %13)
br label %36
36: ; preds = %34, %18
%37 = phi i32 [ 1, %18 ], [ 0, %34 ]
ret i32 %37
}