#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 */
typedef int /*<<< orphan*/ msg_len;

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, unsigned short, int);
int /*<<< orphan*/ perror(char *);
int read(int, ...);
int /*<<< orphan*/ stderr;
int msg_read(int fd, char *buf, int max_len) {
  unsigned short msg_len;
  int bytes_r = 0, bytes_r_tot = 0;
  if ((bytes_r = read(fd, &msg_len, sizeof(msg_len))) < 0) {
    perror("Read msg_len");
    return -1;
  } else if (bytes_r < sizeof(msg_len)) {
    return 0;
  }
  if (msg_len > max_len - 1) {
    fprintf(stderr, "Got msg_len %d larger than max_len %d\n", msg_len,
            max_len);
    return -1;
  }
  while (bytes_r_tot < msg_len) {
    if ((bytes_r = read(fd, buf + bytes_r_tot, msg_len - bytes_r_tot)) < 0)
      return -1;
    else if (bytes_r == 0) {
      buf[bytes_r_tot] = '\0';
      return 0;
    }
    bytes_r_tot += bytes_r;
  }
  buf[msg_len] = '\0';
  return msg_len;
}


x86 assembly (clang, -O0):
<msg_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x16(%rbp),%rsi
mov    $0x2,%edx
mov    $0x0,%al
call   1030 <read@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    1187 <msg_read+0x57>
lea    0xe8a(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1273 <msg_read+0x143>
movslq -0x1c(%rbp),%rax
cmp    $0x2,%rax
jae    11a1 <msg_read+0x71>
movl   $0x0,-0x4(%rbp)
jmp    1273 <msg_read+0x143>
jmp    11a6 <msg_read+0x76>
movzwl -0x16(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jle    11e3 <msg_read+0xb3>
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
mov    -0x16(%rbp),%ax
mov    -0x14(%rbp),%ecx
lea    0xe3e(%rip),%rsi
movzwl %ax,%edx
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1273 <msg_read+0x143>
jmp    11e8 <msg_read+0xb8>
mov    -0x20(%rbp),%eax
movzwl -0x16(%rbp),%ecx
cmp    %ecx,%eax
jge    1260 <msg_read+0x130>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x20(%rbp),%rax
add    %rax,%rsi
movzwl -0x16(%rbp),%edx
sub    -0x20(%rbp),%edx
mov    $0x0,%al
call   1030 <read@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    122b <msg_read+0xfb>
movl   $0xffffffff,-0x4(%rbp)
jmp    1273 <msg_read+0x143>
cmpl   $0x0,-0x1c(%rbp)
jne    124d <msg_read+0x11d>
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x4(%rbp)
jmp    1273 <msg_read+0x143>
jmp    1252 <msg_read+0x122>
mov    -0x1c(%rbp),%eax
add    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    11e8 <msg_read+0xb8>
mov    -0x10(%rbp),%rax
movzwl -0x16(%rbp),%ecx
movb   $0x0,(%rax,%rcx,1)
movzwl -0x16(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<msg_read>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edx,%r15d
mov    %rsi,%r14
mov    %edi,%r12d
xor    %ebx,%ebx
lea    0xe(%rsp),%rsi
mov    $0x2,%edx
xor    %eax,%eax
call   1030 <read@plt>
test   %eax,%eax
js     11d7 <msg_read+0xa7>
cmp    $0x2,%eax
jb     11ef <msg_read+0xbf>
movzwl 0xe(%rsp),%edx
cmp    %r15d,%edx
jge    11ac <msg_read+0x7c>
test   %edx,%edx
je     11cb <msg_read+0x9b>
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nop
movslq %ebp,%rax
lea    (%r14,%rax,1),%rbx
sub    %eax,%edx
mov    %r12d,%edi
mov    %rbx,%rsi
xor    %eax,%eax
call   1030 <read@plt>
test   %eax,%eax
js     11e3 <msg_read+0xb3>
je     11ea <msg_read+0xba>
add    %eax,%ebp
movzwl 0xe(%rsp),%eax
movzwl %ax,%edx
cmp    %edx,%ebp
jl     1180 <msg_read+0x50>
jmp    11cd <msg_read+0x9d>
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
lea    0xe51(%rip),%rsi
mov    %r15d,%ecx
call   1040 <fprintf@plt>
mov    $0xffffffff,%ebx
jmp    11ef <msg_read+0xbf>
xor    %eax,%eax
movzwl %ax,%ebx
movb   $0x0,(%r14,%rbx,1)
jmp    11ef <msg_read+0xbf>
lea    0xe22(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%ebx
jmp    11ef <msg_read+0xbf>
movb   $0x0,(%rbx)
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"Read msg_len\00"
@stderr = global i32 0
@.str.1 = [39 x i8] c"Got msg_len %d larger than max_len %d\0A\00"
define i32 @msg_read(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i16
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %9
store i32 0, i32* %10
%11 = load i32, i32* %5
%12 = call i32 (i32, ...) @read(i32 %11, i16* %8, i64 2)
store i32 %12, i32* %9
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %3
%15 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %72
16: ; preds = %3
%17 = load i32, i32* %9
%18 = sext i32 %17 to i64
%19 = icmp ult i64 %18, 2
br i1 %19, label %20, label %21
20: ; preds = %16
store i32 0, i32* %4
br label %72
21: ; preds = %16
br label %22
22: ; preds = %21
%23 = load i16, i16* %8
%24 = zext i16 %23 to i32
%25 = load i32, i32* %7
%26 = sub nsw i32 %25, 1
%27 = icmp sgt i32 %24, %26
br i1 %27, label %28, label %33
28: ; preds = %22
%29 = load i32, i32* @stderr
%30 = load i16, i16* %8
%31 = load i32, i32* %7
%32 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i16 zeroext %30, i32 %31)
store i32 -1, i32* %4
br label %72
33: ; preds = %22
br label %34
34: ; preds = %61, %33
%35 = load i32, i32* %10
%36 = load i16, i16* %8
%37 = zext i16 %36 to i32
%38 = icmp slt i32 %35, %37
br i1 %38, label %39, label %65
39: ; preds = %34
%40 = load i32, i32* %5
%41 = load i8*, i8** %6
%42 = load i32, i32* %10
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i8, i8* %41, i64 %43
%45 = load i16, i16* %8
%46 = zext i16 %45 to i32
%47 = load i32, i32* %10
%48 = sub nsw i32 %46, %47
%49 = call i32 (i32, ...) @read(i32 %40, i8* %44, i32 %48)
store i32 %49, i32* %9
%50 = icmp slt i32 %49, 0
br i1 %50, label %51, label %52
51: ; preds = %39
store i32 -1, i32* %4
br label %72
52: ; preds = %39
%53 = load i32, i32* %9
%54 = icmp eq i32 %53, 0
br i1 %54, label %55, label %60
55: ; preds = %52
%56 = load i8*, i8** %6
%57 = load i32, i32* %10
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds i8, i8* %56, i64 %58
store i8 0, i8* %59
store i32 0, i32* %4
br label %72
60: ; preds = %52
br label %61
61: ; preds = %60
%62 = load i32, i32* %9
%63 = load i32, i32* %10
%64 = add nsw i32 %63, %62
store i32 %64, i32* %10
br label %34
65: ; preds = %34
%66 = load i8*, i8** %6
%67 = load i16, i16* %8
%68 = zext i16 %67 to i64
%69 = getelementptr inbounds i8, i8* %66, i64 %68
store i8 0, i8* %69
%70 = load i16, i16* %8
%71 = zext i16 %70 to i32
store i32 %71, i32* %4
br label %72
72: ; preds = %65, %55, %51, %28, %20, %14
%73 = load i32, i32* %4
ret i32 %73
}

LLVM IR (-O2):
@.str = [13 x i8] c"Read msg_len\00"
@stderr = global i32 0
@.str.1 = [39 x i8] c"Got msg_len %d larger than max_len %d\0A\00"
define i32 @msg_read(i32 %0, i8* %1, i32 %2) {
%4 = alloca i16
%5 = bitcast i16* %4 to i8*
%6 = call i32 (i32, ...) @read(i32 %0, i16* %4, i64 2)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %3
%9 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %42
10: ; preds = %3
%11 = icmp ult i32 %6, 2
br i1 %11, label %42, label %12
12: ; preds = %10
%13 = load i16, i16* %4
%14 = zext i16 %13 to i32
%15 = icmp slt i32 %14, %2
br i1 %15, label %16, label %18
16: ; preds = %12
%17 = icmp eq i16 %13, 0
br i1 %17, label %37, label %25
18: ; preds = %12
%19 = load i32, i32* @stderr
%20 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i16 zeroext %13, i32 %2)
br label %42
21: ; preds = %33
%22 = load i16, i16* %4
%23 = zext i16 %22 to i32
%24 = icmp slt i32 %35, %23
br i1 %24, label %25, label %37
25: ; preds = %16, %21
%26 = phi i32 [ %23, %21 ], [ %14, %16 ]
%27 = phi i32 [ %35, %21 ], [ 0, %16 ]
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %1, i64 %28
%30 = sub nsw i32 %26, %27
%31 = call i32 (i32, ...) @read(i32 %0, i8* %29, i32 %30)
%32 = icmp slt i32 %31, 0
br i1 %32, label %42, label %33
33: ; preds = %25
%34 = icmp eq i32 %31, 0
%35 = add nsw i32 %31, %27
br i1 %34, label %36, label %21
36: ; preds = %33
store i8 0, i8* %29
br label %42
37: ; preds = %21, %16
%38 = phi i16 [ 0, %16 ], [ %22, %21 ]
%39 = zext i16 %38 to i32
%40 = zext i16 %38 to i64
%41 = getelementptr inbounds i8, i8* %1, i64 %40
store i8 0, i8* %41
br label %42
42: ; preds = %25, %10, %37, %36, %18, %8
%43 = phi i32 [ -1, %8 ], [ -1, %18 ], [ 0, %36 ], [ %39, %37 ], [ 0, %10 ], [ -1, %25 ]
ret i32 %43
}