#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 mg_connection {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ assert(int);
unsigned long long get_content_length(struct mg_connection *);
int /*<<< orphan*/ memmove(char *, char *, int);
void shift_to_next(struct mg_connection *conn, char *buf, int req_len,
                   int *nread) {
  long long unsigned int cl;
  int over_len, body_len;
  cl = get_content_length(conn);
  over_len = *nread - req_len;
  assert(over_len >= 0);
  if (cl == ((long long unsigned int)~0)) {
    body_len = 0;
  } else if (cl < (long long unsigned int)over_len) {
    body_len = (int)cl;
  } else {
    body_len = over_len;
  }
  *nread -= req_len + body_len;
  (void)memmove(buf, buf + req_len + body_len, *nread);
}


x86 assembly (clang, -O0):
<shift_to_next>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <get_content_length@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
sub    -0x14(%rbp),%eax
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
setge  %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
cmpq   $0xffffffffffffffff,-0x28(%rbp)
jne    1188 <shift_to_next+0x58>
movl   $0x0,-0x30(%rbp)
jmp    11b0 <shift_to_next+0x80>
mov    -0x28(%rbp),%rax
movslq -0x2c(%rbp),%rcx
cmp    %rcx,%rax
jae    11a5 <shift_to_next+0x75>
mov    -0x28(%rbp),%rax
mov    %eax,-0x30(%rbp)
jmp    11ab <shift_to_next+0x7b>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x30(%rbp)
jmp    11b0 <shift_to_next+0x80>
mov    -0x14(%rbp),%edx
add    -0x30(%rbp),%edx
mov    -0x20(%rbp),%rax
mov    (%rax),%ecx
sub    %edx,%ecx
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x10(%rbp),%rsi
movslq -0x14(%rbp),%rax
add    %rax,%rsi
movslq -0x30(%rbp),%rax
add    %rax,%rsi
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
call   1040 <memmove@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<shift_to_next>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r12
mov    %edx,%r15d
mov    %rsi,%r14
call   1050 <get_content_length@plt>
mov    %rax,%r13
movslq (%r12),%rbp
movslq %r15d,%rbx
sub    %rbx,%rbp
mov    %ebp,%edi
not    %edi
shr    $0x1f,%edi
call   1030 <assert@plt>
cmp    %rbp,%r13
mov    %r13d,%eax
cmovae %ebp,%eax
xor    %ecx,%ecx
cmp    $0xffffffffffffffff,%r13
cmovne %eax,%ecx
mov    (%r12),%edx
sub    %ebx,%edx
sub    %ecx,%edx
mov    %edx,(%r12)
add    %r14,%rbx
movslq %ecx,%rsi
add    %rbx,%rsi
mov    %r14,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <memmove@plt>

LLVM IR (-O0):
%struct.mg_connection = type { i32 }
define void @shift_to_next(%struct.mg_connection* %0, i8* %1, i32 %2, i32* %3) {
%5 = alloca %struct.mg_connection*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i64
%10 = alloca i32
%11 = alloca i32
store %struct.mg_connection* %0, %struct.mg_connection** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32* %3, i32** %8
%12 = load %struct.mg_connection*, %struct.mg_connection** %5
%13 = call i64 @get_content_length(%struct.mg_connection* %12)
store i64 %13, i64* %9
%14 = load i32*, i32** %8
%15 = load i32, i32* %14
%16 = load i32, i32* %7
%17 = sub nsw i32 %15, %16
store i32 %17, i32* %10
%18 = load i32, i32* %10
%19 = icmp sge i32 %18, 0
%20 = zext i1 %19 to i32
%21 = call i32 @assert(i32 %20)
%22 = load i64, i64* %9
%23 = icmp eq i64 %22, -1
br i1 %23, label %24, label %25
24: ; preds = %4
store i32 0, i32* %11
br label %36
25: ; preds = %4
%26 = load i64, i64* %9
%27 = load i32, i32* %10
%28 = sext i32 %27 to i64
%29 = icmp ult i64 %26, %28
br i1 %29, label %30, label %33
30: ; preds = %25
%31 = load i64, i64* %9
%32 = trunc i64 %31 to i32
store i32 %32, i32* %11
br label %35
33: ; preds = %25
%34 = load i32, i32* %10
store i32 %34, i32* %11
br label %35
35: ; preds = %33, %30
br label %36
36: ; preds = %35, %24
%37 = load i32, i32* %7
%38 = load i32, i32* %11
%39 = add nsw i32 %37, %38
%40 = load i32*, i32** %8
%41 = load i32, i32* %40
%42 = sub nsw i32 %41, %39
store i32 %42, i32* %40
%43 = load i8*, i8** %6
%44 = load i8*, i8** %6
%45 = load i32, i32* %7
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i8, i8* %44, i64 %46
%48 = load i32, i32* %11
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i8, i8* %47, i64 %49
%51 = load i32*, i32** %8
%52 = load i32, i32* %51
%53 = call i32 @memmove(i8* %43, i8* %50, i32 %52)
ret void
}

LLVM IR (-O2):
%struct.mg_connection = type { i32 }
define void @shift_to_next(%struct.mg_connection* %0, i8* %1, i32 %2, i32* nocapture %3) {
%5 = tail call i64 @get_content_length(%struct.mg_connection* %0)
%6 = load i32, i32* %3
%7 = sub nsw i32 %6, %2
%8 = xor i32 %7, -1
%9 = lshr i32 %8, 31
%10 = tail call i32 @assert(i32 %9)
%11 = icmp eq i64 %5, -1
%12 = sext i32 %7 to i64
%13 = icmp ult i64 %5, %12
%14 = trunc i64 %5 to i32
%15 = select i1 %13, i32 %14, i32 %7
%16 = select i1 %11, i32 0, i32 %15
%17 = load i32, i32* %3
%18 = add i32 %16, %2
%19 = sub i32 %17, %18
store i32 %19, i32* %3
%20 = sext i32 %2 to i64
%21 = getelementptr inbounds i8, i8* %1, i64 %20
%22 = sext i32 %16 to i64
%23 = getelementptr inbounds i8, i8* %21, i64 %22
%24 = tail call i32 @memmove(i8* %1, i8* %23, i32 %19)
ret void
}