#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 char uint8_t;

/* Variables and functions */
int write_bits(char *, int *, short, int, int *);
void write_val(char *out, int *out_pos, uint8_t *store, int *p, short val,
               int vlen) {
  int vp = 0;
  while (1) {
    int ret = write_bits(store, p, val, vlen, &vp);
    if (ret == 1) {
      out[*out_pos] = *store;
      (*out_pos)++;
      (*store) = 0;
      (*p) = 0;
    } else if (ret == 2) {
      break;
    }
  }
}


x86 assembly (clang, -O0):
<write_val>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %r8w,%ax
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %ax,-0x22(%rbp)
mov    %r9d,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x22(%rbp),%ax
mov    -0x28(%rbp),%ecx
lea    -0x2c(%rbp),%r8
movswl %ax,%edx
call   1030 <write_bits@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x1,-0x30(%rbp)
jne    1198 <write_val+0x88>
mov    -0x18(%rbp),%rax
mov    (%rax),%dl
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
movslq (%rcx),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rax
movl   $0x0,(%rax)
jmp    11ac <write_val+0x9c>
cmpl   $0x2,-0x30(%rbp)
jne    11a7 <write_val+0x97>
jmp    11b1 <write_val+0xa1>
jmp    11ac <write_val+0x9c>
jmp    113b <write_val+0x2b>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_val>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r9d,%r14d
mov    %rcx,%rbx
mov    %rdx,%rbp
mov    %rsi,%r15
mov    %rdi,0x10(%rsp)
movl   $0x0,0xc(%rsp)
movswl %r8w,%r13d
lea    0xc(%rsp),%r12
jmp    116d <write_val+0x5d>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movzbl 0x0(%rbp),%eax
movslq (%r15),%rcx
mov    0x10(%rsp),%rdx
mov    %al,(%rdx,%rcx,1)
addl   $0x1,(%r15)
movb   $0x0,0x0(%rbp)
movl   $0x0,(%rbx)
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %r13d,%edx
mov    %r14d,%ecx
mov    %r12,%r8
call   1030 <write_bits@plt>
cmp    $0x1,%eax
je     1150 <write_val+0x40>
cmp    $0x2,%eax
jne    116d <write_val+0x5d>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define void @write_val(i8* nocapture writeonly %0, i32* nocapture %1, i8* %2, i32* %3, i16 signext %4, i32 %5) {
%7 = alloca i32
%8 = bitcast i32* %7 to i8*
store i32 0, i32* %7
br label %9
9: ; preds = %18, %6
%10 = call i32 @write_bits(i8* %2, i32* %3, i16 signext %4, i32 %5, i32* %7)
switch i32 %10, label %18 [
i32 1, label %11
i32 2, label %19
]
11: ; preds = %9
%12 = load i8, i8* %2
%13 = load i32, i32* %1
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds i8, i8* %0, i64 %14
store i8 %12, i8* %15
%16 = load i32, i32* %1
%17 = add nsw i32 %16, 1
store i32 %17, i32* %1
store i8 0, i8* %2
store i32 0, i32* %3
br label %18
18: ; preds = %11, %9
br label %9
19: ; preds = %9
ret void
}