void replace_double_comma(char cmd[], int put_null) {
  char *p = cmd;
  while (*cmd != '\0') {
    if (cmd[0] == ',') {
      if (cmd[1] == ',') {
        *p++ = *cmd++;
        cmd++;
        continue;
      } else if (put_null) {
        break;
      }
    }
    *p++ = *cmd++;
  }
  *p = '\0';
}


x86 assembly (clang, -O0):
<replace_double_comma>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11b7 <replace_double_comma+0xb7>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2c,%eax
jne    1190 <replace_double_comma+0x90>
mov    -0x8(%rbp),%rax
movsbl 0x1(%rax),%eax
cmp    $0x2c,%eax
jne    1177 <replace_double_comma+0x77>
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
mov    (%rax),%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    1113 <replace_double_comma+0x13>
cmpl   $0x0,-0xc(%rbp)
je     1186 <replace_double_comma+0x86>
jmp    11b7 <replace_double_comma+0xb7>
jmp    118b <replace_double_comma+0x8b>
jmp    1190 <replace_double_comma+0x90>
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
mov    (%rax),%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
jmp    1113 <replace_double_comma+0x13>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
pop    %rbp
ret

x86 assembly (clang, -O2):
<replace_double_comma>:
mov    %rdi,%rax
jmp    111b <replace_double_comma+0x1b>
cs nopw 0x0(%rax,%rax,1)
nop
movb   $0x2c,(%rax)
add    $0x2,%rdi
add    $0x1,%rax
movzbl (%rdi),%ecx
cmp    $0x2c,%cl
je     1130 <replace_double_comma+0x30>
test   %cl,%cl
jne    113a <replace_double_comma+0x3a>
jmp    1146 <replace_double_comma+0x46>
nopl   0x0(%rax)
cmpb   $0x2c,0x1(%rdi)
je     1110 <replace_double_comma+0x10>
test   %esi,%esi
jne    1146 <replace_double_comma+0x46>
add    $0x1,%rdi
mov    %cl,(%rax)
add    $0x1,%rax
jmp    111b <replace_double_comma+0x1b>
movb   $0x0,(%rax)
ret

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

LLVM IR (-O2):
define void @replace_double_comma(i8* nocapture %0, i32 %1) {
%3 = icmp eq i32 %1, 0
br label %4
4: ; preds = %14, %2
%5 = phi i8* [ %0, %2 ], [ %15, %14 ]
%6 = phi i8* [ %0, %2 ], [ %16, %14 ]
%7 = load i8, i8* %5
switch i8 %7, label %18 [
i8 0, label %20
i8 44, label %8
]
8: ; preds = %4
%9 = getelementptr inbounds i8, i8* %5, i64 1
%10 = load i8, i8* %9
%11 = icmp eq i8 %10, 44
br i1 %11, label %12, label %17
12: ; preds = %8
store i8 44, i8* %6
%13 = getelementptr inbounds i8, i8* %5, i64 2
br label %14
14: ; preds = %12, %18
%15 = phi i8* [ %13, %12 ], [ %19, %18 ]
%16 = getelementptr inbounds i8, i8* %6, i64 1
br label %4
17: ; preds = %8
br i1 %3, label %18, label %20
18: ; preds = %4, %17
%19 = getelementptr inbounds i8, i8* %5, i64 1
store i8 %7, i8* %6
br label %14
20: ; preds = %4, %17
store i8 0, i8* %6
ret void
}