#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 */
size_t strlen(char *);
char *strtok_r(char *, char *, char **);
char *util_str_lines(char *str, char **ctx) {
  char *s = strtok_r(str, "\n", ctx);
  size_t len = 0;
  if (s == NULL) {
    return s;
  }
  len = strlen(s);
  if (s[len - 1] == '\r') {
    s[len - 1] = 0;
  }
  return s;
}


x86 assembly (clang, -O0):
<util_str_lines>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xec1(%rip),%rsi
call   1040 <strtok_r@plt>
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1168 <util_str_lines+0x48>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11a6 <util_str_lines+0x86>
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xd,%eax
jne    119e <util_str_lines+0x7e>
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<util_str_lines>:
push   %rbx
mov    %rsi,%rdx
lea    0xed5(%rip),%rsi
call   1040 <strtok_r@plt>
mov    %rax,%rbx
test   %rax,%rax
je     114c <util_str_lines+0x2c>
mov    %rbx,%rdi
call   1030 <strlen@plt>
cmpb   $0xd,-0x1(%rax,%rbx,1)
jne    114c <util_str_lines+0x2c>
movb   $0x0,-0x1(%rbx,%rax,1)
mov    %rbx,%rax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [2 x i8] c"\0A\00"
define i8* @util_str_lines(i8* %0, i8** %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i64
store i8* %0, i8** %4
store i8** %1, i8*** %5
%8 = load i8*, i8** %4
%9 = load i8**, i8*** %5
%10 = call i8* @strtok_r(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %9)
store i8* %10, i8** %6
store i64 0, i64* %7
%11 = load i8*, i8** %6
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = load i8*, i8** %6
store i8* %14, i8** %3
br label %32
15: ; preds = %2
%16 = load i8*, i8** %6
%17 = call i64 @strlen(i8* %16)
store i64 %17, i64* %7
%18 = load i8*, i8** %6
%19 = load i64, i64* %7
%20 = sub i64 %19, 1
%21 = getelementptr inbounds i8, i8* %18, i64 %20
%22 = load i8, i8* %21
%23 = sext i8 %22 to i32
%24 = icmp eq i32 %23, 13
br i1 %24, label %25, label %30
25: ; preds = %15
%26 = load i8*, i8** %6
%27 = load i64, i64* %7
%28 = sub i64 %27, 1
%29 = getelementptr inbounds i8, i8* %26, i64 %28
store i8 0, i8* %29
br label %30
30: ; preds = %25, %15
%31 = load i8*, i8** %6
store i8* %31, i8** %3
br label %32
32: ; preds = %30, %13
%33 = load i8*, i8** %3
ret i8* %33
}

LLVM IR (-O2):
@.str = [2 x i8] c"\0A\00"
define i8* @util_str_lines(i8* %0, i8** %1) {
%3 = tail call i8* @strtok_r(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %1)
%4 = icmp eq i8* %3, null
br i1 %4, label %12, label %5
5: ; preds = %2
%6 = tail call i64 @strlen(i8* %3)
%7 = add i64 %6, -1
%8 = getelementptr inbounds i8, i8* %3, i64 %7
%9 = load i8, i8* %8
%10 = icmp eq i8 %9, 13
br i1 %10, label %11, label %12
11: ; preds = %5
store i8 0, i8* %8
br label %12
12: ; preds = %5, %11, %2
ret i8* %3
}