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

/* Variables and functions */
unsigned int PICA_msggetsize(unsigned char *, unsigned int,
                             struct PICA_msginfo const *, unsigned int);
int /*<<< orphan*/ PICA_processmsg(unsigned char *, unsigned int, void *,
                                   struct PICA_msginfo const *, unsigned int);
int /*<<< orphan*/ memmove(unsigned char *, unsigned char *, unsigned int);
unsigned int PICA_processdatastream(unsigned char *buf, unsigned int *read_pos,
                                    void *arg, const struct PICA_msginfo *msgs,
                                    unsigned int nmsg) {
  while (*read_pos) {
    unsigned int uret;
    uret = PICA_msggetsize(buf, *read_pos, msgs, nmsg);
    if (!uret)
      return 0;
    if (uret > *read_pos)
      break;
    if (!PICA_processmsg(buf, uret, arg, msgs, nmsg))
      return 0;
    *read_pos -= uret;
    memmove(buf, buf + uret, *read_pos);
  }
  return 1;
}


x86 assembly (clang, -O0):
<PICA_processdatastream>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8d,-0x2c(%rbp)
mov    -0x18(%rbp),%rax
cmpl   $0x0,(%rax)
je     11f4 <PICA_processdatastream+0xc4>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
mov    -0x28(%rbp),%rdx
mov    -0x2c(%rbp),%ecx
call   1040 <PICA_msggetsize@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jne    1188 <PICA_processdatastream+0x58>
movl   $0x0,-0x4(%rbp)
jmp    11fb <PICA_processdatastream+0xcb>
mov    -0x30(%rbp),%eax
mov    -0x18(%rbp),%rcx
cmp    (%rcx),%eax
jbe    119c <PICA_processdatastream+0x6c>
jmp    11f4 <PICA_processdatastream+0xc4>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%esi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    -0x2c(%rbp),%r8d
call   1030 <PICA_processmsg@plt>
cmp    $0x0,%eax
jne    11c9 <PICA_processdatastream+0x99>
movl   $0x0,-0x4(%rbp)
jmp    11fb <PICA_processdatastream+0xcb>
mov    -0x30(%rbp),%edx
mov    -0x18(%rbp),%rax
mov    (%rax),%ecx
sub    %edx,%ecx
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x30(%rbp),%eax
add    %rax,%rsi
mov    -0x18(%rbp),%rax
mov    (%rax),%edx
call   1050 <memmove@plt>
jmp    114c <PICA_processdatastream+0x1c>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<PICA_processdatastream>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,(%rsp)
mov    %rsi,%rbp
mov    (%rsi),%esi
test   %esi,%esi
je     11b1 <PICA_processdatastream+0x81>
mov    %r8d,%r15d
mov    %rcx,%r12
mov    %rdi,%r13
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r13,%rdi
mov    %r12,%rdx
mov    %r15d,%ecx
call   1040 <PICA_msggetsize@plt>
test   %eax,%eax
je     11b6 <PICA_processdatastream+0x86>
mov    %eax,%r14d
cmp    0x0(%rbp),%eax
ja     11b1 <PICA_processdatastream+0x81>
mov    %r13,%rdi
mov    %r14d,%esi
mov    (%rsp),%rdx
mov    %r12,%rcx
mov    %r15d,%r8d
call   1030 <PICA_processmsg@plt>
test   %eax,%eax
je     11b6 <PICA_processdatastream+0x86>
mov    0x0(%rbp),%edx
sub    %r14d,%edx
mov    %edx,0x0(%rbp)
mov    %r14d,%esi
add    %r13,%rsi
mov    %r13,%rdi
call   1050 <memmove@plt>
mov    0x0(%rbp),%esi
test   %esi,%esi
jne    1160 <PICA_processdatastream+0x30>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.PICA_msginfo = type { i32 }
define i32 @PICA_processdatastream(i8* %0, i32* nocapture %1, i8* %2, %struct.PICA_msginfo* %3, i32 %4) {
%6 = load i32, i32* %1
%7 = icmp eq i32 %6, 0
br i1 %7, label %26, label %8
8: ; preds = %5, %18
%9 = phi i32 [ %24, %18 ], [ %6, %5 ]
%10 = tail call i32 @PICA_msggetsize(i8* %0, i32 %9, %struct.PICA_msginfo* %3, i32 %4)
%11 = icmp eq i32 %10, 0
br i1 %11, label %26, label %12
12: ; preds = %8
%13 = load i32, i32* %1
%14 = icmp ugt i32 %10, %13
br i1 %14, label %26, label %15
15: ; preds = %12
%16 = tail call i32 @PICA_processmsg(i8* %0, i32 %10, i8* %2, %struct.PICA_msginfo* %3, i32 %4)
%17 = icmp eq i32 %16, 0
br i1 %17, label %26, label %18
18: ; preds = %15
%19 = load i32, i32* %1
%20 = sub i32 %19, %10
store i32 %20, i32* %1
%21 = zext i32 %10 to i64
%22 = getelementptr inbounds i8, i8* %0, i64 %21
%23 = tail call i32 @memmove(i8* %0, i8* %22, i32 %20)
%24 = load i32, i32* %1
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %8
26: ; preds = %18, %12, %8, %15, %5
%27 = phi i32 [ 1, %5 ], [ 0, %15 ], [ 0, %8 ], [ 1, %12 ], [ 1, %18 ]
ret i32 %27
}