#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 */
int clear_bit(int *, int);
int /*<<< orphan*/ printf(char *, int);
int set_bit(int *, int);
int toggle_bit(int *ptr, int n) {
  if (*ptr & (1 << n)) {
    printf("%dth bit is set.\n", n);
    return clear_bit(ptr, n);
  } else {
    printf("%dth bit is clear.\n", n);
    return set_bit(ptr, n);
  }
}


x86 assembly (clang, -O0):
<toggle_bit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    -0x14(%rbp),%ecx
mov    $0x1,%edx
shl    %cl,%edx
mov    %edx,%ecx
and    %ecx,%eax
cmp    $0x0,%eax
je     117f <toggle_bit+0x4f>
mov    -0x14(%rbp),%esi
lea    0xe9a(%rip),%rdi
call   1030 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1040 <clear_bit@plt>
mov    %eax,-0x4(%rbp)
jmp    119d <toggle_bit+0x6d>
mov    -0x14(%rbp),%esi
lea    0xe89(%rip),%rdi
call   1030 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1050 <set_bit@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<toggle_bit>:
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %rdi,%r14
mov    (%rdi),%eax
bt     %esi,%eax
jae    115f <toggle_bit+0x2f>
lea    0xeb9(%rip),%rdi
mov    %ebx,%esi
call   1030 <printf@plt>
mov    %r14,%rdi
mov    %ebx,%esi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1040 <clear_bit@plt>
lea    0xeac(%rip),%rdi
mov    %ebx,%esi
call   1030 <printf@plt>
mov    %r14,%rdi
mov    %ebx,%esi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1050 <set_bit@plt>

LLVM IR (-O0):
@.str = [18 x i8] c"%dth bit is set.\0A\00"
@.str.1 = [20 x i8] c"%dth bit is clear.\0A\00"
define i32 @toggle_bit(i32* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
store i32* %0, i32** %4
store i32 %1, i32* %5
%6 = load i32*, i32** %4
%7 = load i32, i32* %6
%8 = load i32, i32* %5
%9 = shl i32 1, %8
%10 = and i32 %7, %9
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %18
12: ; preds = %2
%13 = load i32, i32* %5
%14 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = load i32*, i32** %4
%16 = load i32, i32* %5
%17 = call i32 @clear_bit(i32* %15, i32 %16)
store i32 %17, i32* %3
br label %24
18: ; preds = %2
%19 = load i32, i32* %5
%20 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %19)
%21 = load i32*, i32** %4
%22 = load i32, i32* %5
%23 = call i32 @set_bit(i32* %21, i32 %22)
store i32 %23, i32* %3
br label %24
24: ; preds = %18, %12
%25 = load i32, i32* %3
ret i32 %25
}

LLVM IR (-O2):
@.str = [18 x i8] c"%dth bit is set.\0A\00"
@.str.1 = [20 x i8] c"%dth bit is clear.\0A\00"
define i32 @toggle_bit(i32* %0, i32 %1) {
%3 = load i32, i32* %0
%4 = shl nuw i32 1, %1
%5 = and i32 %3, %4
%6 = icmp eq i32 %5, 0
br i1 %6, label %10, label %7
7: ; preds = %2
%8 = tail call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %1)
%9 = tail call i32 @clear_bit(i32* %0, i32 %1)
br label %13
10: ; preds = %2
%11 = tail call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %1)
%12 = tail call i32 @set_bit(i32* %0, i32 %1)
br label %13
13: ; preds = %10, %7
%14 = phi i32 [ %9, %7 ], [ %12, %10 ]
ret i32 %14
}