#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 */
char *memchr(char *, int, int);
char *check(char *buf, int max) {
  char *ptr;
  if (buf[0] == 1 && (ptr = memchr(buf + 1, 0xD, max - 1)) != NULL) {
    *ptr = '\0';
    buf[max - 1] = '\0';
    return buf + 1;
  }
  return NULL;
}


x86 assembly (clang, -O0):
<check>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x1,%eax
jne    117e <check+0x6e>
mov    -0x10(%rbp),%rdi
add    $0x1,%rdi
mov    -0x14(%rbp),%edx
sub    $0x1,%edx
mov    $0xd,%esi
call   1030 <memchr@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     117e <check+0x6e>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    1186 <check+0x76>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check>:
push   %r15
push   %r14
push   %rbx
cmpb   $0x1,(%rdi)
jne    1147 <check+0x37>
mov    %rdi,%rbx
lea    0x1(%rdi),%r14
movslq %esi,%r15
add    $0xffffffffffffffff,%r15
mov    %r14,%rdi
mov    $0xd,%esi
mov    %r15d,%edx
call   1030 <memchr@plt>
test   %rax,%rax
je     1147 <check+0x37>
movb   $0x0,(%rax)
movb   $0x0,(%rbx,%r15,1)
jmp    114a <check+0x3a>
xor    %r14d,%r14d
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i8* @check(i8* %0, i32 %1) {
%3 = load i8, i8* %0
%4 = icmp eq i8 %3, 1
br i1 %4, label %5, label %13
5: ; preds = %2
%6 = getelementptr inbounds i8, i8* %0, i64 1
%7 = add nsw i32 %1, -1
%8 = tail call i8* @memchr(i8* %6, i32 13, i32 %7)
%9 = icmp eq i8* %8, null
br i1 %9, label %13, label %10
10: ; preds = %5
store i8 0, i8* %8
%11 = sext i32 %7 to i64
%12 = getelementptr inbounds i8, i8* %0, i64 %11
store i8 0, i8* %12
br label %13
13: ; preds = %2, %5, %10
%14 = phi i8* [ %6, %10 ], [ null, %5 ], [ null, %2 ]
ret i8* %14
}