#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*/ fail();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ headers_consume(int, char *);
char *headers_explain_state(int /*<<< orphan*/);
int /*<<< orphan*/ headers_get_state();
scalar_t__ headers_is_error(int /*<<< orphan*/);
char *malloc(int);
int /*<<< orphan*/ memcpy(char *, char *, int);
int /*<<< orphan*/ stderr;
void test_consume(int size, int len, char *str) {
  int parts = len / size;
  int rem = len % size;
  while (parts--) {
    char *part = malloc(size + 1);
    part[size] = 0;
    memcpy(part, str, size);
    str += size;
    headers_consume(size, part);
  }
  if (rem) {
    char *part = malloc(rem + 1);
    part[rem] = 0;
    memcpy(part, str, rem);
    if (headers_is_error(headers_consume(rem, part))) {
      fprintf(stderr,
              "  "
              "Error: %s\n",
              headers_explain_state(headers_get_state()));
      fail();
    }
  }
}


x86 assembly (clang, -O0):
<test_consume>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    -0x8(%rbp),%eax
cltd
idivl  -0x4(%rbp)
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%eax
cltd
idivl  -0x4(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x14(%rbp)
cmp    $0x0,%eax
je     1207 <test_consume+0x87>
mov    -0x4(%rbp),%edi
add    $0x1,%edi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movslq -0x4(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x4(%rbp),%edx
call   1070 <memcpy@plt>
mov    -0x4(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x20(%rbp),%rsi
call   1040 <headers_consume@plt>
jmp    11a6 <test_consume+0x26>
cmpl   $0x0,-0x18(%rbp)
je     1291 <test_consume+0x111>
mov    -0x18(%rbp),%edi
add    $0x1,%edi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1070 <memcpy@plt>
mov    -0x18(%rbp),%edi
mov    -0x28(%rbp),%rsi
call   1040 <headers_consume@plt>
mov    %eax,%edi
call   10a0 <headers_is_error@plt>
cmp    $0x0,%rax
je     128c <test_consume+0x10c>
mov    0x2d80(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    $0x0,%al
call   1050 <headers_get_state@plt>
mov    %eax,%edi
call   1090 <headers_explain_state@plt>
mov    -0x2c(%rbp),%edi
mov    %rax,%rdx
lea    0xd80(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x0,%al
call   1030 <fail@plt>
jmp    1291 <test_consume+0x111>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_consume>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r13
mov    %esi,%eax
cltd
idiv   %edi
mov    %rdx,(%rsp)
test   %eax,%eax
je     11e1 <test_consume+0x61>
mov    %edi,%r15d
mov    %eax,%ebp
lea    0x1(%r15),%r12d
movslq %edi,%r14
nopw   0x0(%rax,%rax,1)
mov    %r12d,%edi
call   1080 <malloc@plt>
mov    %rax,%rbx
movb   $0x0,(%rax,%r14,1)
mov    %rax,%rdi
mov    %r13,%rsi
mov    %r15d,%edx
call   1070 <memcpy@plt>
add    %r14,%r13
mov    %r15d,%edi
mov    %rbx,%rsi
call   1040 <headers_consume@plt>
add    $0xffffffff,%ebp
jne    11b0 <test_consume+0x30>
mov    (%rsp),%rbp
test   %ebp,%ebp
je     125b <test_consume+0xdb>
lea    0x1(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,%rbx
movslq %ebp,%rax
movb   $0x0,(%rbx,%rax,1)
mov    %rbx,%rdi
mov    %r13,%rsi
mov    %ebp,%edx
call   1070 <memcpy@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1040 <headers_consume@plt>
mov    %eax,%edi
call   10a0 <headers_is_error@plt>
test   %rax,%rax
je     125b <test_consume+0xdb>
mov    0x2dbb(%rip),%rax
mov    (%rax),%ebx
xor    %eax,%eax
call   1050 <headers_get_state@plt>
mov    %eax,%edi
call   1090 <headers_explain_state@plt>
lea    0xdc4(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1060 <fprintf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <fail@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [13 x i8] c" Error: %s\0A\00"
define void @test_consume(i32 %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i8*
store i32 %0, i32* %4
store i32 %1, i32* %5
store i8* %2, i8** %6
%11 = load i32, i32* %5
%12 = load i32, i32* %4
%13 = sdiv i32 %11, %12
store i32 %13, i32* %7
%14 = load i32, i32* %5
%15 = load i32, i32* %4
%16 = srem i32 %14, %15
store i32 %16, i32* %8
br label %17
17: ; preds = %21, %3
%18 = load i32, i32* %7
%19 = add nsw i32 %18, -1
store i32 %19, i32* %7
%20 = icmp ne i32 %18, 0
br i1 %20, label %21, label %40
21: ; preds = %17
%22 = load i32, i32* %4
%23 = add nsw i32 %22, 1
%24 = call i8* @malloc(i32 %23)
store i8* %24, i8** %9
%25 = load i8*, i8** %9
%26 = load i32, i32* %4
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %25, i64 %27
store i8 0, i8* %28
%29 = load i8*, i8** %9
%30 = load i8*, i8** %6
%31 = load i32, i32* %4
%32 = call i32 @memcpy(i8* %29, i8* %30, i32 %31)
%33 = load i32, i32* %4
%34 = load i8*, i8** %6
%35 = sext i32 %33 to i64
%36 = getelementptr inbounds i8, i8* %34, i64 %35
store i8* %36, i8** %6
%37 = load i32, i32* %4
%38 = load i8*, i8** %9
%39 = call i32 @headers_consume(i32 %37, i8* %38)
br label %17
40: ; preds = %17
%41 = load i32, i32* %8
%42 = icmp ne i32 %41, 0
br i1 %42, label %43, label %67
43: ; preds = %40
%44 = load i32, i32* %8
%45 = add nsw i32 %44, 1
%46 = call i8* @malloc(i32 %45)
store i8* %46, i8** %10
%47 = load i8*, i8** %10
%48 = load i32, i32* %8
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i8, i8* %47, i64 %49
store i8 0, i8* %50
%51 = load i8*, i8** %10
%52 = load i8*, i8** %6
%53 = load i32, i32* %8
%54 = call i32 @memcpy(i8* %51, i8* %52, i32 %53)
%55 = load i32, i32* %8
%56 = load i8*, i8** %10
%57 = call i32 @headers_consume(i32 %55, i8* %56)
%58 = call i64 @headers_is_error(i32 %57)
%59 = icmp ne i64 %58, 0
br i1 %59, label %60, label %66
60: ; preds = %43
%61 = load i32, i32* @stderr
%62 = call i32 (...) @headers_get_state()
%63 = call i8* @headers_explain_state(i32 %62)
%64 = call i32 @fprintf(i32 %61, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %63)
%65 = call i32 (...) @fail()
br label %66
66: ; preds = %60, %43
br label %67
67: ; preds = %66, %40
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [13 x i8] c" Error: %s\0A\00"
define void @test_consume(i32 %0, i32 %1, i8* %2) {
%4 = sdiv i32 %1, %0
%5 = srem i32 %1, %0
%6 = icmp eq i32 %4, 0
br i1 %6, label %20, label %7
7: ; preds = %3
%8 = add nsw i32 %0, 1
%9 = sext i32 %0 to i64
br label %10
10: ; preds = %7, %10
%11 = phi i8* [ %2, %7 ], [ %17, %10 ]
%12 = phi i32 [ %4, %7 ], [ %13, %10 ]
%13 = add nsw i32 %12, -1
%14 = tail call i8* @malloc(i32 %8)
%15 = getelementptr inbounds i8, i8* %14, i64 %9
store i8 0, i8* %15
%16 = tail call i32 @memcpy(i8* %14, i8* %11, i32 %0)
%17 = getelementptr inbounds i8, i8* %11, i64 %9
%18 = tail call i32 @headers_consume(i32 %0, i8* %14)
%19 = icmp eq i32 %13, 0
br i1 %19, label %20, label %10
20: ; preds = %10, %3
%21 = phi i8* [ %2, %3 ], [ %17, %10 ]
%22 = icmp eq i32 %5, 0
br i1 %22, label %38, label %23
23: ; preds = %20
%24 = add nsw i32 %5, 1
%25 = tail call i8* @malloc(i32 %24)
%26 = sext i32 %5 to i64
%27 = getelementptr inbounds i8, i8* %25, i64 %26
store i8 0, i8* %27
%28 = tail call i32 @memcpy(i8* %25, i8* %21, i32 %5)
%29 = tail call i32 @headers_consume(i32 %5, i8* %25)
%30 = tail call i64 @headers_is_error(i32 %29)
%31 = icmp eq i64 %30, 0
br i1 %31, label %38, label %32
32: ; preds = %23
%33 = load i32, i32* @stderr
%34 = tail call i32 (...) @headers_get_state()
%35 = tail call i8* @headers_explain_state(i32 %34)
%36 = tail call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %35)
%37 = tail call i32 (...) @fail()
br label %38
38: ; preds = %23, %32, %20
ret void
}