#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 ntohs(int);
int stun_init_channel_message_str(uint16_t chnumber, unsigned char *buf,
                                  size_t *len, int length, int do_padding) {
  uint16_t rlen = (uint16_t)length;
  if (length < 0 || ((65507) < (4 + length)))
    return -1;
  ((uint16_t *)(buf))[0] = ntohs(chnumber);
  ((uint16_t *)(buf))[1] = ntohs((uint16_t)length);
  if (do_padding && (rlen & 0x0003))
    rlen = ((rlen >> 2) + 1) << 2;
  *len = 4 + rlen;
  return 0;
}


x86 assembly (clang, -O0):
<stun_init_channel_message_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8d,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jl     114d <stun_init_channel_message_str+0x3d>
mov    -0x1c(%rbp),%ecx
add    $0x4,%ecx
mov    $0xffe3,%eax
cmp    %ecx,%eax
jge    1159 <stun_init_channel_message_str+0x49>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b9 <stun_init_channel_message_str+0xa9>
mov    -0x8(%rbp),%edi
call   1030 <ntohs@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x1c(%rbp),%edi
call   1030 <ntohs@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x4(%rax)
cmpl   $0x0,-0x20(%rbp)
je     11a2 <stun_init_channel_message_str+0x92>
mov    -0x24(%rbp),%eax
and    $0x3,%eax
cmp    $0x0,%eax
je     11a2 <stun_init_channel_message_str+0x92>
mov    -0x24(%rbp),%eax
sar    $0x2,%eax
add    $0x1,%eax
shl    $0x2,%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
add    $0x4,%eax
movslq %eax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<stun_init_channel_message_str>:
mov    $0xffffffff,%eax
cmp    $0xffdf,%ecx
ja     1165 <stun_init_channel_message_str+0x55>
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %r8d,%r15d
mov    %ecx,%ebx
mov    %rdx,%r14
mov    %rsi,%rbp
call   1030 <ntohs@plt>
mov    %eax,0x0(%rbp)
mov    %ebx,%edi
call   1030 <ntohs@plt>
lea    0x4(%rbx),%ecx
and    $0xfffffffc,%ecx
test   $0x3,%bl
cmove  %ebx,%ecx
test   %r15d,%r15d
cmove  %ebx,%ecx
mov    %eax,0x4(%rbp)
add    $0x4,%ecx
mov    %rcx,(%r14)
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @stun_init_channel_message_str(i32 %0, i8* %1, i64* %2, i32 %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i64*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %7
store i8* %1, i8** %8
store i64* %2, i64** %9
store i32 %3, i32* %10
store i32 %4, i32* %11
%13 = load i32, i32* %10
store i32 %13, i32* %12
%14 = load i32, i32* %10
%15 = icmp slt i32 %14, 0
br i1 %15, label %20, label %16
16: ; preds = %5
%17 = load i32, i32* %10
%18 = add nsw i32 4, %17
%19 = icmp slt i32 65507, %18
br i1 %19, label %20, label %21
20: ; preds = %16, %5
store i32 -1, i32* %6
br label %48
21: ; preds = %16
%22 = load i32, i32* %7
%23 = call i32 @ntohs(i32 %22)
%24 = load i8*, i8** %8
%25 = bitcast i8* %24 to i32*
%26 = getelementptr inbounds i32, i32* %25, i64 0
store i32 %23, i32* %26
%27 = load i32, i32* %10
%28 = call i32 @ntohs(i32 %27)
%29 = load i8*, i8** %8
%30 = bitcast i8* %29 to i32*
%31 = getelementptr inbounds i32, i32* %30, i64 1
store i32 %28, i32* %31
%32 = load i32, i32* %11
%33 = icmp ne i32 %32, 0
br i1 %33, label %34, label %43
34: ; preds = %21
%35 = load i32, i32* %12
%36 = and i32 %35, 3
%37 = icmp ne i32 %36, 0
br i1 %37, label %38, label %43
38: ; preds = %34
%39 = load i32, i32* %12
%40 = ashr i32 %39, 2
%41 = add nsw i32 %40, 1
%42 = shl i32 %41, 2
store i32 %42, i32* %12
br label %43
43: ; preds = %38, %34, %21
%44 = load i32, i32* %12
%45 = add nsw i32 4, %44
%46 = sext i32 %45 to i64
%47 = load i64*, i64** %9
store i64 %46, i64* %47
store i32 0, i32* %6
br label %48
48: ; preds = %43, %20
%49 = load i32, i32* %6
ret i32 %49
}

LLVM IR (-O2):
define i32 @stun_init_channel_message_str(i32 %0, i8* nocapture writeonly %1, i64* nocapture writeonly %2, i32 %3, i32 %4) {
%6 = icmp ugt i32 %3, 65503
br i1 %6, label %22, label %7
7: ; preds = %5
%8 = tail call i32 @ntohs(i32 %0)
%9 = bitcast i8* %1 to i32*
store i32 %8, i32* %9
%10 = tail call i32 @ntohs(i32 %3)
%11 = getelementptr inbounds i8, i8* %1, i64 4
%12 = bitcast i8* %11 to i32*
store i32 %10, i32* %12
%13 = icmp eq i32 %4, 0
%14 = and i32 %3, 3
%15 = icmp eq i32 %14, 0
%16 = or i1 %13, %15
%17 = add nuw nsw i32 %3, 4
%18 = and i32 %17, -4
%19 = select i1 %16, i32 %3, i32 %18
%20 = add nuw nsw i32 %19, 4
%21 = zext i32 %20 to i64
store i64 %21, i64* %2
br label %22
22: ; preds = %5, %7
%23 = phi i32 [ 0, %7 ], [ -1, %5 ]
ret i32 %23
}