#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 */
typedef int ssize_t;

/* Variables and functions */
int find_delim(char *, size_t);
int /*<<< orphan*/ memmove(char *, char *, size_t);
char *strndup(char *, int);
char *pop_buffer(char *buf, size_t *fill, size_t len) {
  char *r;
  ssize_t z;
  z = find_delim(buf, *fill);
  if (z == -1)
    return NULL;
  r = strndup(buf, z);
  if (r == NULL)
    return NULL;
  memmove(buf, buf + z + 1, *fill - z - 1);
  *fill -= z + 1;
  return r;
}


x86 assembly (clang, -O0):
<pop_buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
call   1040 <find_delim@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    116e <pop_buffer+0x3e>
movq   $0x0,-0x8(%rbp)
jmp    11de <pop_buffer+0xae>
mov    -0x10(%rbp),%rdi
mov    -0x2c(%rbp),%esi
call   1030 <strndup@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1196 <pop_buffer+0x66>
movq   $0x0,-0x8(%rbp)
jmp    11de <pop_buffer+0xae>
mov    -0x10(%rbp),%rdi
mov    -0x10(%rbp),%rsi
movslq -0x2c(%rbp),%rax
add    %rax,%rsi
add    $0x1,%rsi
mov    -0x18(%rbp),%rax
mov    (%rax),%rdx
movslq -0x2c(%rbp),%rax
sub    %rax,%rdx
sub    $0x1,%rdx
call   1050 <memmove@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
movslq %eax,%rdx
mov    -0x18(%rbp),%rax
mov    (%rax),%rcx
sub    %rdx,%rcx
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pop_buffer>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    (%rsi),%rsi
call   1040 <find_delim@plt>
cmp    $0xffffffff,%eax
je     1182 <pop_buffer+0x52>
mov    %eax,%ebp
mov    %rbx,%rdi
mov    %eax,%esi
call   1030 <strndup@plt>
test   %rax,%rax
je     1182 <pop_buffer+0x52>
mov    %rax,%r15
movslq %ebp,%rdx
lea    (%rbx,%rdx,1),%rsi
add    $0x1,%rsi
not    %rdx
add    (%r14),%rdx
mov    %rbx,%rdi
call   1050 <memmove@plt>
add    $0x1,%ebp
movslq %ebp,%rax
sub    %rax,(%r14)
jmp    1185 <pop_buffer+0x55>
xor    %r15d,%r15d
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @pop_buffer(i8* %0, i64* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i32
store i8* %0, i8** %5
store i64* %1, i64** %6
store i64 %2, i64* %7
%10 = load i8*, i8** %5
%11 = load i64*, i64** %6
%12 = load i64, i64* %11
%13 = call i32 @find_delim(i8* %10, i64 %12)
store i32 %13, i32* %9
%14 = load i32, i32* %9
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %17
16: ; preds = %3
store i8* null, i8** %4
br label %45
17: ; preds = %3
%18 = load i8*, i8** %5
%19 = load i32, i32* %9
%20 = call i8* @strndup(i8* %18, i32 %19)
store i8* %20, i8** %8
%21 = load i8*, i8** %8
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %24
23: ; preds = %17
store i8* null, i8** %4
br label %45
24: ; preds = %17
%25 = load i8*, i8** %5
%26 = load i8*, i8** %5
%27 = load i32, i32* %9
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %26, i64 %28
%30 = getelementptr inbounds i8, i8* %29, i64 1
%31 = load i64*, i64** %6
%32 = load i64, i64* %31
%33 = load i32, i32* %9
%34 = sext i32 %33 to i64
%35 = sub i64 %32, %34
%36 = sub i64 %35, 1
%37 = call i32 @memmove(i8* %25, i8* %30, i64 %36)
%38 = load i32, i32* %9
%39 = add nsw i32 %38, 1
%40 = sext i32 %39 to i64
%41 = load i64*, i64** %6
%42 = load i64, i64* %41
%43 = sub i64 %42, %40
store i64 %43, i64* %41
%44 = load i8*, i8** %8
store i8* %44, i8** %4
br label %45
45: ; preds = %24, %23, %16
%46 = load i8*, i8** %4
ret i8* %46
}

LLVM IR (-O2):
define noalias i8* @pop_buffer(i8* %0, i64* nocapture %1, i64 %2) {
%4 = load i64, i64* %1
%5 = tail call i32 @find_delim(i8* %0, i64 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %22, label %7
7: ; preds = %3
%8 = tail call i8* @strndup(i8* %0, i32 %5)
%9 = icmp eq i8* %8, null
br i1 %9, label %22, label %10
10: ; preds = %7
%11 = sext i32 %5 to i64
%12 = getelementptr inbounds i8, i8* %0, i64 %11
%13 = getelementptr inbounds i8, i8* %12, i64 1
%14 = load i64, i64* %1
%15 = xor i64 %11, -1
%16 = add i64 %14, %15
%17 = tail call i32 @memmove(i8* %0, i8* %13, i64 %16)
%18 = add nuw nsw i32 %5, 1
%19 = sext i32 %18 to i64
%20 = load i64, i64* %1
%21 = sub i64 %20, %19
store i64 %21, i64* %1
br label %22
22: ; preds = %7, %3, %10
%23 = phi i8* [ %8, %10 ], [ null, %3 ], [ null, %7 ]
ret i8* %23
}