#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 uint16_t;

/* Variables and functions */
int /*<<< orphan*/ bcopy(unsigned char const *, unsigned char *, int);
int ntohs(int);
int stun_get_command_message_len_str(unsigned char *, size_t);
int /*<<< orphan*/ stun_set_command_message_len_str(unsigned char *, int);
int stun_attr_add_str(unsigned char *buf, size_t *len, uint16_t attr,
                      const unsigned char *avalue, int alen) {
  if (alen < 0)
    alen = 0;
  unsigned char tmp[1];
  if (!avalue) {
    alen = 0;
    avalue = tmp;
  }
  int clen = stun_get_command_message_len_str(buf, *len);
  int newlen = clen + 4 + alen;
  int newlenrem4 = newlen & 0x00000003;
  if (newlenrem4) {
    newlen = newlen + (4 - newlenrem4);
  }
  if (newlen >= (65507))
    return -1;
  else {
    unsigned char *attr_start = buf + clen;
    uint16_t *attr_start_16t = (uint16_t *)attr_start;
    stun_set_command_message_len_str(buf, newlen);
    *len = newlen;
    attr_start_16t[0] = ntohs(attr);
    attr_start_16t[1] = ntohs(alen);
    if (alen > 0)
      bcopy((avalue), (attr_start + 4), (alen));
    return 0;
  }
}


x86 assembly (clang, -O0):
<stun_attr_add_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8d,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    116c <stun_attr_add_str+0x2c>
movl   $0x0,-0x2c(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1186 <stun_attr_add_str+0x46>
movl   $0x0,-0x2c(%rbp)
lea    -0x2d(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
call   1060 <stun_get_command_message_len_str@plt>
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%eax
add    $0x4,%eax
add    -0x2c(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%eax
and    $0x3,%eax
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
je     11c8 <stun_attr_add_str+0x88>
mov    -0x38(%rbp),%eax
mov    $0x4,%ecx
sub    -0x3c(%rbp),%ecx
add    %ecx,%eax
mov    %eax,-0x38(%rbp)
cmpl   $0xffe3,-0x38(%rbp)
jl     11e1 <stun_attr_add_str+0xa1>
movl   $0xffffffff,-0x4(%rbp)
jmp    1255 <stun_attr_add_str+0x115>
mov    -0x10(%rbp),%rax
movslq -0x34(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x38(%rbp),%esi
call   1030 <stun_set_command_message_len_str@plt>
movslq -0x38(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x1c(%rbp),%edi
call   1050 <ntohs@plt>
mov    %eax,%ecx
mov    -0x50(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x2c(%rbp),%edi
call   1050 <ntohs@plt>
mov    %eax,%ecx
mov    -0x50(%rbp),%rax
mov    %ecx,0x4(%rax)
cmpl   $0x0,-0x2c(%rbp)
jle    124e <stun_attr_add_str+0x10e>
mov    -0x28(%rbp),%rdi
mov    -0x48(%rbp),%rsi
add    $0x4,%rsi
mov    -0x2c(%rbp),%edx
call   1040 <bcopy@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<stun_attr_add_str>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8d,%ebx
xor    %eax,%eax
test   %r8d,%r8d
cmovs  %eax,%ebx
mov    %edx,%r12d
mov    %rsi,%r13
mov    %rdi,%r15
test   %rcx,%rcx
cmove  %eax,%ebx
lea    0x3(%rsp),%r14
cmovne %rcx,%r14
mov    (%rsi),%rsi
call   1060 <stun_get_command_message_len_str@plt>
lea    (%rbx,%rax,1),%ecx
add    $0x4,%ecx
and    $0x3,%ecx
mov    $0x4,%edx
sub    %ecx,%edx
test   %ecx,%ecx
cmove  %ecx,%edx
lea    (%rbx,%rax,1),%ecx
lea    (%rdx,%rcx,1),%ebp
add    $0x4,%ebp
mov    $0xffffffff,%ecx
cmp    $0xffe2,%ebp
jg     11ec <stun_attr_add_str+0xac>
mov    %r12d,0x4(%rsp)
movslq %eax,%r12
mov    %r15,%rdi
mov    %ebp,%esi
call   1030 <stun_set_command_message_len_str@plt>
movslq %ebp,%rax
mov    %rax,0x0(%r13)
mov    0x4(%rsp),%edi
call   1040 <ntohs@plt>
mov    %eax,(%r15,%r12,1)
mov    %ebx,%edi
call   1040 <ntohs@plt>
mov    %eax,0x4(%r15,%r12,1)
test   %ebx,%ebx
je     11ea <stun_attr_add_str+0xaa>
lea    (%r15,%r12,1),%rdi
add    $0x4,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1050 <memmove@plt>
xor    %ecx,%ecx
mov    %ecx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @stun_attr_add_str(i8* %0, i64* %1, i32 %2, i8* %3, i32 %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64*
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
%12 = alloca [1 x i8]
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i8*
%17 = alloca i32*
store i8* %0, i8** %7
store i64* %1, i64** %8
store i32 %2, i32* %9
store i8* %3, i8** %10
store i32 %4, i32* %11
%18 = load i32, i32* %11
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %5
store i32 0, i32* %11
br label %21
21: ; preds = %20, %5
%22 = load i8*, i8** %10
%23 = icmp ne i8* %22, null
br i1 %23, label %26, label %24
24: ; preds = %21
store i32 0, i32* %11
%25 = getelementptr inbounds [1 x i8], [1 x i8]* %12, i64 0, i64 0
store i8* %25, i8** %10
br label %26
26: ; preds = %24, %21
%27 = load i8*, i8** %7
%28 = load i64*, i64** %8
%29 = load i64, i64* %28
%30 = call i32 @stun_get_command_message_len_str(i8* %27, i64 %29)
store i32 %30, i32* %13
%31 = load i32, i32* %13
%32 = add nsw i32 %31, 4
%33 = load i32, i32* %11
%34 = add nsw i32 %32, %33
store i32 %34, i32* %14
%35 = load i32, i32* %14
%36 = and i32 %35, 3
store i32 %36, i32* %15
%37 = load i32, i32* %15
%38 = icmp ne i32 %37, 0
br i1 %38, label %39, label %44
39: ; preds = %26
%40 = load i32, i32* %14
%41 = load i32, i32* %15
%42 = sub nsw i32 4, %41
%43 = add nsw i32 %40, %42
store i32 %43, i32* %14
br label %44
44: ; preds = %39, %26
%45 = load i32, i32* %14
%46 = icmp sge i32 %45, 65507
br i1 %46, label %47, label %48
47: ; preds = %44
store i32 -1, i32* %6
br label %78
48: ; preds = %44
%49 = load i8*, i8** %7
%50 = load i32, i32* %13
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i8, i8* %49, i64 %51
store i8* %52, i8** %16
%53 = load i8*, i8** %16
%54 = bitcast i8* %53 to i32*
store i32* %54, i32** %17
%55 = load i8*, i8** %7
%56 = load i32, i32* %14
%57 = call i32 @stun_set_command_message_len_str(i8* %55, i32 %56)
%58 = load i32, i32* %14
%59 = sext i32 %58 to i64
%60 = load i64*, i64** %8
store i64 %59, i64* %60
%61 = load i32, i32* %9
%62 = call i32 @ntohs(i32 %61)
%63 = load i32*, i32** %17
%64 = getelementptr inbounds i32, i32* %63, i64 0
store i32 %62, i32* %64
%65 = load i32, i32* %11
%66 = call i32 @ntohs(i32 %65)
%67 = load i32*, i32** %17
%68 = getelementptr inbounds i32, i32* %67, i64 1
store i32 %66, i32* %68
%69 = load i32, i32* %11
%70 = icmp sgt i32 %69, 0
br i1 %70, label %71, label %77
71: ; preds = %48
%72 = load i8*, i8** %10
%73 = load i8*, i8** %16
%74 = getelementptr inbounds i8, i8* %73, i64 4
%75 = load i32, i32* %11
%76 = call i32 @bcopy(i8* %72, i8* %74, i32 %75)
br label %77
77: ; preds = %71, %48
store i32 0, i32* %6
br label %78
78: ; preds = %77, %47
%79 = load i32, i32* %6
ret i32 %79
}

LLVM IR (-O2):
define i32 @stun_attr_add_str(i8* %0, i64* nocapture %1, i32 %2, i8* %3, i32 %4) {
%6 = alloca [1 x i8]
%7 = icmp sgt i32 %4, 0
%8 = select i1 %7, i32 %4, i32 0
%9 = getelementptr inbounds [1 x i8], [1 x i8]* %6, i64 0, i64 0
%10 = icmp eq i8* %3, null
%11 = select i1 %10, i32 0, i32 %8
%12 = select i1 %10, i8* %9, i8* %3
%13 = load i64, i64* %1
%14 = tail call i32 @stun_get_command_message_len_str(i8* %0, i64 %13)
%15 = add nuw i32 %11, 4
%16 = add i32 %15, %14
%17 = and i32 %16, 3
%18 = icmp eq i32 %17, 0
%19 = sub nuw nsw i32 4, %17
%20 = select i1 %18, i32 0, i32 %19
%21 = add nsw i32 %20, %16
%22 = icmp sgt i32 %21, 65506
br i1 %22, label %35, label %23
23: ; preds = %5
%24 = sext i32 %14 to i64
%25 = getelementptr inbounds i8, i8* %0, i64 %24
%26 = bitcast i8* %25 to i32*
%27 = tail call i32 @stun_set_command_message_len_str(i8* %0, i32 %21)
%28 = sext i32 %21 to i64
store i64 %28, i64* %1
%29 = tail call i32 @ntohs(i32 %2)
store i32 %29, i32* %26
%30 = tail call i32 @ntohs(i32 %11)
%31 = getelementptr inbounds i8, i8* %25, i64 4
%32 = bitcast i8* %31 to i32*
store i32 %30, i32* %32
%33 = icmp eq i32 %11, 0
br i1 %33, label %35, label %34
34: ; preds = %23
br label %35
35: ; preds = %23, %34, %5
%36 = phi i32 [ -1, %5 ], [ 0, %34 ], [ 0, %23 ]
ret i32 %36
}