#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 uint8_t;
typedef int int32_t;

/* Variables and functions */
size_t uint32_pack(int, int *);
size_t int32_pack(int32_t value, uint8_t *out) {
  if (value < 0) {
    out[0] = value | 0x80;
    out[1] = (value >> 7) | 0x80;
    out[2] = (value >> 14) | 0x80;
    out[3] = (value >> 21) | 0x80;
    out[4] = (value >> 28) | 0x80;
    out[5] = out[6] = out[7] = out[8] = 0xff;
    out[9] = 0x01;
    return 10;
  } else {
    return uint32_pack(value, out);
  }
}


x86 assembly (clang, -O0):
<int32_pack>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11c8 <int32_pack+0xb8>
mov    -0xc(%rbp),%ecx
or     $0x80,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0xc(%rbp),%ecx
sar    $0x7,%ecx
or     $0x80,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0xc(%rbp),%ecx
sar    $0xe,%ecx
or     $0x80,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0xc(%rbp),%ecx
sar    $0x15,%ecx
or     $0x80,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,0xc(%rax)
mov    -0xc(%rbp),%ecx
sar    $0x1c,%ecx
or     $0x80,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,0x10(%rax)
mov    -0x18(%rbp),%rax
movl   $0xff,0x20(%rax)
mov    -0x18(%rbp),%rax
movl   $0xff,0x1c(%rax)
mov    -0x18(%rbp),%rax
movl   $0xff,0x18(%rax)
mov    -0x18(%rbp),%rax
movl   $0xff,0x14(%rax)
mov    -0x18(%rbp),%rax
movl   $0x1,0x24(%rax)
movq   $0xa,-0x8(%rbp)
jmp    11d8 <int32_pack+0xc8>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1030 <uint32_pack@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<int32_pack>:
test   %edi,%edi
js     1119 <int32_pack+0x9>
jmp    1030 <uint32_pack@plt>
mov    %edi,%eax
or     $0x80,%eax
mov    %eax,(%rsi)
mov    %edi,%eax
sar    $0x7,%eax
or     $0x80,%eax
mov    %eax,0x4(%rsi)
mov    %edi,%eax
sar    $0xe,%eax
or     $0x80,%eax
mov    %eax,0x8(%rsi)
mov    %edi,%eax
sar    $0x15,%eax
or     $0x80,%eax
mov    %eax,0xc(%rsi)
sar    $0x1c,%edi
or     $0x80,%edi
mov    %edi,0x10(%rsi)
movaps 0xea4(%rip),%xmm0
movups %xmm0,0x14(%rsi)
movl   $0x1,0x24(%rsi)
mov    $0xa,%eax
ret

LLVM IR (-O0):
define i64 @int32_pack(i32 %0, i32* %1) {
%3 = alloca i64
%4 = alloca i32
%5 = alloca i32*
store i32 %0, i32* %4
store i32* %1, i32** %5
%6 = load i32, i32* %4
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %43
8: ; preds = %2
%9 = load i32, i32* %4
%10 = or i32 %9, 128
%11 = load i32*, i32** %5
%12 = getelementptr inbounds i32, i32* %11, i64 0
store i32 %10, i32* %12
%13 = load i32, i32* %4
%14 = ashr i32 %13, 7
%15 = or i32 %14, 128
%16 = load i32*, i32** %5
%17 = getelementptr inbounds i32, i32* %16, i64 1
store i32 %15, i32* %17
%18 = load i32, i32* %4
%19 = ashr i32 %18, 14
%20 = or i32 %19, 128
%21 = load i32*, i32** %5
%22 = getelementptr inbounds i32, i32* %21, i64 2
store i32 %20, i32* %22
%23 = load i32, i32* %4
%24 = ashr i32 %23, 21
%25 = or i32 %24, 128
%26 = load i32*, i32** %5
%27 = getelementptr inbounds i32, i32* %26, i64 3
store i32 %25, i32* %27
%28 = load i32, i32* %4
%29 = ashr i32 %28, 28
%30 = or i32 %29, 128
%31 = load i32*, i32** %5
%32 = getelementptr inbounds i32, i32* %31, i64 4
store i32 %30, i32* %32
%33 = load i32*, i32** %5
%34 = getelementptr inbounds i32, i32* %33, i64 8
store i32 255, i32* %34
%35 = load i32*, i32** %5
%36 = getelementptr inbounds i32, i32* %35, i64 7
store i32 255, i32* %36
%37 = load i32*, i32** %5
%38 = getelementptr inbounds i32, i32* %37, i64 6
store i32 255, i32* %38
%39 = load i32*, i32** %5
%40 = getelementptr inbounds i32, i32* %39, i64 5
store i32 255, i32* %40
%41 = load i32*, i32** %5
%42 = getelementptr inbounds i32, i32* %41, i64 9
store i32 1, i32* %42
store i64 10, i64* %3
br label %47
43: ; preds = %2
%44 = load i32, i32* %4
%45 = load i32*, i32** %5
%46 = call i64 @uint32_pack(i32 %44, i32* %45)
store i64 %46, i64* %3
br label %47
47: ; preds = %43, %8
%48 = load i64, i64* %3
ret i64 %48
}

LLVM IR (-O2):
define i64 @int32_pack(i32 %0, i32* %1) {
%3 = icmp slt i32 %0, 0
br i1 %3, label %4, label %21
4: ; preds = %2
%5 = or i32 %0, 128
store i32 %5, i32* %1
%6 = ashr i32 %0, 7
%7 = or i32 %6, 128
%8 = getelementptr inbounds i32, i32* %1, i64 1
store i32 %7, i32* %8
%9 = ashr i32 %0, 14
%10 = or i32 %9, 128
%11 = getelementptr inbounds i32, i32* %1, i64 2
store i32 %10, i32* %11
%12 = ashr i32 %0, 21
%13 = or i32 %12, 128
%14 = getelementptr inbounds i32, i32* %1, i64 3
store i32 %13, i32* %14
%15 = ashr i32 %0, 28
%16 = or i32 %15, 128
%17 = getelementptr inbounds i32, i32* %1, i64 4
store i32 %16, i32* %17
%18 = getelementptr inbounds i32, i32* %1, i64 5
%19 = bitcast i32* %18 to <4 x i32>*
store <4 x i32> <i32 255, i32 255, i32 255, i32 255>, <4 x i32>* %19
%20 = getelementptr inbounds i32, i32* %1, i64 9
store i32 1, i32* %20
br label %23
21: ; preds = %2
%22 = tail call i64 @uint32_pack(i32 %0, i32* %1)
br label %23
23: ; preds = %21, %4
%24 = phi i64 [ 10, %4 ], [ %22, %21 ]
ret i64 %24
}