#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_write(char);
int layer2_write(char *chunk, int len) {
  static int _error = 0;
  if ((_error == -1))
    return _error = -1;
  if (len < 0 || len > 16)
    return -1;
  if (((layer1_write(len)) == -1))
    return _error = -1;
  for (unsigned i = 0; i < len; ++i)
    if (((layer1_write(chunk[i])) == -1))
      return _error = -1;
  return len;
}


x86 assembly (clang, -O0):
<layer2_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpl   $0xffffffff,0x2f06(%rip)
jne    1142 <layer2_write+0x32>
movl   $0xffffffff,0x2ef6(%rip)
ff ff ff
movl   $0xffffffff,-0x4(%rbp)
jmp    11e7 <layer2_write+0xd7>
cmpl   $0x0,-0x14(%rbp)
jl     1156 <layer2_write+0x46>
cmpl   $0x10,-0x14(%rbp)
jle    1162 <layer2_write+0x52>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e7 <layer2_write+0xd7>
mov    -0x14(%rbp),%eax
movsbl %al,%edi
call   1030 <layer1_write@plt>
cmp    $0xffffffff,%eax
jne    118c <layer2_write+0x7c>
movl   $0xffffffff,0x2eac(%rip)
ff ff ff
movl   $0xffffffff,-0x4(%rbp)
jmp    11e7 <layer2_write+0xd7>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jae    11e1 <layer2_write+0xd1>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
movsbl (%rax,%rcx,1),%edi
call   1030 <layer1_write@plt>
cmp    $0xffffffff,%eax
jne    11ce <layer2_write+0xbe>
movl   $0xffffffff,0x2e6a(%rip)
ff ff ff
movl   $0xffffffff,-0x4(%rbp)
jmp    11e7 <layer2_write+0xd7>
jmp    11d3 <layer2_write+0xc3>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1193 <layer2_write+0x83>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<layer2_write>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
cmpb   $0x0,0x2f0e(%rip)
je     112e <layer2_write+0x1e>
movb   $0x1,0x2f05(%rip)
mov    $0xffffffff,%eax
jmp    113b <layer2_write+0x2b>
mov    %esi,%r14d
mov    $0xffffffff,%eax
cmp    $0x10,%esi
jbe    1146 <layer2_write+0x36>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rdi,%rbx
movsbl %r14b,%edi
call   1030 <layer1_write@plt>
cmp    $0xffffffff,%eax
je     1120 <layer2_write+0x10>
test   %r14d,%r14d
je     118c <layer2_write+0x7c>
mov    %r14d,%r15d
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movsbl (%rbx,%rbp,1),%edi
call   1030 <layer1_write@plt>
cmp    $0xffffffff,%eax
je     1120 <layer2_write+0x10>
add    $0x1,%rbp
cmp    %rbp,%r15
jne    1170 <layer2_write+0x60>
mov    %r14d,%eax
jmp    113b <layer2_write+0x2b>
xor    %eax,%eax
jmp    113b <layer2_write+0x2b>

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

LLVM IR (-O2):
@layer2_write._error = internal unnamed_addr global i1 false
define i32 @layer2_write(i8* nocapture %0, i32 %1) {
%3 = load i1, i1* @layer2_write._error
br i1 %3, label %23, label %4
4: ; preds = %2
%5 = icmp ugt i32 %1, 16
br i1 %5, label %24, label %6
6: ; preds = %4
%7 = trunc i32 %1 to i8
%8 = tail call i32 @layer1_write(i8 signext %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %23, label %10
10: ; preds = %6
%11 = icmp eq i32 %1, 0
br i1 %11, label %24, label %12
12: ; preds = %10
%13 = zext i32 %1 to i64
br label %16
14: ; preds = %16
%15 = icmp eq i64 %22, %13
br i1 %15, label %24, label %16
16: ; preds = %12, %14
%17 = phi i64 [ 0, %12 ], [ %22, %14 ]
%18 = getelementptr inbounds i8, i8* %0, i64 %17
%19 = load i8, i8* %18
%20 = tail call i32 @layer1_write(i8 signext %19)
%21 = icmp eq i32 %20, -1
%22 = add nuw nsw i64 %17, 1
br i1 %21, label %23, label %14
23: ; preds = %16, %6, %2
store i1 true, i1* @layer2_write._error
br label %24
24: ; preds = %14, %23, %10, %4
%25 = phi i32 [ -1, %4 ], [ 0, %10 ], [ -1, %23 ], [ %1, %14 ]
ret i32 %25
}