#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 /*<<< orphan*/ clean_up(char *);
int /*<<< orphan*/ r_read(int /*<<< orphan*/ *, ...);
int /*<<< orphan*/ xxxx(char *, short);
void read_string(char *s, FILE *fp, size_t len) {
  short n;
  r_read(fp, &n, sizeof(short));
  if (n <= 0 || (size_t)(unsigned short)n > len) {
    clean_up("read_string: corrupt game file");
  }
  r_read(fp, s, n);
  xxxx(s, n);
  s[n - 1] = 0;
}


x86 assembly (clang, -O0):
<read_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x1a(%rbp),%rsi
mov    $0x2,%edx
mov    $0x0,%al
call   1050 <r_read@plt>
movswl -0x1a(%rbp),%eax
cmp    $0x0,%eax
jle    1173 <read_string+0x43>
movzwl -0x1a(%rbp),%eax
cmp    -0x18(%rbp),%rax
jbe    117f <read_string+0x4f>
lea    0xe86(%rip),%rdi
call   1030 <clean_up@plt>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
movswl -0x1a(%rbp),%edx
mov    $0x0,%al
call   1050 <r_read@plt>
mov    -0x8(%rbp),%rdi
movswl -0x1a(%rbp),%esi
call   1040 <xxxx@plt>
mov    -0x8(%rbp),%rax
movswl -0x1a(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_string>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0xe(%rsp),%rsi
mov    $0x2,%edx
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <r_read@plt>
movzwl 0xe(%rsp),%eax
test   %ax,%ax
jle    1168 <read_string+0x38>
movzwl %ax,%ecx
cmp    %r15,%rcx
jbe    1179 <read_string+0x49>
lea    0xe91(%rip),%rdi
call   1030 <clean_up@plt>
movzwl 0xe(%rsp),%eax
movswl %ax,%edx
mov    %r14,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <r_read@plt>
movswl 0xe(%rsp),%esi
mov    %rbx,%rdi
call   1040 <xxxx@plt>
movswq 0xe(%rsp),%rax
movb   $0x0,-0x1(%rax,%rbx,1)
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [31 x i8] c"read_string: corrupt game file\00"
define void @read_string(i8* %0, i32* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i64
%7 = alloca i16
store i8* %0, i8** %4
store i32* %1, i32** %5
store i64 %2, i64* %6
%8 = load i32*, i32** %5
%9 = call i32 (i32*, ...) @r_read(i32* %8, i16* %7, i64 2)
%10 = load i16, i16* %7
%11 = sext i16 %10 to i32
%12 = icmp sle i32 %11, 0
br i1 %12, label %18, label %13
13: ; preds = %3
%14 = load i16, i16* %7
%15 = zext i16 %14 to i64
%16 = load i64, i64* %6
%17 = icmp ugt i64 %15, %16
br i1 %17, label %18, label %20
18: ; preds = %13, %3
%19 = call i32 @clean_up(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
br label %20
20: ; preds = %18, %13
%21 = load i32*, i32** %5
%22 = load i8*, i8** %4
%23 = load i16, i16* %7
%24 = sext i16 %23 to i32
%25 = call i32 (i32*, ...) @r_read(i32* %21, i8* %22, i32 %24)
%26 = load i8*, i8** %4
%27 = load i16, i16* %7
%28 = call i32 @xxxx(i8* %26, i16 signext %27)
%29 = load i8*, i8** %4
%30 = load i16, i16* %7
%31 = sext i16 %30 to i32
%32 = sub nsw i32 %31, 1
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %29, i64 %33
store i8 0, i8* %34
ret void
}

LLVM IR (-O2):
@.str = [31 x i8] c"read_string: corrupt game file\00"
define void @read_string(i8* %0, i32* %1, i64 %2) {
%4 = alloca i16
%5 = bitcast i16* %4 to i8*
%6 = call i32 (i32*, ...) @r_read(i32* %1, i16* %4, i64 2)
%7 = load i16, i16* %4
%8 = icmp slt i16 %7, 1
%9 = zext i16 %7 to i64
%10 = icmp ugt i64 %9, %2
%11 = or i1 %8, %10
br i1 %11, label %12, label %15
12: ; preds = %3
%13 = call i32 @clean_up(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
%14 = load i16, i16* %4
br label %15
15: ; preds = %3, %12
%16 = phi i16 [ %7, %3 ], [ %14, %12 ]
%17 = sext i16 %16 to i32
%18 = call i32 (i32*, ...) @r_read(i32* %1, i8* %0, i32 %17)
%19 = load i16, i16* %4
%20 = call i32 @xxxx(i8* %0, i16 signext %19)
%21 = load i16, i16* %4
%22 = sext i16 %21 to i64
%23 = add nsw i64 %22, -1
%24 = getelementptr inbounds i8, i8* %0, i64 %23
store i8 0, i8* %24
ret void
}