#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 u_int64_t;
typedef int u_char;
struct sshbuf {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ memcpy(int *, void const *, size_t);
int sshbuf_reserve(struct sshbuf *, size_t, int **);
int sshbuf_put_string(struct sshbuf *buf, const void *v, size_t len) {
  u_char *d;
  int r;
  if (len > 0x8000000 - 4) {
    ;
    return -9;
  }
  if ((r = sshbuf_reserve(buf, len + 4, &d)) < 0)
    return r;
  do {
    ((u_char *)(d))[0] = (((u_int64_t)(len)) >> 24) & 0xff;
    ((u_char *)(d))[1] = (((u_int64_t)(len)) >> 16) & 0xff;
    ((u_char *)(d))[2] = (((u_int64_t)(len)) >> 8) & 0xff;
    ((u_char *)(d))[3] = ((u_int64_t)(len)) & 0xff;
  } while (0);
  if (len != 0)
    memcpy(d + 4, v, len);
  return 0;
}


x86 assembly (clang, -O0):
<sshbuf_put_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x7fffffc,-0x20(%rbp)
07
jbe    114e <sshbuf_put_string+0x2e>
movl   $0xfffffff7,-0x4(%rbp)
jmp    11fa <sshbuf_put_string+0xda>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
add    $0x4,%rsi
lea    -0x28(%rbp),%rdx
call   1030 <sshbuf_reserve@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jge    117a <sshbuf_put_string+0x5a>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11fa <sshbuf_put_string+0xda>
jmp    117f <sshbuf_put_string+0x5f>
mov    -0x20(%rbp),%rax
mov    %eax,%ecx
sar    $0x18,%ecx
and    $0xff,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
mov    %eax,%ecx
sar    $0x10,%ecx
and    $0xff,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x20(%rbp),%rax
mov    %eax,%ecx
sar    $0x8,%ecx
and    $0xff,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0x20(%rbp),%rax
mov    %eax,%ecx
and    $0xff,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,0xc(%rax)
cmpq   $0x0,-0x20(%rbp)
je     11f3 <sshbuf_put_string+0xd3>
mov    -0x28(%rbp),%rdi
add    $0x10,%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <memcpy@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sshbuf_put_string>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    $0xfffffff7,%ebp
cmp    $0x7fffffc,%rdx
ja     118b <sshbuf_put_string+0x6b>
mov    %rdx,%rbx
mov    %rsi,%r14
lea    0x4(%rdx),%rsi
lea    0x8(%rsp),%rdx
call   1030 <sshbuf_reserve@plt>
test   %eax,%eax
js     1189 <sshbuf_put_string+0x69>
mov    %ebx,%eax
shr    $0x18,%eax
mov    0x8(%rsp),%rdi
mov    %eax,(%rdi)
mov    %ebx,%eax
shr    $0x10,%eax
movzbl %al,%eax
mov    %eax,0x4(%rdi)
movzbl %bh,%eax
mov    %eax,0x8(%rdi)
movzbl %bl,%eax
mov    %eax,0xc(%rdi)
xor    %ebp,%ebp
test   %rbx,%rbx
je     118b <sshbuf_put_string+0x6b>
add    $0x10,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1040 <memcpy@plt>
jmp    118b <sshbuf_put_string+0x6b>
mov    %eax,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sshbuf = type { i32 }
define i32 @sshbuf_put_string(%struct.sshbuf* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca %struct.sshbuf*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32*
%9 = alloca i32
store %struct.sshbuf* %0, %struct.sshbuf** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%10 = load i64, i64* %7
%11 = icmp ugt i64 %10, 134217724
br i1 %11, label %12, label %13
12: ; preds = %3
store i32 -9, i32* %4
br label %56
13: ; preds = %3
%14 = load %struct.sshbuf*, %struct.sshbuf** %5
%15 = load i64, i64* %7
%16 = add i64 %15, 4
%17 = call i32 @sshbuf_reserve(%struct.sshbuf* %14, i64 %16, i32** %8)
store i32 %17, i32* %9
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %13
%20 = load i32, i32* %9
store i32 %20, i32* %4
br label %56
21: ; preds = %13
br label %22
22: ; preds = %21
%23 = load i64, i64* %7
%24 = trunc i64 %23 to i32
%25 = ashr i32 %24, 24
%26 = and i32 %25, 255
%27 = load i32*, i32** %8
%28 = getelementptr inbounds i32, i32* %27, i64 0
store i32 %26, i32* %28
%29 = load i64, i64* %7
%30 = trunc i64 %29 to i32
%31 = ashr i32 %30, 16
%32 = and i32 %31, 255
%33 = load i32*, i32** %8
%34 = getelementptr inbounds i32, i32* %33, i64 1
store i32 %32, i32* %34
%35 = load i64, i64* %7
%36 = trunc i64 %35 to i32
%37 = ashr i32 %36, 8
%38 = and i32 %37, 255
%39 = load i32*, i32** %8
%40 = getelementptr inbounds i32, i32* %39, i64 2
store i32 %38, i32* %40
%41 = load i64, i64* %7
%42 = trunc i64 %41 to i32
%43 = and i32 %42, 255
%44 = load i32*, i32** %8
%45 = getelementptr inbounds i32, i32* %44, i64 3
store i32 %43, i32* %45
br label %46
46: ; preds = %22
%47 = load i64, i64* %7
%48 = icmp ne i64 %47, 0
br i1 %48, label %49, label %55
49: ; preds = %46
%50 = load i32*, i32** %8
%51 = getelementptr inbounds i32, i32* %50, i64 4
%52 = load i8*, i8** %6
%53 = load i64, i64* %7
%54 = call i32 @memcpy(i32* %51, i8* %52, i64 %53)
br label %55
55: ; preds = %49, %46
store i32 0, i32* %4
br label %56
56: ; preds = %55, %19, %12
%57 = load i32, i32* %4
ret i32 %57
}

LLVM IR (-O2):
%struct.sshbuf = type { i32 }
define i32 @sshbuf_put_string(%struct.sshbuf* %0, i8* %1, i64 %2) {
%4 = alloca i32*
%5 = bitcast i32** %4 to i8*
%6 = icmp ugt i64 %2, 134217724
br i1 %6, label %27, label %7
7: ; preds = %3
%8 = add nuw nsw i64 %2, 4
%9 = call i32 @sshbuf_reserve(%struct.sshbuf* %0, i64 %8, i32** %4)
%10 = icmp slt i32 %9, 0
br i1 %10, label %27, label %11
11: ; preds = %7
%12 = trunc i64 %2 to i32
%13 = lshr i32 %12, 24
%14 = load i32*, i32** %4
store i32 %13, i32* %14
%15 = lshr i32 %12, 16
%16 = and i32 %15, 255
%17 = getelementptr inbounds i32, i32* %14, i64 1
store i32 %16, i32* %17
%18 = lshr i32 %12, 8
%19 = and i32 %18, 255
%20 = getelementptr inbounds i32, i32* %14, i64 2
store i32 %19, i32* %20
%21 = and i32 %12, 255
%22 = getelementptr inbounds i32, i32* %14, i64 3
store i32 %21, i32* %22
%23 = icmp eq i64 %2, 0
br i1 %23, label %27, label %24
24: ; preds = %11
%25 = getelementptr inbounds i32, i32* %14, i64 4
%26 = call i32 @memcpy(i32* %25, i8* %1, i64 %2)
br label %27
27: ; preds = %11, %24, %7, %3
%28 = phi i32 [ -9, %3 ], [ %9, %7 ], [ 0, %24 ], [ 0, %11 ]
ret i32 %28
}