#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 /*<<< orphan*/ fix_string(char *, int);
int read(int, char *, int);
int /*<<< orphan*/ strcpy(char *, char *);
int readprot(register int fd, register char *ptr, register int maxlen) {
  int n, rc;
  char buffer[512];
  if ((rc = read(fd, buffer, 2)) == 2) {
    n = (unsigned char)buffer[0] << 8;
    n += (unsigned char)buffer[1];
  } else
    return (-1);
  if ((rc = read(fd, buffer, n)) == n) {
    fix_string(buffer, n);
    buffer[n] = 0;
    strcpy(ptr, buffer);
    return (n);
  }
  return (-1);
}


x86 assembly (clang, -O0):
<readprot>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x220(%rbp),%rsi
mov    $0x2,%edx
call   1050 <read@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x2,%eax
jne    1184 <readprot+0x54>
movzbl -0x220(%rbp),%eax
shl    $0x8,%eax
mov    %eax,-0x18(%rbp)
movzbl -0x21f(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    1190 <readprot+0x60>
movl   $0xffffffff,-0x4(%rbp)
jmp    11eb <readprot+0xbb>
mov    -0x8(%rbp),%edi
lea    -0x220(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1050 <read@plt>
mov    %eax,-0x1c(%rbp)
cmp    -0x18(%rbp),%eax
jne    11e4 <readprot+0xb4>
lea    -0x220(%rbp),%rdi
mov    -0x18(%rbp),%esi
call   1040 <fix_string@plt>
movslq -0x18(%rbp),%rax
movb   $0x0,-0x220(%rbp,%rax,1)
mov    -0x10(%rbp),%rdi
lea    -0x220(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11eb <readprot+0xbb>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readprot>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x208,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
mov    %rsp,%rsi
mov    $0x2,%edx
call   1050 <read@plt>
mov    $0xffffffff,%r15d
cmp    $0x2,%eax
jne    1194 <readprot+0x64>
movzwl (%rsp),%ebx
shl    $0x30,%rbx
bswap  %rbx
mov    %rsp,%rsi
mov    %ebp,%edi
mov    %ebx,%edx
call   1050 <read@plt>
cmp    %ebx,%eax
jne    1194 <readprot+0x64>
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    %ebx,%esi
call   1040 <fix_string@plt>
movb   $0x0,(%rsp,%rbx,1)
mov    %r14,%rdi
mov    %rbp,%rsi
call   1030 <strcpy@plt>
mov    %ebx,%r15d
mov    %r15d,%eax
add    $0x208,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @readprot(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca [512 x i8]
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%11 = load i32, i32* %5
%12 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 0
%13 = call i32 @read(i32 %11, i8* %12, i32 2)
store i32 %13, i32* %9
%14 = icmp eq i32 %13, 2
br i1 %14, label %15, label %25
15: ; preds = %3
%16 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 0
%17 = load i8, i8* %16
%18 = zext i8 %17 to i32
%19 = shl i32 %18, 8
store i32 %19, i32* %8
%20 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 1
%21 = load i8, i8* %20
%22 = zext i8 %21 to i32
%23 = load i32, i32* %8
%24 = add nsw i32 %23, %22
store i32 %24, i32* %8
br label %26
25: ; preds = %3
store i32 -1, i32* %4
br label %45
26: ; preds = %15
%27 = load i32, i32* %5
%28 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 0
%29 = load i32, i32* %8
%30 = call i32 @read(i32 %27, i8* %28, i32 %29)
store i32 %30, i32* %9
%31 = load i32, i32* %8
%32 = icmp eq i32 %30, %31
br i1 %32, label %33, label %44
33: ; preds = %26
%34 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 0
%35 = load i32, i32* %8
%36 = call i32 @fix_string(i8* %34, i32 %35)
%37 = load i32, i32* %8
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 %38
store i8 0, i8* %39
%40 = load i8*, i8** %6
%41 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 0
%42 = call i32 @strcpy(i8* %40, i8* %41)
%43 = load i32, i32* %8
store i32 %43, i32* %4
br label %45
44: ; preds = %26
store i32 -1, i32* %4
br label %45
45: ; preds = %44, %33, %25
%46 = load i32, i32* %4
ret i32 %46
}

LLVM IR (-O2):
define i32 @readprot(i32 %0, i8* %1, i32 %2) {
%4 = alloca [512 x i8]
%5 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%6 = call i32 @read(i32 %0, i8* %5, i32 2)
%7 = icmp eq i32 %6, 2
br i1 %7, label %8, label %23
8: ; preds = %3
%9 = load i8, i8* %5
%10 = zext i8 %9 to i32
%11 = shl nuw nsw i32 %10, 8
%12 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 1
%13 = load i8, i8* %12
%14 = zext i8 %13 to i32
%15 = or i32 %11, %14
%16 = call i32 @read(i32 %0, i8* %5, i32 %15)
%17 = icmp eq i32 %16, %15
br i1 %17, label %18, label %23
18: ; preds = %8
%19 = call i32 @fix_string(i8* %5, i32 %15)
%20 = zext i32 %15 to i64
%21 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 %20
store i8 0, i8* %21
%22 = call i32 @strcpy(i8* %1, i8* %5)
br label %23
23: ; preds = %8, %3, %18
%24 = phi i32 [ %15, %18 ], [ -1, %3 ], [ -1, %8 ]
ret i32 %24
}