#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 */
struct editor {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ editor_addline(struct editor *, int /*<<< orphan*/,
                                  int /*<<< orphan*/, char *,
                                  int /*<<< orphan*/);
char *strchr(char *, char);
int strlen(char *);
int editor_addchunk(struct editor *e, int line, int pos, char *buf,
                    unsigned f) {
  char *p, *s = buf;
  while ((p = strchr(s, '\n')) != NULL) {
    *p = 0;
    editor_addline(e, line++, 0, s, 0);
    *p = '\n';
    s = p + 1;
  }
  editor_addline(e, line++, 0, s, 0);
  return strlen(buf);
}


x86 assembly (clang, -O0):
<editor_addchunk>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    $0xa,%esi
call   1050 <strchr@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     11ab <editor_addchunk+0x7b>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    %esi,%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0x30(%rbp),%rcx
xor    %r8d,%r8d
mov    %r8d,%edx
call   1030 <editor_addline@plt>
mov    -0x28(%rbp),%rax
movb   $0xa,(%rax)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    1152 <editor_addchunk+0x22>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    %esi,%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0x30(%rbp),%rcx
xor    %r8d,%r8d
mov    %r8d,%edx
call   1030 <editor_addline@plt>
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<editor_addchunk>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %esi,%ebp
mov    %rdi,%r15
mov    %rcx,%rdi
mov    $0xa,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     11a1 <editor_addchunk+0x71>
mov    %rax,%r12
mov    %r14,%rbx
nopl   0x0(%rax,%rax,1)
movb   $0x0,(%r12)
lea    0x1(%rbp),%r13d
mov    %r15,%rdi
mov    %ebp,%esi
xor    %edx,%edx
mov    %rbx,%rcx
xor    %r8d,%r8d
call   1030 <editor_addline@plt>
movb   $0xa,(%r12)
mov    %r12,%rbx
add    $0x1,%rbx
mov    %rbx,%rdi
mov    $0xa,%esi
call   1050 <strchr@plt>
mov    %rax,%r12
mov    %r13d,%ebp
test   %rax,%rax
jne    1160 <editor_addchunk+0x30>
jmp    11a7 <editor_addchunk+0x77>
mov    %ebp,%r13d
mov    %r14,%rbx
mov    %r15,%rdi
mov    %r13d,%esi
xor    %edx,%edx
mov    %rbx,%rcx
xor    %r8d,%r8d
call   1030 <editor_addline@plt>
mov    %r14,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <strlen@plt>

LLVM IR (-O0):
%struct.editor = type { i32 }
define i32 @editor_addchunk(%struct.editor* %0, i32 %1, i32 %2, i8* %3, i32 %4) {
%6 = alloca %struct.editor*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i8*
store %struct.editor* %0, %struct.editor** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i8* %3, i8** %9
store i32 %4, i32* %10
%13 = load i8*, i8** %9
store i8* %13, i8** %12
br label %14
14: ; preds = %18, %5
%15 = load i8*, i8** %12
%16 = call i8* @strchr(i8* %15, i8 signext 10)
store i8* %16, i8** %11
%17 = icmp ne i8* %16, null
br i1 %17, label %18, label %28
18: ; preds = %14
%19 = load i8*, i8** %11
store i8 0, i8* %19
%20 = load %struct.editor*, %struct.editor** %6
%21 = load i32, i32* %7
%22 = add nsw i32 %21, 1
store i32 %22, i32* %7
%23 = load i8*, i8** %12
%24 = call i32 @editor_addline(%struct.editor* %20, i32 %21, i32 0, i8* %23, i32 0)
%25 = load i8*, i8** %11
store i8 10, i8* %25
%26 = load i8*, i8** %11
%27 = getelementptr inbounds i8, i8* %26, i64 1
store i8* %27, i8** %12
br label %14
28: ; preds = %14
%29 = load %struct.editor*, %struct.editor** %6
%30 = load i32, i32* %7
%31 = add nsw i32 %30, 1
store i32 %31, i32* %7
%32 = load i8*, i8** %12
%33 = call i32 @editor_addline(%struct.editor* %29, i32 %30, i32 0, i8* %32, i32 0)
%34 = load i8*, i8** %9
%35 = call i32 @strlen(i8* %34)
ret i32 %35
}

LLVM IR (-O2):
%struct.editor = type { i32 }
define i32 @editor_addchunk(%struct.editor* %0, i32 %1, i32 %2, i8* %3, i32 %4) {
%6 = tail call i8* @strchr(i8* dereferenceable(1) %3, i8 signext 10)
%7 = icmp eq i8* %6, null
br i1 %7, label %17, label %8
8: ; preds = %5, %8
%9 = phi i8* [ %15, %8 ], [ %6, %5 ]
%10 = phi i8* [ %14, %8 ], [ %3, %5 ]
%11 = phi i32 [ %12, %8 ], [ %1, %5 ]
store i8 0, i8* %9
%12 = add nsw i32 %11, 1
%13 = tail call i32 @editor_addline(%struct.editor* %0, i32 %11, i32 0, i8* %10, i32 0)
store i8 10, i8* %9
%14 = getelementptr inbounds i8, i8* %9, i64 1
%15 = tail call i8* @strchr(i8* dereferenceable(1) %14, i8 signext 10)
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %8
17: ; preds = %8, %5
%18 = phi i32 [ %1, %5 ], [ %12, %8 ]
%19 = phi i8* [ %3, %5 ], [ %14, %8 ]
%20 = tail call i32 @editor_addline(%struct.editor* %0, i32 %18, i32 0, i8* %19, i32 0)
%21 = tail call i32 @strlen(i8* %3)
ret i32 %21
}