#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 */
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
char *ptReplaceStr(char *, char *, char *);
char *strchr(char *, char);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int cut_rest(char **data, char **rest) {
  char *p = strchr(*rest, '\n');
  if (p) {
    *p = '\0';
    *data = (char *)malloc(strlen(*rest) + 2);
    if (*data == NULL) {
      return -1;
    }
    strcpy(*data, *rest);
    *(*data + strlen(*rest)) = '\n';
    *(*data + strlen(*rest) + 1) = '\0';
    *p = '\n';
    char *t = ptReplaceStr(*rest, *data, "");
    if (!t) {
      return -1;
    }
    free(*rest);
    *rest = t;
  } else {
    *data = NULL;
  }
  return 0;
}


x86 assembly (clang, -O0):
<cut_rest>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    $0xa,%esi
call   1060 <strchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1285 <cut_rest+0x125>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1050 <strlen@plt>
mov    %eax,%edi
add    $0x2,%edi
call   1070 <malloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11d1 <cut_rest+0x71>
movl   $0xffffffff,-0x4(%rbp)
jmp    1297 <cut_rest+0x137>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
call   1040 <strcpy@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
movslq %ecx,%rcx
movb   $0xa,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
movb   $0x0,0x1(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
movb   $0xa,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xdb7(%rip),%rdx
call   1080 <ptReplaceStr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1269 <cut_rest+0x109>
movl   $0xffffffff,-0x4(%rbp)
jmp    1297 <cut_rest+0x137>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
jmp    1290 <cut_rest+0x130>
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cut_rest>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%rbx
mov    %rdi,%r14
mov    (%rsi),%rdi
mov    $0xa,%esi
call   1060 <strchr@plt>
test   %rax,%rax
je     11ff <cut_rest+0x9f>
mov    %rax,%rbp
movb   $0x0,(%rax)
mov    (%rbx),%rdi
call   1050 <strlen@plt>
lea    0x2(%rax),%edi
call   1070 <malloc@plt>
mov    %rax,(%r14)
mov    $0xffffffff,%r15d
test   %rax,%rax
je     1209 <cut_rest+0xa9>
mov    (%rbx),%rsi
mov    %rax,%rdi
call   1040 <strcpy@plt>
mov    (%r14),%r12
mov    (%rbx),%rdi
call   1050 <strlen@plt>
cltq
movb   $0xa,(%r12,%rax,1)
mov    (%r14),%r12
mov    (%rbx),%rdi
call   1050 <strlen@plt>
cltq
movb   $0x0,0x1(%r12,%rax,1)
movb   $0xa,0x0(%rbp)
mov    (%rbx),%rdi
mov    (%r14),%rsi
lea    0xe1b(%rip),%rdx
call   1080 <ptReplaceStr@plt>
test   %rax,%rax
je     1209 <cut_rest+0xa9>
mov    %rax,%rbp
mov    (%rbx),%rdi
call   1030 <free@plt>
mov    %rbp,(%rbx)
jmp    1206 <cut_rest+0xa6>
movq   $0x0,(%r14)
xor    %r15d,%r15d
mov    %r15d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [1 x i8] zeroinitializer
define i32 @cut_rest(i8** %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
store i8** %0, i8*** %4
store i8** %1, i8*** %5
%8 = load i8**, i8*** %5
%9 = load i8*, i8** %8
%10 = call i8* @strchr(i8* %9, i8 signext 10)
store i8* %10, i8** %6
%11 = load i8*, i8** %6
%12 = icmp ne i8* %11, null
br i1 %12, label %13, label %62
13: ; preds = %2
%14 = load i8*, i8** %6
store i8 0, i8* %14
%15 = load i8**, i8*** %5
%16 = load i8*, i8** %15
%17 = call i32 @strlen(i8* %16)
%18 = add nsw i32 %17, 2
%19 = call i64 @malloc(i32 %18)
%20 = inttoptr i64 %19 to i8*
%21 = load i8**, i8*** %4
store i8* %20, i8** %21
%22 = load i8**, i8*** %4
%23 = load i8*, i8** %22
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %26
25: ; preds = %13
store i32 -1, i32* %3
br label %65
26: ; preds = %13
%27 = load i8**, i8*** %4
%28 = load i8*, i8** %27
%29 = load i8**, i8*** %5
%30 = load i8*, i8** %29
%31 = call i32 @strcpy(i8* %28, i8* %30)
%32 = load i8**, i8*** %4
%33 = load i8*, i8** %32
%34 = load i8**, i8*** %5
%35 = load i8*, i8** %34
%36 = call i32 @strlen(i8* %35)
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %33, i64 %37
store i8 10, i8* %38
%39 = load i8**, i8*** %4
%40 = load i8*, i8** %39
%41 = load i8**, i8*** %5
%42 = load i8*, i8** %41
%43 = call i32 @strlen(i8* %42)
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %40, i64 %44
%46 = getelementptr inbounds i8, i8* %45, i64 1
store i8 0, i8* %46
%47 = load i8*, i8** %6
store i8 10, i8* %47
%48 = load i8**, i8*** %5
%49 = load i8*, i8** %48
%50 = load i8**, i8*** %4
%51 = load i8*, i8** %50
%52 = call i8* @ptReplaceStr(i8* %49, i8* %51, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
store i8* %52, i8** %7
%53 = load i8*, i8** %7
%54 = icmp ne i8* %53, null
br i1 %54, label %56, label %55
55: ; preds = %26
store i32 -1, i32* %3
br label %65
56: ; preds = %26
%57 = load i8**, i8*** %5
%58 = load i8*, i8** %57
%59 = call i32 @free(i8* %58)
%60 = load i8*, i8** %7
%61 = load i8**, i8*** %5
store i8* %60, i8** %61
br label %64
62: ; preds = %2
%63 = load i8**, i8*** %4
store i8* null, i8** %63
br label %64
64: ; preds = %62, %56
store i32 0, i32* %3
br label %65
65: ; preds = %64, %55, %25
%66 = load i32, i32* %3
ret i32 %66
}

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
define i32 @cut_rest(i8** nocapture %0, i8** nocapture %1) {
%3 = load i8*, i8** %1
%4 = tail call i8* @strchr(i8* dereferenceable(1) %3, i8 signext 10)
%5 = icmp eq i8* %4, null
br i1 %5, label %34, label %6
6: ; preds = %2
store i8 0, i8* %4
%7 = load i8*, i8** %1
%8 = tail call i32 @strlen(i8* %7)
%9 = add nsw i32 %8, 2
%10 = tail call i64 @malloc(i32 %9)
%11 = inttoptr i64 %10 to i8*
store i8* %11, i8** %0
%12 = icmp eq i64 %10, 0
br i1 %12, label %35, label %13
13: ; preds = %6
%14 = load i8*, i8** %1
%15 = tail call i32 @strcpy(i8* %11, i8* %14)
%16 = load i8*, i8** %0
%17 = load i8*, i8** %1
%18 = tail call i32 @strlen(i8* %17)
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %16, i64 %19
store i8 10, i8* %20
%21 = load i8*, i8** %0
%22 = load i8*, i8** %1
%23 = tail call i32 @strlen(i8* %22)
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %21, i64 %24
%26 = getelementptr inbounds i8, i8* %25, i64 1
store i8 0, i8* %26
store i8 10, i8* %4
%27 = load i8*, i8** %1
%28 = load i8*, i8** %0
%29 = tail call i8* @ptReplaceStr(i8* %27, i8* %28, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
%30 = icmp eq i8* %29, null
br i1 %30, label %35, label %31
31: ; preds = %13
%32 = load i8*, i8** %1
%33 = tail call i32 @free(i8* %32)
store i8* %29, i8** %1
br label %35
34: ; preds = %2
store i8* null, i8** %0
br label %35
35: ; preds = %34, %31, %13, %6
%36 = phi i32 [ -1, %6 ], [ -1, %13 ], [ 0, %31 ], [ 0, %34 ]
ret i32 %36
}