#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__ read(int /*<<< orphan*/, char *, int);
int get_cmd(char *buf, int size) {
  int i = 0;
  char ch;
  while (1) {
    if (read(0, &ch, 1) < 0) {
      break;
    }
    if (ch == '\n') {
      break;
    }
    if (ch == '\r')
      continue;
    buf[i++] = ch;
    if (i >= size - 1)
      break;
  }
  buf[i] = 0x00;
  buf[size - 1] = 0x00;
  return (i);
}


x86 assembly (clang, -O0):
<get_cmd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
xor    %edi,%edi
lea    -0x11(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x0,%rax
jge    1145 <get_cmd+0x35>
jmp    119c <get_cmd+0x8c>
movsbl -0x11(%rbp),%eax
cmp    $0xa,%eax
jne    1157 <get_cmd+0x47>
jmp    119c <get_cmd+0x8c>
movsbl -0x11(%rbp),%eax
cmp    $0xd,%eax
jne    1169 <get_cmd+0x59>
jmp    1126 <get_cmd+0x16>
mov    -0x11(%rbp),%dl
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x10(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x10(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jl     1197 <get_cmd+0x87>
jmp    119c <get_cmd+0x8c>
jmp    1126 <get_cmd+0x16>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_cmd>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r15d
mov    %rdi,%r14
add    $0xffffffff,%r15d
cmp    $0x2,%r15d
mov    $0x1,%r12d
cmovge %r15d,%r12d
xor    %ebp,%ebp
lea    0xf(%rsp),%rbx
nopl   0x0(%rax,%rax,1)
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
test   %rax,%rax
js     1170 <get_cmd+0x60>
movzbl 0xf(%rsp),%eax
cmp    $0xa,%al
je     1170 <get_cmd+0x60>
cmp    $0xd,%al
je     1140 <get_cmd+0x30>
mov    %al,(%r14,%rbp,1)
add    $0x1,%rbp
cmp    %r12,%rbp
jne    1140 <get_cmd+0x30>
jmp    1173 <get_cmd+0x63>
mov    %ebp,%r12d
movslq %r12d,%rax
movb   $0x0,(%r14,%rax,1)
movslq %r15d,%rcx
movb   $0x0,(%r14,%rcx,1)
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @get_cmd(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8
store i8* %0, i8** %3
store i32 %1, i32* %4
store i32 0, i32* %5
br label %7
7: ; preds = %2, %20, %33
%8 = call i64 @read(i32 0, i8* %6, i32 1)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %7
br label %34
11: ; preds = %7
%12 = load i8, i8* %6
%13 = sext i8 %12 to i32
%14 = icmp eq i32 %13, 10
br i1 %14, label %15, label %16
15: ; preds = %11
br label %34
16: ; preds = %11
%17 = load i8, i8* %6
%18 = sext i8 %17 to i32
%19 = icmp eq i32 %18, 13
br i1 %19, label %20, label %21
20: ; preds = %16
br label %7
21: ; preds = %16
%22 = load i8, i8* %6
%23 = load i8*, i8** %3
%24 = load i32, i32* %5
%25 = add nsw i32 %24, 1
store i32 %25, i32* %5
%26 = sext i32 %24 to i64
%27 = getelementptr inbounds i8, i8* %23, i64 %26
store i8 %22, i8* %27
%28 = load i32, i32* %5
%29 = load i32, i32* %4
%30 = sub nsw i32 %29, 1
%31 = icmp sge i32 %28, %30
br i1 %31, label %32, label %33
32: ; preds = %21
br label %34
33: ; preds = %21
br label %7
34: ; preds = %32, %15, %10
%35 = load i8*, i8** %3
%36 = load i32, i32* %5
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %35, i64 %37
store i8 0, i8* %38
%39 = load i8*, i8** %3
%40 = load i32, i32* %4
%41 = sub nsw i32 %40, 1
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8, i8* %39, i64 %42
store i8 0, i8* %43
%44 = load i32, i32* %5
ret i32 %44
}

LLVM IR (-O2):
define i32 @get_cmd(i8* nocapture writeonly %0, i32 %1) {
%3 = alloca i8
%4 = add i32 %1, -1
%5 = call i32 @llvm.smax.i32(i32 %4, i32 1)
%6 = zext i32 %5 to i64
br label %7
7: ; preds = %17, %2
%8 = phi i64 [ %18, %17 ], [ 0, %2 ]
br label %9
9: ; preds = %7, %15
%10 = call i64 @read(i32 0, i8* %3, i32 1)
%11 = icmp slt i64 %10, 0
%12 = load i8, i8* %3
%13 = icmp eq i8 %12, 10
%14 = select i1 %11, i1 true, i1 %13
br i1 %14, label %21, label %15
15: ; preds = %9
%16 = icmp eq i8 %12, 13
br i1 %16, label %9, label %17
17: ; preds = %15
%18 = add nuw nsw i64 %8, 1
%19 = getelementptr inbounds i8, i8* %0, i64 %8
store i8 %12, i8* %19
%20 = icmp eq i64 %18, %6
br i1 %20, label %23, label %7
21: ; preds = %9
%22 = trunc i64 %8 to i32
br label %23
23: ; preds = %17, %21
%24 = phi i32 [ %22, %21 ], [ %5, %17 ]
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %0, i64 %25
store i8 0, i8* %26
%27 = sext i32 %4 to i64
%28 = getelementptr inbounds i8, i8* %0, i64 %27
store i8 0, i8* %28
ret i32 %24
}