#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 ILibAlignOnFourByteBoundary(char *, int);
int /*<<< orphan*/ memcpy(char *, char *, int);
unsigned short ntohs(unsigned short);
int ILibStun_AddSctpChunkArg(char *buffer, int ptr, unsigned short otype,
                             char *data, int datalen) {
  ptr = ILibAlignOnFourByteBoundary(buffer, ptr);
  ((unsigned short *)(buffer + ptr))[0] = ntohs(otype);
  ((unsigned short *)(buffer + ptr))[1] = ntohs((unsigned short)(4 + datalen));
  if (datalen != 0)
    memcpy(buffer + ptr + 4, data, datalen);
  return ptr + 4 + datalen;
}


x86 assembly (clang, -O0):
<ILibStun_AddSctpChunkArg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %dx,%ax
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %ax,-0xe(%rbp)
mov    %rcx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1050 <ILibAlignOnFourByteBoundary@plt>
mov    %eax,-0xc(%rbp)
movzwl -0xe(%rbp),%edi
call   1040 <ntohs@plt>
mov    %ax,%dx
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
mov    %dx,(%rax,%rcx,1)
mov    -0x1c(%rbp),%eax
add    $0x4,%eax
movzwl %ax,%edi
call   1040 <ntohs@plt>
mov    %ax,%dx
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
mov    %dx,0x2(%rax,%rcx,1)
cmpl   $0x0,-0x1c(%rbp)
je     11b8 <ILibStun_AddSctpChunkArg+0x88>
mov    -0x8(%rbp),%rdi
movslq -0xc(%rbp),%rax
add    %rax,%rdi
add    $0x4,%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <memcpy@plt>
mov    -0xc(%rbp),%eax
add    $0x4,%eax
add    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ILibStun_AddSctpChunkArg>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %r8d,%r12d
mov    %rcx,%r14
mov    %edx,%ebp
mov    %rdi,%rbx
call   1050 <ILibAlignOnFourByteBoundary@plt>
mov    %eax,%r15d
mov    %ebp,%edi
call   1040 <ntohs@plt>
movslq %r15d,%rbp
mov    %ax,(%rbx,%rbp,1)
lea    0x4(%r12),%eax
movzwl %ax,%edi
call   1040 <ntohs@plt>
mov    %ax,0x2(%rbx,%rbp,1)
test   %r12d,%r12d
je     1185 <ILibStun_AddSctpChunkArg+0x55>
add    %rbp,%rbx
add    $0x4,%rbx
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r12d,%edx
call   1030 <memcpy@plt>
lea    (%r12,%r15,1),%eax
add    $0x4,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @ILibStun_AddSctpChunkArg(i8* %0, i32 %1, i16 zeroext %2, i8* %3, i32 %4) {
%6 = tail call i32 @ILibAlignOnFourByteBoundary(i8* %0, i32 %1)
%7 = tail call zeroext i16 @ntohs(i16 zeroext %2)
%8 = sext i32 %6 to i64
%9 = getelementptr inbounds i8, i8* %0, i64 %8
%10 = bitcast i8* %9 to i16*
store i16 %7, i16* %10
%11 = trunc i32 %4 to i16
%12 = add i16 %11, 4
%13 = tail call zeroext i16 @ntohs(i16 zeroext %12)
%14 = getelementptr inbounds i8, i8* %9, i64 2
%15 = bitcast i8* %14 to i16*
store i16 %13, i16* %15
%16 = icmp eq i32 %4, 0
br i1 %16, label %20, label %17
17: ; preds = %5
%18 = getelementptr inbounds i8, i8* %9, i64 4
%19 = tail call i32 @memcpy(i8* %18, i8* %3, i32 %4)
br label %20
20: ; preds = %17, %5
%21 = add i32 %4, 4
%22 = add i32 %21, %6
ret i32 %22
}