#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_read(char *, int);
int layer3_read(char *msg, int max) {
  static int _error = 0;
  if ((_error == -1))
    return _error = -1;
  int bytes_remaining = max;
  char *msg_seek = msg;
  int bytes_read;
  do {
    if (((bytes_read = layer2_read(msg_seek, bytes_remaining)) == -1))
      return _error = -1;
    msg_seek += bytes_read;
    bytes_remaining -= bytes_read;
  } while (bytes_read == 16);
  return msg_seek - msg;
}


x86 assembly (clang, -O0):
<layer3_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpl   $0xffffffff,0x2f06(%rip)
jne    1142 <layer3_read+0x32>
movl   $0xffffffff,0x2ef6(%rip)
ff ff ff
movl   $0xffffffff,-0x4(%rbp)
jmp    11b2 <layer3_read+0xa2>
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%esi
call   1030 <layer2_read@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
jne    117e <layer3_read+0x6e>
movl   $0xffffffff,0x2eba(%rip)
ff ff ff
movl   $0xffffffff,-0x4(%rbp)
jmp    11b2 <layer3_read+0xa2>
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     1150 <layer3_read+0x40>
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<layer3_read>:
push   %rbp
push   %r14
push   %rbx
cmpb   $0x0,0x2f11(%rip)
je     112b <layer3_read+0x1b>
movb   $0x1,0x2f08(%rip)
mov    $0xffffffff,%ebx
jmp    115f <layer3_read+0x4f>
mov    %esi,%ebp
mov    %rdi,%r14
mov    %rdi,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbx,%rdi
mov    %ebp,%esi
call   1030 <layer2_read@plt>
cmp    $0xffffffff,%eax
je     111d <layer3_read+0xd>
movslq %eax,%rcx
add    %rcx,%rbx
sub    %eax,%ebp
cmp    $0x10,%eax
je     1140 <layer3_read+0x30>
sub    %r14d,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@layer3_read._error = internal global i32 0
define i32 @layer3_read(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_read._error
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %12
11: ; preds = %2
store i32 -1, i32* @layer3_read._error
store i32 -1, i32* %3
br label %39
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 = %29, %12
%16 = load i8*, i8** %7
%17 = load i32, i32* %6
%18 = call i32 @layer2_read(i8* %16, i32 %17)
store i32 %18, i32* %8
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %21
20: ; preds = %15
store i32 -1, i32* @layer3_read._error
store i32 -1, i32* %3
br label %39
21: ; preds = %15
%22 = load i32, i32* %8
%23 = load i8*, i8** %7
%24 = sext i32 %22 to i64
%25 = getelementptr inbounds i8, i8* %23, i64 %24
store i8* %25, i8** %7
%26 = load i32, i32* %8
%27 = load i32, i32* %6
%28 = sub nsw i32 %27, %26
store i32 %28, i32* %6
br label %29
29: ; preds = %21
%30 = load i32, i32* %8
%31 = icmp eq i32 %30, 16
br i1 %31, label %15, label %32
32: ; preds = %29
%33 = load i8*, i8** %7
%34 = load i8*, i8** %4
%35 = ptrtoint i8* %33 to i64
%36 = ptrtoint i8* %34 to i64
%37 = sub i64 %35, %36
%38 = trunc i64 %37 to i32
store i32 %38, i32* %3
br label %39
39: ; preds = %32, %20, %11
%40 = load i32, i32* %3
ret i32 %40
}

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