#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 read(int, char *, int);
int read_form(int fd, char *buf, int max_size) {
  int length;
  if (read(fd, buf, 1) != 1) {
    return 0;
  }
  length = (int)((unsigned)buf[0] & 0xff);
  if (length > max_size - 1) {
    return 0;
  }
  if (read(fd, buf, length) != length) {
    return 0;
  }
  buf[length] = '\0';
  return 1;
}


x86 assembly (clang, -O0):
<read_form>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x1,%eax
je     1148 <read_form+0x38>
movl   $0x0,-0x4(%rbp)
jmp    11ab <read_form+0x9b>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
and    $0xff,%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jle    1174 <read_form+0x64>
movl   $0x0,-0x4(%rbp)
jmp    11ab <read_form+0x9b>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1030 <read@plt>
cmp    -0x18(%rbp),%eax
je     1198 <read_form+0x88>
movl   $0x0,-0x4(%rbp)
jmp    11ab <read_form+0x9b>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_form>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r12d
mov    %rsi,%rbx
mov    %edi,%ebp
mov    $0x1,%edx
call   1030 <read@plt>
xor    %r14d,%r14d
cmp    $0x1,%eax
jne    115a <read_form+0x4a>
movzbl (%rbx),%r15d
cmp    %r12d,%r15d
jge    115a <read_form+0x4a>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1030 <read@plt>
cmp    %r15d,%eax
jne    115a <read_form+0x4a>
mov    %r15d,%eax
movb   $0x0,(%rbx,%rax,1)
mov    $0x1,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @read_form(i32 %0, i8* nocapture %1, i32 %2) {
%4 = tail call i32 @read(i32 %0, i8* %1, i32 1)
%5 = icmp eq i32 %4, 1
br i1 %5, label %6, label %16
6: ; preds = %3
%7 = load i8, i8* %1
%8 = zext i8 %7 to i32
%9 = icmp slt i32 %8, %2
br i1 %9, label %10, label %16
10: ; preds = %6
%11 = tail call i32 @read(i32 %0, i8* %1, i32 %8)
%12 = icmp eq i32 %11, %8
br i1 %12, label %13, label %16
13: ; preds = %10
%14 = zext i8 %7 to i64
%15 = getelementptr inbounds i8, i8* %1, i64 %14
store i8 0, i8* %15
br label %16
16: ; preds = %10, %6, %3, %13
%17 = phi i32 [ 1, %13 ], [ 0, %3 ], [ 0, %6 ], [ 0, %10 ]
ret i32 %17
}