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

/* Variables and functions */
int iso_utf8_2gsm(char *, int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ snprintf(char *, size_t, char *, char *);
int /*<<< orphan*/ strlen(char *);
int text2pdu(char *, int, char *, int /*<<< orphan*/);
int encode_7bit_packed(char *text, char *dest, size_t size_dest) {
  int len;
  int i;
  char buffer[512];
  char buffer2[512];
  char padding = '\r';
  len = iso_utf8_2gsm(text, strlen(text), buffer2, sizeof(buffer2));
  if ((len % 8 == 7) || (len % 8 == 0 && len && buffer2[len - 1] == padding)) {
    if ((size_t)len < sizeof(buffer2) - 1) {
      buffer2[len++] = padding;
    }
  }
  i = text2pdu(buffer2, len, buffer, 0);
  snprintf(dest, size_dest, "%s", buffer);
  return i;
}


x86 assembly (clang, -O0):
<encode_7bit_packed>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movb   $0xd,-0x421(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x430(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x430(%rbp),%rdi
mov    %eax,%esi
lea    -0x420(%rbp),%rdx
mov    $0x200,%ecx
call   1050 <iso_utf8_2gsm@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
cmp    $0x7,%edx
je     11e0 <encode_7bit_packed+0xa0>
mov    -0x1c(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    120f <encode_7bit_packed+0xcf>
cmpl   $0x0,-0x1c(%rbp)
je     120f <encode_7bit_packed+0xcf>
mov    -0x1c(%rbp),%eax
sub    $0x1,%eax
cltq
movsbl -0x420(%rbp,%rax,1),%eax
ff
movsbl -0x421(%rbp),%ecx
cmp    %ecx,%eax
jne    120f <encode_7bit_packed+0xcf>
movslq -0x1c(%rbp),%rax
cmp    $0x1ff,%rax
jae    120a <encode_7bit_packed+0xca>
mov    -0x421(%rbp),%cl
mov    -0x1c(%rbp),%eax
mov    %eax,%edx
add    $0x1,%edx
mov    %edx,-0x1c(%rbp)
cltq
mov    %cl,-0x420(%rbp,%rax,1)
jmp    120f <encode_7bit_packed+0xcf>
lea    -0x420(%rbp),%rdi
mov    -0x1c(%rbp),%esi
lea    -0x220(%rbp),%rdx
xor    %ecx,%ecx
call   1030 <text2pdu@plt>
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x220(%rbp),%rcx
lea    0xdc0(%rip),%rdx
call   1060 <snprintf@plt>
mov    -0x20(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<encode_7bit_packed>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x400,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
call   1040 <strlen@plt>
mov    %rsp,%r12
mov    %rbx,%rdi
mov    %eax,%esi
mov    %r12,%rdx
mov    $0x200,%ecx
call   1050 <iso_utf8_2gsm@plt>
lea    0x7(%rax),%edx
test   %eax,%eax
cmovns %eax,%edx
and    $0xfffffff8,%edx
mov    %eax,%ecx
sub    %edx,%ecx
cmp    $0x7,%ecx
jne    118f <encode_7bit_packed+0x4f>
cmp    $0x1fe,%eax
jbe    11a9 <encode_7bit_packed+0x69>
jmp    11b2 <encode_7bit_packed+0x72>
test   %eax,%eax
je     11b2 <encode_7bit_packed+0x72>
test   %ecx,%ecx
jne    11b2 <encode_7bit_packed+0x72>
movslq %eax,%rcx
cmpb   $0xd,-0x1(%rcx,%r12,1)
jne    11b2 <encode_7bit_packed+0x72>
cmp    $0x1ff,%eax
jae    11b2 <encode_7bit_packed+0x72>
mov    %eax,%ecx
add    $0x1,%eax
movb   $0xd,(%rsp,%rcx,1)
mov    %rsp,%rdi
lea    0x200(%rsp),%rbx
mov    %eax,%esi
mov    %rbx,%rdx
xor    %ecx,%ecx
call   1030 <text2pdu@plt>
mov    %eax,%ebp
lea    0xe2e(%rip),%rdx
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rbx,%rcx
call   1060 <snprintf@plt>
mov    %ebp,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [3 x i8] c"%s\00"
define i32 @encode_7bit_packed(i8* %0, i8* %1, i64 %2) {
%4 = alloca [512 x i8]
%5 = alloca [512 x i8]
%6 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%7 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%8 = tail call i32 @strlen(i8* %0)
%9 = call i32 @iso_utf8_2gsm(i8* %0, i32 %8, i8* %7, i32 512)
%10 = srem i32 %9, 8
%11 = icmp eq i32 %10, 7
br i1 %11, label %24, label %12
12: ; preds = %3
%13 = icmp eq i32 %10, 0
%14 = icmp ne i32 %9, 0
%15 = and i1 %14, %13
br i1 %15, label %16, label %30
16: ; preds = %12
%17 = add nsw i32 %9, -1
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 %18
%20 = load i8, i8* %19
%21 = icmp eq i8 %20, 13
%22 = icmp ult i32 %9, 511
%23 = select i1 %21, i1 %22, i1 false
br i1 %23, label %26, label %30
24: ; preds = %3
%25 = icmp ult i32 %9, 511
br i1 %25, label %26, label %30
26: ; preds = %16, %24
%27 = add nuw nsw i32 %9, 1
%28 = zext i32 %9 to i64
%29 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 %28
store i8 13, i8* %29
br label %30
30: ; preds = %24, %26, %16, %12
%31 = phi i32 [ %27, %26 ], [ %9, %24 ], [ %9, %16 ], [ %9, %12 ]
%32 = call i32 @text2pdu(i8* %7, i32 %31, i8* %6, i32 0)
%33 = call i32 @snprintf(i8* %1, i64 %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %6)
ret i32 %32
}