#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 /*<<< orphan*/ coding;

/* Variables and functions */
int code_convert(char *, char *, char *, int, char *, size_t);
int /*<<< orphan*/ memcpy(char *, char *, size_t);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
size_t strlen(char *);
int /*<<< orphan*/ to_upper_string(char *);
int sms_encode(char *sms_coding, char *in, size_t inlen, char *out,
               size_t outlen) {
  int outleft;
  char coding[256];
  char tmp[512];
  int j;
  tmp[0] = 0xFE;
  tmp[1] = 0xFF;
  memcpy(&tmp[2], in, inlen);
  j = 2 + inlen;
  memset(coding, 0, sizeof(coding));
  memcpy(coding, sms_coding, strlen(sms_coding));
  to_upper_string(coding);
  outleft = code_convert("UCS-2BE", coding, tmp, j, out, outlen);
  if (outleft <= 0) {
    return -1;
  }
  out[outlen - outleft] = '\0';
  return outlen - outleft;
}


x86 assembly (clang, -O0):
<sms_encode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x360,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
movb   $0xfe,-0x340(%rbp)
movb   $0xff,-0x33f(%rbp)
lea    -0x340(%rbp),%rdi
add    $0x2,%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1050 <memcpy@plt>
mov    -0x20(%rbp),%rax
add    $0x2,%rax
mov    %eax,-0x344(%rbp)
lea    -0x140(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1040 <memset@plt>
lea    -0x140(%rbp),%rax
mov    %rax,-0x358(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x350(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x358(%rbp),%rdi
mov    -0x350(%rbp),%rsi
mov    %rax,%rdx
call   1050 <memcpy@plt>
lea    -0x140(%rbp),%rdi
call   1070 <to_upper_string@plt>
lea    -0x140(%rbp),%rsi
lea    -0x340(%rbp),%rdx
mov    -0x344(%rbp),%ecx
mov    -0x28(%rbp),%r8
mov    -0x30(%rbp),%r9
lea    0xde3(%rip),%rdi
call   1060 <code_convert@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jg     123b <sms_encode+0xeb>
movl   $0xffffffff,-0x4(%rbp)
jmp    125c <sms_encode+0x10c>
mov    -0x28(%rbp),%rax
mov    -0x30(%rbp),%rcx
movslq -0x34(%rbp),%rdx
sub    %rdx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x30(%rbp),%rax
movslq -0x34(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x360,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sms_encode>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x300,%rsp
mov    %r8,%r15
mov    %rcx,%r14
mov    %rdx,%rbx
mov    %rdi,%r12
movw   $0xfffe,(%rsp)
lea    0x2(%rsp),%rdi
call   1050 <memcpy@plt>
add    $0x2,%ebx
lea    0x200(%rsp),%r13
mov    %r13,%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1040 <memset@plt>
mov    %r12,%rdi
call   1030 <strlen@plt>
mov    %r13,%rdi
mov    %r12,%rsi
mov    %rax,%rdx
call   1050 <memcpy@plt>
mov    %r13,%rdi
call   1070 <to_upper_string@plt>
lea    0xe45(%rip),%rdi
mov    %rsp,%rdx
mov    %r13,%rsi
mov    %ebx,%ecx
mov    %r14,%r8
mov    %r15,%r9
call   1060 <code_convert@plt>
test   %eax,%eax
jle    11de <sms_encode+0x8e>
mov    %eax,%eax
sub    %rax,%r15
movb   $0x0,(%r14,%r15,1)
jmp    11e4 <sms_encode+0x94>
mov    $0xffffffff,%r15d
mov    %r15d,%eax
add    $0x300,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [8 x i8] c"UCS-2BE\00"
define i32 @sms_encode(i8* %0, i8* %1, i64 %2, i8* %3, i64 %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i32
%13 = alloca [256 x i8]
%14 = alloca [512 x i8]
%15 = alloca i32
store i8* %0, i8** %7
store i8* %1, i8** %8
store i64 %2, i64* %9
store i8* %3, i8** %10
store i64 %4, i64* %11
%16 = getelementptr inbounds [512 x i8], [512 x i8]* %14, i64 0, i64 0
store i8 -2, i8* %16
%17 = getelementptr inbounds [512 x i8], [512 x i8]* %14, i64 0, i64 1
store i8 -1, i8* %17
%18 = getelementptr inbounds [512 x i8], [512 x i8]* %14, i64 0, i64 2
%19 = load i8*, i8** %8
%20 = load i64, i64* %9
%21 = call i32 @memcpy(i8* %18, i8* %19, i64 %20)
%22 = load i64, i64* %9
%23 = add i64 2, %22
%24 = trunc i64 %23 to i32
store i32 %24, i32* %15
%25 = getelementptr inbounds [256 x i8], [256 x i8]* %13, i64 0, i64 0
%26 = call i32 @memset(i8* %25, i32 0, i32 256)
%27 = getelementptr inbounds [256 x i8], [256 x i8]* %13, i64 0, i64 0
%28 = load i8*, i8** %7
%29 = load i8*, i8** %7
%30 = call i64 @strlen(i8* %29)
%31 = call i32 @memcpy(i8* %27, i8* %28, i64 %30)
%32 = getelementptr inbounds [256 x i8], [256 x i8]* %13, i64 0, i64 0
%33 = call i32 @to_upper_string(i8* %32)
%34 = getelementptr inbounds [256 x i8], [256 x i8]* %13, i64 0, i64 0
%35 = getelementptr inbounds [512 x i8], [512 x i8]* %14, i64 0, i64 0
%36 = load i32, i32* %15
%37 = load i8*, i8** %10
%38 = load i64, i64* %11
%39 = call i32 @code_convert(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %34, i8* %35, i32 %36, i8* %37, i64 %38)
store i32 %39, i32* %12
%40 = load i32, i32* %12
%41 = icmp sle i32 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %5
store i32 -1, i32* %6
br label %55
43: ; preds = %5
%44 = load i8*, i8** %10
%45 = load i64, i64* %11
%46 = load i32, i32* %12
%47 = sext i32 %46 to i64
%48 = sub i64 %45, %47
%49 = getelementptr inbounds i8, i8* %44, i64 %48
store i8 0, i8* %49
%50 = load i64, i64* %11
%51 = load i32, i32* %12
%52 = sext i32 %51 to i64
%53 = sub i64 %50, %52
%54 = trunc i64 %53 to i32
store i32 %54, i32* %6
br label %55
55: ; preds = %43, %42
%56 = load i32, i32* %6
ret i32 %56
}

LLVM IR (-O2):
@.str = [8 x i8] c"UCS-2BE\00"
define i32 @sms_encode(i8* %0, i8* %1, i64 %2, i8* %3, i64 %4) {
%6 = alloca [256 x i8]
%7 = alloca [512 x i8]
%8 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%9 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
store i8 -2, i8* %9
%10 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 1
store i8 -1, i8* %10
%11 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 2
%12 = call i32 @memcpy(i8* %11, i8* %1, i64 %2)
%13 = trunc i64 %2 to i32
%14 = add i32 %13, 2
%15 = call i32 @memset(i8* %8, i32 0, i32 256)
%16 = call i64 @strlen(i8* dereferenceable(1) %0)
%17 = call i32 @memcpy(i8* %8, i8* %0, i64 %16)
%18 = call i32 @to_upper_string(i8* %8)
%19 = call i32 @code_convert(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %8, i8* %9, i32 %14, i8* %3, i64 %4)
%20 = icmp slt i32 %19, 1
br i1 %20, label %26, label %21
21: ; preds = %5
%22 = zext i32 %19 to i64
%23 = sub i64 %4, %22
%24 = getelementptr inbounds i8, i8* %3, i64 %23
store i8 0, i8* %24
%25 = trunc i64 %23 to i32
br label %26
26: ; preds = %5, %21
%27 = phi i32 [ %25, %21 ], [ -1, %5 ]
ret i32 %27
}