#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 /*<<< orphan*/ memcpy(char *, char *, size_t);
size_t strlen(char *);
char *strstr(char *, char *);
size_t log_nickname(unsigned char *data, size_t length, char *buf,
                    size_t buflen) {
  char *nick = strstr((char *)data, "NICK ");
  if (nick) {
    memcpy(buf, nick + 5, length - (nick - (char *)data) - 5);
    size_t i;
    size_t len = length - (nick - (char *)data);
    for (i = 0; i < len; i++) {
      if (buf[i] == '\r' || buf[i] == '\n') {
        buf[i] = '\0';
        return strlen(buf);
      }
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<log_nickname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%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     121d <log_nickname+0xed>
mov    -0x20(%rbp),%rdi
mov    -0x30(%rbp),%rsi
add    $0x5,%rsi
mov    -0x18(%rbp),%rdx
mov    -0x30(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
sub    %rax,%rdx
sub    $0x5,%rdx
call   1040 <memcpy@plt>
mov    -0x18(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    -0x10(%rbp),%rdx
sub    %rdx,%rcx
sub    %rcx,%rax
mov    %rax,-0x40(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x40(%rbp),%rax
jae    1218 <log_nickname+0xe8>
mov    -0x20(%rbp),%rax
mov    -0x38(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xd,%eax
je     11e4 <log_nickname+0xb4>
mov    -0x20(%rbp),%rax
mov    -0x38(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    1202 <log_nickname+0xd2>
mov    -0x20(%rbp),%rax
mov    -0x38(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x8(%rbp)
jmp    1225 <log_nickname+0xf5>
jmp    1207 <log_nickname+0xd7>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    11ac <log_nickname+0x7c>
jmp    121d <log_nickname+0xed>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<log_nickname>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0xeb8(%rip),%rsi
call   1050 <strstr@plt>
test   %rax,%rax
je     1198 <log_nickname+0x68>
sub    %rax,%rbx
add    $0x5,%rax
lea    (%rbx,%r14,1),%r12
lea    (%rbx,%r14,1),%rdx
add    $0xfffffffffffffffb,%rdx
mov    %r15,%rdi
mov    %rax,%rsi
call   1040 <memcpy@plt>
test   %r12,%r12
je     1198 <log_nickname+0x68>
xor    %eax,%eax
nopw   0x0(%rax,%rax,1)
movzbl (%r15,%rax,1),%ecx
cmp    $0xd,%cl
je     11a6 <log_nickname+0x76>
cmp    $0xa,%cl
je     11a6 <log_nickname+0x76>
add    $0x1,%rax
cmp    %r12,%rax
jb     1180 <log_nickname+0x50>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
movb   $0x0,(%r15,%rax,1)
mov    %r15,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1030 <strlen@plt>

LLVM IR (-O0):
@.str = [6 x i8] c"NICK \00"
define i64 @log_nickname(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*
%11 = alloca i64
%12 = alloca i64
store i8* %0, i8** %6
store i64 %1, i64* %7
store i8* %2, i8** %8
store i64 %3, i64* %9
%13 = load i8*, i8** %6
%14 = call i8* @strstr(i8* %13, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i8* %14, i8** %10
%15 = load i8*, i8** %10
%16 = icmp ne i8* %15, null
br i1 %16, label %17, label %66
17: ; preds = %4
%18 = load i8*, i8** %8
%19 = load i8*, i8** %10
%20 = getelementptr inbounds i8, i8* %19, i64 5
%21 = load i64, i64* %7
%22 = load i8*, i8** %10
%23 = load i8*, i8** %6
%24 = ptrtoint i8* %22 to i64
%25 = ptrtoint i8* %23 to i64
%26 = sub i64 %24, %25
%27 = sub i64 %21, %26
%28 = sub i64 %27, 5
%29 = call i32 @memcpy(i8* %18, i8* %20, i64 %28)
%30 = load i64, i64* %7
%31 = load i8*, i8** %10
%32 = load i8*, i8** %6
%33 = ptrtoint i8* %31 to i64
%34 = ptrtoint i8* %32 to i64
%35 = sub i64 %33, %34
%36 = sub i64 %30, %35
store i64 %36, i64* %12
store i64 0, i64* %11
br label %37
37: ; preds = %62, %17
%38 = load i64, i64* %11
%39 = load i64, i64* %12
%40 = icmp ult i64 %38, %39
br i1 %40, label %41, label %65
41: ; preds = %37
%42 = load i8*, i8** %8
%43 = load i64, i64* %11
%44 = getelementptr inbounds i8, i8* %42, i64 %43
%45 = load i8, i8* %44
%46 = sext i8 %45 to i32
%47 = icmp eq i32 %46, 13
br i1 %47, label %55, label %48
48: ; preds = %41
%49 = load i8*, i8** %8
%50 = load i64, i64* %11
%51 = getelementptr inbounds i8, i8* %49, i64 %50
%52 = load i8, i8* %51
%53 = sext i8 %52 to i32
%54 = icmp eq i32 %53, 10
br i1 %54, label %55, label %61
55: ; preds = %48, %41
%56 = load i8*, i8** %8
%57 = load i64, i64* %11
%58 = getelementptr inbounds i8, i8* %56, i64 %57
store i8 0, i8* %58
%59 = load i8*, i8** %8
%60 = call i64 @strlen(i8* %59)
store i64 %60, i64* %5
br label %67
61: ; preds = %48
br label %62
62: ; preds = %61
%63 = load i64, i64* %11
%64 = add i64 %63, 1
store i64 %64, i64* %11
br label %37
65: ; preds = %37
br label %66
66: ; preds = %65, %4
store i64 0, i64* %5
br label %67
67: ; preds = %66, %55
%68 = load i64, i64* %5
ret i64 %68
}

LLVM IR (-O2):
@.str = [6 x i8] c"NICK \00"
define i64 @log_nickname(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 %26, label %7
7: ; preds = %4
%8 = getelementptr inbounds i8, i8* %5, i64 5
%9 = ptrtoint i8* %5 to i64
%10 = ptrtoint i8* %0 to i64
%11 = sub i64 %10, %9
%12 = add i64 %11, %1
%13 = add i64 %12, -5
%14 = tail call i32 @memcpy(i8* %2, i8* %8, i64 %13)
%15 = icmp eq i64 %12, 0
br i1 %15, label %26, label %16
16: ; preds = %7, %23
%17 = phi i64 [ %24, %23 ], [ 0, %7 ]
%18 = getelementptr inbounds i8, i8* %2, i64 %17
%19 = load i8, i8* %18
switch i8 %19, label %23 [
i8 13, label %20
i8 10, label %20
]
20: ; preds = %16, %16
%21 = getelementptr inbounds i8, i8* %2, i64 %17
store i8 0, i8* %21
%22 = tail call i64 @strlen(i8* dereferenceable(1) %2)
br label %26
23: ; preds = %16
%24 = add nuw i64 %17, 1
%25 = icmp ult i64 %24, %12
br i1 %25, label %16, label %26
26: ; preds = %23, %7, %4, %20
%27 = phi i64 [ %22, %20 ], [ 0, %4 ], [ 0, %7 ], [ 0, %23 ]
ret i64 %27
}