#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 */
struct rpc_buffer {
  int dummy;
};

/* Variables and functions */
scalar_t__ buffer_read_char(struct rpc_buffer *, char *);
scalar_t__ buffer_read_data(struct rpc_buffer *, int, char **);
int /*<<< orphan*/ buffer_read_pad(struct rpc_buffer *);
int /*<<< orphan*/ memcpy(int *, char *, int);
int buffer_read_string(struct rpc_buffer *buf, int *len, char **x) {
  unsigned char c;
  if (buffer_read_char(buf, (char *)&c) < 0) {
    return -1;
  }
  *len = c;
  if (*len == 255) {
    return -1;
  }
  if (*len == 254) {
    char *t;
    *len = 0;
    if (buffer_read_data(buf, 3, &t) < 0) {
      return -1;
    }
    memcpy(len, t, 3);
  }
  if (buffer_read_data(buf, *len, x) < 0) {
    return -1;
  }
  buffer_read_pad(buf);
  return 1;
}


x86 assembly (clang, -O0):
<buffer_read_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x21(%rbp),%rsi
call   1030 <buffer_read_char@plt>
cmp    $0x0,%rax
jge    1177 <buffer_read_string+0x37>
movl   $0xffffffff,-0x4(%rbp)
jmp    122a <buffer_read_string+0xea>
movzbl -0x21(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
cmpl   $0xff,(%rax)
jne    119d <buffer_read_string+0x5d>
movl   $0xffffffff,-0x4(%rbp)
jmp    122a <buffer_read_string+0xea>
mov    -0x18(%rbp),%rax
cmpl   $0xfe,(%rax)
jne    11f1 <buffer_read_string+0xb1>
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    $0x3,%esi
lea    -0x30(%rbp),%rdx
call   1040 <buffer_read_data@plt>
cmp    $0x0,%rax
jge    11df <buffer_read_string+0x9f>
movl   $0xffffffff,-0x4(%rbp)
jmp    122a <buffer_read_string+0xea>
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    $0x3,%edx
call   1050 <memcpy@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
mov    -0x20(%rbp),%rdx
call   1040 <buffer_read_data@plt>
cmp    $0x0,%rax
jge    121a <buffer_read_string+0xda>
movl   $0xffffffff,-0x4(%rbp)
jmp    122a <buffer_read_string+0xea>
mov    -0x10(%rbp),%rdi
call   1060 <buffer_read_pad@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<buffer_read_string>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r14
mov    %rsi,%rbp
mov    %rdi,%rbx
lea    0xf(%rsp),%rsi
call   1030 <buffer_read_char@plt>
mov    $0xffffffff,%r15d
test   %rax,%rax
js     11cf <buffer_read_string+0x8f>
movzbl 0xf(%rsp),%esi
mov    %esi,0x0(%rbp)
cmp    $0xff,%esi
je     11cf <buffer_read_string+0x8f>
cmp    $0xfe,%sil
jne    11b1 <buffer_read_string+0x71>
movl   $0x0,0x0(%rbp)
lea    0x10(%rsp),%rdx
mov    %rbx,%rdi
mov    $0x3,%esi
call   1040 <buffer_read_data@plt>
test   %rax,%rax
js     11cf <buffer_read_string+0x8f>
mov    0x10(%rsp),%rsi
mov    %rbp,%rdi
mov    $0x3,%edx
call   1050 <memcpy@plt>
mov    0x0(%rbp),%esi
mov    %rbx,%rdi
mov    %r14,%rdx
call   1040 <buffer_read_data@plt>
test   %rax,%rax
js     11cf <buffer_read_string+0x8f>
mov    %rbx,%rdi
call   1060 <buffer_read_pad@plt>
mov    $0x1,%r15d
mov    %r15d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.rpc_buffer = type { i32 }
define i32 @buffer_read_string(%struct.rpc_buffer* %0, i32* %1, i8** %2) {
%4 = alloca i32
%5 = alloca %struct.rpc_buffer*
%6 = alloca i32*
%7 = alloca i8**
%8 = alloca i8
%9 = alloca i8*
store %struct.rpc_buffer* %0, %struct.rpc_buffer** %5
store i32* %1, i32** %6
store i8** %2, i8*** %7
%10 = load %struct.rpc_buffer*, %struct.rpc_buffer** %5
%11 = call i64 @buffer_read_char(%struct.rpc_buffer* %10, i8* %8)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %3
store i32 -1, i32* %4
br label %47
14: ; preds = %3
%15 = load i8, i8* %8
%16 = zext i8 %15 to i32
%17 = load i32*, i32** %6
store i32 %16, i32* %17
%18 = load i32*, i32** %6
%19 = load i32, i32* %18
%20 = icmp eq i32 %19, 255
br i1 %20, label %21, label %22
21: ; preds = %14
store i32 -1, i32* %4
br label %47
22: ; preds = %14
%23 = load i32*, i32** %6
%24 = load i32, i32* %23
%25 = icmp eq i32 %24, 254
br i1 %25, label %26, label %36
26: ; preds = %22
%27 = load i32*, i32** %6
store i32 0, i32* %27
%28 = load %struct.rpc_buffer*, %struct.rpc_buffer** %5
%29 = call i64 @buffer_read_data(%struct.rpc_buffer* %28, i32 3, i8** %9)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %26
store i32 -1, i32* %4
br label %47
32: ; preds = %26
%33 = load i32*, i32** %6
%34 = load i8*, i8** %9
%35 = call i32 @memcpy(i32* %33, i8* %34, i32 3)
br label %36
36: ; preds = %32, %22
%37 = load %struct.rpc_buffer*, %struct.rpc_buffer** %5
%38 = load i32*, i32** %6
%39 = load i32, i32* %38
%40 = load i8**, i8*** %7
%41 = call i64 @buffer_read_data(%struct.rpc_buffer* %37, i32 %39, i8** %40)
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %44
43: ; preds = %36
store i32 -1, i32* %4
br label %47
44: ; preds = %36
%45 = load %struct.rpc_buffer*, %struct.rpc_buffer** %5
%46 = call i32 @buffer_read_pad(%struct.rpc_buffer* %45)
store i32 1, i32* %4
br label %47
47: ; preds = %44, %43, %31, %21, %13
%48 = load i32, i32* %4
ret i32 %48
}

LLVM IR (-O2):
%struct.rpc_buffer = type { i32 }
define i32 @buffer_read_string(%struct.rpc_buffer* %0, i32* %1, i8** %2) {
%4 = alloca i8
%5 = alloca i8*
%6 = call i64 @buffer_read_char(%struct.rpc_buffer* %0, i8* %4)
%7 = icmp slt i64 %6, 0
br i1 %7, label %26, label %8
8: ; preds = %3
%9 = load i8, i8* %4
%10 = zext i8 %9 to i32
store i32 %10, i32* %1
switch i8 %9, label %19 [
i8 -1, label %26
i8 -2, label %11
]
11: ; preds = %8
%12 = bitcast i8** %5 to i8*
store i32 0, i32* %1
%13 = call i64 @buffer_read_data(%struct.rpc_buffer* %0, i32 3, i8** %5)
%14 = icmp slt i64 %13, 0
br i1 %14, label %25, label %15
15: ; preds = %11
%16 = load i8*, i8** %5
%17 = call i32 @memcpy(i32* %1, i8* %16, i32 3)
%18 = load i32, i32* %1
br label %19
19: ; preds = %15, %8
%20 = phi i32 [ %18, %15 ], [ %10, %8 ]
%21 = call i64 @buffer_read_data(%struct.rpc_buffer* %0, i32 %20, i8** %2)
%22 = icmp slt i64 %21, 0
br i1 %22, label %26, label %23
23: ; preds = %19
%24 = call i32 @buffer_read_pad(%struct.rpc_buffer* %0)
br label %26
25: ; preds = %11
br label %26
26: ; preds = %19, %25, %8, %3, %23
%27 = phi i32 [ 1, %23 ], [ -1, %3 ], [ -1, %8 ], [ -1, %25 ], [ -1, %19 ]
ret i32 %27
}