#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 len1;

/* Variables and functions */
scalar_t__ alloca(int);
int /*<<< orphan*/ base64_encode(char *, char *, int);
scalar_t__ malloc(int);
int /*<<< orphan*/ sprintf(char *, char *, char const *, char const *);
int strlen(char const *);
char *basic_authentication_encode(const char *user, const char *passwd,
                                  const char *header) {
  char *t1, *t2, *res;
  int len1 = strlen(user) + 1 + strlen(passwd);
  int len2 = (4 * (((len1) + 2) / 3));
  t1 = (char *)alloca(len1 + 1);
  sprintf(t1, "%s:%s", user, passwd);
  t2 = (char *)alloca(1 + len2);
  base64_encode(t1, t2, len1);
  res = (char *)malloc(len2 + 11 + strlen(header));
  sprintf(res, "%s: Basic %s\r\n", header, t2);
  return res;
}


x86 assembly (clang, -O0):
<basic_authentication_encode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
add    $0x1,%eax
mov    %eax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x40(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%eax
add    $0x2,%eax
mov    $0x3,%ecx
cltd
idiv   %ecx
shl    $0x2,%eax
mov    %eax,-0x38(%rbp)
mov    -0x34(%rbp),%edi
add    $0x1,%edi
call   1040 <alloca@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xe44(%rip),%rsi
call   1070 <sprintf@plt>
mov    -0x38(%rbp),%edi
add    $0x1,%edi
call   1040 <alloca@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x34(%rbp),%edx
call   1060 <base64_encode@plt>
mov    -0x38(%rbp),%eax
add    $0xb,%eax
mov    %eax,-0x3c(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x3c(%rbp),%edi
add    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x28(%rbp),%rcx
lea    0xdf3(%rip),%rsi
call   1070 <sprintf@plt>
mov    -0x30(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<basic_authentication_encode>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r12
mov    %rdi,%r13
call   1030 <strlen@plt>
mov    %eax,%ebx
mov    %r12,%rdi
call   1030 <strlen@plt>
lea    (%rbx,%rax,1),%r15d
add    $0x1,%r15d
lea    (%rbx,%rax,1),%ecx
add    $0x3,%ecx
movslq %ecx,%rcx
imul   $0x55555556,%rcx,%rbp
mov    %rbp,%rcx
shr    $0x3f,%rcx
shr    $0x20,%rbp
add    %ecx,%ebp
lea    (%rbx,%rax,1),%edi
add    $0x2,%edi
call   1040 <alloca@plt>
mov    %rax,%rbx
lea    0xe53(%rip),%rsi
mov    %rax,%rdi
mov    %r13,%rdx
mov    %r12,%rcx
call   1070 <sprintf@plt>
lea    0x1(,%rbp,4),%edi
call   1040 <alloca@plt>
mov    %rax,%r12
mov    %rbx,%rdi
mov    %rax,%rsi
mov    %r15d,%edx
call   1060 <base64_encode@plt>
mov    %r14,%rdi
call   1030 <strlen@plt>
lea    (%rax,%rbp,4),%edi
add    $0xb,%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
lea    0xe11(%rip),%rsi
mov    %rax,%rdi
mov    %r14,%rdx
mov    %r12,%rcx
call   1070 <sprintf@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"%s:%s\00"
@.str.1 = [15 x i8] c"%s: Basic %s\0D\0A\00"
define i8* @basic_authentication_encode(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
%12 = load i8*, i8** %4
%13 = call i32 @strlen(i8* %12)
%14 = add nsw i32 %13, 1
%15 = load i8*, i8** %5
%16 = call i32 @strlen(i8* %15)
%17 = add nsw i32 %14, %16
store i32 %17, i32* %10
%18 = load i32, i32* %10
%19 = add nsw i32 %18, 2
%20 = sdiv i32 %19, 3
%21 = mul nsw i32 4, %20
store i32 %21, i32* %11
%22 = load i32, i32* %10
%23 = add nsw i32 %22, 1
%24 = call i64 @alloca(i32 %23)
%25 = inttoptr i64 %24 to i8*
store i8* %25, i8** %7
%26 = load i8*, i8** %7
%27 = load i8*, i8** %4
%28 = load i8*, i8** %5
%29 = call i32 @sprintf(i8* %26, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %27, i8* %28)
%30 = load i32, i32* %11
%31 = add nsw i32 1, %30
%32 = call i64 @alloca(i32 %31)
%33 = inttoptr i64 %32 to i8*
store i8* %33, i8** %8
%34 = load i8*, i8** %7
%35 = load i8*, i8** %8
%36 = load i32, i32* %10
%37 = call i32 @base64_encode(i8* %34, i8* %35, i32 %36)
%38 = load i32, i32* %11
%39 = add nsw i32 %38, 11
%40 = load i8*, i8** %6
%41 = call i32 @strlen(i8* %40)
%42 = add nsw i32 %39, %41
%43 = call i64 @malloc(i32 %42)
%44 = inttoptr i64 %43 to i8*
store i8* %44, i8** %9
%45 = load i8*, i8** %9
%46 = load i8*, i8** %6
%47 = load i8*, i8** %8
%48 = call i32 @sprintf(i8* %45, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %46, i8* %47)
%49 = load i8*, i8** %9
ret i8* %49
}

LLVM IR (-O2):
@.str = [6 x i8] c"%s:%s\00"
@.str.1 = [15 x i8] c"%s: Basic %s\0D\0A\00"
define i8* @basic_authentication_encode(i8* %0, i8* %1, i8* %2) {
%4 = tail call i32 @strlen(i8* %0)
%5 = add nsw i32 %4, 1
%6 = tail call i32 @strlen(i8* %1)
%7 = add nsw i32 %5, %6
%8 = add nsw i32 %7, 2
%9 = sdiv i32 %8, 3
%10 = shl nsw i32 %9, 2
%11 = add nsw i32 %7, 1
%12 = tail call i64 @alloca(i32 %11)
%13 = inttoptr i64 %12 to i8*
%14 = tail call i32 @sprintf(i8* dereferenceable(1) %13, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1)
%15 = or i32 %10, 1
%16 = tail call i64 @alloca(i32 %15)
%17 = inttoptr i64 %16 to i8*
%18 = tail call i32 @base64_encode(i8* %13, i8* %17, i32 %7)
%19 = add nsw i32 %10, 11
%20 = tail call i32 @strlen(i8* %2)
%21 = add nsw i32 %19, %20
%22 = tail call i64 @malloc(i32 %21)
%23 = inttoptr i64 %22 to i8*
%24 = tail call i32 @sprintf(i8* dereferenceable(1) %23, i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %2, i8* %17)
ret i8* %23
}