#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 EncodePDUMessage(char const *, int const, unsigned char *, int const);
int EncodePhoneNumber(char const *, unsigned char *, int const);
unsigned char strlen(char const *);
int EncodeSMS(const char *service_center_number, const char *phone_number,
              const char *sms_text, const int sms_text_length,
              unsigned char *output_buffer, const int buffer_size) {
  if (buffer_size < 2)
    return -1;
  int output_buffer_length = 0;
  int length = 0;
  if (service_center_number && strlen(service_center_number) > 0) {
    output_buffer[1] = 0x91;
    length = EncodePhoneNumber(service_center_number, output_buffer + 2,
                               buffer_size - 2);
    if (length < 0 && length >= 254)
      return -1;
    length++;
  }
  output_buffer[0] = length;
  output_buffer_length = length + 1;
  if (output_buffer_length + 4 > buffer_size)
    return -1;
  output_buffer[output_buffer_length++] = 0x11;
  output_buffer[output_buffer_length++] = 0x00;
  output_buffer[output_buffer_length] = strlen(phone_number);
  output_buffer[output_buffer_length + 1] = 0x91;
  length =
      EncodePhoneNumber(phone_number, output_buffer + output_buffer_length + 2,
                        buffer_size - output_buffer_length - 2);
  output_buffer_length += length + 2;
  if (output_buffer_length + 4 > buffer_size)
    return -1;
  output_buffer[output_buffer_length++] = 0x00;
  output_buffer[output_buffer_length++] = 0x00;
  output_buffer[output_buffer_length++] = 0xB0;
  if (sms_text_length > 160)
    return -1;
  output_buffer[output_buffer_length++] = sms_text_length;
  length = EncodePDUMessage(sms_text, sms_text_length,
                            output_buffer + output_buffer_length,
                            buffer_size - output_buffer_length);
  if (length < 0)
    return -1;
  output_buffer_length += length;
  return output_buffer_length;
}


x86 assembly (clang, -O0):
<EncodeSMS>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    %r8,-0x30(%rbp)
mov    %r9d,-0x34(%rbp)
cmpl   $0x2,-0x34(%rbp)
jge    1165 <EncodeSMS+0x35>
movl   $0xffffffff,-0x4(%rbp)
jmp    1368 <EncodeSMS+0x238>
movl   $0x0,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11e1 <EncodeSMS+0xb1>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
movzbl %al,%eax
cmp    $0x0,%eax
jle    11e1 <EncodeSMS+0xb1>
mov    -0x30(%rbp),%rax
movb   $0x91,0x1(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
add    $0x2,%rsi
mov    -0x34(%rbp),%edx
sub    $0x2,%edx
call   1040 <EncodePhoneNumber@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    11d8 <EncodeSMS+0xa8>
cmpl   $0xfe,-0x3c(%rbp)
jl     11d8 <EncodeSMS+0xa8>
movl   $0xffffffff,-0x4(%rbp)
jmp    1368 <EncodeSMS+0x238>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    %al,%cl
mov    -0x30(%rbp),%rax
mov    %cl,(%rax)
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%eax
add    $0x4,%eax
cmp    -0x34(%rbp),%eax
jle    1210 <EncodeSMS+0xe0>
movl   $0xffffffff,-0x4(%rbp)
jmp    1368 <EncodeSMS+0x238>
mov    -0x30(%rbp),%rax
mov    -0x38(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x38(%rbp)
movslq %ecx,%rcx
movb   $0x11,(%rax,%rcx,1)
mov    -0x30(%rbp),%rax
mov    -0x38(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x38(%rbp)
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %al,%dl
mov    -0x30(%rbp),%rax
movslq -0x38(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x30(%rbp),%rax
mov    -0x38(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x91,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%rsi
movslq -0x38(%rbp),%rax
add    %rax,%rsi
add    $0x2,%rsi
mov    -0x34(%rbp),%edx
sub    -0x38(%rbp),%edx
sub    $0x2,%edx
call   1040 <EncodePhoneNumber@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
add    $0x2,%eax
add    -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%eax
add    $0x4,%eax
cmp    -0x34(%rbp),%eax
jle    12ae <EncodeSMS+0x17e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1368 <EncodeSMS+0x238>
mov    -0x30(%rbp),%rax
mov    -0x38(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x38(%rbp)
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x30(%rbp),%rax
mov    -0x38(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x38(%rbp)
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x30(%rbp),%rax
mov    -0x38(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x38(%rbp)
movslq %ecx,%rcx
movb   $0xb0,(%rax,%rcx,1)
cmpl   $0xa0,-0x24(%rbp)
jle    1309 <EncodeSMS+0x1d9>
movl   $0xffffffff,-0x4(%rbp)
jmp    1368 <EncodeSMS+0x238>
mov    -0x24(%rbp),%eax
mov    %al,%dl
mov    -0x30(%rbp),%rax
mov    -0x38(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x38(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    -0x30(%rbp),%rdx
movslq -0x38(%rbp),%rax
add    %rax,%rdx
mov    -0x34(%rbp),%ecx
sub    -0x38(%rbp),%ecx
call   1050 <EncodePDUMessage@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    1359 <EncodeSMS+0x229>
movl   $0xffffffff,-0x4(%rbp)
jmp    1368 <EncodeSMS+0x238>
mov    -0x3c(%rbp),%eax
add    -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<EncodeSMS>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    $0xffffffff,%eax
cmp    $0x2,%r9d
jl     11ec <EncodeSMS+0xbc>
mov    %r9d,%ebx
mov    %r8,%rbp
mov    %ecx,%r13d
mov    %rdi,%r14
mov    %rsi,%r12
mov    %rdx,0x10(%rsp)
xor    %r15d,%r15d
test   %rdi,%rdi
je     118f <EncodeSMS+0x5f>
mov    %r14,%rdi
call   1030 <strlen@plt>
test   %al,%al
je     118f <EncodeSMS+0x5f>
movb   $0x91,0x1(%rbp)
lea    0x2(%rbp),%rsi
lea    -0x2(%rbx),%edx
mov    %r14,%rdi
call   1040 <EncodePhoneNumber@plt>
mov    %eax,%r15d
add    $0x1,%r15d
mov    %r15b,0x0(%rbp)
lea    0x5(%r15),%r14d
cmp    %ebx,%r14d
jg     11e7 <EncodeSMS+0xb7>
mov    %r13d,0xc(%rsp)
movslq %r15d,%r13
movw   $0x11,0x1(%r13,%rbp,1)
mov    %r12,%rdi
call   1030 <strlen@plt>
mov    %al,0x3(%r13,%rbp,1)
movb   $0x91,0x4(%r13,%rbp,1)
lea    0x5(,%rbp,1),%rsi
add    %r13,%rsi
mov    %ebx,%edx
sub    %r15d,%edx
add    $0xfffffffb,%edx
mov    %r12,%rdi
call   1040 <EncodePhoneNumber@plt>
lea    (%r14,%rax,1),%r15d
add    $0x4,%r15d
cmp    %ebx,%r15d
jle    11fb <EncodeSMS+0xcb>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
add    %eax,%r14d
movslq %r14d,%rcx
movw   $0x0,0x0(%rbp,%rcx,1)
movb   $0xb0,0x2(%rbp,%rcx,1)
mov    0xc(%rsp),%esi
cmp    $0xa0,%esi
mov    $0xffffffff,%eax
jg     11ec <EncodeSMS+0xbc>
mov    %sil,0x3(%rcx,%rbp,1)
movslq %r15d,%rdx
add    %rbp,%rdx
sub    %r15d,%ebx
mov    0x10(%rsp),%rdi
mov    %ebx,%ecx
call   1050 <EncodePDUMessage@plt>
add    %eax,%r15d
test   %eax,%eax
mov    $0xffffffff,%eax
cmovns %r15d,%eax
jmp    11ec <EncodeSMS+0xbc>

LLVM IR (-O0):
define i32 @EncodeSMS(i8* %0, i8* %1, i8* %2, i32 %3, i8* %4, i32 %5) {
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
store i8* %0, i8** %8
store i8* %1, i8** %9
store i8* %2, i8** %10
store i32 %3, i32* %11
store i8* %4, i8** %12
store i32 %5, i32* %13
%16 = load i32, i32* %13
%17 = icmp slt i32 %16, 2
br i1 %17, label %18, label %19
18: ; preds = %6
store i32 -1, i32* %7
br label %144
19: ; preds = %6
store i32 0, i32* %14
store i32 0, i32* %15
%20 = load i8*, i8** %8
%21 = icmp ne i8* %20, null
br i1 %21, label %22, label %45
22: ; preds = %19
%23 = load i8*, i8** %8
%24 = call zeroext i8 @strlen(i8* %23)
%25 = zext i8 %24 to i32
%26 = icmp sgt i32 %25, 0
br i1 %26, label %27, label %45
27: ; preds = %22
%28 = load i8*, i8** %12
%29 = getelementptr inbounds i8, i8* %28, i64 1
store i8 -111, i8* %29
%30 = load i8*, i8** %8
%31 = load i8*, i8** %12
%32 = getelementptr inbounds i8, i8* %31, i64 2
%33 = load i32, i32* %13
%34 = sub nsw i32 %33, 2
%35 = call i32 @EncodePhoneNumber(i8* %30, i8* %32, i32 %34)
store i32 %35, i32* %15
%36 = load i32, i32* %15
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %42
38: ; preds = %27
%39 = load i32, i32* %15
%40 = icmp sge i32 %39, 254
br i1 %40, label %41, label %42
41: ; preds = %38
store i32 -1, i32* %7
br label %144
42: ; preds = %38, %27
%43 = load i32, i32* %15
%44 = add nsw i32 %43, 1
store i32 %44, i32* %15
br label %45
45: ; preds = %42, %22, %19
%46 = load i32, i32* %15
%47 = trunc i32 %46 to i8
%48 = load i8*, i8** %12
%49 = getelementptr inbounds i8, i8* %48, i64 0
store i8 %47, i8* %49
%50 = load i32, i32* %15
%51 = add nsw i32 %50, 1
store i32 %51, i32* %14
%52 = load i32, i32* %14
%53 = add nsw i32 %52, 4
%54 = load i32, i32* %13
%55 = icmp sgt i32 %53, %54
br i1 %55, label %56, label %57
56: ; preds = %45
store i32 -1, i32* %7
br label %144
57: ; preds = %45
%58 = load i8*, i8** %12
%59 = load i32, i32* %14
%60 = add nsw i32 %59, 1
store i32 %60, i32* %14
%61 = sext i32 %59 to i64
%62 = getelementptr inbounds i8, i8* %58, i64 %61
store i8 17, i8* %62
%63 = load i8*, i8** %12
%64 = load i32, i32* %14
%65 = add nsw i32 %64, 1
store i32 %65, i32* %14
%66 = sext i32 %64 to i64
%67 = getelementptr inbounds i8, i8* %63, i64 %66
store i8 0, i8* %67
%68 = load i8*, i8** %9
%69 = call zeroext i8 @strlen(i8* %68)
%70 = load i8*, i8** %12
%71 = load i32, i32* %14
%72 = sext i32 %71 to i64
%73 = getelementptr inbounds i8, i8* %70, i64 %72
store i8 %69, i8* %73
%74 = load i8*, i8** %12
%75 = load i32, i32* %14
%76 = add nsw i32 %75, 1
%77 = sext i32 %76 to i64
%78 = getelementptr inbounds i8, i8* %74, i64 %77
store i8 -111, i8* %78
%79 = load i8*, i8** %9
%80 = load i8*, i8** %12
%81 = load i32, i32* %14
%82 = sext i32 %81 to i64
%83 = getelementptr inbounds i8, i8* %80, i64 %82
%84 = getelementptr inbounds i8, i8* %83, i64 2
%85 = load i32, i32* %13
%86 = load i32, i32* %14
%87 = sub nsw i32 %85, %86
%88 = sub nsw i32 %87, 2
%89 = call i32 @EncodePhoneNumber(i8* %79, i8* %84, i32 %88)
store i32 %89, i32* %15
%90 = load i32, i32* %15
%91 = add nsw i32 %90, 2
%92 = load i32, i32* %14
%93 = add nsw i32 %92, %91
store i32 %93, i32* %14
%94 = load i32, i32* %14
%95 = add nsw i32 %94, 4
%96 = load i32, i32* %13
%97 = icmp sgt i32 %95, %96
br i1 %97, label %98, label %99
98: ; preds = %57
store i32 -1, i32* %7
br label %144
99: ; preds = %57
%100 = load i8*, i8** %12
%101 = load i32, i32* %14
%102 = add nsw i32 %101, 1
store i32 %102, i32* %14
%103 = sext i32 %101 to i64
%104 = getelementptr inbounds i8, i8* %100, i64 %103
store i8 0, i8* %104
%105 = load i8*, i8** %12
%106 = load i32, i32* %14
%107 = add nsw i32 %106, 1
store i32 %107, i32* %14
%108 = sext i32 %106 to i64
%109 = getelementptr inbounds i8, i8* %105, i64 %108
store i8 0, i8* %109
%110 = load i8*, i8** %12
%111 = load i32, i32* %14
%112 = add nsw i32 %111, 1
store i32 %112, i32* %14
%113 = sext i32 %111 to i64
%114 = getelementptr inbounds i8, i8* %110, i64 %113
store i8 -80, i8* %114
%115 = load i32, i32* %11
%116 = icmp sgt i32 %115, 160
br i1 %116, label %117, label %118
117: ; preds = %99
store i32 -1, i32* %7
br label %144
118: ; preds = %99
%119 = load i32, i32* %11
%120 = trunc i32 %119 to i8
%121 = load i8*, i8** %12
%122 = load i32, i32* %14
%123 = add nsw i32 %122, 1
store i32 %123, i32* %14
%124 = sext i32 %122 to i64
%125 = getelementptr inbounds i8, i8* %121, i64 %124
store i8 %120, i8* %125
%126 = load i8*, i8** %10
%127 = load i32, i32* %11
%128 = load i8*, i8** %12
%129 = load i32, i32* %14
%130 = sext i32 %129 to i64
%131 = getelementptr inbounds i8, i8* %128, i64 %130
%132 = load i32, i32* %13
%133 = load i32, i32* %14
%134 = sub nsw i32 %132, %133
%135 = call i32 @EncodePDUMessage(i8* %126, i32 %127, i8* %131, i32 %134)
store i32 %135, i32* %15
%136 = load i32, i32* %15
%137 = icmp slt i32 %136, 0
br i1 %137, label %138, label %139
138: ; preds = %118
store i32 -1, i32* %7
br label %144
139: ; preds = %118
%140 = load i32, i32* %15
%141 = load i32, i32* %14
%142 = add nsw i32 %141, %140
store i32 %142, i32* %14
%143 = load i32, i32* %14
store i32 %143, i32* %7
br label %144
144: ; preds = %139, %138, %117, %98, %56, %41, %18
%145 = load i32, i32* %7
ret i32 %145
}

LLVM IR (-O2):
define i32 @EncodeSMS(i8* %0, i8* %1, i8* %2, i32 %3, i8* %4, i32 %5) {
%7 = icmp slt i32 %5, 2
br i1 %7, label %67, label %8
8: ; preds = %6
%9 = icmp eq i8* %0, null
br i1 %9, label %19, label %10
10: ; preds = %8
%11 = tail call zeroext i8 @strlen(i8* %0)
%12 = icmp eq i8 %11, 0
br i1 %12, label %19, label %13
13: ; preds = %10
%14 = getelementptr inbounds i8, i8* %4, i64 1
store i8 -111, i8* %14
%15 = getelementptr inbounds i8, i8* %4, i64 2
%16 = add nsw i32 %5, -2
%17 = tail call i32 @EncodePhoneNumber(i8* %0, i8* %15, i32 %16)
%18 = add nsw i32 %17, 1
br label %19
19: ; preds = %13, %10, %8
%20 = phi i32 [ %18, %13 ], [ 0, %10 ], [ 0, %8 ]
%21 = trunc i32 %20 to i8
store i8 %21, i8* %4
%22 = add i32 %20, 5
%23 = icmp sgt i32 %22, %5
br i1 %23, label %67, label %24
24: ; preds = %19
%25 = add nsw i32 %20, 1
%26 = add nsw i32 %20, 2
%27 = sext i32 %25 to i64
%28 = getelementptr inbounds i8, i8* %4, i64 %27
store i8 17, i8* %28
%29 = add nsw i32 %20, 3
%30 = sext i32 %26 to i64
%31 = getelementptr inbounds i8, i8* %4, i64 %30
store i8 0, i8* %31
%32 = tail call zeroext i8 @strlen(i8* %1)
%33 = sext i32 %29 to i64
%34 = getelementptr inbounds i8, i8* %4, i64 %33
store i8 %32, i8* %34
%35 = add nsw i32 %20, 4
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %4, i64 %36
store i8 -111, i8* %37
%38 = getelementptr inbounds i8, i8* %34, i64 2
%39 = sub i32 %5, %20
%40 = add i32 %39, -5
%41 = tail call i32 @EncodePhoneNumber(i8* %1, i8* %38, i32 %40)
%42 = add i32 %22, %41
%43 = add nsw i32 %42, 4
%44 = icmp sgt i32 %43, %5
br i1 %44, label %67, label %45
45: ; preds = %24
%46 = add nsw i32 %42, 1
%47 = sext i32 %42 to i64
%48 = getelementptr inbounds i8, i8* %4, i64 %47
store i8 0, i8* %48
%49 = add nsw i32 %42, 2
%50 = sext i32 %46 to i64
%51 = getelementptr inbounds i8, i8* %4, i64 %50
store i8 0, i8* %51
%52 = sext i32 %49 to i64
%53 = getelementptr inbounds i8, i8* %4, i64 %52
store i8 -80, i8* %53
%54 = icmp sgt i32 %3, 160
br i1 %54, label %67, label %55
55: ; preds = %45
%56 = add nsw i32 %42, 3
%57 = trunc i32 %3 to i8
%58 = sext i32 %56 to i64
%59 = getelementptr inbounds i8, i8* %4, i64 %58
store i8 %57, i8* %59
%60 = sext i32 %43 to i64
%61 = getelementptr inbounds i8, i8* %4, i64 %60
%62 = sub nsw i32 %5, %43
%63 = tail call i32 @EncodePDUMessage(i8* %2, i32 %3, i8* %61, i32 %62)
%64 = add nsw i32 %63, %43
%65 = icmp sgt i32 %63, -1
%66 = select i1 %65, i32 %64, i32 -1
br label %67
67: ; preds = %55, %19, %24, %45, %6
%68 = phi i32 [ -1, %6 ], [ -1, %19 ], [ -1, %24 ], [ -1, %45 ], [ %66, %55 ]
ret i32 %68
}