#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 */
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
char *strrchr(char *, char);
char *strstr(char *, char *);
void cut_log(char *msg, char *cut_str, int max_len) {
  char *log_end;
  char tmp_str[1024];
  int log_end_len;
  int log_pre_len;
  int last_tag = 0;
  log_end = strstr(msg, cut_str);
  if (log_end == NULL) {
    msg[max_len] = '\0';
    return;
  }
  log_end_len = strlen(log_end);
  if (strcmp(cut_str, "]") == 0) {
    log_end = strrchr(msg, ']');
    log_end_len = strlen(log_end);
  }
  log_pre_len = max_len - log_end_len;
  msg[log_pre_len] = '\0';
  strcpy(tmp_str, log_end);
  strcpy(msg + log_pre_len, tmp_str);
}


x86 assembly (clang, -O0):
<cut_log>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x42c(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <strstr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    119d <cut_log+0x4d>
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    122f <cut_log+0xdf>
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x424(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe49(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    11e7 <cut_log+0x97>
mov    -0x8(%rbp),%rdi
mov    $0x5d,%esi
call   1050 <strrchr@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x424(%rbp)
mov    -0x14(%rbp),%eax
sub    -0x424(%rbp),%eax
mov    %eax,-0x428(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x428(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
lea    -0x420(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x8(%rbp),%rdi
movslq -0x428(%rbp),%rax
add    %rax,%rdi
lea    -0x420(%rbp),%rsi
call   1030 <strcpy@plt>
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cut_log>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x400,%rsp
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %rdi,%r12
call   1070 <strstr@plt>
test   %rax,%rax
je     11d9 <cut_log+0x89>
mov    %rax,%rbp
mov    %rax,%rdi
call   1040 <strlen@plt>
mov    %eax,%r15d
lea    0xe79(%rip),%rsi
mov    %rbx,%rdi
call   1060 <strcmp@plt>
test   %rax,%rax
jne    11af <cut_log+0x5f>
mov    %r12,%rdi
mov    $0x5d,%esi
call   1050 <strrchr@plt>
mov    %rax,%rbp
mov    %rax,%rdi
call   1040 <strlen@plt>
mov    %eax,%r15d
sub    %r15d,%r14d
movslq %r14d,%rax
lea    (%r12,%rax,1),%rbx
movb   $0x0,(%r12,%rax,1)
mov    %rsp,%r14
mov    %r14,%rdi
mov    %rbp,%rsi
call   1030 <strcpy@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
jmp    11e1 <cut_log+0x91>
movslq %r14d,%rax
movb   $0x0,(%r12,%rax,1)
add    $0x400,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"]\00"
define void @cut_log(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca [1024 x i8]
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 0, i32* %11
%12 = load i8*, i8** %4
%13 = load i8*, i8** %5
%14 = call i8* @strstr(i8* %12, i8* %13)
store i8* %14, i8** %7
%15 = load i8*, i8** %7
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %22
17: ; preds = %3
%18 = load i8*, i8** %4
%19 = load i32, i32* %6
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %18, i64 %20
store i8 0, i8* %21
br label %50
22: ; preds = %3
%23 = load i8*, i8** %7
%24 = call i32 @strlen(i8* %23)
store i32 %24, i32* %9
%25 = load i8*, i8** %5
%26 = call i64 @strcmp(i8* %25, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %33
28: ; preds = %22
%29 = load i8*, i8** %4
%30 = call i8* @strrchr(i8* %29, i8 signext 93)
store i8* %30, i8** %7
%31 = load i8*, i8** %7
%32 = call i32 @strlen(i8* %31)
store i32 %32, i32* %9
br label %33
33: ; preds = %28, %22
%34 = load i32, i32* %6
%35 = load i32, i32* %9
%36 = sub nsw i32 %34, %35
store i32 %36, i32* %10
%37 = load i8*, i8** %4
%38 = load i32, i32* %10
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %37, i64 %39
store i8 0, i8* %40
%41 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%42 = load i8*, i8** %7
%43 = call i32 @strcpy(i8* %41, i8* %42)
%44 = load i8*, i8** %4
%45 = load i32, i32* %10
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i8, i8* %44, i64 %46
%48 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%49 = call i32 @strcpy(i8* %47, i8* %48)
br label %50
50: ; preds = %33, %17
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"]\00"
define void @cut_log(i8* %0, i8* %1, i32 %2) {
%4 = alloca [1024 x i8]
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%6 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) %1)
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %11
8: ; preds = %3
%9 = sext i32 %2 to i64
%10 = getelementptr inbounds i8, i8* %0, i64 %9
store i8 0, i8* %10
br label %26
11: ; preds = %3
%12 = tail call i32 @strlen(i8* %6)
%13 = tail call i64 @strcmp(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %11
%16 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 93)
%17 = tail call i32 @strlen(i8* %16)
br label %18
18: ; preds = %15, %11
%19 = phi i32 [ %17, %15 ], [ %12, %11 ]
%20 = phi i8* [ %16, %15 ], [ %6, %11 ]
%21 = sub nsw i32 %2, %19
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %0, i64 %22
store i8 0, i8* %23
%24 = call i32 @strcpy(i8* %5, i8* %20)
%25 = call i32 @strcpy(i8* %23, i8* %5)
br label %26
26: ; preds = %18, %8
ret void
}