#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 */
unsigned char *malloc(size_t);
int /*<<< orphan*/ memset(unsigned char *, int /*<<< orphan*/, size_t);
size_t otrl_base64_decode(unsigned char *, char *, size_t);
char *strchr(char *, char);
char *strstr(char const *, char *);
int otrl_base64_otr_decode(const char *msg, unsigned char **bufp,
                           size_t *lenp) {
  char *otrtag, *endtag;
  size_t msglen, rawlen;
  unsigned char *rawmsg;
  otrtag = strstr(msg, "?OTR:");
  if (!otrtag) {
    return -2;
  }
  endtag = strchr(otrtag, '.');
  if (endtag) {
    msglen = endtag - otrtag;
  } else {
    return -2;
  }
  otrtag += 5;
  msglen -= 5;
  rawlen = (((msglen + 4 - 1) / 4) * 3);
  rawmsg = malloc(rawlen);
  if (!rawmsg && rawlen > 0) {
    return -1;
  }
  memset(rawmsg, 0, rawlen);
  rawlen = otrl_base64_decode(rawmsg, otrtag, msglen);
  *bufp = rawmsg;
  *lenp = rawlen;
  return 0;
}


x86 assembly (clang, -O0):
<otrl_base64_otr_decode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe91(%rip),%rsi
call   1070 <strstr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    118f <otrl_base64_otr_decode+0x3f>
movl   $0xfffffffe,-0x4(%rbp)
jmp    126c <otrl_base64_otr_decode+0x11c>
mov    -0x28(%rbp),%rdi
mov    $0x2e,%esi
call   1030 <strchr@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     11c0 <otrl_base64_otr_decode+0x70>
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x38(%rbp)
jmp    11cc <otrl_base64_otr_decode+0x7c>
movl   $0xfffffffe,-0x4(%rbp)
jmp    126c <otrl_base64_otr_decode+0x11c>
mov    -0x28(%rbp),%rax
add    $0x5,%rax
mov    %rax,-0x28(%rbp)
mov    -0x38(%rbp),%rax
sub    $0x5,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
add    $0x4,%rax
sub    $0x1,%rax
shr    $0x2,%rax
imul   $0x3,%rax,%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    122b <otrl_base64_otr_decode+0xdb>
cmpq   $0x0,-0x40(%rbp)
jbe    122b <otrl_base64_otr_decode+0xdb>
movl   $0xffffffff,-0x4(%rbp)
jmp    126c <otrl_base64_otr_decode+0x11c>
mov    -0x48(%rbp),%rdi
mov    -0x40(%rbp),%rdx
xor    %esi,%esi
call   1040 <memset@plt>
mov    -0x48(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x38(%rbp),%rdx
call   1050 <otrl_base64_decode@plt>
mov    %rax,-0x40(%rbp)
mov    -0x48(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x40(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<otrl_base64_otr_decode>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r15
mov    %rsi,%rbp
lea    0xe95(%rip),%rsi
call   1070 <strstr@plt>
mov    $0xfffffffe,%r14d
test   %rax,%rax
je     1206 <otrl_base64_otr_decode+0xb6>
mov    %rax,%rbx
mov    %rbp,0x8(%rsp)
mov    %r15,0x10(%rsp)
mov    %rax,%rdi
mov    $0x2e,%esi
call   1030 <strchr@plt>
test   %rax,%rax
je     1206 <otrl_base64_otr_decode+0xb6>
mov    %rax,%r12
sub    %rbx,%r12
lea    -0x2(%r12),%r15
mov    %r15,%rax
shr    $0x2,%rax
lea    (%rax,%rax,2),%rbp
mov    %rbp,%rdi
call   1060 <malloc@plt>
mov    %rax,%r13
test   %rax,%rax
jne    11d0 <otrl_base64_otr_decode+0x80>
mov    $0xffffffff,%r14d
cmp    $0x3,%r15
ja     1206 <otrl_base64_otr_decode+0xb6>
add    $0xfffffffffffffffb,%r12
add    $0x5,%rbx
xor    %r14d,%r14d
mov    %r13,%rdi
xor    %esi,%esi
mov    %rbp,%rdx
call   1040 <memset@plt>
mov    %r13,%rdi
mov    %rbx,%rsi
mov    %r12,%rdx
call   1050 <otrl_base64_decode@plt>
mov    0x8(%rsp),%rcx
mov    %r13,(%rcx)
mov    0x10(%rsp),%rcx
mov    %rax,(%rcx)
mov    %r14d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"?OTR:\00"
define i32 @otrl_base64_otr_decode(i8* %0, i8** %1, i64* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8**
%7 = alloca i64*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i64
%12 = alloca i8*
store i8* %0, i8** %5
store i8** %1, i8*** %6
store i64* %2, i64** %7
%13 = load i8*, i8** %5
%14 = call i8* @strstr(i8* %13, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i8* %14, i8** %8
%15 = load i8*, i8** %8
%16 = icmp ne i8* %15, null
br i1 %16, label %18, label %17
17: ; preds = %3
store i32 -2, i32* %4
br label %60
18: ; preds = %3
%19 = load i8*, i8** %8
%20 = call i8* @strchr(i8* %19, i8 signext 46)
store i8* %20, i8** %9
%21 = load i8*, i8** %9
%22 = icmp ne i8* %21, null
br i1 %22, label %23, label %29
23: ; preds = %18
%24 = load i8*, i8** %9
%25 = load i8*, i8** %8
%26 = ptrtoint i8* %24 to i64
%27 = ptrtoint i8* %25 to i64
%28 = sub i64 %26, %27
store i64 %28, i64* %10
br label %30
29: ; preds = %18
store i32 -2, i32* %4
br label %60
30: ; preds = %23
%31 = load i8*, i8** %8
%32 = getelementptr inbounds i8, i8* %31, i64 5
store i8* %32, i8** %8
%33 = load i64, i64* %10
%34 = sub i64 %33, 5
store i64 %34, i64* %10
%35 = load i64, i64* %10
%36 = add i64 %35, 4
%37 = sub i64 %36, 1
%38 = udiv i64 %37, 4
%39 = mul i64 %38, 3
store i64 %39, i64* %11
%40 = load i64, i64* %11
%41 = call i8* @malloc(i64 %40)
store i8* %41, i8** %12
%42 = load i8*, i8** %12
%43 = icmp ne i8* %42, null
br i1 %43, label %48, label %44
44: ; preds = %30
%45 = load i64, i64* %11
%46 = icmp ugt i64 %45, 0
br i1 %46, label %47, label %48
47: ; preds = %44
store i32 -1, i32* %4
br label %60
48: ; preds = %44, %30
%49 = load i8*, i8** %12
%50 = load i64, i64* %11
%51 = call i32 @memset(i8* %49, i32 0, i64 %50)
%52 = load i8*, i8** %12
%53 = load i8*, i8** %8
%54 = load i64, i64* %10
%55 = call i64 @otrl_base64_decode(i8* %52, i8* %53, i64 %54)
store i64 %55, i64* %11
%56 = load i8*, i8** %12
%57 = load i8**, i8*** %6
store i8* %56, i8** %57
%58 = load i64, i64* %11
%59 = load i64*, i64** %7
store i64 %58, i64* %59
store i32 0, i32* %4
br label %60
60: ; preds = %48, %47, %29, %17
%61 = load i32, i32* %4
ret i32 %61
}

LLVM IR (-O2):
@.str = [6 x i8] c"?OTR:\00"
define i32 @otrl_base64_otr_decode(i8* %0, i8** nocapture writeonly %1, i64* nocapture writeonly %2) {
%4 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i8* %4, null
br i1 %5, label %25, label %6
6: ; preds = %3
%7 = tail call i8* @strchr(i8* %4, i8 signext 46)
%8 = icmp eq i8* %7, null
br i1 %8, label %25, label %9
9: ; preds = %6
%10 = ptrtoint i8* %7 to i64
%11 = ptrtoint i8* %4 to i64
%12 = sub i64 %10, %11
%13 = add i64 %12, -2
%14 = lshr i64 %13, 2
%15 = mul nuw i64 %14, 3
%16 = tail call i8* @malloc(i64 %15)
%17 = icmp eq i8* %16, null
%18 = icmp ugt i64 %13, 3
%19 = select i1 %17, i1 %18, i1 false
br i1 %19, label %25, label %20
20: ; preds = %9
%21 = add i64 %12, -5
%22 = getelementptr inbounds i8, i8* %4, i64 5
%23 = tail call i32 @memset(i8* %16, i32 0, i64 %15)
%24 = tail call i64 @otrl_base64_decode(i8* %16, i8* %22, i64 %21)
store i8* %16, i8** %1
store i64 %24, i64* %2
br label %25
25: ; preds = %9, %6, %3, %20
%26 = phi i32 [ 0, %20 ], [ -2, %3 ], [ -2, %6 ], [ -1, %9 ]
ret i32 %26
}