#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 int /*<<< orphan*/ tbuf;

/* Variables and functions */
int snprintf(char *, int, char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ strncpy(char *, char const *, int);
int /*<<< orphan*/ trim_CRLF(char *);
int convert_str(const char *ori, char *buf, int len) {
  char tbuf[1024];
  if (len < sizeof(tbuf))
    return 0;
  strncpy(tbuf, ori, 1024);
  trim_CRLF(tbuf);
  if (tbuf[0] == 0x1A && tbuf[1] == '\0') {
    strcpy(tbuf, "Ctrl+z");
  }
  return snprintf(buf, len, "TX:[%s]\r\n", tbuf);
}


x86 assembly (clang, -O0):
<convert_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
cmp    $0x400,%rax
jae    1172 <convert_str+0x32>
movl   $0x0,-0x4(%rbp)
jmp    11e3 <convert_str+0xa3>
lea    -0x420(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x400,%edx
call   1030 <strncpy@plt>
lea    -0x420(%rbp),%rdi
call   1060 <trim_CRLF@plt>
movsbl -0x420(%rbp),%eax
cmp    $0x1a,%eax
jne    11c6 <convert_str+0x86>
movsbl -0x41f(%rbp),%eax
cmp    $0x0,%eax
jne    11c6 <convert_str+0x86>
lea    -0x420(%rbp),%rdi
lea    0xe3f(%rip),%rsi
call   1040 <strcpy@plt>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
lea    -0x420(%rbp),%rcx
lea    0xe2c(%rip),%rdx
call   1050 <snprintf@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<convert_str>:
xor    %eax,%eax
cmp    $0x400,%edx
jb     11b2 <convert_str+0x72>
push   %r15
push   %r14
push   %rbx
sub    $0x400,%rsp
mov    %edx,%ebx
mov    %rsi,%r14
mov    %rdi,%rsi
mov    %rsp,%r15
mov    %r15,%rdi
mov    $0x400,%edx
call   1030 <strncpy@plt>
mov    %r15,%rdi
call   1060 <trim_CRLF@plt>
cmpb   $0x1a,(%rsp)
jne    1192 <convert_str+0x52>
cmpb   $0x0,0x1(%rsp)
jne    1192 <convert_str+0x52>
lea    0xe76(%rip),%rsi
mov    %rsp,%rdi
call   1040 <strcpy@plt>
lea    0xe6e(%rip),%rdx
mov    %rsp,%rcx
mov    %r14,%rdi
mov    %ebx,%esi
call   1050 <snprintf@plt>
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [7 x i8] c"Ctrl+z\00"
@.str.1 = [10 x i8] c"TX:[%s]\0D\0A\00"
define i32 @convert_str(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca [1024 x i8]
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%9 = load i32, i32* %7
%10 = sext i32 %9 to i64
%11 = icmp ult i64 %10, 1024
br i1 %11, label %12, label %13
12: ; preds = %3
store i32 0, i32* %4
br label %36
13: ; preds = %3
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%15 = load i8*, i8** %5
%16 = call i32 @strncpy(i8* %14, i8* %15, i32 1024)
%17 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%18 = call i32 @trim_CRLF(i8* %17)
%19 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%20 = load i8, i8* %19
%21 = sext i8 %20 to i32
%22 = icmp eq i32 %21, 26
br i1 %22, label %23, label %31
23: ; preds = %13
%24 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 1
%25 = load i8, i8* %24
%26 = sext i8 %25 to i32
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %23
%29 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%30 = call i32 @strcpy(i8* %29, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %31
31: ; preds = %28, %23, %13
%32 = load i8*, i8** %6
%33 = load i32, i32* %7
%34 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%35 = call i32 @snprintf(i8* %32, i32 %33, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %34)
store i32 %35, i32* %4
br label %36
36: ; preds = %31, %12
%37 = load i32, i32* %4
ret i32 %37
}

LLVM IR (-O2):
@.str = [7 x i8] c"Ctrl+z\00"
@.str.1 = [10 x i8] c"TX:[%s]\0D\0A\00"
define i32 @convert_str(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 = icmp ult i32 %2, 1024
br i1 %6, label %20, label %7
7: ; preds = %3
%8 = call i32 @strncpy(i8* %5, i8* %0, i32 1024)
%9 = call i32 @trim_CRLF(i8* %5)
%10 = load i8, i8* %5
%11 = icmp eq i8 %10, 26
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 1
%13 = load i8, i8* %12
%14 = icmp eq i8 %13, 0
%15 = select i1 %11, i1 %14, i1 false
br i1 %15, label %16, label %18
16: ; preds = %7
%17 = call i32 @strcpy(i8* %5, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %18
18: ; preds = %16, %7
%19 = call i32 @snprintf(i8* %1, i32 %2, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %5)
br label %20
20: ; preds = %3, %18
%21 = phi i32 [ %19, %18 ], [ 0, %3 ]
ret i32 %21
}