#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 */
struct telnetd_state {
  int dummy;
};

/* Variables and functions */
char *alloc_line();
int /*<<< orphan*/ sendline(struct telnetd_state *, char *);
unsigned int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
void telnetd_output(struct telnetd_state *s, char *str1, char *str2) {
  static unsigned len;
  char *line;
  line = alloc_line();
  if (line != NULL) {
    len = strlen(str1);
    strncpy(line, str1, 36);
    if (len < 36) {
      strncpy(line + len, str2, 36 - len);
    }
    len = strlen(line);
    if (len < 36 - 2) {
      line[len] = 0x0d;
      line[len + 1] = 0x0a;
      line[len + 2] = 0;
    }
    sendline(s, line);
  }
}


x86 assembly (clang, -O0):
<telnetd_output>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    $0x0,%al
call   1040 <alloc_line@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1216 <telnetd_output+0xd6>
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,0x2ecb(%rip)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x24,%edx
call   1030 <strncpy@plt>
cmpl   $0x24,0x2eb2(%rip)
jae    11b9 <telnetd_output+0x79>
mov    -0x20(%rbp),%rdi
mov    0x2ea2(%rip),%eax
add    %rax,%rdi
mov    -0x18(%rbp),%rsi
mov    $0x24,%edx
sub    0x2e90(%rip),%edx
call   1030 <strncpy@plt>
mov    -0x20(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,0x2e7c(%rip)
cmpl   $0x22,0x2e75(%rip)
jae    1209 <telnetd_output+0xc9>
mov    -0x20(%rbp),%rax
mov    0x2e65(%rip),%ecx
movb   $0xd,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    0x2e57(%rip),%ecx
add    $0x1,%ecx
mov    %ecx,%ecx
movb   $0xa,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    0x2e44(%rip),%ecx
add    $0x2,%ecx
mov    %ecx,%ecx
movb   $0x0,(%rax,%rcx,1)
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1050 <sendline@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<telnetd_output>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%r14
xor    %eax,%eax
call   1040 <alloc_line@plt>
test   %rax,%rax
je     11d4 <telnetd_output+0x94>
mov    %rax,%rbx
mov    %r12,%rdi
call   1060 <strlen@plt>
mov    %eax,0x2ed6(%rip)
mov    $0x24,%ebp
mov    %rbx,%rdi
mov    %r12,%rsi
mov    $0x24,%edx
call   1030 <strncpy@plt>
mov    0x2ebb(%rip),%eax
cmp    $0x23,%rax
ja     11a1 <telnetd_output+0x61>
mov    %rbx,%rdi
add    %rax,%rdi
sub    %eax,%ebp
mov    %r15,%rsi
mov    %ebp,%edx
call   1030 <strncpy@plt>
mov    %rbx,%rdi
call   1060 <strlen@plt>
mov    %eax,0x2e95(%rip)
cmp    $0x21,%eax
ja     11c1 <telnetd_output+0x81>
mov    %eax,%eax
movw   $0xa0d,(%rbx,%rax,1)
movb   $0x0,0x2(%rbx,%rax,1)
mov    %r14,%rdi
mov    %rbx,%rsi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <sendline@plt>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.telnetd_state = type { i32 }
@telnetd_output.len = internal global i32 0
define void @telnetd_output(%struct.telnetd_state* %0, i8* %1, i8* %2) {
%4 = alloca %struct.telnetd_state*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
store %struct.telnetd_state* %0, %struct.telnetd_state** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
%8 = call i8* (...) @alloc_line()
store i8* %8, i8** %7
%9 = load i8*, i8** %7
%10 = icmp ne i8* %9, null
br i1 %10, label %11, label %52
11: ; preds = %3
%12 = load i8*, i8** %5
%13 = call i32 @strlen(i8* %12)
store i32 %13, i32* @telnetd_output.len
%14 = load i8*, i8** %7
%15 = load i8*, i8** %5
%16 = call i32 @strncpy(i8* %14, i8* %15, i32 36)
%17 = load i32, i32* @telnetd_output.len
%18 = icmp ult i32 %17, 36
br i1 %18, label %19, label %28
19: ; preds = %11
%20 = load i8*, i8** %7
%21 = load i32, i32* @telnetd_output.len
%22 = zext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %20, i64 %22
%24 = load i8*, i8** %6
%25 = load i32, i32* @telnetd_output.len
%26 = sub i32 36, %25
%27 = call i32 @strncpy(i8* %23, i8* %24, i32 %26)
br label %28
28: ; preds = %19, %11
%29 = load i8*, i8** %7
%30 = call i32 @strlen(i8* %29)
store i32 %30, i32* @telnetd_output.len
%31 = load i32, i32* @telnetd_output.len
%32 = icmp ult i32 %31, 34
br i1 %32, label %33, label %48
33: ; preds = %28
%34 = load i8*, i8** %7
%35 = load i32, i32* @telnetd_output.len
%36 = zext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %34, i64 %36
store i8 13, i8* %37
%38 = load i8*, i8** %7
%39 = load i32, i32* @telnetd_output.len
%40 = add i32 %39, 1
%41 = zext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %38, i64 %41
store i8 10, i8* %42
%43 = load i8*, i8** %7
%44 = load i32, i32* @telnetd_output.len
%45 = add i32 %44, 2
%46 = zext i32 %45 to i64
%47 = getelementptr inbounds i8, i8* %43, i64 %46
store i8 0, i8* %47
br label %48
48: ; preds = %33, %28
%49 = load %struct.telnetd_state*, %struct.telnetd_state** %4
%50 = load i8*, i8** %7
%51 = call i32 @sendline(%struct.telnetd_state* %49, i8* %50)
br label %52
52: ; preds = %48, %3
ret void
}

LLVM IR (-O2):
%struct.telnetd_state = type { i32 }
@telnetd_output.len = internal unnamed_addr global i32 0
define void @telnetd_output(%struct.telnetd_state* %0, i8* %1, i8* %2) {
%4 = tail call i8* (...) @alloc_line()
%5 = icmp eq i8* %4, null
br i1 %5, label %30, label %6
6: ; preds = %3
%7 = tail call i32 @strlen(i8* %1)
store i32 %7, i32* @telnetd_output.len
%8 = tail call i32 @strncpy(i8* %4, i8* %1, i32 36)
%9 = load i32, i32* @telnetd_output.len
%10 = icmp ult i32 %9, 36
br i1 %10, label %11, label %16
11: ; preds = %6
%12 = zext i32 %9 to i64
%13 = getelementptr inbounds i8, i8* %4, i64 %12
%14 = sub nuw nsw i32 36, %9
%15 = tail call i32 @strncpy(i8* %13, i8* %2, i32 %14)
br label %16
16: ; preds = %11, %6
%17 = tail call i32 @strlen(i8* %4)
store i32 %17, i32* @telnetd_output.len
%18 = icmp ult i32 %17, 34
br i1 %18, label %19, label %28
19: ; preds = %16
%20 = zext i32 %17 to i64
%21 = getelementptr inbounds i8, i8* %4, i64 %20
store i8 13, i8* %21
%22 = add nuw nsw i32 %17, 1
%23 = zext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %4, i64 %23
store i8 10, i8* %24
%25 = add nuw nsw i32 %17, 2
%26 = zext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %4, i64 %26
store i8 0, i8* %27
br label %28
28: ; preds = %19, %16
%29 = tail call i32 @sendline(%struct.telnetd_state* %0, i8* %4)
br label %30
30: ; preds = %28, %3
ret void
}