void netbuf_add_32bitnum(unsigned char *buf, int buflen, int *offset,
                         unsigned int num) {
  unsigned char *start = buf + *offset;
  unsigned char *p = start;
  *p++ = (num >> 24) & 0xff;
  *p++ = (num >> 16) & 0xff;
  *p++ = (num >> 8) & 0xff;
  *p++ = (num)&0xff;
  *offset += 4;
}


x86 assembly (clang, -O0):
<netbuf_add_32bitnum>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
movslq (%rcx),%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x1c(%rbp),%eax
shr    $0x18,%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x30(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x30(%rbp)
mov    %cl,(%rax)
mov    -0x1c(%rbp),%eax
shr    $0x10,%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x30(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x30(%rbp)
mov    %cl,(%rax)
mov    -0x1c(%rbp),%eax
shr    $0x8,%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x30(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x30(%rbp)
mov    %cl,(%rax)
mov    -0x1c(%rbp),%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x30(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x30(%rbp)
mov    %cl,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%ecx
add    $0x4,%ecx
mov    %ecx,(%rax)
pop    %rbp
ret

x86 assembly (clang, -O2):
<netbuf_add_32bitnum>:
movslq (%rdx),%rax
bswap  %ecx
mov    %ecx,(%rdi,%rax,1)
addl   $0x4,(%rdx)
ret

LLVM IR (-O0):
define void @netbuf_add_32bitnum(i8* %0, i32 %1, i32* %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i8*
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32* %2, i32** %7
store i32 %3, i32* %8
%11 = load i8*, i8** %5
%12 = load i32*, i32** %7
%13 = load i32, i32* %12
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds i8, i8* %11, i64 %14
store i8* %15, i8** %9
%16 = load i8*, i8** %9
store i8* %16, i8** %10
%17 = load i32, i32* %8
%18 = lshr i32 %17, 24
%19 = and i32 %18, 255
%20 = trunc i32 %19 to i8
%21 = load i8*, i8** %10
%22 = getelementptr inbounds i8, i8* %21, i32 1
store i8* %22, i8** %10
store i8 %20, i8* %21
%23 = load i32, i32* %8
%24 = lshr i32 %23, 16
%25 = and i32 %24, 255
%26 = trunc i32 %25 to i8
%27 = load i8*, i8** %10
%28 = getelementptr inbounds i8, i8* %27, i32 1
store i8* %28, i8** %10
store i8 %26, i8* %27
%29 = load i32, i32* %8
%30 = lshr i32 %29, 8
%31 = and i32 %30, 255
%32 = trunc i32 %31 to i8
%33 = load i8*, i8** %10
%34 = getelementptr inbounds i8, i8* %33, i32 1
store i8* %34, i8** %10
store i8 %32, i8* %33
%35 = load i32, i32* %8
%36 = and i32 %35, 255
%37 = trunc i32 %36 to i8
%38 = load i8*, i8** %10
%39 = getelementptr inbounds i8, i8* %38, i32 1
store i8* %39, i8** %10
store i8 %37, i8* %38
%40 = load i32*, i32** %7
%41 = load i32, i32* %40
%42 = add nsw i32 %41, 4
store i32 %42, i32* %40
ret void
}

LLVM IR (-O2):
define void @netbuf_add_32bitnum(i8* nocapture writeonly %0, i32 %1, i32* nocapture %2, i32 %3) {
%5 = load i32, i32* %2
%6 = sext i32 %5 to i64
%7 = getelementptr inbounds i8, i8* %0, i64 %6
%8 = lshr i32 %3, 24
%9 = trunc i32 %8 to i8
%10 = getelementptr inbounds i8, i8* %7, i64 1
store i8 %9, i8* %7
%11 = lshr i32 %3, 16
%12 = trunc i32 %11 to i8
%13 = getelementptr inbounds i8, i8* %10, i64 1
store i8 %12, i8* %10
%14 = lshr i32 %3, 8
%15 = trunc i32 %14 to i8
%16 = getelementptr inbounds i8, i8* %13, i64 1
store i8 %15, i8* %13
%17 = trunc i32 %3 to i8
store i8 %17, i8* %16
%18 = load i32, i32* %2
%19 = add nsw i32 %18, 4
store i32 %19, i32* %2
ret void
}