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

/* Variables and functions */
int bwrite_endian(ulong x, int bytes, unsigned char *buf, int endian) {
  unsigned char u1, u2, u3, u4;
  if (bytes == 1) {
    u1 = x & 0xFF;
    *buf++ = u1;
    return 1;
  } else if (bytes == 2) {
    u1 = x & 0xFF;
    u2 = (x >> 8) & 0xFF;
    if (endian) {
      *buf++ = u1;
      *buf++ = u2;
    } else {
      *buf++ = u2;
      *buf++ = u1;
    }
    return 2;
  } else if (bytes == 4) {
    u1 = x & 0xFF;
    u2 = (x >> 8) & 0xFF;
    u3 = (x >> 16) & 0xFF;
    u4 = (x >> 24) & 0xFF;
    if (endian) {
      *buf++ = u1;
      *buf++ = u2;
      *buf++ = u3;
      *buf++ = u4;
    } else {
      *buf++ = u4;
      *buf++ = u3;
      *buf++ = u2;
      *buf++ = u1;
    }
    return 4;
  } else {
    return -1;
  }
}


x86 assembly (clang, -O0):
<bwrite_endian>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
cmpl   $0x1,-0xc(%rbp)
jne    1146 <bwrite_endian+0x46>
mov    -0x8(%rbp),%eax
and    $0xff,%eax
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    12d5 <bwrite_endian+0x1d5>
cmpl   $0x2,-0xc(%rbp)
jne    11d4 <bwrite_endian+0xd4>
mov    -0x8(%rbp),%eax
and    $0xff,%eax
mov    %al,-0x1d(%rbp)
mov    -0x8(%rbp),%eax
sar    $0x8,%eax
and    $0xff,%eax
mov    %al,-0x1e(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     11a0 <bwrite_endian+0xa0>
mov    -0x1d(%rbp),%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
mov    -0x1e(%rbp),%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
jmp    11c8 <bwrite_endian+0xc8>
mov    -0x1e(%rbp),%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
mov    -0x1d(%rbp),%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
movl   $0x2,-0x4(%rbp)
jmp    12d5 <bwrite_endian+0x1d5>
cmpl   $0x4,-0xc(%rbp)
jne    12ce <bwrite_endian+0x1ce>
mov    -0x8(%rbp),%eax
and    $0xff,%eax
mov    %al,-0x1d(%rbp)
mov    -0x8(%rbp),%eax
sar    $0x8,%eax
and    $0xff,%eax
mov    %al,-0x1e(%rbp)
mov    -0x8(%rbp),%eax
sar    $0x10,%eax
and    $0xff,%eax
mov    %al,-0x1f(%rbp)
mov    -0x8(%rbp),%eax
sar    $0x18,%eax
and    $0xff,%eax
mov    %al,-0x20(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1272 <bwrite_endian+0x172>
mov    -0x1d(%rbp),%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
mov    -0x1e(%rbp),%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
mov    -0x1f(%rbp),%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
mov    -0x20(%rbp),%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
jmp    12c2 <bwrite_endian+0x1c2>
mov    -0x20(%rbp),%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
mov    -0x1f(%rbp),%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
mov    -0x1e(%rbp),%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
mov    -0x1d(%rbp),%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
movl   $0x4,-0x4(%rbp)
jmp    12d5 <bwrite_endian+0x1d5>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<bwrite_endian>:
cmp    $0x4,%esi
je     111d <bwrite_endian+0x1d>
cmp    $0x2,%esi
je     1142 <bwrite_endian+0x42>
mov    $0xffffffff,%eax
cmp    $0x1,%esi
jne    116a <bwrite_endian+0x6a>
mov    %dil,(%rdx)
mov    $0x1,%eax
ret
mov    %edi,%r8d
shr    $0x8,%r8d
mov    %edi,%esi
shr    $0x10,%esi
mov    %edi,%eax
shr    $0x18,%eax
test   %ecx,%ecx
je     1157 <bwrite_endian+0x57>
mov    %dil,(%rdx)
mov    %r8b,0x1(%rdx)
mov    %sil,0x2(%rdx)
mov    %al,0x3(%rdx)
jmp    1165 <bwrite_endian+0x65>
mov    %edi,%eax
shr    $0x8,%eax
test   %ecx,%ecx
je     116b <bwrite_endian+0x6b>
mov    %dil,(%rdx)
mov    %al,0x1(%rdx)
mov    $0x2,%eax
ret
mov    %al,(%rdx)
mov    %sil,0x1(%rdx)
mov    %r8b,0x2(%rdx)
mov    %dil,0x3(%rdx)
mov    $0x4,%eax
ret
mov    %al,(%rdx)
mov    %dil,0x1(%rdx)
mov    $0x2,%eax
ret

LLVM IR (-O0):
define i32 @bwrite_endian(i32 %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i8
%11 = alloca i8
%12 = alloca i8
%13 = alloca i8
store i32 %0, i32* %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i32 %3, i32* %9
%14 = load i32, i32* %7
%15 = icmp eq i32 %14, 1
br i1 %15, label %16, label %23
16: ; preds = %4
%17 = load i32, i32* %6
%18 = and i32 %17, 255
%19 = trunc i32 %18 to i8
store i8 %19, i8* %10
%20 = load i8, i8* %10
%21 = load i8*, i8** %8
%22 = getelementptr inbounds i8, i8* %21, i32 1
store i8* %22, i8** %8
store i8 %20, i8* %21
store i32 1, i32* %5
br label %100
23: ; preds = %4
%24 = load i32, i32* %7
%25 = icmp eq i32 %24, 2
br i1 %25, label %26, label %51
26: ; preds = %23
%27 = load i32, i32* %6
%28 = and i32 %27, 255
%29 = trunc i32 %28 to i8
store i8 %29, i8* %10
%30 = load i32, i32* %6
%31 = ashr i32 %30, 8
%32 = and i32 %31, 255
%33 = trunc i32 %32 to i8
store i8 %33, i8* %11
%34 = load i32, i32* %9
%35 = icmp ne i32 %34, 0
br i1 %35, label %36, label %43
36: ; preds = %26
%37 = load i8, i8* %10
%38 = load i8*, i8** %8
%39 = getelementptr inbounds i8, i8* %38, i32 1
store i8* %39, i8** %8
store i8 %37, i8* %38
%40 = load i8, i8* %11
%41 = load i8*, i8** %8
%42 = getelementptr inbounds i8, i8* %41, i32 1
store i8* %42, i8** %8
store i8 %40, i8* %41
br label %50
43: ; preds = %26
%44 = load i8, i8* %11
%45 = load i8*, i8** %8
%46 = getelementptr inbounds i8, i8* %45, i32 1
store i8* %46, i8** %8
store i8 %44, i8* %45
%47 = load i8, i8* %10
%48 = load i8*, i8** %8
%49 = getelementptr inbounds i8, i8* %48, i32 1
store i8* %49, i8** %8
store i8 %47, i8* %48
br label %50
50: ; preds = %43, %36
store i32 2, i32* %5
br label %100
51: ; preds = %23
%52 = load i32, i32* %7
%53 = icmp eq i32 %52, 4
br i1 %53, label %54, label %99
54: ; preds = %51
%55 = load i32, i32* %6
%56 = and i32 %55, 255
%57 = trunc i32 %56 to i8
store i8 %57, i8* %10
%58 = load i32, i32* %6
%59 = ashr i32 %58, 8
%60 = and i32 %59, 255
%61 = trunc i32 %60 to i8
store i8 %61, i8* %11
%62 = load i32, i32* %6
%63 = ashr i32 %62, 16
%64 = and i32 %63, 255
%65 = trunc i32 %64 to i8
store i8 %65, i8* %12
%66 = load i32, i32* %6
%67 = ashr i32 %66, 24
%68 = and i32 %67, 255
%69 = trunc i32 %68 to i8
store i8 %69, i8* %13
%70 = load i32, i32* %9
%71 = icmp ne i32 %70, 0
br i1 %71, label %72, label %85
72: ; preds = %54
%73 = load i8, i8* %10
%74 = load i8*, i8** %8
%75 = getelementptr inbounds i8, i8* %74, i32 1
store i8* %75, i8** %8
store i8 %73, i8* %74
%76 = load i8, i8* %11
%77 = load i8*, i8** %8
%78 = getelementptr inbounds i8, i8* %77, i32 1
store i8* %78, i8** %8
store i8 %76, i8* %77
%79 = load i8, i8* %12
%80 = load i8*, i8** %8
%81 = getelementptr inbounds i8, i8* %80, i32 1
store i8* %81, i8** %8
store i8 %79, i8* %80
%82 = load i8, i8* %13
%83 = load i8*, i8** %8
%84 = getelementptr inbounds i8, i8* %83, i32 1
store i8* %84, i8** %8
store i8 %82, i8* %83
br label %98
85: ; preds = %54
%86 = load i8, i8* %13
%87 = load i8*, i8** %8
%88 = getelementptr inbounds i8, i8* %87, i32 1
store i8* %88, i8** %8
store i8 %86, i8* %87
%89 = load i8, i8* %12
%90 = load i8*, i8** %8
%91 = getelementptr inbounds i8, i8* %90, i32 1
store i8* %91, i8** %8
store i8 %89, i8* %90
%92 = load i8, i8* %11
%93 = load i8*, i8** %8
%94 = getelementptr inbounds i8, i8* %93, i32 1
store i8* %94, i8** %8
store i8 %92, i8* %93
%95 = load i8, i8* %10
%96 = load i8*, i8** %8
%97 = getelementptr inbounds i8, i8* %96, i32 1
store i8* %97, i8** %8
store i8 %95, i8* %96
br label %98
98: ; preds = %85, %72
store i32 4, i32* %5
br label %100
99: ; preds = %51
store i32 -1, i32* %5
br label %100
100: ; preds = %99, %98, %50, %16
%101 = load i32, i32* %5
ret i32 %101
}

LLVM IR (-O2):
define i32 @bwrite_endian(i32 %0, i32 %1, i8* nocapture writeonly %2, i32 %3) {
switch i32 %1, label %31 [
i32 1, label %5
i32 2, label %7
i32 4, label %15
]
5: ; preds = %4
%6 = trunc i32 %0 to i8
store i8 %6, i8* %2
br label %31
7: ; preds = %4
%8 = trunc i32 %0 to i8
%9 = lshr i32 %0, 8
%10 = trunc i32 %9 to i8
%11 = icmp eq i32 %3, 0
%12 = getelementptr inbounds i8, i8* %2, i64 1
br i1 %11, label %14, label %13
13: ; preds = %7
store i8 %8, i8* %2
store i8 %10, i8* %12
br label %31
14: ; preds = %7
store i8 %10, i8* %2
store i8 %8, i8* %12
br label %31
15: ; preds = %4
%16 = trunc i32 %0 to i8
%17 = lshr i32 %0, 8
%18 = trunc i32 %17 to i8
%19 = lshr i32 %0, 16
%20 = trunc i32 %19 to i8
%21 = lshr i32 %0, 24
%22 = trunc i32 %21 to i8
%23 = icmp eq i32 %3, 0
%24 = getelementptr inbounds i8, i8* %2, i64 1
br i1 %23, label %28, label %25
25: ; preds = %15
store i8 %16, i8* %2
%26 = getelementptr inbounds i8, i8* %2, i64 2
store i8 %18, i8* %24
%27 = getelementptr inbounds i8, i8* %2, i64 3
store i8 %20, i8* %26
store i8 %22, i8* %27
br label %31
28: ; preds = %15
store i8 %22, i8* %2
%29 = getelementptr inbounds i8, i8* %2, i64 2
store i8 %20, i8* %24
%30 = getelementptr inbounds i8, i8* %2, i64 3
store i8 %18, i8* %29
store i8 %16, i8* %30
br label %31
31: ; preds = %4, %25, %28, %13, %14, %5
%32 = phi i32 [ 1, %5 ], [ 2, %14 ], [ 2, %13 ], [ 4, %28 ], [ 4, %25 ], [ -1, %4 ]
ret i32 %32
}