#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 void *uint8_t;
typedef int int16_t;

/* Variables and functions */
void *read_bits(int);
int read_value();
int16_t get_value(int16_t old_value) {
  uint8_t tmp = read_bits(1);
  int16_t res = 0;
  if (tmp) {
    res = read_bits(4);
    if (res & (1 << (4 - 1))) {
      res |= ~((1 << 4) - 1);
    }
    res += old_value;
  } else {
    res = read_value();
  }
  return res;
}


x86 assembly (clang, -O0):
<get_value>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    $0x1,%edi
call   1040 <read_bits@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     117e <get_value+0x5e>
mov    $0x4,%edi
call   1040 <read_bits@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
and    $0x8,%eax
cmp    $0x0,%eax
je     1170 <get_value+0x50>
mov    -0x14(%rbp),%eax
or     $0xfffffff0,%eax
mov    %eax,-0x14(%rbp)
mov    -0x4(%rbp),%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
jmp    1188 <get_value+0x68>
mov    $0x0,%al
call   1030 <read_value@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_value>:
push   %rbx
mov    %edi,%ebx
mov    $0x1,%edi
call   1040 <read_bits@plt>
test   %rax,%rax
je     114a <get_value+0x2a>
mov    $0x4,%edi
call   1040 <read_bits@plt>
mov    %eax,%ecx
or     $0xfffffff0,%ecx
test   $0x8,%al
cmovne %ecx,%eax
add    %ebx,%eax
pop    %rbx
ret
xor    %eax,%eax
pop    %rbx
jmp    1030 <read_value@plt>

LLVM IR (-O0):
define i32 @get_value(i32 %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
store i32 %0, i32* %2
%5 = call i8* @read_bits(i32 1)
store i8* %5, i8** %3
store i32 0, i32* %4
%6 = load i8*, i8** %3
%7 = icmp ne i8* %6, null
br i1 %7, label %8, label %21
8: ; preds = %1
%9 = call i8* @read_bits(i32 4)
%10 = ptrtoint i8* %9 to i32
store i32 %10, i32* %4
%11 = load i32, i32* %4
%12 = and i32 %11, 8
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %8
%15 = load i32, i32* %4
%16 = or i32 %15, -16
store i32 %16, i32* %4
br label %17
17: ; preds = %14, %8
%18 = load i32, i32* %2
%19 = load i32, i32* %4
%20 = add nsw i32 %19, %18
store i32 %20, i32* %4
br label %23
21: ; preds = %1
%22 = call i32 (...) @read_value()
store i32 %22, i32* %4
br label %23
23: ; preds = %21, %17
%24 = load i32, i32* %4
ret i32 %24
}

LLVM IR (-O2):
define i32 @get_value(i32 %0) {
%2 = tail call i8* @read_bits(i32 1)
%3 = icmp eq i8* %2, null
br i1 %3, label %13, label %4
4: ; preds = %1
%5 = tail call i8* @read_bits(i32 4)
%6 = ptrtoint i8* %5 to i64
%7 = trunc i64 %6 to i32
%8 = and i32 %7, 8
%9 = icmp eq i32 %8, 0
%10 = or i32 %7, -16
%11 = select i1 %9, i32 %7, i32 %10
%12 = add nsw i32 %11, %0
br label %15
13: ; preds = %1
%14 = tail call i32 (...) @read_value()
br label %15
15: ; preds = %13, %4
%16 = phi i32 [ %12, %4 ], [ %14, %13 ]
ret i32 %16
}