#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*/ free(char *);
int /*<<< orphan*/ printf(char *);
char *readline(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
int console_read_command(char *out, int max_len) {
  char *buf = readline("(mspdebug) ");
  if (!buf) {
    printf("\n");
    return 1;
  }
  strncpy(out, buf, max_len);
  out[max_len - 1] = 0;
  free(buf);
  return 0;
}


x86 assembly (clang, -O0):
<console_read_command>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
lea    0xeaa(%rip),%rdi
call   1060 <readline@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1182 <console_read_command+0x42>
lea    0xe9b(%rip),%rdi
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    11b3 <console_read_command+0x73>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <strncpy@plt>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<console_read_command>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebp
mov    %rdi,%r14
lea    0xeb0(%rip),%rdi
call   1060 <readline@plt>
test   %rax,%rax
je     117f <console_read_command+0x3f>
mov    %rax,%rbx
mov    %r14,%rdi
mov    %rax,%rsi
mov    %ebp,%edx
call   1050 <strncpy@plt>
movslq %ebp,%rax
movb   $0x0,-0x1(%rax,%r14,1)
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
jmp    118e <console_read_command+0x4e>
mov    $0xa,%edi
call   1040 <putchar@plt>
mov    $0x1,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [12 x i8] c"(mspdebug) \00"
@.str.1 = [2 x i8] c"\0A\00"
define i32 @console_read_command(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
%7 = call i8* @readline(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
store i8* %7, i8** %6
%8 = load i8*, i8** %6
%9 = icmp ne i8* %8, null
br i1 %9, label %12, label %10
10: ; preds = %2
%11 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %24
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = load i8*, i8** %6
%15 = load i32, i32* %5
%16 = call i32 @strncpy(i8* %13, i8* %14, i32 %15)
%17 = load i8*, i8** %4
%18 = load i32, i32* %5
%19 = sub nsw i32 %18, 1
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %17, i64 %20
store i8 0, i8* %21
%22 = load i8*, i8** %6
%23 = call i32 @free(i8* %22)
store i32 0, i32* %3
br label %24
24: ; preds = %12, %10
%25 = load i32, i32* %3
ret i32 %25
}

LLVM IR (-O2):
@.str = [12 x i8] c"(mspdebug) \00"
define i32 @console_read_command(i8* %0, i32 %1) {
%3 = tail call i8* @readline(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @putchar(i32 10)
br label %13
7: ; preds = %2
%8 = tail call i32 @strncpy(i8* %0, i8* %3, i32 %1)
%9 = add nsw i32 %1, -1
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds i8, i8* %0, i64 %10
store i8 0, i8* %11
%12 = tail call i32 @free(i8* %3)
br label %13
13: ; preds = %7, %5
%14 = phi i32 [ 0, %7 ], [ 1, %5 ]
ret i32 %14
}