#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*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stdin;
int strlen(char *);
void cmdinp(char *cmd, int len) {
  char *p;
  do {
    printf("cpm> ");
    if (fgets(cmd, len - 1, stdin) == NULL)
      exit(0);
    p = cmd + strlen(cmd);
    p[-1] = 0;
  } while (*cmd);
}


x86 assembly (clang, -O0):
<cmdinp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    0xeaa(%rip),%rdi
call   1040 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
sub    $0x1,%esi
mov    0x2e7c(%rip),%rax
mov    (%rax),%edx
call   1050 <fgets@plt>
cmp    $0x0,%rax
jne    1184 <cmdinp+0x44>
xor    %edi,%edi
call   1060 <exit@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movb   $0x0,-0x1(%rax)
mov    -0x8(%rbp),%rax
cmpb   $0x0,(%rax)
jne    114f <cmdinp+0xf>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cmdinp>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
add    $0xffffffff,%ebp
lea    0xeaa(%rip),%r14
mov    0x2e8b(%rip),%r15
nopl   (%rax)
mov    %r14,%rdi
call   1040 <printf@plt>
mov    (%r15),%edx
mov    %rbx,%rdi
mov    %ebp,%esi
call   1050 <fgets@plt>
test   %rax,%rax
je     1199 <cmdinp+0x59>
mov    %rbx,%rdi
call   1030 <strlen@plt>
cltq
movb   $0x0,-0x1(%rbx,%rax,1)
cmpb   $0x0,(%rbx)
jne    1160 <cmdinp+0x20>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %edi,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [6 x i8] c"cpm> \00"
@stdin = global i32 0
define void @cmdinp(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
store i8* %0, i8** %3
store i32 %1, i32* %4
br label %6
6: ; preds = %24, %2
%7 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%8 = load i8*, i8** %3
%9 = load i32, i32* %4
%10 = sub nsw i32 %9, 1
%11 = load i32, i32* @stdin
%12 = call i32* @fgets(i8* %8, i32 %10, i32 %11)
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %16
14: ; preds = %6
%15 = call i32 @exit(i32 0)
unreachable
16: ; preds = %6
%17 = load i8*, i8** %3
%18 = load i8*, i8** %3
%19 = call i32 @strlen(i8* %18)
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %17, i64 %20
store i8* %21, i8** %5
%22 = load i8*, i8** %5
%23 = getelementptr inbounds i8, i8* %22, i64 -1
store i8 0, i8* %23
br label %24
24: ; preds = %16
%25 = load i8*, i8** %3
%26 = load i8, i8* %25
%27 = icmp ne i8 %26, 0
br i1 %27, label %6, label %28
28: ; preds = %24
ret void
}

LLVM IR (-O2):
@.str = [6 x i8] c"cpm> \00"
@stdin = global i32 0
define void @cmdinp(i8* %0, i32 %1) {
%3 = add nsw i32 %1, -1
%4 = getelementptr inbounds i8, i8* %0, i64 -1
br label %5
5: ; preds = %12, %2
%6 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%7 = load i32, i32* @stdin
%8 = tail call i32* @fgets(i8* %0, i32 %3, i32 %7)
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %12
10: ; preds = %5
%11 = tail call i32 @exit(i32 0)
unreachable
12: ; preds = %5
%13 = tail call i32 @strlen(i8* %0)
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds i8, i8* %4, i64 %14
store i8 0, i8* %15
%16 = load i8, i8* %0
%17 = icmp eq i8 %16, 0
br i1 %17, label %18, label %5
18: ; preds = %12
ret void
}