#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 /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int write(int, unsigned char *, int);
void PutNum(long num, int f, int endianness, int bytes) {
  int i;
  unsigned char c;
  if (!endianness)
    i = 0;
  else
    i = bytes - 1;
  while (bytes--) {
    c = (num >> (i << 3)) & 0xff;
    if (write(f, &c, 1) == -1) {
      perror("Could not write to output.");
      exit(1);
    }
    if (endianness)
      i--;
    else
      i++;
  }
}


x86 assembly (clang, -O0):
<PutNum>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
cmpl   $0x0,-0x10(%rbp)
jne    115b <PutNum+0x2b>
movl   $0x0,-0x18(%rbp)
jmp    1164 <PutNum+0x34>
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1169 <PutNum+0x39>
mov    -0x14(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x14(%rbp)
cmp    $0x0,%eax
je     11eb <PutNum+0xbb>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%ecx
shl    $0x3,%ecx
mov    %ecx,%ecx
sar    %cl,%rax
and    $0xff,%rax
mov    %al,-0x19(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    11c5 <PutNum+0x95>
lea    0xe4a(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
cmpl   $0x0,-0x10(%rbp)
je     11dd <PutNum+0xad>
mov    -0x18(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x18(%rbp)
jmp    11e6 <PutNum+0xb6>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1169 <PutNum+0x39>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<PutNum>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %ecx,%ecx
je     119b <PutNum+0x6b>
mov    %ecx,%ebx
mov    %esi,%r14d
mov    %rdi,%r15
lea    -0x1(%rbx),%ebp
xor    %r13d,%r13d
test   %edx,%edx
sete   %r13b
cmove  %edx,%ebp
shl    $0x4,%r13d
add    $0xfffffff8,%r13d
shl    $0x3,%ebp
lea    0x7(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
mov    %r15,%rax
mov    %ebp,%ecx
sar    %cl,%rax
mov    %al,0x7(%rsp)
mov    %r14d,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     11aa <PutNum+0x7a>
add    $0xffffffff,%ebx
add    %r13d,%ebp
test   %ebx,%ebx
jne    1170 <PutNum+0x40>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe4f(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@.str = [27 x i8] c"Could not write to output.\00"
define void @PutNum(i64 %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8
store i64 %0, i64* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%11 = load i32, i32* %7
%12 = icmp ne i32 %11, 0
br i1 %12, label %14, label %13
13: ; preds = %4
store i32 0, i32* %9
br label %17
14: ; preds = %4
%15 = load i32, i32* %8
%16 = sub nsw i32 %15, 1
store i32 %16, i32* %9
br label %17
17: ; preds = %14, %13
br label %18
18: ; preds = %45, %17
%19 = load i32, i32* %8
%20 = add nsw i32 %19, -1
store i32 %20, i32* %8
%21 = icmp ne i32 %19, 0
br i1 %21, label %22, label %46
22: ; preds = %18
%23 = load i64, i64* %5
%24 = load i32, i32* %9
%25 = shl i32 %24, 3
%26 = zext i32 %25 to i64
%27 = ashr i64 %23, %26
%28 = and i64 %27, 255
%29 = trunc i64 %28 to i8
store i8 %29, i8* %10
%30 = load i32, i32* %6
%31 = call i32 @write(i32 %30, i8* %10, i32 1)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %36
33: ; preds = %22
%34 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %22
%37 = load i32, i32* %7
%38 = icmp ne i32 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %36
%40 = load i32, i32* %9
%41 = add nsw i32 %40, -1
store i32 %41, i32* %9
br label %45
42: ; preds = %36
%43 = load i32, i32* %9
%44 = add nsw i32 %43, 1
store i32 %44, i32* %9
br label %45
45: ; preds = %42, %39
br label %18
46: ; preds = %18
ret void
}

LLVM IR (-O2):
@.str = [27 x i8] c"Could not write to output.\00"
define void @PutNum(i64 %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i8
%6 = icmp eq i32 %2, 0
%7 = icmp eq i32 %3, 0
br i1 %7, label %28, label %8
8: ; preds = %4
%9 = add nsw i32 %3, -1
%10 = select i1 %6, i32 0, i32 %9
%11 = select i1 %6, i32 1, i32 -1
br label %12
12: ; preds = %8, %25
%13 = phi i32 [ %3, %8 ], [ %15, %25 ]
%14 = phi i32 [ %10, %8 ], [ %26, %25 ]
%15 = add nsw i32 %13, -1
%16 = shl i32 %14, 3
%17 = zext i32 %16 to i64
%18 = ashr i64 %0, %17
%19 = trunc i64 %18 to i8
store i8 %19, i8* %5
%20 = call i32 @write(i32 %1, i8* %5, i32 1)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %25
22: ; preds = %12
%23 = tail call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%24 = tail call i32 @exit(i32 1)
unreachable
25: ; preds = %12
%26 = add nsw i32 %14, %11
%27 = icmp eq i32 %15, 0
br i1 %27, label %28, label %12
28: ; preds = %25, %4
ret void
}