#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*/ FILE;

/* Variables and functions */
int fread(char *, int, int, int /*<<< orphan*/ *);
int readstring(char *buf, int bufsize, FILE *f) {
  char lenbuf[2];
  int len, t;
  t = fread(lenbuf, 1, 2, f);
  if (t != 2) {
    return -1;
  }
  len = (((int)lenbuf[0]) << 8) | lenbuf[1];
  if (len > bufsize) {
    return -1;
  }
  t = fread(buf, 1, len, f);
  if ((t != len) || (buf[len - 1] != 0)) {
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<readstring>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
lea    -0x22(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
mov    $0x2,%edx
call   1030 <fread@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x2,-0x2c(%rbp)
je     1153 <readstring+0x43>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cc <readstring+0xbc>
movsbl -0x22(%rbp),%eax
shl    $0x8,%eax
movsbl -0x21(%rbp),%ecx
or     %ecx,%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x14(%rbp),%eax
jle    117b <readstring+0x6b>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cc <readstring+0xbc>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x28(%rbp),%eax
jne    11b9 <readstring+0xa9>
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
je     11c5 <readstring+0xb5>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cc <readstring+0xbc>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readstring>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%rbx
mov    %esi,%r15d
mov    %rdi,%r14
lea    0xe(%rsp),%rdi
mov    $0x1,%esi
mov    $0x2,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    $0xffffffff,%r12d
cmp    $0x2,%eax
jne    117f <readstring+0x6f>
movsbq 0xe(%rsp),%rax
shl    $0x8,%rax
movsbq 0xf(%rsp),%rbp
or     %rax,%rbp
cmp    %r15d,%ebp
jg     117f <readstring+0x6f>
mov    %r14,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
cmp    %ebp,%eax
jne    117f <readstring+0x6f>
xor    %eax,%eax
cmp    -0x1(%rbp,%r14,1),%al
sbb    %r12d,%r12d
mov    %r12d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @readstring(i8* nocapture %0, i32 %1, i32* nocapture %2) {
%4 = alloca [2 x i8]
%5 = getelementptr inbounds [2 x i8], [2 x i8]* %4, i64 0, i64 0
%6 = call i32 @fread(i8* %5, i32 1, i32 2, i32* %2)
%7 = icmp eq i32 %6, 2
br i1 %7, label %8, label %27
8: ; preds = %3
%9 = load i8, i8* %5
%10 = sext i8 %9 to i32
%11 = shl nsw i32 %10, 8
%12 = getelementptr inbounds [2 x i8], [2 x i8]* %4, i64 0, i64 1
%13 = load i8, i8* %12
%14 = sext i8 %13 to i32
%15 = or i32 %11, %14
%16 = icmp sgt i32 %15, %1
br i1 %16, label %27, label %17
17: ; preds = %8
%18 = tail call i32 @fread(i8* %0, i32 1, i32 %15, i32* %2)
%19 = icmp eq i32 %18, %15
br i1 %19, label %20, label %27
20: ; preds = %17
%21 = add nsw i32 %15, -1
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %0, i64 %22
%24 = load i8, i8* %23
%25 = icmp ne i8 %24, 0
%26 = sext i1 %25 to i32
br label %27
27: ; preds = %20, %17, %8, %3
%28 = phi i32 [ -1, %3 ], [ -1, %8 ], [ -1, %17 ], [ %26, %20 ]
ret i32 %28
}