#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 layer1_read(char *);
int layer2_read(char *chunk, int max) {
  static int _error = 0;
  if ((_error == -1))
    return _error = -1;
  char len;
  if (((layer1_read(&len)) == -1))
    return _error = -1;
  if ((len < 0 || len > 16))
    return _error = -1;
  if ((len > max))
    return _error = -1;
  for (int i = 0; i < len; ++i)
    if (((layer1_read(chunk + i)) == -1))
      return _error = -1;
  return len;
}


x86 assembly (clang, -O0):
<layer2_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpl   $0xffffffff,0x2f06(%rip)
jne    1142 <layer2_read+0x32>
movl   $0xffffffff,0x2ef6(%rip)
ff ff ff
movl   $0xffffffff,-0x4(%rbp)
jmp    121c <layer2_read+0x10c>
lea    -0x15(%rbp),%rdi
call   1030 <layer1_read@plt>
cmp    $0xffffffff,%eax
jne    116a <layer2_read+0x5a>
movl   $0xffffffff,0x2ece(%rip)
ff ff ff
movl   $0xffffffff,-0x4(%rbp)
jmp    121c <layer2_read+0x10c>
movsbl -0x15(%rbp),%eax
cmp    $0x0,%eax
jl     1184 <layer2_read+0x74>
movsbl -0x15(%rbp),%eax
cmp    $0x10,%eax
jle    119a <layer2_read+0x8a>
movl   $0xffffffff,0x2e9e(%rip)
ff ff ff
movl   $0xffffffff,-0x4(%rbp)
jmp    121c <layer2_read+0x10c>
movsbl -0x15(%rbp),%eax
cmp    -0x14(%rbp),%eax
jle    11bd <layer2_read+0xad>
movl   $0xffffffff,0x2e7b(%rip)
ff ff ff
movl   $0xffffffff,-0x4(%rbp)
jmp    121c <layer2_read+0x10c>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
movsbl -0x15(%rbp),%ecx
cmp    %ecx,%eax
jge    1215 <layer2_read+0x105>
mov    -0x10(%rbp),%rdi
movslq -0x1c(%rbp),%rax
add    %rax,%rdi
call   1030 <layer1_read@plt>
cmp    $0xffffffff,%eax
jne    1202 <layer2_read+0xf2>
movl   $0xffffffff,0x2e36(%rip)
ff ff ff
movl   $0xffffffff,-0x4(%rbp)
jmp    121c <layer2_read+0x10c>
jmp    1207 <layer2_read+0xf7>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11c4 <layer2_read+0xb4>
movsbl -0x15(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<layer2_read>:
push   %rbp
push   %rbx
push   %rax
cmpb   $0x1,0x2f12(%rip)
je     113e <layer2_read+0x2e>
mov    %esi,%ebp
mov    %rdi,%rbx
lea    0x7(%rsp),%rdi
call   1030 <layer1_read@plt>
cmp    $0xffffffff,%eax
je     113e <layer2_read+0x2e>
movzbl 0x7(%rsp),%eax
cmp    $0x11,%eax
jae    113e <layer2_read+0x2e>
cmp    %ebp,%eax
jle    1151 <layer2_read+0x41>
movb   $0x1,0x2ee7(%rip)
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
test   %eax,%eax
je     117f <layer2_read+0x6f>
xor    %ebp,%ebp
nopw   0x0(%rax,%rax,1)
lea    (%rbx,%rbp,1),%rdi
call   1030 <layer1_read@plt>
cmp    $0xffffffff,%eax
je     113e <layer2_read+0x2e>
add    $0x1,%rbp
movsbq 0x7(%rsp),%rax
cmp    %rax,%rbp
jl     1160 <layer2_read+0x50>
jmp    114a <layer2_read+0x3a>
xor    %eax,%eax
jmp    114a <layer2_read+0x3a>

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

LLVM IR (-O2):
@layer2_read._error = internal unnamed_addr global i1 false
define i32 @layer2_read(i8* %0, i32 %1) {
%3 = alloca i8
%4 = load i1, i1* @layer2_read._error
br i1 %4, label %5, label %6
5: ; preds = %2
store i1 true, i1* @layer2_read._error
br label %35
6: ; preds = %2
%7 = call i32 @layer1_read(i8* %3)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %10
9: ; preds = %6
store i1 true, i1* @layer2_read._error
br label %33
10: ; preds = %6
%11 = load i8, i8* %3
%12 = icmp ugt i8 %11, 16
br i1 %12, label %13, label %14
13: ; preds = %10
store i1 true, i1* @layer2_read._error
br label %33
14: ; preds = %10
%15 = zext i8 %11 to i32
%16 = icmp sgt i32 %15, %1
br i1 %16, label %19, label %17
17: ; preds = %14
%18 = icmp eq i8 %11, 0
br i1 %18, label %33, label %24
19: ; preds = %14
store i1 true, i1* @layer2_read._error
br label %33
20: ; preds = %24
%21 = load i8, i8* %3
%22 = sext i8 %21 to i64
%23 = icmp slt i64 %29, %22
br i1 %23, label %24, label %31
24: ; preds = %17, %20
%25 = phi i64 [ %29, %20 ], [ 0, %17 ]
%26 = getelementptr inbounds i8, i8* %0, i64 %25
%27 = call i32 @layer1_read(i8* %26)
%28 = icmp eq i32 %27, -1
%29 = add nuw nsw i64 %25, 1
br i1 %28, label %30, label %20
30: ; preds = %24
store i1 true, i1* @layer2_read._error
br label %33
31: ; preds = %20
%32 = sext i8 %21 to i32
br label %33
33: ; preds = %17, %31, %30, %19, %13, %9
%34 = phi i32 [ -1, %9 ], [ -1, %13 ], [ -1, %19 ], [ -1, %30 ], [ 0, %17 ], [ %32, %31 ]
br label %35
35: ; preds = %33, %5
%36 = phi i32 [ -1, %5 ], [ %34, %33 ]
ret i32 %36
}