#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 char uint8_t;

/* Variables and functions */
char *memchr(char *, char, size_t);
int /*<<< orphan*/ memcpy(char *, char *, int);
size_t mschapv2_username_clean(uint8_t *username, size_t usernamelen,
                               uint8_t *usernameclean) {
  static char *ptr;
  ptr = memchr(username, '\\', usernamelen);
  if (ptr == NULL) {
    memcpy(usernameclean, username, usernamelen);
    return usernamelen;
  }
  memcpy(usernameclean, ptr + 1, username + usernamelen - (uint8_t *)ptr - 1);
  return (username + usernamelen - (uint8_t *)ptr - 1);
}


x86 assembly (clang, -O0):
<mschapv2_username_clean>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x5c,%esi
call   1030 <memchr@plt>
mov    %rax,0x2eeb(%rip)
cmpq   $0x0,0x2ee3(%rip)
jne    117b <mschapv2_username_clean+0x5b>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rax
mov    %eax,%edx
call   1040 <memcpy@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11c1 <mschapv2_username_clean+0xa1>
mov    -0x20(%rbp),%rdi
mov    0x2eb2(%rip),%rsi
add    $0x1,%rsi
mov    -0x10(%rbp),%rax
add    -0x18(%rbp),%rax
mov    0x2e9f(%rip),%rcx
sub    %rcx,%rax
sub    $0x1,%rax
mov    %eax,%edx
call   1040 <memcpy@plt>
mov    -0x10(%rbp),%rax
add    -0x18(%rbp),%rax
mov    0x2e82(%rip),%rcx
sub    %rcx,%rax
sub    $0x1,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mschapv2_username_clean>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %rdi,%r14
mov    $0x5c,%esi
mov    %rbx,%rdx
call   1030 <memchr@plt>
mov    %rax,0x2ef6(%rip)
test   %rax,%rax
je     116e <mschapv2_username_clean+0x4e>
mov    %rax,%rsi
add    $0x1,%rsi
add    %rbx,%r14
not    %eax
lea    (%r14,%rax,1),%edx
mov    %r15,%rdi
call   1040 <memcpy@plt>
mov    0x2ed2(%rip),%rbx
not    %rbx
add    %r14,%rbx
jmp    117b <mschapv2_username_clean+0x5b>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   1040 <memcpy@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@mschapv2_username_clean.ptr = internal global i8* null
define i64 @mschapv2_username_clean(i8* %0, i64 %1, i8* %2) {
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
store i8* %0, i8** %5
store i64 %1, i64* %6
store i8* %2, i8** %7
%8 = load i8*, i8** %5
%9 = load i64, i64* %6
%10 = call i8* @memchr(i8* %8, i8 signext 92, i64 %9)
store i8* %10, i8** @mschapv2_username_clean.ptr
%11 = load i8*, i8** @mschapv2_username_clean.ptr
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %20
13: ; preds = %3
%14 = load i8*, i8** %7
%15 = load i8*, i8** %5
%16 = load i64, i64* %6
%17 = trunc i64 %16 to i32
%18 = call i32 @memcpy(i8* %14, i8* %15, i32 %17)
%19 = load i64, i64* %6
store i64 %19, i64* %4
br label %42
20: ; preds = %3
%21 = load i8*, i8** %7
%22 = load i8*, i8** @mschapv2_username_clean.ptr
%23 = getelementptr inbounds i8, i8* %22, i64 1
%24 = load i8*, i8** %5
%25 = load i64, i64* %6
%26 = getelementptr inbounds i8, i8* %24, i64 %25
%27 = load i8*, i8** @mschapv2_username_clean.ptr
%28 = ptrtoint i8* %26 to i64
%29 = ptrtoint i8* %27 to i64
%30 = sub i64 %28, %29
%31 = sub nsw i64 %30, 1
%32 = trunc i64 %31 to i32
%33 = call i32 @memcpy(i8* %21, i8* %23, i32 %32)
%34 = load i8*, i8** %5
%35 = load i64, i64* %6
%36 = getelementptr inbounds i8, i8* %34, i64 %35
%37 = load i8*, i8** @mschapv2_username_clean.ptr
%38 = ptrtoint i8* %36 to i64
%39 = ptrtoint i8* %37 to i64
%40 = sub i64 %38, %39
%41 = sub nsw i64 %40, 1
store i64 %41, i64* %4
br label %42
42: ; preds = %20, %13
%43 = load i64, i64* %4
ret i64 %43
}

LLVM IR (-O2):
@mschapv2_username_clean.ptr = internal unnamed_addr global i8* null
define i64 @mschapv2_username_clean(i8* %0, i64 %1, i8* %2) {
%4 = tail call i8* @memchr(i8* %0, i8 signext 92, i64 %1)
store i8* %4, i8** @mschapv2_username_clean.ptr
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %9
6: ; preds = %3
%7 = trunc i64 %1 to i32
%8 = tail call i32 @memcpy(i8* %2, i8* %0, i32 %7)
br label %22
9: ; preds = %3
%10 = getelementptr inbounds i8, i8* %4, i64 1
%11 = getelementptr inbounds i8, i8* %0, i64 %1
%12 = ptrtoint i8* %11 to i64
%13 = ptrtoint i8* %4 to i64
%14 = xor i64 %13, -1
%15 = add i64 %14, %12
%16 = trunc i64 %15 to i32
%17 = tail call i32 @memcpy(i8* %2, i8* %10, i32 %16)
%18 = load i8*, i8** @mschapv2_username_clean.ptr
%19 = ptrtoint i8* %18 to i64
%20 = xor i64 %19, -1
%21 = add i64 %20, %12
br label %22
22: ; preds = %9, %6
%23 = phi i64 [ %1, %6 ], [ %21, %9 ]
ret i64 %23
}