#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*/ sprintf(char *, char *, unsigned char const *,
                           unsigned char const *);
int /*<<< orphan*/ strcpy(char *, char *);
int to_int(unsigned char const *, int);
int pieceoftotal(const unsigned char *data, int data_len, int offset, int min,
                 int max, int *p_err_no, int *p_err_posn, char err_msg[50],
                 const int length_only) {
  (void)max;
  data_len -= offset;
  if (data_len < min || (data_len && data_len < 4)) {
    return 0;
  }
  if (!length_only && data_len) {
    int pieces, total;
    pieces = to_int(data + offset, 2);
    if (pieces == 0) {
      *p_err_no = 3;
      *p_err_posn = offset + 1;
      strcpy(err_msg, "Piece number cannot be zero");
      return 0;
    }
    total = to_int(data + offset + 2, 2);
    if (total == 0) {
      *p_err_no = 3;
      *p_err_posn = offset + 1;
      strcpy(err_msg, "Total number cannot be zero");
      return 0;
    }
    if (pieces > total) {
      *p_err_no = 3;
      *p_err_posn = offset + 1;
      sprintf(err_msg, "Piece number '%.2s' exceeds total '%.2s'",
              data + offset, data + offset + 2);
      return 0;
    }
  }
  return 1;
}


x86 assembly (clang, -O0):
<pieceoftotal>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    0x20(%rbp),%eax
mov    0x18(%rbp),%rax
mov    0x10(%rbp),%rax
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8d,-0x20(%rbp)
mov    %r9,-0x28(%rbp)
mov    -0x18(%rbp),%ecx
mov    -0x14(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jl     1183 <pieceoftotal+0x53>
cmpl   $0x0,-0x14(%rbp)
je     118f <pieceoftotal+0x5f>
cmpl   $0x4,-0x14(%rbp)
jge    118f <pieceoftotal+0x5f>
movl   $0x0,-0x4(%rbp)
jmp    12b3 <pieceoftotal+0x183>
cmpl   $0x0,0x20(%rbp)
jne    12ac <pieceoftotal+0x17c>
cmpl   $0x0,-0x14(%rbp)
je     12ac <pieceoftotal+0x17c>
mov    -0x10(%rbp),%rdi
movslq -0x18(%rbp),%rax
add    %rax,%rdi
mov    $0x2,%esi
call   1040 <to_int@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jne    11f7 <pieceoftotal+0xc7>
mov    -0x28(%rbp),%rax
movl   $0x3,(%rax)
mov    -0x18(%rbp),%ecx
add    $0x1,%ecx
mov    0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    0x18(%rbp),%rdi
lea    0xe1a(%rip),%rsi
call   1030 <strcpy@plt>
movl   $0x0,-0x4(%rbp)
jmp    12b3 <pieceoftotal+0x183>
mov    -0x10(%rbp),%rdi
movslq -0x18(%rbp),%rax
add    %rax,%rdi
add    $0x2,%rdi
mov    $0x2,%esi
call   1040 <to_int@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jne    124f <pieceoftotal+0x11f>
mov    -0x28(%rbp),%rax
movl   $0x3,(%rax)
mov    -0x18(%rbp),%ecx
add    $0x1,%ecx
mov    0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    0x18(%rbp),%rdi
lea    0xdde(%rip),%rsi
call   1030 <strcpy@plt>
movl   $0x0,-0x4(%rbp)
jmp    12b3 <pieceoftotal+0x183>
mov    -0x2c(%rbp),%eax
cmp    -0x30(%rbp),%eax
jle    12a7 <pieceoftotal+0x177>
mov    -0x28(%rbp),%rax
movl   $0x3,(%rax)
mov    -0x18(%rbp),%ecx
add    $0x1,%ecx
mov    0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    0x18(%rbp),%rdi
mov    -0x10(%rbp),%rdx
movslq -0x18(%rbp),%rax
add    %rax,%rdx
mov    -0x10(%rbp),%rcx
movslq -0x18(%rbp),%rax
add    %rax,%rcx
add    $0x2,%rcx
lea    0xda2(%rip),%rsi
call   1050 <sprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12b3 <pieceoftotal+0x183>
jmp    12ac <pieceoftotal+0x17c>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pieceoftotal>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
sub    %edx,%esi
xor    %ebp,%ebp
cmp    %ecx,%esi
jl     1212 <pieceoftotal+0xe2>
mov    %r9,%r15
mov    %edx,%ebx
mov    %rdi,%r14
test   %esi,%esi
je     115c <pieceoftotal+0x2c>
cmp    $0x4,%esi
jl     1212 <pieceoftotal+0xe2>
cmpl   $0x0,0x50(%rsp)
mov    $0x1,%ebp
jne    1212 <pieceoftotal+0xe2>
test   %esi,%esi
je     1212 <pieceoftotal+0xe2>
mov    0x40(%rsp),%r13
movslq %ebx,%rax
add    %rax,%r14
mov    %r14,%rdi
mov    $0x2,%esi
call   1040 <to_int@plt>
test   %eax,%eax
je     11d7 <pieceoftotal+0xa7>
mov    %eax,%r12d
lea    0x2(%r14),%r13
mov    %r13,%rdi
mov    $0x2,%esi
call   1040 <to_int@plt>
test   %eax,%eax
je     11ee <pieceoftotal+0xbe>
cmp    %eax,%r12d
jle    1212 <pieceoftotal+0xe2>
movl   $0x3,(%r15)
add    $0x1,%ebx
mov    0x40(%rsp),%rax
mov    %ebx,(%rax)
lea    0xe73(%rip),%rsi
mov    0x48(%rsp),%rdi
mov    %r14,%rdx
mov    %r13,%rcx
call   1050 <sprintf@plt>
jmp    1210 <pieceoftotal+0xe0>
movl   $0x3,(%r15)
add    $0x1,%ebx
mov    %ebx,0x0(%r13)
lea    0xe14(%rip),%rsi
jmp    1206 <pieceoftotal+0xd6>
movl   $0x3,(%r15)
add    $0x1,%ebx
mov    0x40(%rsp),%rax
mov    %ebx,(%rax)
lea    0xe16(%rip),%rsi
mov    0x48(%rsp),%rdi
call   1030 <strcpy@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [28 x i8] c"Piece number cannot be zero\00"
@.str.1 = [28 x i8] c"Total number cannot be zero\00"
@.str.2 = [41 x i8] c"Piece number '%.2s' exceeds total '%.2s'\00"
define i32 @pieceoftotal(i8* %0, i32 %1, i32 %2, i32 %3, i32 %4, i32* %5, i32* %6, i8* %7, i32 %8) {
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32*
%17 = alloca i32*
%18 = alloca i8*
%19 = alloca i32
%20 = alloca i32
%21 = alloca i32
store i8* %0, i8** %11
store i32 %1, i32* %12
store i32 %2, i32* %13
store i32 %3, i32* %14
store i32 %4, i32* %15
store i32* %5, i32** %16
store i32* %6, i32** %17
store i8* %7, i8** %18
store i32 %8, i32* %19
%22 = load i32, i32* %15
%23 = load i32, i32* %13
%24 = load i32, i32* %12
%25 = sub nsw i32 %24, %23
store i32 %25, i32* %12
%26 = load i32, i32* %12
%27 = load i32, i32* %14
%28 = icmp slt i32 %26, %27
br i1 %28, label %35, label %29
29: ; preds = %9
%30 = load i32, i32* %12
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %36
32: ; preds = %29
%33 = load i32, i32* %12
%34 = icmp slt i32 %33, 4
br i1 %34, label %35, label %36
35: ; preds = %32, %9
store i32 0, i32* %10
br label %95
36: ; preds = %32, %29
%37 = load i32, i32* %19
%38 = icmp ne i32 %37, 0
br i1 %38, label %94, label %39
39: ; preds = %36
%40 = load i32, i32* %12
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %94
42: ; preds = %39
%43 = load i8*, i8** %11
%44 = load i32, i32* %13
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i8, i8* %43, i64 %45
%47 = call i32 @to_int(i8* %46, i32 2)
store i32 %47, i32* %20
%48 = load i32, i32* %20
%49 = icmp eq i32 %48, 0
br i1 %49, label %50, label %57
50: ; preds = %42
%51 = load i32*, i32** %16
store i32 3, i32* %51
%52 = load i32, i32* %13
%53 = add nsw i32 %52, 1
%54 = load i32*, i32** %17
store i32 %53, i32* %54
%55 = load i8*, i8** %18
%56 = call i32 @strcpy(i8* %55, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %10
br label %95
57: ; preds = %42
%58 = load i8*, i8** %11
%59 = load i32, i32* %13
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds i8, i8* %58, i64 %60
%62 = getelementptr inbounds i8, i8* %61, i64 2
%63 = call i32 @to_int(i8* %62, i32 2)
store i32 %63, i32* %21
%64 = load i32, i32* %21
%65 = icmp eq i32 %64, 0
br i1 %65, label %66, label %73
66: ; preds = %57
%67 = load i32*, i32** %16
store i32 3, i32* %67
%68 = load i32, i32* %13
%69 = add nsw i32 %68, 1
%70 = load i32*, i32** %17
store i32 %69, i32* %70
%71 = load i8*, i8** %18
%72 = call i32 @strcpy(i8* %71, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %10
br label %95
73: ; preds = %57
%74 = load i32, i32* %20
%75 = load i32, i32* %21
%76 = icmp sgt i32 %74, %75
br i1 %76, label %77, label %93
77: ; preds = %73
%78 = load i32*, i32** %16
store i32 3, i32* %78
%79 = load i32, i32* %13
%80 = add nsw i32 %79, 1
%81 = load i32*, i32** %17
store i32 %80, i32* %81
%82 = load i8*, i8** %18
%83 = load i8*, i8** %11
%84 = load i32, i32* %13
%85 = sext i32 %84 to i64
%86 = getelementptr inbounds i8, i8* %83, i64 %85
%87 = load i8*, i8** %11
%88 = load i32, i32* %13
%89 = sext i32 %88 to i64
%90 = getelementptr inbounds i8, i8* %87, i64 %89
%91 = getelementptr inbounds i8, i8* %90, i64 2
%92 = call i32 @sprintf(i8* %82, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i8* %86, i8* %91)
store i32 0, i32* %10
br label %95
93: ; preds = %73
br label %94
94: ; preds = %93, %39, %36
store i32 1, i32* %10
br label %95
95: ; preds = %94, %77, %66, %50, %35
%96 = load i32, i32* %10
ret i32 %96
}

LLVM IR (-O2):
@.str = [28 x i8] c"Piece number cannot be zero\00"
@.str.1 = [28 x i8] c"Total number cannot be zero\00"
@.str.2 = [41 x i8] c"Piece number '%.2s' exceeds total '%.2s'\00"
define i32 @pieceoftotal(i8* %0, i32 %1, i32 %2, i32 %3, i32 %4, i32* nocapture writeonly %5, i32* nocapture writeonly %6, i8* %7, i32 %8) {
%10 = sub nsw i32 %1, %2
%11 = icmp slt i32 %10, %3
br i1 %11, label %39, label %12
12: ; preds = %9
%13 = icmp ne i32 %10, 0
%14 = icmp slt i32 %10, 4
%15 = and i1 %13, %14
br i1 %15, label %39, label %16
16: ; preds = %12
%17 = icmp eq i32 %8, 0
%18 = select i1 %17, i1 %13, i1 false
br i1 %18, label %19, label %39
19: ; preds = %16
%20 = sext i32 %2 to i64
%21 = getelementptr inbounds i8, i8* %0, i64 %20
%22 = tail call i32 @to_int(i8* %21, i32 2)
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %19
store i32 3, i32* %5
%25 = add nsw i32 %2, 1
store i32 %25, i32* %6
%26 = tail call i32 @strcpy(i8* %7, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %39
27: ; preds = %19
%28 = getelementptr inbounds i8, i8* %21, i64 2
%29 = tail call i32 @to_int(i8* %28, i32 2)
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %27
store i32 3, i32* %5
%32 = add nsw i32 %2, 1
store i32 %32, i32* %6
%33 = tail call i32 @strcpy(i8* %7, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
br label %39
34: ; preds = %27
%35 = icmp sgt i32 %22, %29
br i1 %35, label %36, label %39
36: ; preds = %34
store i32 3, i32* %5
%37 = add nsw i32 %2, 1
store i32 %37, i32* %6
%38 = tail call i32 @sprintf(i8* dereferenceable(1) %7, i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i8* %21, i8* %28)
br label %39
39: ; preds = %36, %31, %24, %16, %34, %9, %12
%40 = phi i32 [ 0, %12 ], [ 0, %9 ], [ 1, %34 ], [ 1, %16 ], [ 0, %24 ], [ 0, %31 ], [ 0, %36 ]
ret i32 %40
}