void unary_minus(const char *b, char *out) {
  while (*b != '\0') {
    if (*b == '-') {
      *out++ = '+';
      b++;
    } else if (*b == '+') {
      *out++ = '-';
      b++;
    } else {
      *out++ = *b++;
    }
  }
  *out = '\0';
}


x86 assembly (clang, -O0):
<unary_minus>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11ae <unary_minus+0xae>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2d,%eax
jne    114f <unary_minus+0x4f>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movb   $0x2b,(%rax)
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    11a9 <unary_minus+0xa9>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2b,%eax
jne    1182 <unary_minus+0x82>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movb   $0x2d,(%rax)
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    11a4 <unary_minus+0xa4>
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
mov    (%rax),%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
jmp    11a9 <unary_minus+0xa9>
jmp    110c <unary_minus+0xc>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
pop    %rbp
ret

x86 assembly (clang, -O2):
<unary_minus>:
xor    %eax,%eax
jmp    1119 <unary_minus+0x19>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    $0x2d,%dl
mov    %dl,(%rsi,%rax,1)
add    $0x1,%rax
movzbl (%rdi,%rax,1),%ecx
cmp    $0x2b,%cl
je     1110 <unary_minus+0x10>
mov    $0x2b,%dl
cmp    $0x2d,%cl
je     1112 <unary_minus+0x12>
test   %cl,%cl
je     1131 <unary_minus+0x31>
mov    %ecx,%edx
jmp    1112 <unary_minus+0x12>
movb   $0x0,(%rsi,%rax,1)
ret

LLVM IR (-O0):
define void @unary_minus(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
store i8* %0, i8** %3
store i8* %1, i8** %4
br label %5
5: ; preds = %37, %2
%6 = load i8*, i8** %3
%7 = load i8, i8* %6
%8 = sext i8 %7 to i32
%9 = icmp ne i32 %8, 0
br i1 %9, label %10, label %38
10: ; preds = %5
%11 = load i8*, i8** %3
%12 = load i8, i8* %11
%13 = sext i8 %12 to i32
%14 = icmp eq i32 %13, 45
br i1 %14, label %15, label %20
15: ; preds = %10
%16 = load i8*, i8** %4
%17 = getelementptr inbounds i8, i8* %16, i32 1
store i8* %17, i8** %4
store i8 43, i8* %16
%18 = load i8*, i8** %3
%19 = getelementptr inbounds i8, i8* %18, i32 1
store i8* %19, i8** %3
br label %37
20: ; preds = %10
%21 = load i8*, i8** %3
%22 = load i8, i8* %21
%23 = sext i8 %22 to i32
%24 = icmp eq i32 %23, 43
br i1 %24, label %25, label %30
25: ; preds = %20
%26 = load i8*, i8** %4
%27 = getelementptr inbounds i8, i8* %26, i32 1
store i8* %27, i8** %4
store i8 45, i8* %26
%28 = load i8*, i8** %3
%29 = getelementptr inbounds i8, i8* %28, i32 1
store i8* %29, i8** %3
br label %36
30: ; preds = %20
%31 = load i8*, i8** %3
%32 = getelementptr inbounds i8, i8* %31, i32 1
store i8* %32, i8** %3
%33 = load i8, i8* %31
%34 = load i8*, i8** %4
%35 = getelementptr inbounds i8, i8* %34, i32 1
store i8* %35, i8** %4
store i8 %33, i8* %34
br label %36
36: ; preds = %30, %25
br label %37
37: ; preds = %36, %15
br label %5
38: ; preds = %5
%39 = load i8*, i8** %4
store i8 0, i8* %39
ret void
}

LLVM IR (-O2):
define void @unary_minus(i8* nocapture %0, i8* nocapture writeonly %1) {
br label %3
3: ; preds = %9, %2
%4 = phi i8* [ %0, %2 ], [ %11, %9 ]
%5 = phi i8* [ %1, %2 ], [ %12, %9 ]
%6 = load i8, i8* %4
switch i8 %6, label %8 [
i8 0, label %13
i8 45, label %9
i8 43, label %7
]
7: ; preds = %3
br label %9
8: ; preds = %3
br label %9
9: ; preds = %3, %7, %8
%10 = phi i8 [ 45, %7 ], [ %6, %8 ], [ 43, %3 ]
store i8 %10, i8* %5
%11 = getelementptr inbounds i8, i8* %4, i64 1
%12 = getelementptr inbounds i8, i8* %5, i64 1
br label %3
13: ; preds = %3
store i8 0, i8* %5
ret void
}