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


x86 assembly (clang, -O0):
<gotr_b64_dec>:
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    0xea1(%rip),%rsi
call   1060 <strstr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    117f <gotr_b64_dec+0x3f>
movl   $0xfffffffe,-0x4(%rbp)
jmp    126a <gotr_b64_dec+0x12a>
mov    -0x10(%rbp),%rax
movsbl 0x6(%rax),%eax
cmp    $0x31,%eax
je     119c <gotr_b64_dec+0x5c>
movl   $0xfffffffe,-0x4(%rbp)
jmp    126a <gotr_b64_dec+0x12a>
mov    -0x28(%rbp),%rdi
mov    $0x2e,%esi
call   1030 <strchr@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     11cd <gotr_b64_dec+0x8d>
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x38(%rbp)
jmp    11d9 <gotr_b64_dec+0x99>
movl   $0xfffffffe,-0x4(%rbp)
jmp    126a <gotr_b64_dec+0x12a>
mov    -0x28(%rbp),%rax
add    $0x7,%rax
mov    %rax,-0x28(%rbp)
mov    -0x38(%rbp),%rax
sub    $0x7,%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   1050 <malloc@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    1238 <gotr_b64_dec+0xf8>
cmpq   $0x0,-0x40(%rbp)
jbe    1238 <gotr_b64_dec+0xf8>
movl   $0xffffffff,-0x4(%rbp)
jmp    126a <gotr_b64_dec+0x12a>
mov    -0x48(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x38(%rbp),%rdx
call   1040 <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):
<gotr_b64_dec>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
lea    0xea5(%rip),%rsi
call   1060 <strstr@plt>
mov    $0xfffffffe,%ebp
test   %rax,%rax
je     11d1 <gotr_b64_dec+0x91>
cmpb   $0x31,0x6(%rbx)
jne    11d1 <gotr_b64_dec+0x91>
mov    %rax,%r13
mov    %rax,%rdi
mov    $0x2e,%esi
call   1030 <strchr@plt>
test   %rax,%rax
je     11d1 <gotr_b64_dec+0x91>
mov    %rax,%r12
sub    %r13,%r12
lea    -0x4(%r12),%rbx
mov    %rbx,%rax
shr    $0x2,%rax
lea    (%rax,%rax,2),%rdi
call   1050 <malloc@plt>
test   %rax,%rax
jne    11b0 <gotr_b64_dec+0x70>
mov    $0xffffffff,%ebp
cmp    $0x3,%rbx
ja     11d1 <gotr_b64_dec+0x91>
add    $0xfffffffffffffff9,%r12
add    $0x7,%r13
mov    %rax,%rdi
mov    %r13,%rsi
mov    %r12,%rdx
mov    %rax,%rbx
call   1040 <otrl_base64_decode@plt>
mov    %rbx,(%r15)
mov    %rax,(%r14)
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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