#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*/ u_int32_t;
typedef int /*<<< orphan*/ u_char;
struct sshbuf {
  int dummy;
};

/* Variables and functions */
int sshbuf_get(struct sshbuf *, int /*<<< orphan*/ *, int /*<<< orphan*/);
int sshbuf_get_u32(struct sshbuf *, int /*<<< orphan*/ *);
int sshbuf_peek_string_direct(struct sshbuf *, int /*<<< orphan*/ *,
                              int /*<<< orphan*/ *);
int sshbuf_reserve(struct sshbuf *, int /*<<< orphan*/, int /*<<< orphan*/ **);
int sshbuf_get_stringb(struct sshbuf *buf, struct sshbuf *v) {
  u_int32_t len;
  u_char *p;
  int r;
  if ((r = sshbuf_peek_string_direct(buf, NULL, NULL)) != 0 ||
      (r = sshbuf_get_u32(buf, &len)) != 0 ||
      (r = sshbuf_reserve(v, len, &p)) != 0 ||
      (r = sshbuf_get(buf, p, len)) != 0)
    return r;
  return 0;
}


x86 assembly (clang, -O0):
<sshbuf_get_stringb>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   1050 <sshbuf_peek_string_direct@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jne    11bd <sshbuf_get_stringb+0x7d>
mov    -0x10(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
call   1060 <sshbuf_get_u32@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jne    11bd <sshbuf_get_stringb+0x7d>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
lea    -0x28(%rbp),%rdx
call   1030 <sshbuf_reserve@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jne    11bd <sshbuf_get_stringb+0x7d>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1040 <sshbuf_get@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
je     11c8 <sshbuf_get_stringb+0x88>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11cf <sshbuf_get_stringb+0x8f>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sshbuf_get_stringb>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
xor    %esi,%esi
xor    %edx,%edx
call   1050 <sshbuf_peek_string_direct@plt>
test   %eax,%eax
jne    1191 <sshbuf_get_stringb+0x51>
lea    0xc(%rsp),%rsi
mov    %rbx,%rdi
call   1060 <sshbuf_get_u32@plt>
test   %eax,%eax
jne    1191 <sshbuf_get_stringb+0x51>
mov    0xc(%rsp),%esi
lea    0x10(%rsp),%rdx
mov    %r14,%rdi
call   1030 <sshbuf_reserve@plt>
test   %eax,%eax
jne    1191 <sshbuf_get_stringb+0x51>
mov    0x10(%rsp),%rsi
mov    0xc(%rsp),%edx
mov    %rbx,%rdi
call   1040 <sshbuf_get@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.sshbuf = type { i32 }
define i32 @sshbuf_get_stringb(%struct.sshbuf* %0, %struct.sshbuf* %1) {
%3 = alloca i32
%4 = alloca %struct.sshbuf*
%5 = alloca %struct.sshbuf*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
store %struct.sshbuf* %0, %struct.sshbuf** %4
store %struct.sshbuf* %1, %struct.sshbuf** %5
%9 = load %struct.sshbuf*, %struct.sshbuf** %4
%10 = call i32 @sshbuf_peek_string_direct(%struct.sshbuf* %9, i32* null, i32* null)
store i32 %10, i32* %8
%11 = icmp ne i32 %10, 0
br i1 %11, label %27, label %12
12: ; preds = %2
%13 = load %struct.sshbuf*, %struct.sshbuf** %4
%14 = call i32 @sshbuf_get_u32(%struct.sshbuf* %13, i32* %6)
store i32 %14, i32* %8
%15 = icmp ne i32 %14, 0
br i1 %15, label %27, label %16
16: ; preds = %12
%17 = load %struct.sshbuf*, %struct.sshbuf** %5
%18 = load i32, i32* %6
%19 = call i32 @sshbuf_reserve(%struct.sshbuf* %17, i32 %18, i32** %7)
store i32 %19, i32* %8
%20 = icmp ne i32 %19, 0
br i1 %20, label %27, label %21
21: ; preds = %16
%22 = load %struct.sshbuf*, %struct.sshbuf** %4
%23 = load i32*, i32** %7
%24 = load i32, i32* %6
%25 = call i32 @sshbuf_get(%struct.sshbuf* %22, i32* %23, i32 %24)
store i32 %25, i32* %8
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %21, %16, %12, %2
%28 = load i32, i32* %8
store i32 %28, i32* %3
br label %30
29: ; preds = %21
store i32 0, i32* %3
br label %30
30: ; preds = %29, %27
%31 = load i32, i32* %3
ret i32 %31
}

LLVM IR (-O2):
%struct.sshbuf = type { i32 }
define i32 @sshbuf_get_stringb(%struct.sshbuf* %0, %struct.sshbuf* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = bitcast i32* %3 to i8*
%6 = bitcast i32** %4 to i8*
%7 = tail call i32 @sshbuf_peek_string_direct(%struct.sshbuf* %0, i32* null, i32* null)
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %20
9: ; preds = %2
%10 = call i32 @sshbuf_get_u32(%struct.sshbuf* %0, i32* %3)
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %20
12: ; preds = %9
%13 = load i32, i32* %3
%14 = call i32 @sshbuf_reserve(%struct.sshbuf* %1, i32 %13, i32** %4)
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %12
%17 = load i32*, i32** %4
%18 = load i32, i32* %3
%19 = call i32 @sshbuf_get(%struct.sshbuf* %0, i32* %17, i32 %18)
br label %20
20: ; preds = %16, %2, %9, %12
%21 = phi i32 [ %7, %2 ], [ %10, %9 ], [ %14, %12 ], [ %19, %16 ]
ret i32 %21
}