#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 unsigned long uint32_t;
typedef int int32_t;

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int32_t SignExtend(uint32_t u32, int32_t bits) {
  assert(0 < bits && bits <= 32);
  if (u32 & (1UL << (bits - 1))) {
    return (int32_t)(u32 | (0xFFFFFFFFU << (bits - 1)));
  } else {
    return (int32_t)u32;
  }
}


x86 assembly (clang, -O0):
<SignExtend>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
xor    %eax,%eax
xor    %ecx,%ecx
cmp    -0x14(%rbp),%ecx
mov    %al,-0x15(%rbp)
jge    1139 <SignExtend+0x29>
cmpl   $0x20,-0x14(%rbp)
setle  %al
mov    %al,-0x15(%rbp)
mov    -0x15(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
mov    %ecx,%ecx
mov    $0x1,%edx
shl    %cl,%rdx
mov    %rdx,%rcx
and    %rcx,%rax
cmp    $0x0,%rax
je     118a <SignExtend+0x7a>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
mov    $0xffffffff,%edx
shl    %cl,%edx
mov    %edx,%ecx
mov    %ecx,%ecx
or     %rcx,%rax
mov    %eax,-0x4(%rbp)
jmp    1191 <SignExtend+0x81>
mov    -0x10(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<SignExtend>:
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %rdi,%r14
add    $0xffffffff,%ebx
xor    %edi,%edi
cmp    $0x20,%ebx
setb   %dil
call   1030 <assert@plt>
mov    $0xffffffff,%eax
mov    %ebx,%ecx
shl    %cl,%eax
xor    %ecx,%ecx
bt     %rbx,%r14
cmovb  %eax,%ecx
or     %ecx,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
define i32 @SignExtend(i64 %0, i32 %1) {
%3 = add i32 %1, -1
%4 = icmp ult i32 %3, 32
%5 = zext i1 %4 to i32
%6 = tail call i32 @assert(i32 %5)
%7 = zext i32 %3 to i64
%8 = shl nuw i64 1, %7
%9 = and i64 %8, %0
%10 = icmp eq i64 %9, 0
%11 = shl nsw i32 -1, %3
%12 = zext i32 %11 to i64
%13 = select i1 %10, i64 0, i64 %12
%14 = or i64 %13, %0
%15 = trunc i64 %14 to i32
ret i32 %15
}