#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 */
int sscanf(char *, char *, char *);
size_t strlen(char *);
char *strstr(char *, char *);
size_t log_username(unsigned char *data, size_t length, char *buf,
                    size_t buflen) {
  char *user = strstr((char *)data, "USER ");
  if (user && sscanf(user, "USER %64s \"%*[^\"]\" \"%*[^\"]\" :%*[^\r\n]\r\n",
                     buf) == 1)
    return strlen(buf);
  return 0;
}


x86 assembly (clang, -O0):
<log_username>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xead(%rip),%rsi
call   1050 <strstr@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     1196 <log_username+0x66>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    0xe90(%rip),%rsi
call   1040 <sscanf@plt>
cmp    $0x1,%eax
jne    1196 <log_username+0x66>
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x8(%rbp)
jmp    119e <log_username+0x6e>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<log_username>:
push   %rbx
mov    %rdx,%rbx
lea    0xec5(%rip),%rsi
call   1050 <strstr@plt>
test   %rax,%rax
je     1165 <log_username+0x35>
lea    0xeba(%rip),%rsi
mov    %rax,%rdi
mov    %rbx,%rdx
call   1040 <sscanf@plt>
cmp    $0x1,%eax
jne    1165 <log_username+0x35>
mov    %rbx,%rdi
pop    %rbx
jmp    1030 <strlen@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [6 x i8] c"USER \00"
@.str.1 = [39 x i8] c"USER %64s \22%*[^\22]\22 \22%*[^\22]\22 :%*[^\0D\0A]\0D\0A\00"
define i64 @log_username(i8* %0, i64 %1, i8* %2, i64 %3) {
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i8*
store i8* %0, i8** %6
store i64 %1, i64* %7
store i8* %2, i8** %8
store i64 %3, i64* %9
%11 = load i8*, i8** %6
%12 = call i8* @strstr(i8* %11, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i8* %12, i8** %10
%13 = load i8*, i8** %10
%14 = icmp ne i8* %13, null
br i1 %14, label %15, label %23
15: ; preds = %4
%16 = load i8*, i8** %10
%17 = load i8*, i8** %8
%18 = call i32 @sscanf(i8* %16, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i8* %17)
%19 = icmp eq i32 %18, 1
br i1 %19, label %20, label %23
20: ; preds = %15
%21 = load i8*, i8** %8
%22 = call i64 @strlen(i8* %21)
store i64 %22, i64* %5
br label %24
23: ; preds = %15, %4
store i64 0, i64* %5
br label %24
24: ; preds = %23, %20
%25 = load i64, i64* %5
ret i64 %25
}

LLVM IR (-O2):
@.str = [6 x i8] c"USER \00"
@.str.1 = [39 x i8] c"USER %64s \22%*[^\22]\22 \22%*[^\22]\22 :%*[^\0D\0A]\0D\0A\00"
define i64 @log_username(i8* %0, i64 %1, i8* %2, i64 %3) {
%5 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i8* %5, null
br i1 %6, label %12, label %7
7: ; preds = %4
%8 = tail call i32 @sscanf(i8* %5, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i8* %2)
%9 = icmp eq i32 %8, 1
br i1 %9, label %10, label %12
10: ; preds = %7
%11 = tail call i64 @strlen(i8* dereferenceable(1) %2)
br label %12
12: ; preds = %4, %7, %10
%13 = phi i64 [ %11, %10 ], [ 0, %7 ], [ 0, %4 ]
ret i64 %13
}