#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 int /*<<< orphan*/ x;
typedef int /*<<< orphan*/ mask;

/* Variables and functions */
int CHAR_BIT;
int /*<<< orphan*/ printBits(int, unsigned int *);
int /*<<< orphan*/ printf(char *);
int invert(unsigned x, int p, int n) {
  int sz = sizeof(x) * CHAR_BIT;
  if ((n > (p + 1)) | (p >= sz)) {
    printf("invalid argument/s\n");
    return 0;
  }
  unsigned int mask;
  mask = (n == sz) ? ~0 : (~(~0 << n) << (p - n + 1));
  printf("mask: \n");
  printBits(sizeof(mask), &mask);
  printf("\n");
  return (~(x & mask) ^ ~mask) | (x & ~mask);
}


x86 assembly (clang, -O0):
<invert>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    0x2ea8(%rip),%rax
movslq (%rax),%rax
shl    $0x2,%rax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%eax
mov    -0xc(%rbp),%ecx
add    $0x1,%ecx
cmp    %ecx,%eax
setg   %al
and    $0x1,%al
movzbl %al,%eax
mov    -0xc(%rbp),%ecx
cmp    -0x14(%rbp),%ecx
setge  %cl
and    $0x1,%cl
movzbl %cl,%ecx
or     %ecx,%eax
cmp    $0x0,%eax
je     1187 <invert+0x67>
lea    0xe8a(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1208 <invert+0xe8>
mov    -0x10(%rbp),%eax
cmp    -0x14(%rbp),%eax
jne    11a0 <invert+0x80>
mov    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
jmp    11bb <invert+0x9b>
mov    -0x10(%rbp),%ecx
mov    $0xffffffff,%eax
shl    %cl,%eax
xor    $0xffffffff,%eax
mov    -0xc(%rbp),%ecx
sub    -0x10(%rbp),%ecx
add    $0x1,%ecx
shl    %cl,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x18(%rbp)
lea    0xe4c(%rip),%rdi
call   1040 <printf@plt>
mov    $0x4,%edi
lea    -0x18(%rbp),%rsi
call   1030 <printBits@plt>
lea    0xe38(%rip),%rdi
call   1040 <printf@plt>
mov    -0x8(%rbp),%eax
and    -0x18(%rbp),%eax
xor    $0xffffffff,%eax
mov    -0x18(%rbp),%ecx
xor    $0xffffffff,%ecx
xor    %ecx,%eax
mov    -0x8(%rbp),%ecx
mov    -0x18(%rbp),%edx
xor    $0xffffffff,%edx
and    %edx,%ecx
or     %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<invert>:
push   %rbx
sub    $0x10,%rsp
lea    0x1(%rsi),%eax
sub    %edx,%eax
jl     1198 <invert+0x68>
mov    %edi,%ebx
mov    0x2e9b(%rip),%rcx
mov    (%rcx),%edi
shl    $0x2,%edi
cmp    %esi,%edi
jle    1198 <invert+0x68>
mov    $0xffffffff,%r8d
mov    $0xffffffff,%esi
mov    %edx,%ecx
shl    %cl,%esi
not    %esi
mov    %eax,%ecx
shl    %cl,%esi
cmp    %edx,%edi
cmove  %r8d,%esi
mov    %esi,0xc(%rsp)
lea    0xe8c(%rip),%rdi
call   1040 <puts@plt>
lea    0xc(%rsp),%rsi
mov    $0x4,%edi
call   1050 <printBits@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
xor    0xc(%rsp),%ebx
jmp    11a6 <invert+0x76>
lea    0xe68(%rip),%rdi
call   1040 <puts@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@CHAR_BIT = global i32 0
@.str = [20 x i8] c"invalid argument/s\0A\00"
@.str.1 = [8 x i8] c"mask: \0A\00"
@.str.2 = [2 x i8] c"\0A\00"
define i32 @invert(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%10 = load i32, i32* @CHAR_BIT
%11 = sext i32 %10 to i64
%12 = mul i64 4, %11
%13 = trunc i64 %12 to i32
store i32 %13, i32* %8
%14 = load i32, i32* %7
%15 = load i32, i32* %6
%16 = add nsw i32 %15, 1
%17 = icmp sgt i32 %14, %16
%18 = zext i1 %17 to i32
%19 = load i32, i32* %6
%20 = load i32, i32* %8
%21 = icmp sge i32 %19, %20
%22 = zext i1 %21 to i32
%23 = or i32 %18, %22
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %3
%26 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %4
br label %58
27: ; preds = %3
%28 = load i32, i32* %7
%29 = load i32, i32* %8
%30 = icmp eq i32 %28, %29
br i1 %30, label %31, label %32
31: ; preds = %27
br label %41
32: ; preds = %27
%33 = load i32, i32* %7
%34 = shl i32 -1, %33
%35 = xor i32 %34, -1
%36 = load i32, i32* %6
%37 = load i32, i32* %7
%38 = sub nsw i32 %36, %37
%39 = add nsw i32 %38, 1
%40 = shl i32 %35, %39
br label %41
41: ; preds = %32, %31
%42 = phi i32 [ -1, %31 ], [ %40, %32 ]
store i32 %42, i32* %9
%43 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%44 = call i32 @printBits(i32 4, i32* %9)
%45 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%46 = load i32, i32* %5
%47 = load i32, i32* %9
%48 = and i32 %46, %47
%49 = xor i32 %48, -1
%50 = load i32, i32* %9
%51 = xor i32 %50, -1
%52 = xor i32 %49, %51
%53 = load i32, i32* %5
%54 = load i32, i32* %9
%55 = xor i32 %54, -1
%56 = and i32 %53, %55
%57 = or i32 %52, %56
store i32 %57, i32* %4
br label %58
58: ; preds = %41, %25
%59 = load i32, i32* %4
ret i32 %59
}

LLVM IR (-O2):
@CHAR_BIT = global i32 0
@str = [7 x i8] c"mask: \00"
@str.3 = [19 x i8] c"invalid argument/s\00"
define i32 @invert(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = load i32, i32* @CHAR_BIT
%6 = shl i32 %5, 2
%7 = add i32 %1, 1
%8 = icmp slt i32 %7, %2
%9 = icmp sle i32 %6, %1
%10 = or i1 %8, %9
br i1 %10, label %11, label %13
11: ; preds = %3
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.3, i64 0, i64 0))
br label %26
13: ; preds = %3
%14 = bitcast i32* %4 to i8*
%15 = icmp eq i32 %6, %2
%16 = shl nsw i32 -1, %2
%17 = xor i32 %16, -1
%18 = sub i32 %7, %2
%19 = shl i32 %17, %18
%20 = select i1 %15, i32 -1, i32 %19
store i32 %20, i32* %4
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str, i64 0, i64 0))
%22 = call i32 @printBits(i32 4, i32* %4)
%23 = call i32 @putchar(i32 10)
%24 = load i32, i32* %4
%25 = xor i32 %24, %0
br label %26
26: ; preds = %13, %11
%27 = phi i32 [ 0, %11 ], [ %25, %13 ]
ret i32 %27
}