#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*/ code_convert(char *, char *, char *, scalar_t__, char *,
                                int);
int gsm_encode_pdu(char *, char const *, int /*<<< orphan*/, char, char *,
                   char *);
int /*<<< orphan*/ strcpy(char *, char *);
scalar_t__ strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, int);
scalar_t__ strstr(char *, char *);
int /*<<< orphan*/ to_upper_string(char *);
int gsm_encode_pdu_ucs2(const char *smsc, const char *dest, char *sms_data,
                        const char *coding, char *pdu) {
  char mesg[1024];
  char text_coding[256];
  char TP_DCS;
  if (coding == NULL || strlen(coding) == 0) {
    strcpy(text_coding, "ASCII");
  } else {
    strncpy(text_coding, coding, 256);
    to_upper_string(text_coding);
  }
  if (strstr(text_coding, "UTF-8")) {
    strncpy(mesg, sms_data, 1024);
    TP_DCS = 0;
    unsigned char *p = (unsigned char *)mesg;
    while (*p != '\0') {
      if (*p >= 128) {
        TP_DCS = 8;
        break;
      }
      p++;
    }
  } else if (strstr(text_coding, "ASCII")) {
    strncpy(mesg, sms_data, 1024);
    TP_DCS = 0;
  } else {
    code_convert(text_coding, "UTF-8", sms_data, strlen(sms_data), mesg, 1024);
    TP_DCS = 8;
  }
  return gsm_encode_pdu(smsc == NULL ? "" : smsc, dest, 0, TP_DCS, mesg, pdu);
}


x86 assembly (clang, -O0):
<gsm_encode_pdu_ucs2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x560,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11ad <gsm_encode_pdu_ucs2+0x3d>
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
cmp    $0x0,%rax
jne    11c5 <gsm_encode_pdu_ucs2+0x55>
lea    -0x530(%rbp),%rdi
lea    0xe45(%rip),%rsi
call   1040 <strcpy@plt>
jmp    11e6 <gsm_encode_pdu_ucs2+0x76>
lea    -0x530(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    $0x100,%edx
call   1030 <strncpy@plt>
lea    -0x530(%rbp),%rdi
call   1080 <to_upper_string@plt>
lea    -0x530(%rbp),%rdi
lea    0xe12(%rip),%rsi
call   1090 <strstr@plt>
cmp    $0x0,%rax
je     127d <gsm_encode_pdu_ucs2+0x10d>
lea    -0x430(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    $0x400,%edx
call   1030 <strncpy@plt>
movb   $0x0,-0x531(%rbp)
lea    -0x430(%rbp),%rax
mov    %rax,-0x540(%rbp)
mov    -0x540(%rbp),%rax
movzbl (%rax),%eax
cmp    $0x0,%eax
je     1278 <gsm_encode_pdu_ucs2+0x108>
mov    -0x540(%rbp),%rax
movzbl (%rax),%eax
cmp    $0x80,%eax
jl     1261 <gsm_encode_pdu_ucs2+0xf1>
movb   $0x8,-0x531(%rbp)
jmp    1278 <gsm_encode_pdu_ucs2+0x108>
mov    -0x540(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x540(%rbp)
jmp    122d <gsm_encode_pdu_ucs2+0xbd>
jmp    1313 <gsm_encode_pdu_ucs2+0x1a3>
lea    -0x530(%rbp),%rdi
lea    0xd75(%rip),%rsi
call   1090 <strstr@plt>
cmp    $0x0,%rax
je     12bb <gsm_encode_pdu_ucs2+0x14b>
lea    -0x430(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    $0x400,%edx
call   1030 <strncpy@plt>
movb   $0x0,-0x531(%rbp)
jmp    130e <gsm_encode_pdu_ucs2+0x19e>
lea    -0x530(%rbp),%rax
mov    %rax,-0x550(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x548(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x550(%rbp),%rdi
mov    -0x548(%rbp),%rdx
mov    %rax,%rcx
lea    -0x430(%rbp),%r8
lea    0xd0a(%rip),%rsi
mov    $0x400,%r9d
call   1070 <code_convert@plt>
movb   $0x8,-0x531(%rbp)
jmp    1313 <gsm_encode_pdu_ucs2+0x1a3>
cmpq   $0x0,-0x8(%rbp)
jne    1331 <gsm_encode_pdu_ucs2+0x1c1>
lea    0xce6(%rip),%rax
mov    %rax,-0x558(%rbp)
jmp    133c <gsm_encode_pdu_ucs2+0x1cc>
mov    -0x8(%rbp),%rax
mov    %rax,-0x558(%rbp)
mov    -0x558(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x531(%rbp),%al
lea    -0x430(%rbp),%r8
mov    -0x28(%rbp),%r9
xor    %edx,%edx
movsbl %al,%ecx
call   1060 <gsm_encode_pdu@plt>
add    $0x560,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gsm_encode_pdu_ucs2>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x500,%rsp
mov    %r8,%r14
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %rdi,%r12
test   %rcx,%rcx
je     11b3 <gsm_encode_pdu_ucs2+0x43>
cmpb   $0x0,(%rcx)
je     11b3 <gsm_encode_pdu_ucs2+0x43>
mov    %rsp,%r13
mov    %r13,%rdi
mov    %rcx,%rsi
mov    $0x100,%edx
call   1030 <strncpy@plt>
mov    %r13,%rdi
call   1080 <to_upper_string@plt>
jmp    11c2 <gsm_encode_pdu_ucs2+0x52>
lea    0xe46(%rip),%rsi
mov    %rsp,%rdi
call   1040 <strcpy@plt>
lea    0xe3d(%rip),%rsi
mov    %rsp,%rdi
call   1090 <strstr@plt>
test   %rax,%rax
je     1211 <gsm_encode_pdu_ucs2+0xa1>
lea    0x100(%rsp),%rdi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1030 <strncpy@plt>
mov    0x100(%rsp),%al
test   %al,%al
je     123a <gsm_encode_pdu_ucs2+0xca>
lea    0x101(%rsp),%rcx
xchg   %ax,%ax
test   %al,%al
js     1269 <gsm_encode_pdu_ucs2+0xf9>
movzbl (%rcx),%eax
add    $0x1,%rcx
test   %al,%al
jne    1200 <gsm_encode_pdu_ucs2+0x90>
jmp    123a <gsm_encode_pdu_ucs2+0xca>
lea    0xde8(%rip),%rsi
mov    %rsp,%rdi
call   1090 <strstr@plt>
test   %rax,%rax
je     123e <gsm_encode_pdu_ucs2+0xce>
lea    0x100(%rsp),%rdi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1030 <strncpy@plt>
xor    %eax,%eax
jmp    126b <gsm_encode_pdu_ucs2+0xfb>
mov    %rbx,%rdi
call   1050 <strlen@plt>
lea    0xdb9(%rip),%rsi
mov    %rsp,%rdi
lea    0x100(%rsp),%r8
mov    %rbx,%rdx
mov    %rax,%rcx
mov    $0x400,%r9d
call   1070 <code_convert@plt>
mov    $0x8,%al
test   %r12,%r12
lea    0xd96(%rip),%rdi
cmovne %r12,%rdi
movzbl %al,%ecx
lea    0x100(%rsp),%r8
mov    %r15,%rsi
xor    %edx,%edx
mov    %r14,%r9
call   1060 <gsm_encode_pdu@plt>
add    $0x500,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [6 x i8] c"ASCII\00"
@.str.1 = [6 x i8] c"UTF-8\00"
@.str.2 = [1 x i8] zeroinitializer
define i32 @gsm_encode_pdu_ucs2(i8* %0, i8* %1, i8* %2, i8* %3, i8* %4) {
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca [1024 x i8]
%12 = alloca [256 x i8]
%13 = alloca i8
%14 = alloca i8*
store i8* %0, i8** %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i8* %3, i8** %9
store i8* %4, i8** %10
%15 = load i8*, i8** %9
%16 = icmp eq i8* %15, null
br i1 %16, label %21, label %17
17: ; preds = %5
%18 = load i8*, i8** %9
%19 = call i64 @strlen(i8* %18)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %17, %5
%22 = getelementptr inbounds [256 x i8], [256 x i8]* %12, i64 0, i64 0
%23 = call i32 @strcpy(i8* %22, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %30
24: ; preds = %17
%25 = getelementptr inbounds [256 x i8], [256 x i8]* %12, i64 0, i64 0
%26 = load i8*, i8** %9
%27 = call i32 @strncpy(i8* %25, i8* %26, i32 256)
%28 = getelementptr inbounds [256 x i8], [256 x i8]* %12, i64 0, i64 0
%29 = call i32 @to_upper_string(i8* %28)
br label %30
30: ; preds = %24, %21
%31 = getelementptr inbounds [256 x i8], [256 x i8]* %12, i64 0, i64 0
%32 = call i64 @strstr(i8* %31, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %54
34: ; preds = %30
%35 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%36 = load i8*, i8** %8
%37 = call i32 @strncpy(i8* %35, i8* %36, i32 1024)
store i8 0, i8* %13
%38 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
store i8* %38, i8** %14
br label %39
39: ; preds = %50, %34
%40 = load i8*, i8** %14
%41 = load i8, i8* %40
%42 = zext i8 %41 to i32
%43 = icmp ne i32 %42, 0
br i1 %43, label %44, label %53
44: ; preds = %39
%45 = load i8*, i8** %14
%46 = load i8, i8* %45
%47 = zext i8 %46 to i32
%48 = icmp sge i32 %47, 128
br i1 %48, label %49, label %50
49: ; preds = %44
store i8 8, i8* %13
br label %53
50: ; preds = %44
%51 = load i8*, i8** %14
%52 = getelementptr inbounds i8, i8* %51, i32 1
store i8* %52, i8** %14
br label %39
53: ; preds = %49, %39
br label %70
54: ; preds = %30
%55 = getelementptr inbounds [256 x i8], [256 x i8]* %12, i64 0, i64 0
%56 = call i64 @strstr(i8* %55, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%57 = icmp ne i64 %56, 0
br i1 %57, label %58, label %62
58: ; preds = %54
%59 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%60 = load i8*, i8** %8
%61 = call i32 @strncpy(i8* %59, i8* %60, i32 1024)
store i8 0, i8* %13
br label %69
62: ; preds = %54
%63 = getelementptr inbounds [256 x i8], [256 x i8]* %12, i64 0, i64 0
%64 = load i8*, i8** %8
%65 = load i8*, i8** %8
%66 = call i64 @strlen(i8* %65)
%67 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%68 = call i32 @code_convert(i8* %63, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %64, i64 %66, i8* %67, i32 1024)
store i8 8, i8* %13
br label %69
69: ; preds = %62, %58
br label %70
70: ; preds = %69, %53
%71 = load i8*, i8** %6
%72 = icmp eq i8* %71, null
br i1 %72, label %73, label %74
73: ; preds = %70
br label %76
74: ; preds = %70
%75 = load i8*, i8** %6
br label %76
76: ; preds = %74, %73
%77 = phi i8* [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0), %73 ], [ %75, %74 ]
%78 = load i8*, i8** %7
%79 = load i8, i8* %13
%80 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%81 = load i8*, i8** %10
%82 = call i32 @gsm_encode_pdu(i8* %77, i8* %78, i32 0, i8 signext %79, i8* %80, i8* %81)
ret i32 %82
}

LLVM IR (-O2):
@.str = [6 x i8] c"ASCII\00"
@.str.1 = [6 x i8] c"UTF-8\00"
@.str.2 = [1 x i8] zeroinitializer
define i32 @gsm_encode_pdu_ucs2(i8* %0, i8* %1, i8* %2, i8* %3, i8* %4) {
%6 = alloca [1024 x i8]
%7 = alloca [256 x i8]
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%9 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%10 = icmp eq i8* %3, null
br i1 %10, label %14, label %11
11: ; preds = %5
%12 = load i8, i8* %3
%13 = icmp eq i8 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %11, %5
%15 = call i32 @strcpy(i8* %9, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %19
16: ; preds = %11
%17 = call i32 @strncpy(i8* %9, i8* %3, i32 256)
%18 = call i32 @to_upper_string(i8* %9)
br label %19
19: ; preds = %16, %14
%20 = call i64 @strstr(i8* %9, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%21 = icmp eq i64 %20, 0
br i1 %21, label %34, label %22
22: ; preds = %19
%23 = call i32 @strncpy(i8* %8, i8* %2, i32 1024)
%24 = load i8, i8* %8
%25 = icmp eq i8 %24, 0
br i1 %25, label %42, label %29
26: ; preds = %29
%27 = load i8, i8* %33
%28 = icmp eq i8 %27, 0
br i1 %28, label %42, label %29
29: ; preds = %22, %26
%30 = phi i8 [ %27, %26 ], [ %24, %22 ]
%31 = phi i8* [ %33, %26 ], [ %8, %22 ]
%32 = icmp slt i8 %30, 0
%33 = getelementptr inbounds i8, i8* %31, i64 1
br i1 %32, label %42, label %26
34: ; preds = %19
%35 = call i64 @strstr(i8* %9, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%36 = icmp eq i64 %35, 0
br i1 %36, label %39, label %37
37: ; preds = %34
%38 = call i32 @strncpy(i8* %8, i8* %2, i32 1024)
br label %42
39: ; preds = %34
%40 = call i64 @strlen(i8* dereferenceable(1) %2)
%41 = call i32 @code_convert(i8* %9, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %2, i64 %40, i8* %8, i32 1024)
br label %42
42: ; preds = %29, %26, %22, %37, %39
%43 = phi i8 [ 0, %37 ], [ 8, %39 ], [ 0, %22 ], [ 8, %29 ], [ 0, %26 ]
%44 = icmp eq i8* %0, null
%45 = select i1 %44, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0), i8* %0
%46 = call i32 @gsm_encode_pdu(i8* %45, i8* %1, i32 0, i8 signext %43, i8* %8, i8* %4)
ret i32 %46
}