#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 EINVAL;
int /*<<< orphan*/ memcpy(char *, char const *, unsigned short);
int /*<<< orphan*/ read_word(char const *, unsigned short *);
int read_string(const char *ptr, char *value, int size) {
  ;
  unsigned short len = 0;
  ptr += read_word(ptr, &len);
  if (len >= size)
    return -EINVAL;
  value[len] = '\0';
  if (len) {
    memcpy(value, ptr, len);
    return len + sizeof(short);
  }
  return 4;
}


x86 assembly (clang, -O0):
<read_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movw   $0x0,-0x1e(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x1e(%rbp),%rsi
call   1040 <read_word@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
movzwl -0x1e(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jl     1176 <read_string+0x56>
xor    %eax,%eax
mov    0x2e74(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11b8 <read_string+0x98>
mov    -0x18(%rbp),%rax
movzwl -0x1e(%rbp),%ecx
movb   $0x0,(%rax,%rcx,1)
cmpw   $0x0,-0x1e(%rbp)
je     11b1 <read_string+0x91>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x1e(%rbp),%ax
movzwl %ax,%edx
call   1030 <memcpy@plt>
movzwl -0x1e(%rbp),%eax
add    $0x2,%rax
mov    %eax,-0x4(%rbp)
jmp    11b8 <read_string+0x98>
movl   $0x4,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_string>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edx,%ebp
mov    %rsi,%r14
mov    %rdi,%rbx
movw   $0x0,0xe(%rsp)
lea    0xe(%rsp),%rsi
call   1040 <read_word@plt>
movzwl 0xe(%rsp),%edx
cmp    %ebp,%edx
jge    116e <read_string+0x4e>
movb   $0x0,(%r14,%rdx,1)
test   %rdx,%rdx
je     117b <read_string+0x5b>
cltq
add    %rax,%rbx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <memcpy@plt>
movzwl 0xe(%rsp),%eax
add    $0x2,%eax
jmp    1180 <read_string+0x60>
mov    0x2e6b(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
jmp    1180 <read_string+0x60>
mov    $0x4,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@EINVAL = global i32 0
define i32 @read_string(i8* %0, i8* %1, i32 %2) {
%4 = alloca i16
%5 = bitcast i16* %4 to i8*
store i16 0, i16* %4
%6 = call i32 @read_word(i8* %0, i16* %4)
%7 = sext i32 %6 to i64
%8 = getelementptr inbounds i8, i8* %0, i64 %7
%9 = load i16, i16* %4
%10 = zext i16 %9 to i32
%11 = icmp slt i32 %10, %2
br i1 %11, label %15, label %12
12: ; preds = %3
%13 = load i32, i32* @EINVAL
%14 = sub nsw i32 0, %13
br label %24
15: ; preds = %3
%16 = zext i16 %9 to i64
%17 = getelementptr inbounds i8, i8* %1, i64 %16
store i8 0, i8* %17
%18 = icmp eq i16 %9, 0
br i1 %18, label %24, label %19
19: ; preds = %15
%20 = call i32 @memcpy(i8* %1, i8* %8, i16 zeroext %9)
%21 = load i16, i16* %4
%22 = zext i16 %21 to i32
%23 = add nuw nsw i32 %22, 2
br label %24
24: ; preds = %15, %19, %12
%25 = phi i32 [ %14, %12 ], [ %23, %19 ], [ 4, %15 ]
ret i32 %25
}