#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 /*<<< orphan*/ memcpy(unsigned char *, char const *, size_t);
size_t strlen(char const *);
int encode_snmp_string(unsigned char *buffer, const char *string_value) {
  size_t length;
  length = strlen(string_value);
  if (length > 0xFFFF) {
    return -1;
  }
  *buffer++ = 0x04;
  if (length > 0xFF) {
    *buffer++ = 0x82;
    *buffer++ = (length >> 8) & 0xFF;
    *buffer++ = length & 0xFF;
  } else if (length > 0x7F) {
    *buffer++ = 0x81;
    *buffer++ = length & 0xFF;
  } else {
    *buffer++ = length & 0x7F;
  }
  memcpy(buffer, string_value, length);
  return 0;
}


x86 assembly (clang, -O0):
<encode_snmp_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0xffff,-0x20(%rbp)
jbe    1157 <encode_snmp_string+0x37>
movl   $0xffffffff,-0x4(%rbp)
jmp    1243 <encode_snmp_string+0x123>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movb   $0x4,(%rax)
cmpq   $0xff,-0x20(%rbp)
jbe    11cc <encode_snmp_string+0xac>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movb   $0x82,(%rax)
mov    -0x20(%rbp),%rax
shr    $0x8,%rax
and    $0xff,%rax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
mov    -0x20(%rbp),%rax
and    $0xff,%rax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
jmp    122b <encode_snmp_string+0x10b>
cmpq   $0x7f,-0x20(%rbp)
jbe    120b <encode_snmp_string+0xeb>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movb   $0x81,(%rax)
mov    -0x20(%rbp),%rax
and    $0xff,%rax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
jmp    1226 <encode_snmp_string+0x106>
mov    -0x20(%rbp),%rax
and    $0x7f,%rax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
jmp    122b <encode_snmp_string+0x10b>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <memcpy@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<encode_snmp_string>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%rbp
mov    %rsi,%rdi
call   1030 <strlen@plt>
mov    $0xffffffff,%ecx
cmp    $0xffff,%rax
ja     1191 <encode_snmp_string+0x71>
movb   $0x4,0x0(%rbp)
cmp    $0x100,%rax
jb     115c <encode_snmp_string+0x3c>
movb   $0x82,0x1(%rbp)
lea    0x3(%rbp),%rcx
mov    %ah,0x2(%rbp)
mov    $0x4,%edx
jmp    117c <encode_snmp_string+0x5c>
cmp    $0x80,%rax
jb     1173 <encode_snmp_string+0x53>
lea    0x2(%rbp),%rcx
movb   $0x81,0x1(%rbp)
mov    $0x3,%edx
jmp    117c <encode_snmp_string+0x5c>
lea    0x1(%rbp),%rcx
mov    $0x2,%edx
add    %rdx,%rbp
mov    %al,(%rcx)
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1040 <memcpy@plt>
xor    %ecx,%ecx
mov    %ecx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @encode_snmp_string(i8* %0, i8* %1) {
%3 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%4 = icmp ugt i64 %3, 65535
br i1 %4, label %23, label %5
5: ; preds = %2
%6 = getelementptr inbounds i8, i8* %0, i64 1
store i8 4, i8* %0
%7 = icmp ugt i64 %3, 255
br i1 %7, label %8, label %13
8: ; preds = %5
%9 = getelementptr inbounds i8, i8* %0, i64 2
store i8 -126, i8* %6
%10 = lshr i64 %3, 8
%11 = trunc i64 %10 to i8
%12 = getelementptr inbounds i8, i8* %0, i64 3
store i8 %11, i8* %9
br label %17
13: ; preds = %5
%14 = icmp ugt i64 %3, 127
br i1 %14, label %15, label %17
15: ; preds = %13
%16 = getelementptr inbounds i8, i8* %0, i64 2
store i8 -127, i8* %6
br label %17
17: ; preds = %13, %15, %8
%18 = phi i64 [ 3, %15 ], [ 4, %8 ], [ 2, %13 ]
%19 = phi i8* [ %16, %15 ], [ %12, %8 ], [ %6, %13 ]
%20 = trunc i64 %3 to i8
%21 = getelementptr inbounds i8, i8* %0, i64 %18
store i8 %20, i8* %19
%22 = tail call i32 @memcpy(i8* %21, i8* %1, i64 %3)
br label %23
23: ; preds = %2, %17
%24 = phi i32 [ 0, %17 ], [ -1, %2 ]
ret i32 %24
}