#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 layer2_write(char *, int);
int min(int, int);
int layer3_write(char *msg, int len) {
  static int _error = 0;
  if ((_error == -1))
    return _error = -1;
  int bytes_remaining = len;
  char *msg_seek = msg;
  int bytes_to_write;
  do {
    bytes_to_write = min(16, bytes_remaining);
    if (((layer2_write(msg_seek, bytes_to_write)) == -1))
      return _error = -1;
    msg_seek += bytes_to_write;
    bytes_remaining -= bytes_to_write;
  } while (bytes_to_write == 16);
  return len;
}


x86 assembly (clang, -O0):
<layer3_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpl   $0xffffffff,0x2efe(%rip)
jne    1152 <layer3_write+0x32>
movl   $0xffffffff,0x2eee(%rip)
ff ff ff
movl   $0xffffffff,-0x4(%rbp)
jmp    11c7 <layer3_write+0xa7>
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%esi
mov    $0x10,%edi
call   1030 <min@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1040 <layer2_write@plt>
cmp    $0xffffffff,%eax
jne    119b <layer3_write+0x7b>
movl   $0xffffffff,0x2ea5(%rip)
ff ff ff
movl   $0xffffffff,-0x4(%rbp)
jmp    11c7 <layer3_write+0xa7>
mov    -0x24(%rbp),%ecx
mov    -0x20(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x24(%rbp),%ecx
mov    -0x18(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x10,-0x24(%rbp)
je     1160 <layer3_write+0x40>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<layer3_write>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
cmpb   $0x0,0x2f06(%rip)
je     113f <layer3_write+0x1f>
movb   $0x1,0x2efd(%rip)
mov    $0xffffffff,%r14d
jmp    117a <layer3_write+0x5a>
mov    %esi,%r14d
mov    %rdi,%r15
mov    %esi,%ebp
nopw   0x0(%rax,%rax,1)
mov    $0x10,%edi
mov    %ebp,%esi
call   1030 <min@plt>
mov    %eax,%ebx
mov    %r15,%rdi
mov    %eax,%esi
call   1040 <layer2_write@plt>
cmp    $0xffffffff,%eax
je     1130 <layer3_write+0x10>
movslq %ebx,%rax
add    %rax,%r15
sub    %ebx,%ebp
cmp    $0x10,%ebx
je     1150 <layer3_write+0x30>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@layer3_write._error = internal global i32 0
define i32 @layer3_write(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%9 = load i32, i32* @layer3_write._error
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %12
11: ; preds = %2
store i32 -1, i32* @layer3_write._error
store i32 -1, i32* %3
br label %36
12: ; preds = %2
%13 = load i32, i32* %5
store i32 %13, i32* %6
%14 = load i8*, i8** %4
store i8* %14, i8** %7
br label %15
15: ; preds = %31, %12
%16 = load i32, i32* %6
%17 = call i32 @min(i32 16, i32 %16)
store i32 %17, i32* %8
%18 = load i8*, i8** %7
%19 = load i32, i32* %8
%20 = call i32 @layer2_write(i8* %18, i32 %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %23
22: ; preds = %15
store i32 -1, i32* @layer3_write._error
store i32 -1, i32* %3
br label %36
23: ; preds = %15
%24 = load i32, i32* %8
%25 = load i8*, i8** %7
%26 = sext i32 %24 to i64
%27 = getelementptr inbounds i8, i8* %25, i64 %26
store i8* %27, i8** %7
%28 = load i32, i32* %8
%29 = load i32, i32* %6
%30 = sub nsw i32 %29, %28
store i32 %30, i32* %6
br label %31
31: ; preds = %23
%32 = load i32, i32* %8
%33 = icmp eq i32 %32, 16
br i1 %33, label %15, label %34
34: ; preds = %31
%35 = load i32, i32* %5
store i32 %35, i32* %3
br label %36
36: ; preds = %34, %22, %11
%37 = load i32, i32* %3
ret i32 %37
}

LLVM IR (-O2):
@layer3_write._error = internal unnamed_addr global i1 false
define i32 @layer3_write(i8* %0, i32 %1) {
%3 = load i1, i1* @layer3_write._error
br i1 %3, label %15, label %4
4: ; preds = %2, %10
%5 = phi i32 [ %13, %10 ], [ %1, %2 ]
%6 = phi i8* [ %12, %10 ], [ %0, %2 ]
%7 = tail call i32 @min(i32 16, i32 %5)
%8 = tail call i32 @layer2_write(i8* %6, i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %15, label %10
10: ; preds = %4
%11 = sext i32 %7 to i64
%12 = getelementptr inbounds i8, i8* %6, i64 %11
%13 = sub nsw i32 %5, %7
%14 = icmp eq i32 %7, 16
br i1 %14, label %4, label %16
15: ; preds = %4, %2
store i1 true, i1* @layer3_write._error
br label %16
16: ; preds = %10, %15
%17 = phi i32 [ -1, %15 ], [ %1, %10 ]
ret i32 %17
}