#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 */
struct sock {
  int dummy;
};

/* Variables and functions */
char *base64_encode(unsigned char *, size_t const, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ memcpy(unsigned char *, char const *, size_t);
unsigned char *mempcpy(unsigned char *, char const *, size_t);
int smtp_get_msg(struct sock *, int);
int /*<<< orphan*/ sock_sendmsg(struct sock *, char *, char *);
int smtp_auth_plain(struct sock *sock, const char *user, size_t user_size,
                    const char *passwd, size_t passwd_size) {
  const size_t total_size = user_size + passwd_size + 2;
  unsigned char buf[total_size + 1], *p = buf;
  *p++ = '\0';
  p = mempcpy(p, user, user_size);
  *p++ = '\0';
  memcpy(p, passwd, passwd_size);
  char *encoded = base64_encode(buf, total_size, NULL);
  if (!encoded)
    exit(1);
  sock_sendmsg(sock, "AUTH PLAIN %s", encoded);
  free(encoded);
  return smtp_get_msg(sock, 1);
}


x86 assembly (clang, -O0):
<smtp_auth_plain>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
lea    0x2(%rax,%rcx,1),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdx
mov    %rdx,%rcx
add    $0x1,%rcx
mov    %rsp,%rax
mov    %rax,-0x38(%rbp)
add    $0x10,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rax
sub    %rdx,%rax
mov    %rax,-0x58(%rbp)
mov    %rax,%rsp
mov    %rcx,-0x40(%rbp)
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x48(%rbp)
movb   $0x0,(%rax)
mov    -0x48(%rbp),%rdi
mov    %rdi,-0x60(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    %rdx,-0x68(%rbp)
call   1040 <memcpy@plt>
mov    -0x68(%rbp),%rcx
mov    -0x60(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x48(%rbp)
movb   $0x0,(%rax)
mov    -0x48(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x58(%rbp),%rdi
mov    -0x30(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1050 <base64_encode@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
jne    1243 <smtp_auth_plain+0xe3>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x8(%rbp),%rdi
mov    -0x50(%rbp),%rdx
lea    0xdae(%rip),%rsi
call   1080 <sock_sendmsg@plt>
mov    -0x50(%rbp),%rdi
call   1030 <free@plt>
mov    -0x8(%rbp),%rdi
mov    $0x1,%esi
call   1060 <smtp_get_msg@plt>
mov    -0x38(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<smtp_auth_plain>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8,%r13
mov    %rcx,%r12
mov    %rdx,%rbx
mov    %rdi,%r15
lea    (%r8,%rdx,1),%rax
add    $0x2,%rax
mov    %rax,-0x30(%rbp)
lea    (%r8,%rdx,1),%rax
add    $0x12,%rax
and    $0xfffffffffffffff0,%rax
mov    %rsp,%rcx
mov    %rcx,%r14
sub    %rax,%r14
neg    %rax
mov    %r14,%rsp
lea    0x1(%r14),%rdi
movb   $0x0,(%rcx,%rax,1)
call   1040 <memcpy@plt>
lea    (%r14,%rbx,1),%rdi
add    $0x2,%rdi
movb   $0x0,0x1(%rbx,%r14,1)
mov    %r12,%rsi
mov    %r13,%rdx
call   1040 <memcpy@plt>
mov    %r14,%rdi
mov    -0x30(%rbp),%rsi
xor    %edx,%edx
call   1050 <base64_encode@plt>
test   %rax,%rax
je     1213 <smtp_auth_plain+0xb3>
mov    %rax,%rbx
lea    0xe1c(%rip),%rsi
mov    %r15,%rdi
mov    %rax,%rdx
call   1080 <sock_sendmsg@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
mov    $0x1,%esi
call   1060 <smtp_get_msg@plt>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.sock = type { i32 }
@.str = [14 x i8] c"AUTH PLAIN %s\00"
define i32 @smtp_auth_plain(%struct.sock* %0, i8* %1, i64 %2, i8* %3, i64 %4) {
%6 = alloca %struct.sock*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i64
%12 = alloca i8*
%13 = alloca i64
%14 = alloca i8*
%15 = alloca i8*
store %struct.sock* %0, %struct.sock** %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i8* %3, i8** %9
store i64 %4, i64* %10
%16 = load i64, i64* %8
%17 = load i64, i64* %10
%18 = add i64 %16, %17
%19 = add i64 %18, 2
store i64 %19, i64* %11
%20 = load i64, i64* %11
%21 = add i64 %20, 1
%22 = call i8* @llvm.stacksave()
store i8* %22, i8** %12
%23 = alloca i8, i64 %21
store i64 %21, i64* %13
store i8* %23, i8** %14
%24 = load i8*, i8** %14
%25 = getelementptr inbounds i8, i8* %24, i32 1
store i8* %25, i8** %14
store i8 0, i8* %24
%26 = load i8*, i8** %14
%27 = load i8*, i8** %7
%28 = load i64, i64* %8
%29 = call i8* @mempcpy(i8* %26, i8* %27, i64 %28)
store i8* %29, i8** %14
%30 = load i8*, i8** %14
%31 = getelementptr inbounds i8, i8* %30, i32 1
store i8* %31, i8** %14
store i8 0, i8* %30
%32 = load i8*, i8** %14
%33 = load i8*, i8** %9
%34 = load i64, i64* %10
%35 = call i32 @memcpy(i8* %32, i8* %33, i64 %34)
%36 = load i64, i64* %11
%37 = call i8* @base64_encode(i8* %23, i64 %36, i32* null)
store i8* %37, i8** %15
%38 = load i8*, i8** %15
%39 = icmp ne i8* %38, null
br i1 %39, label %42, label %40
40: ; preds = %5
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %5
%43 = load %struct.sock*, %struct.sock** %6
%44 = load i8*, i8** %15
%45 = call i32 @sock_sendmsg(%struct.sock* %43, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %44)
%46 = load i8*, i8** %15
%47 = call i32 @free(i8* %46)
%48 = load %struct.sock*, %struct.sock** %6
%49 = call i32 @smtp_get_msg(%struct.sock* %48, i32 1)
%50 = load i8*, i8** %12
ret i32 %49
}

LLVM IR (-O2):
%struct.sock = type { i32 }
@.str = [14 x i8] c"AUTH PLAIN %s\00"
define i32 @smtp_auth_plain(%struct.sock* %0, i8* nocapture %1, i64 %2, i8* %3, i64 %4) {
%6 = add i64 %4, %2
%7 = add i64 %6, 2
%8 = add i64 %6, 3
%9 = alloca i8, i64 %8
%10 = getelementptr inbounds i8, i8* %9, i64 1
store i8 0, i8* %9
%11 = getelementptr inbounds i8, i8* %10, i64 %2
%12 = getelementptr inbounds i8, i8* %11, i64 1
store i8 0, i8* %11
%13 = call i32 @memcpy(i8* %12, i8* %3, i64 %4)
%14 = call i8* @base64_encode(i8* %9, i64 %7, i32* null)
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %18
16: ; preds = %5
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %5
%19 = call i32 @sock_sendmsg(%struct.sock* %0, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %14)
%20 = call i32 @free(i8* %14)
%21 = call i32 @smtp_get_msg(%struct.sock* %0, i32 1)
ret i32 %21
}