#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 */
struct bitstream {
  int dummy;
};

/* Variables and functions */
int bits_get(struct bitstream *, int);
int /*<<< orphan*/ memset(int *, int /*<<< orphan*/, int);
void bits_get_signed_linear_array(struct bitstream *bits, int *array, int size,
                                  int n) {
  if (n == 0) {
    memset(array, 0, sizeof(*array) * size);
  } else
    for (int i = 0; i < size; i++) {
      int v = bits_get(bits, n);
      array[i] = (v >> 1) ^ -(v & 1);
    }
}


x86 assembly (clang, -O0):
<bits_get_signed_linear_array>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    115a <bits_get_signed_linear_array+0x3a>
mov    -0x10(%rbp),%rdi
movslq -0x14(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edx
xor    %esi,%esi
call   1040 <memset@plt>
jmp    11ac <bits_get_signed_linear_array+0x8c>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11a7 <bits_get_signed_linear_array+0x87>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%esi
call   1030 <bits_get@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%edx
sar    $0x1,%edx
mov    -0x20(%rbp),%ecx
and    $0x1,%ecx
xor    %eax,%eax
sub    %ecx,%eax
xor    %eax,%edx
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1161 <bits_get_signed_linear_array+0x41>
jmp    11ac <bits_get_signed_linear_array+0x8c>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bits_get_signed_linear_array>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
test   %ecx,%ecx
je     116c <bits_get_signed_linear_array+0x4c>
test   %edx,%edx
jle    1163 <bits_get_signed_linear_array+0x43>
mov    %ecx,%r12d
mov    %rdi,%r15
mov    %edx,%ebx
xor    %ebp,%ebp
nopl   (%rax)
mov    %r15,%rdi
mov    %r12d,%esi
call   1030 <bits_get@plt>
mov    %eax,%ecx
sar    %ecx
and    $0x1,%eax
neg    %eax
xor    %ecx,%eax
mov    %eax,(%r14,%rbp,4)
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    1140 <bits_get_signed_linear_array+0x20>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
shl    $0x2,%edx
mov    %r14,%rdi
xor    %esi,%esi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <memset@plt>

LLVM IR (-O0):
%struct.bitstream = type { i32 }
define void @bits_get_signed_linear_array(%struct.bitstream* %0, i32* %1, i32 %2, i32 %3) {
%5 = alloca %struct.bitstream*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store %struct.bitstream* %0, %struct.bitstream** %5
store i32* %1, i32** %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%11 = load i32, i32* %8
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %20
13: ; preds = %4
%14 = load i32*, i32** %6
%15 = load i32, i32* %7
%16 = sext i32 %15 to i64
%17 = mul i64 4, %16
%18 = trunc i64 %17 to i32
%19 = call i32 @memset(i32* %14, i32 0, i32 %18)
br label %43
20: ; preds = %4
store i32 0, i32* %9
br label %21
21: ; preds = %39, %20
%22 = load i32, i32* %9
%23 = load i32, i32* %7
%24 = icmp slt i32 %22, %23
br i1 %24, label %25, label %42
25: ; preds = %21
%26 = load %struct.bitstream*, %struct.bitstream** %5
%27 = load i32, i32* %8
%28 = call i32 @bits_get(%struct.bitstream* %26, i32 %27)
store i32 %28, i32* %10
%29 = load i32, i32* %10
%30 = ashr i32 %29, 1
%31 = load i32, i32* %10
%32 = and i32 %31, 1
%33 = sub nsw i32 0, %32
%34 = xor i32 %30, %33
%35 = load i32*, i32** %6
%36 = load i32, i32* %9
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i32, i32* %35, i64 %37
store i32 %34, i32* %38
br label %39
39: ; preds = %25
%40 = load i32, i32* %9
%41 = add nsw i32 %40, 1
store i32 %41, i32* %9
br label %21
42: ; preds = %21
br label %43
43: ; preds = %42, %13
ret void
}

LLVM IR (-O2):
%struct.bitstream = type { i32 }
define void @bits_get_signed_linear_array(%struct.bitstream* %0, i32* %1, i32 %2, i32 %3) {
%5 = icmp eq i32 %3, 0
br i1 %5, label %10, label %6
6: ; preds = %4
%7 = icmp sgt i32 %2, 0
br i1 %7, label %8, label %23
8: ; preds = %6
%9 = zext i32 %2 to i64
br label %13
10: ; preds = %4
%11 = shl i32 %2, 2
%12 = tail call i32 @memset(i32* %1, i32 0, i32 %11)
br label %23
13: ; preds = %8, %13
%14 = phi i64 [ 0, %8 ], [ %21, %13 ]
%15 = tail call i32 @bits_get(%struct.bitstream* %0, i32 %3)
%16 = ashr i32 %15, 1
%17 = and i32 %15, 1
%18 = sub nsw i32 0, %17
%19 = xor i32 %16, %18
%20 = getelementptr inbounds i32, i32* %1, i64 %14
store i32 %19, i32* %20
%21 = add nuw nsw i64 %14, 1
%22 = icmp eq i64 %21, %9
br i1 %22, label %23, label %13
23: ; preds = %13, %6, %10
ret void
}