#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, char *, int);
int read_cmd_line(int fd, char *buf, unsigned int buf_len) {
  unsigned int count = 0;
  char ch;
  while (read(fd, &ch, 1) > 0) {
    if ((ch != '\n') && (ch != '\r') && (ch != '\0')) {
      buf[count] = ch;
      count++;
    } else {
      break;
    }
    if (count == (buf_len - 1)) {
      break;
    }
  }
  return count;
}


x86 assembly (clang, -O0):
<read_cmd_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x0,%rax
jle    11a6 <read_cmd_line+0x96>
movsbl -0x19(%rbp),%eax
cmp    $0xa,%eax
je     1186 <read_cmd_line+0x76>
movsbl -0x19(%rbp),%eax
cmp    $0xd,%eax
je     1186 <read_cmd_line+0x76>
movsbl -0x19(%rbp),%eax
cmp    $0x0,%eax
je     1186 <read_cmd_line+0x76>
mov    -0x19(%rbp),%dl
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
mov    %dl,(%rax,%rcx,1)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    118b <read_cmd_line+0x7b>
jmp    11a6 <read_cmd_line+0x96>
mov    -0x18(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jne    11a1 <read_cmd_line+0x91>
jmp    11a6 <read_cmd_line+0x96>
jmp    1129 <read_cmd_line+0x19>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_cmd_line>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%r14
mov    %edi,%r12d
add    $0xffffffff,%ebp
xor    %ebx,%ebx
lea    0x7(%rsp),%r15
mov    $0x2401,%r13d
jmp    114c <read_cmd_line+0x3c>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
cmp    %ebx,%ebp
je     1174 <read_cmd_line+0x64>
mov    %r12d,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
test   %rax,%rax
jle    1172 <read_cmd_line+0x62>
movzbl 0x7(%rsp),%eax
cmp    $0xd,%rax
ja     1140 <read_cmd_line+0x30>
bt     %rax,%r13
jae    1140 <read_cmd_line+0x30>
mov    %ebx,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @read_cmd_line(i32 %0, i8* nocapture writeonly %1, i32 %2) {
%4 = alloca i8
%5 = add i32 %2, -1
br label %6
6: ; preds = %12, %3
%7 = phi i64 [ %14, %12 ], [ 0, %3 ]
%8 = call i64 @read(i32 %0, i8* %4, i32 1)
%9 = icmp sgt i64 %8, 0
br i1 %9, label %10, label %19
10: ; preds = %6
%11 = load i8, i8* %4
switch i8 %11, label %12 [
i8 13, label %17
i8 10, label %17
i8 0, label %17
]
12: ; preds = %10
%13 = getelementptr inbounds i8, i8* %1, i64 %7
store i8 %11, i8* %13
%14 = add nuw nsw i64 %7, 1
%15 = trunc i64 %14 to i32
%16 = icmp eq i32 %5, %15
br i1 %16, label %21, label %6
17: ; preds = %10, %10, %10
%18 = trunc i64 %7 to i32
br label %21
19: ; preds = %6
%20 = trunc i64 %7 to i32
br label %21
21: ; preds = %12, %19, %17
%22 = phi i32 [ %18, %17 ], [ %20, %19 ], [ %5, %12 ]
ret i32 %22
}