#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 rbsp {
  int dummy;
};

/* Variables and functions */
int rbsp_read_uev(struct rbsp *, unsigned int *);
int rbsp_read_sev(struct rbsp *rbsp, int *val) {
  unsigned int tmp;
  int ret;
  ret = rbsp_read_uev(rbsp, &tmp);
  if (ret)
    return ret;
  if (val) {
    if (tmp & 1)
      *val = (tmp + 1) / 2;
    else
      *val = -(tmp / 2);
  }
  return 0;
}


x86 assembly (clang, -O0):
<rbsp_read_sev>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
call   1030 <rbsp_read_uev@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     1145 <rbsp_read_sev+0x35>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    118f <rbsp_read_sev+0x7f>
cmpq   $0x0,-0x18(%rbp)
je     1188 <rbsp_read_sev+0x78>
mov    -0x1c(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     1173 <rbsp_read_sev+0x63>
mov    -0x1c(%rbp),%ecx
add    $0x1,%ecx
shr    $0x1,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
jmp    1183 <rbsp_read_sev+0x73>
mov    -0x1c(%rbp),%eax
shr    $0x1,%eax
xor    %ecx,%ecx
sub    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
jmp    1188 <rbsp_read_sev+0x78>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rbsp_read_sev>:
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
lea    0xc(%rsp),%rsi
call   1030 <rbsp_read_uev@plt>
test   %rbx,%rbx
je     1144 <rbsp_read_sev+0x34>
test   %eax,%eax
jne    1144 <rbsp_read_sev+0x34>
mov    0xc(%rsp),%ecx
mov    %ecx,%edx
shr    %edx
neg    %edx
mov    %ecx,%esi
add    $0x1,%esi
shr    %esi
test   $0x1,%cl
cmove  %edx,%esi
mov    %esi,(%rbx)
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.rbsp = type { i32 }
define i32 @rbsp_read_sev(%struct.rbsp* %0, i32* %1) {
%3 = alloca i32
%4 = alloca %struct.rbsp*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
store %struct.rbsp* %0, %struct.rbsp** %4
store i32* %1, i32** %5
%8 = load %struct.rbsp*, %struct.rbsp** %4
%9 = call i32 @rbsp_read_uev(%struct.rbsp* %8, i32* %6)
store i32 %9, i32* %7
%10 = load i32, i32* %7
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = load i32, i32* %7
store i32 %13, i32* %3
br label %33
14: ; preds = %2
%15 = load i32*, i32** %5
%16 = icmp ne i32* %15, null
br i1 %16, label %17, label %32
17: ; preds = %14
%18 = load i32, i32* %6
%19 = and i32 %18, 1
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %26
21: ; preds = %17
%22 = load i32, i32* %6
%23 = add i32 %22, 1
%24 = udiv i32 %23, 2
%25 = load i32*, i32** %5
store i32 %24, i32* %25
br label %31
26: ; preds = %17
%27 = load i32, i32* %6
%28 = udiv i32 %27, 2
%29 = sub i32 0, %28
%30 = load i32*, i32** %5
store i32 %29, i32* %30
br label %31
31: ; preds = %26, %21
br label %32
32: ; preds = %31, %14
store i32 0, i32* %3
br label %33
33: ; preds = %32, %12
%34 = load i32, i32* %3
ret i32 %34
}

LLVM IR (-O2):
%struct.rbsp = type { i32 }
define i32 @rbsp_read_sev(%struct.rbsp* %0, i32* writeonly %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = call i32 @rbsp_read_uev(%struct.rbsp* %0, i32* %3)
%6 = icmp ne i32 %5, 0
%7 = icmp eq i32* %1, null
%8 = or i1 %7, %6
br i1 %8, label %18, label %9
9: ; preds = %2
%10 = load i32, i32* %3
%11 = and i32 %10, 1
%12 = icmp eq i32 %11, 0
%13 = lshr i32 %10, 1
%14 = sub nsw i32 0, %13
%15 = add i32 %10, 1
%16 = lshr i32 %15, 1
%17 = select i1 %12, i32 %14, i32 %16
store i32 %17, i32* %1
br label %18
18: ; preds = %9, %2
ret i32 %5
}