#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 */
int /*<<< orphan*/ base64_encode(char *, char *, int);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ snprintf(char *, int, 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));
  int size;
  size = len1 + strlen(":");
  t1 = malloc(size);
  snprintf(t1, size, "%s:%s", user, passwd);
  t2 = malloc(len2 + 1);
  base64_encode(t1, t2, len1);
  size = strlen(header) + strlen(t2) + strlen(": Basic ") + 3;
  res = malloc(size);
  snprintf(res, size, "%s: Basic %s\r\n", header, t2);
  free(t2);
  free(t1);
  return res;
}


x86 assembly (clang, -O0):
<basic_authentication_encode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
mov    %eax,-0x4c(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x4c(%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),%eax
mov    %eax,-0x48(%rbp)
lea    0xe59(%rip),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x48(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x3c(%rbp),%esi
mov    -0x8(%rbp),%rcx
mov    -0x10(%rbp),%r8
lea    0xe2a(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x38(%rbp),%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x34(%rbp),%edx
call   1070 <base64_encode@plt>
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x44(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x44(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x40(%rbp)
lea    0xde6(%rip),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x40(%rbp),%eax
add    %ecx,%eax
add    $0x3,%eax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x3c(%rbp),%esi
mov    -0x18(%rbp),%rcx
mov    -0x28(%rbp),%r8
lea    0xdbb(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rax
add    $0x50,%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,(%rsp)
mov    %rsi,%r15
mov    %rdi,%r12
call   1040 <strlen@plt>
mov    %eax,%ebx
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %ebx,%ebp
add    %eax,%ebp
lea    (%rbx,%rax,1),%r13d
add    $0x1,%r13d
add    %ebx,%eax
add    $0x3,%eax
cltq
imul   $0x55555556,%rax,%rbx
mov    %rbx,%rax
shr    $0x3f,%rax
shr    $0x20,%rbx
add    %eax,%ebx
lea    0xe5e(%rip),%rdi
call   1040 <strlen@plt>
add    %eax,%ebp
add    $0x1,%ebp
mov    %ebp,%edi
call   1060 <malloc@plt>
mov    %rax,%r14
lea    0xe45(%rip),%rdx
mov    %rax,%rdi
mov    %ebp,%esi
mov    %r12,%rcx
mov    %r15,%r8
call   1050 <snprintf@plt>
lea    0x1(,%rbx,4),%edi
call   1060 <malloc@plt>
mov    %rax,%rbp
mov    %r14,%rdi
mov    %rax,%rsi
mov    %r13d,%edx
call   1070 <base64_encode@plt>
mov    (%rsp),%r12
mov    %r12,%rdi
call   1040 <strlen@plt>
mov    %eax,%r15d
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebx
lea    0xdfe(%rip),%rdi
call   1040 <strlen@plt>
add    %r15d,%ebx
add    %eax,%ebx
add    $0x3,%ebx
mov    %ebx,%edi
call   1060 <malloc@plt>
mov    %rax,%r15
lea    0xde9(%rip),%rdx
mov    %rax,%rdi
mov    %ebx,%esi
mov    %r12,%rcx
mov    %rbp,%r8
call   1050 <snprintf@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c":\00"
@.str.1 = [6 x i8] c"%s:%s\00"
@.str.2 = [9 x i8] c": Basic \00"
@.str.3 = [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
%12 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
%13 = load i8*, i8** %4
%14 = call i32 @strlen(i8* %13)
%15 = add nsw i32 %14, 1
%16 = load i8*, i8** %5
%17 = call i32 @strlen(i8* %16)
%18 = add nsw i32 %15, %17
store i32 %18, i32* %10
%19 = load i32, i32* %10
%20 = add nsw i32 %19, 2
%21 = sdiv i32 %20, 3
%22 = mul nsw i32 4, %21
store i32 %22, i32* %11
%23 = load i32, i32* %10
%24 = call i32 @strlen(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%25 = add nsw i32 %23, %24
store i32 %25, i32* %12
%26 = load i32, i32* %12
%27 = call i8* @malloc(i32 %26)
store i8* %27, i8** %7
%28 = load i8*, i8** %7
%29 = load i32, i32* %12
%30 = load i8*, i8** %4
%31 = load i8*, i8** %5
%32 = call i32 @snprintf(i8* %28, i32 %29, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %30, i8* %31)
%33 = load i32, i32* %11
%34 = add nsw i32 %33, 1
%35 = call i8* @malloc(i32 %34)
store i8* %35, i8** %8
%36 = load i8*, i8** %7
%37 = load i8*, i8** %8
%38 = load i32, i32* %10
%39 = call i32 @base64_encode(i8* %36, i8* %37, i32 %38)
%40 = load i8*, i8** %6
%41 = call i32 @strlen(i8* %40)
%42 = load i8*, i8** %8
%43 = call i32 @strlen(i8* %42)
%44 = add nsw i32 %41, %43
%45 = call i32 @strlen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%46 = add nsw i32 %44, %45
%47 = add nsw i32 %46, 3
store i32 %47, i32* %12
%48 = load i32, i32* %12
%49 = call i8* @malloc(i32 %48)
store i8* %49, i8** %9
%50 = load i8*, i8** %9
%51 = load i32, i32* %12
%52 = load i8*, i8** %6
%53 = load i8*, i8** %8
%54 = call i32 @snprintf(i8* %50, i32 %51, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8* %52, i8* %53)
%55 = load i8*, i8** %8
%56 = call i32 @free(i8* %55)
%57 = load i8*, i8** %7
%58 = call i32 @free(i8* %57)
%59 = load i8*, i8** %9
ret i8* %59
}

LLVM IR (-O2):
@.str = [2 x i8] c":\00"
@.str.1 = [6 x i8] c"%s:%s\00"
@.str.2 = [9 x i8] c": Basic \00"
@.str.3 = [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 = tail call i32 @strlen(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%12 = add nsw i32 %7, %11
%13 = tail call i8* @malloc(i32 %12)
%14 = tail call i32 @snprintf(i8* %13, i32 %12, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %1)
%15 = or i32 %10, 1
%16 = tail call i8* @malloc(i32 %15)
%17 = tail call i32 @base64_encode(i8* %13, i8* %16, i32 %7)
%18 = tail call i32 @strlen(i8* %2)
%19 = tail call i32 @strlen(i8* %16)
%20 = tail call i32 @strlen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%21 = add i32 %18, 3
%22 = add i32 %21, %19
%23 = add i32 %22, %20
%24 = tail call i8* @malloc(i32 %23)
%25 = tail call i32 @snprintf(i8* %24, i32 %23, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8* %2, i8* %16)
%26 = tail call i32 @free(i8* %16)
%27 = tail call i32 @free(i8* %13)
ret i8* %24
}