#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 */
unsigned int htonl(unsigned int);
unsigned short htons(unsigned short);
int host2network_byte_order(void *data, int width, int length) {
  int i = 0;
  unsigned int *ch4;
  unsigned short int *ch2;
  if (!data)
    return -1;
  if (width < 2)
    return -2;
  if (length < 2)
    return -3;
  switch (width) {
  case 2:
    ch2 = (unsigned short int *)data;
    for (i = 0; i < length / width; i++) {
      *ch2 = htons(*ch2);
      ch2++;
    }
    break;
  case 4:
    ch4 = (unsigned int *)data;
    for (i = 0; i < length / width; i++) {
      *ch4 = htonl(*ch4);
      ch4++;
    }
    break;
  default:
    return -4;
    break;
  }
  return 0;
}


x86 assembly (clang, -O0):
<host2network_byte_order>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1150 <host2network_byte_order+0x30>
movl   $0xffffffff,-0x4(%rbp)
jmp    126d <host2network_byte_order+0x14d>
cmpl   $0x2,-0x14(%rbp)
jge    1166 <host2network_byte_order+0x46>
movl   $0xfffffffe,-0x4(%rbp)
jmp    126d <host2network_byte_order+0x14d>
cmpl   $0x2,-0x18(%rbp)
jge    117c <host2network_byte_order+0x5c>
movl   $0xfffffffd,-0x4(%rbp)
jmp    126d <host2network_byte_order+0x14d>
mov    -0x14(%rbp),%eax
mov    %eax,-0x34(%rbp)
sub    $0x2,%eax
je     11a1 <host2network_byte_order+0x81>
jmp    1190 <host2network_byte_order+0x70>
mov    -0x34(%rbp),%eax
sub    $0x4,%eax
je     11ff <host2network_byte_order+0xdf>
jmp    125a <host2network_byte_order+0x13a>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x18(%rbp),%eax
cltd
idivl  -0x14(%rbp)
mov    %eax,%ecx
mov    -0x38(%rbp),%eax
cmp    %ecx,%eax
jge    11fa <host2network_byte_order+0xda>
mov    -0x30(%rbp),%rax
movzwl (%rax),%edi
call   1030 <htons@plt>
mov    %ax,%cx
mov    -0x30(%rbp),%rax
mov    %cx,(%rax)
mov    -0x30(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x30(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11b0 <host2network_byte_order+0x90>
jmp    1266 <host2network_byte_order+0x146>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x18(%rbp),%eax
cltd
idivl  -0x14(%rbp)
mov    %eax,%ecx
mov    -0x3c(%rbp),%eax
cmp    %ecx,%eax
jge    1255 <host2network_byte_order+0x135>
mov    -0x28(%rbp),%rax
mov    (%rax),%edi
call   1040 <htonl@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x28(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    120e <host2network_byte_order+0xee>
jmp    1266 <host2network_byte_order+0x146>
movl   $0xfffffffc,-0x4(%rbp)
jmp    126d <host2network_byte_order+0x14d>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<host2network_byte_order>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
test   %rdi,%rdi
je     118a <host2network_byte_order+0x6a>
mov    $0xfffffffe,%r14d
cmp    $0x2,%esi
jl     1190 <host2network_byte_order+0x70>
mov    $0xfffffffd,%r14d
cmp    $0x2,%edx
jl     1190 <host2network_byte_order+0x70>
mov    %rdi,%r12
cmp    $0x4,%esi
je     119f <host2network_byte_order+0x7f>
mov    $0xfffffffc,%r14d
cmp    $0x2,%esi
jne    1190 <host2network_byte_order+0x70>
mov    %edx,%r15d
shr    %r15
xor    %r14d,%r14d
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movzwl (%r12,%rbx,2),%edi
call   1030 <htons@plt>
mov    %ax,(%r12,%rbx,2)
add    $0x1,%rbx
cmp    %ebx,%r15d
jne    1170 <host2network_byte_order+0x50>
jmp    1190 <host2network_byte_order+0x70>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
xor    %r14d,%r14d
cmp    $0x4,%edx
jb     1190 <host2network_byte_order+0x70>
mov    %edx,%r15d
shr    $0x2,%r15
xor    %r14d,%r14d
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    (%r12,%rbx,4),%edi
call   1040 <htonl@plt>
mov    %eax,(%r12,%rbx,4)
add    $0x1,%rbx
cmp    %ebx,%r15d
jne    11c0 <host2network_byte_order+0xa0>
jmp    1190 <host2network_byte_order+0x70>

LLVM IR (-O0):
define i32 @host2network_byte_order(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i16*
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 0, i32* %8
%11 = load i8*, i8** %5
%12 = icmp ne i8* %11, null
br i1 %12, label %14, label %13
13: ; preds = %3
store i32 -1, i32* %4
br label %66
14: ; preds = %3
%15 = load i32, i32* %6
%16 = icmp slt i32 %15, 2
br i1 %16, label %17, label %18
17: ; preds = %14
store i32 -2, i32* %4
br label %66
18: ; preds = %14
%19 = load i32, i32* %7
%20 = icmp slt i32 %19, 2
br i1 %20, label %21, label %22
21: ; preds = %18
store i32 -3, i32* %4
br label %66
22: ; preds = %18
%23 = load i32, i32* %6
switch i32 %23, label %64 [
i32 2, label %24
i32 4, label %44
]
24: ; preds = %22
%25 = load i8*, i8** %5
%26 = bitcast i8* %25 to i16*
store i16* %26, i16** %10
store i32 0, i32* %8
br label %27
27: ; preds = %40, %24
%28 = load i32, i32* %8
%29 = load i32, i32* %7
%30 = load i32, i32* %6
%31 = sdiv i32 %29, %30
%32 = icmp slt i32 %28, %31
br i1 %32, label %33, label %43
33: ; preds = %27
%34 = load i16*, i16** %10
%35 = load i16, i16* %34
%36 = call zeroext i16 @htons(i16 zeroext %35)
%37 = load i16*, i16** %10
store i16 %36, i16* %37
%38 = load i16*, i16** %10
%39 = getelementptr inbounds i16, i16* %38, i32 1
store i16* %39, i16** %10
br label %40
40: ; preds = %33
%41 = load i32, i32* %8
%42 = add nsw i32 %41, 1
store i32 %42, i32* %8
br label %27
43: ; preds = %27
br label %65
44: ; preds = %22
%45 = load i8*, i8** %5
%46 = bitcast i8* %45 to i32*
store i32* %46, i32** %9
store i32 0, i32* %8
br label %47
47: ; preds = %60, %44
%48 = load i32, i32* %8
%49 = load i32, i32* %7
%50 = load i32, i32* %6
%51 = sdiv i32 %49, %50
%52 = icmp slt i32 %48, %51
br i1 %52, label %53, label %63
53: ; preds = %47
%54 = load i32*, i32** %9
%55 = load i32, i32* %54
%56 = call i32 @htonl(i32 %55)
%57 = load i32*, i32** %9
store i32 %56, i32* %57
%58 = load i32*, i32** %9
%59 = getelementptr inbounds i32, i32* %58, i32 1
store i32* %59, i32** %9
br label %60
60: ; preds = %53
%61 = load i32, i32* %8
%62 = add nsw i32 %61, 1
store i32 %62, i32* %8
br label %47
63: ; preds = %47
br label %65
64: ; preds = %22
store i32 -4, i32* %4
br label %66
65: ; preds = %63, %43
store i32 0, i32* %4
br label %66
66: ; preds = %65, %64, %21, %17, %13
%67 = load i32, i32* %4
ret i32 %67
}

LLVM IR (-O2):
define i32 @host2network_byte_order(i8* %0, i32 %1, i32 %2) {
%4 = icmp eq i8* %0, null
br i1 %4, label %34, label %5
5: ; preds = %3
%6 = icmp slt i32 %1, 2
br i1 %6, label %34, label %7
7: ; preds = %5
%8 = icmp slt i32 %2, 2
br i1 %8, label %34, label %9
9: ; preds = %7
switch i32 %1, label %34 [
i32 2, label %10
i32 4, label %21
]
10: ; preds = %9
%11 = lshr i32 %2, 1
%12 = bitcast i8* %0 to i16*
br label %13
13: ; preds = %10, %13
%14 = phi i16* [ %18, %13 ], [ %12, %10 ]
%15 = phi i32 [ %19, %13 ], [ 0, %10 ]
%16 = load i16, i16* %14
%17 = tail call zeroext i16 @htons(i16 zeroext %16)
store i16 %17, i16* %14
%18 = getelementptr inbounds i16, i16* %14, i64 1
%19 = add nuw nsw i32 %15, 1
%20 = icmp eq i32 %19, %11
br i1 %20, label %34, label %13
21: ; preds = %9
%22 = lshr i32 %2, 2
%23 = icmp ugt i32 %2, 3
br i1 %23, label %24, label %34
24: ; preds = %21
%25 = bitcast i8* %0 to i32*
br label %26
26: ; preds = %24, %26
%27 = phi i32* [ %31, %26 ], [ %25, %24 ]
%28 = phi i32 [ %32, %26 ], [ 0, %24 ]
%29 = load i32, i32* %27
%30 = tail call i32 @htonl(i32 %29)
store i32 %30, i32* %27
%31 = getelementptr inbounds i32, i32* %27, i64 1
%32 = add nuw nsw i32 %28, 1
%33 = icmp eq i32 %32, %22
br i1 %33, label %34, label %26
34: ; preds = %26, %13, %21, %9, %7, %5, %3
%35 = phi i32 [ -1, %3 ], [ -2, %5 ], [ -3, %7 ], [ -4, %9 ], [ 0, %21 ], [ 0, %13 ], [ 0, %26 ]
ret i32 %35
}