#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 */
unsigned long pop_unsigned_int(char *, int);
long pop_signed_int(char *source, int num_bits) {
  unsigned long result = pop_unsigned_int(source, num_bits);
  unsigned long test_bit = (unsigned long)(1) << (num_bits - 1);
  if (result & test_bit) {
    result ^= test_bit;
    if (!result)
      return -test_bit;
    else
      return -result;
  } else
    return result;
}


x86 assembly (clang, -O0):
<pop_signed_int>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1030 <pop_unsigned_int@plt>
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
mov    %eax,%eax
mov    %eax,%ecx
mov    $0x1,%eax
shl    %cl,%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
and    -0x28(%rbp),%rax
cmp    $0x0,%rax
je     118c <pop_signed_int+0x7c>
mov    -0x28(%rbp),%rax
xor    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    117d <pop_signed_int+0x6d>
xor    %eax,%eax
sub    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1194 <pop_signed_int+0x84>
xor    %eax,%eax
sub    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1194 <pop_signed_int+0x84>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pop_signed_int>:
push   %rbx
mov    %esi,%ebx
call   1030 <pop_unsigned_int@plt>
add    $0xff,%bl
mov    $0x1,%edx
mov    %ebx,%ecx
shl    %cl,%rdx
movzbl %bl,%ecx
bt     %rcx,%rax
jae    1136 <pop_signed_int+0x26>
xor    %rdx,%rax
je     1138 <pop_signed_int+0x28>
neg    %rax
pop    %rbx
ret
neg    %rdx
mov    %rdx,%rax
pop    %rbx
ret

LLVM IR (-O0):
define i64 @pop_signed_int(i8* %0, i32 %1) {
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i64
%7 = alloca i64
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = load i8*, i8** %4
%9 = load i32, i32* %5
%10 = call i64 @pop_unsigned_int(i8* %8, i32 %9)
store i64 %10, i64* %6
%11 = load i32, i32* %5
%12 = sub nsw i32 %11, 1
%13 = zext i32 %12 to i64
%14 = shl i64 1, %13
store i64 %14, i64* %7
%15 = load i64, i64* %6
%16 = load i64, i64* %7
%17 = and i64 %15, %16
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %31
19: ; preds = %2
%20 = load i64, i64* %7
%21 = load i64, i64* %6
%22 = xor i64 %21, %20
store i64 %22, i64* %6
%23 = load i64, i64* %6
%24 = icmp ne i64 %23, 0
br i1 %24, label %28, label %25
25: ; preds = %19
%26 = load i64, i64* %7
%27 = sub i64 0, %26
store i64 %27, i64* %3
br label %33
28: ; preds = %19
%29 = load i64, i64* %6
%30 = sub i64 0, %29
store i64 %30, i64* %3
br label %33
31: ; preds = %2
%32 = load i64, i64* %6
store i64 %32, i64* %3
br label %33
33: ; preds = %31, %28, %25
%34 = load i64, i64* %3
ret i64 %34
}

LLVM IR (-O2):
define i64 @pop_signed_int(i8* %0, i32 %1) {
%3 = tail call i64 @pop_unsigned_int(i8* %0, i32 %1)
%4 = add nsw i32 %1, -1
%5 = zext i32 %4 to i64
%6 = shl nuw i64 1, %5
%7 = and i64 %3, %6
%8 = icmp eq i64 %7, 0
br i1 %8, label %16, label %9
9: ; preds = %2
%10 = xor i64 %3, %6
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = sub i64 0, %6
br label %16
14: ; preds = %9
%15 = sub i64 0, %10
br label %16
16: ; preds = %2, %14, %12
%17 = phi i64 [ %15, %14 ], [ %13, %12 ], [ %3, %2 ]
ret i64 %17
}