int count_lines(const char *pstr) {
  int count = 0;
  while (pstr && *pstr) {
    if (*pstr == '\n') {
      count++;
    }
    if (*(++pstr) == '\r') {
      pstr++;
    }
  }
  return count;
}


x86 assembly (clang, -O0):
<count_lines>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x8(%rbp)
mov    %al,-0xd(%rbp)
je     112f <count_lines+0x2f>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0xd(%rbp)
mov    -0xd(%rbp),%al
test   $0x1,%al
jne    113f <count_lines+0x3f>
jmp    1185 <count_lines+0x85>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xa,%eax
jne    1158 <count_lines+0x58>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
movsbl 0x1(%rax),%eax
cmp    $0xd,%eax
jne    1180 <count_lines+0x80>
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    110f <count_lines+0xf>
mov    -0xc(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<count_lines>:
xor    %eax,%eax
test   %rdi,%rdi
jne    1114 <count_lines+0x14>
ret
nopl   0x0(%rax,%rax,1)
add    $0x1,%rdi
movzbl (%rdi),%ecx
cmp    $0xa,%cl
je     1130 <count_lines+0x30>
test   %cl,%cl
je     1107 <count_lines+0x7>
cmpb   $0xd,0x1(%rdi)
jne    1110 <count_lines+0x10>
jmp    1139 <count_lines+0x39>
nopl   0x0(%rax,%rax,1)
add    $0x1,%eax
cmpb   $0xd,0x1(%rdi)
jne    1110 <count_lines+0x10>
add    $0x2,%rdi
jmp    1114 <count_lines+0x14>

LLVM IR (-O0):
define i32 @count_lines(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
store i8* %0, i8** %2
store i32 0, i32* %3
br label %4
4: ; preds = %31, %1
%5 = load i8*, i8** %2
%6 = icmp ne i8* %5, null
br i1 %6, label %7, label %12
7: ; preds = %4
%8 = load i8*, i8** %2
%9 = load i8, i8* %8
%10 = sext i8 %9 to i32
%11 = icmp ne i32 %10, 0
br label %12
12: ; preds = %7, %4
%13 = phi i1 [ false, %4 ], [ %11, %7 ]
br i1 %13, label %14, label %32
14: ; preds = %12
%15 = load i8*, i8** %2
%16 = load i8, i8* %15
%17 = sext i8 %16 to i32
%18 = icmp eq i32 %17, 10
br i1 %18, label %19, label %22
19: ; preds = %14
%20 = load i32, i32* %3
%21 = add nsw i32 %20, 1
store i32 %21, i32* %3
br label %22
22: ; preds = %19, %14
%23 = load i8*, i8** %2
%24 = getelementptr inbounds i8, i8* %23, i32 1
store i8* %24, i8** %2
%25 = load i8, i8* %24
%26 = sext i8 %25 to i32
%27 = icmp eq i32 %26, 13
br i1 %27, label %28, label %31
28: ; preds = %22
%29 = load i8*, i8** %2
%30 = getelementptr inbounds i8, i8* %29, i32 1
store i8* %30, i8** %2
br label %31
31: ; preds = %28, %22
br label %4
32: ; preds = %12
%33 = load i32, i32* %3
ret i32 %33
}

LLVM IR (-O2):
define i32 @count_lines(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %16, label %3
3: ; preds = %1, %9
%4 = phi i32 [ %10, %9 ], [ 0, %1 ]
%5 = phi i8* [ %15, %9 ], [ %0, %1 ]
%6 = load i8, i8* %5
switch i8 %6, label %9 [
i8 0, label %16
i8 10, label %7
]
7: ; preds = %3
%8 = add nsw i32 %4, 1
br label %9
9: ; preds = %3, %7
%10 = phi i32 [ %8, %7 ], [ %4, %3 ]
%11 = getelementptr inbounds i8, i8* %5, i64 1
%12 = load i8, i8* %11
%13 = icmp eq i8 %12, 13
%14 = getelementptr inbounds i8, i8* %5, i64 2
%15 = select i1 %13, i8* %14, i8* %11
br label %3
16: ; preds = %3, %1
%17 = phi i32 [ 0, %1 ], [ %4, %3 ]
ret i32 %17
}