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


x86 assembly (clang, -O0):
<log_realname>:
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     11cf <log_realname+0x9f>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    0xe90(%rip),%rsi
call   1040 <sscanf@plt>
cmp    $0x1,%eax
jne    1196 <log_realname+0x66>
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x8(%rbp)
jmp    11d7 <log_realname+0xa7>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    0xe88(%rip),%rsi
call   1040 <sscanf@plt>
cmp    $0x1,%eax
jne    11c5 <log_realname+0x95>
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x8(%rbp)
jmp    11d7 <log_realname+0xa7>
jmp    11ca <log_realname+0x9a>
jmp    11cf <log_realname+0x9f>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<log_realname>:
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
lea    0xec2(%rip),%rsi
call   1050 <strstr@plt>
test   %rax,%rax
je     1188 <log_realname+0x58>
mov    %rax,%rbx
lea    0xeb4(%rip),%rsi
mov    %rax,%rdi
mov    %r14,%rdx
call   1040 <sscanf@plt>
cmp    $0x1,%eax
je     1179 <log_realname+0x49>
lea    0xec4(%rip),%rsi
mov    %rbx,%rdi
mov    %r14,%rdx
call   1040 <sscanf@plt>
cmp    $0x1,%eax
jne    1188 <log_realname+0x58>
mov    %r14,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1030 <strlen@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [6 x i8] c"USER \00"
@.str.1 = [39 x i8] c"USER %*s \22%*[^\22]\22 \22%*[^\22]\22 :%64[^\0D\0A]\0D\0A\00"
@.str.2 = [29 x i8] c"USER %*s %*s %*s :%64[^\0D\0A]\0D\0A\00"
define i64 @log_realname(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 %33
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 %34
23: ; preds = %15
%24 = load i8*, i8** %10
%25 = load i8*, i8** %8
%26 = call i32 @sscanf(i8* %24, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %25)
%27 = icmp eq i32 %26, 1
br i1 %27, label %28, label %31
28: ; preds = %23
%29 = load i8*, i8** %8
%30 = call i64 @strlen(i8* %29)
store i64 %30, i64* %5
br label %34
31: ; preds = %23
br label %32
32: ; preds = %31
br label %33
33: ; preds = %32, %4
store i64 0, i64* %5
br label %34
34: ; preds = %33, %28, %20
%35 = load i64, i64* %5
ret i64 %35
}

LLVM IR (-O2):
@.str = [6 x i8] c"USER \00"
@.str.1 = [39 x i8] c"USER %*s \22%*[^\22]\22 \22%*[^\22]\22 :%64[^\0D\0A]\0D\0A\00"
@.str.2 = [29 x i8] c"USER %*s %*s %*s :%64[^\0D\0A]\0D\0A\00"
define i64 @log_realname(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 %15, 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 %13, label %10
10: ; preds = %7
%11 = tail call i32 @sscanf(i8* %5, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %2)
%12 = icmp eq i32 %11, 1
br i1 %12, label %13, label %15
13: ; preds = %10, %7
%14 = tail call i64 @strlen(i8* dereferenceable(1) %2)
br label %15
15: ; preds = %13, %4, %10
%16 = phi i64 [ 0, %10 ], [ 0, %4 ], [ %14, %13 ]
ret i64 %16
}