fputnam(file, str) struct _buf *file;
char *str;
{
  char c;
  char quoted;
  quoted = 0;
  while (c = *str++) {
    if (c == '\'')
      quoted ^= 1;
    else if (c == '\n')
      putc2('\r', file);
    if (c == '_' && !quoted)
      putc2('Q', file);
    else
      putc2(c, file);
  }
}


x86 assembly (clang, -O0):
<fputnam>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movb   $0x0,-0x1a(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
mov    (%rax),%al
mov    %al,-0x19(%rbp)
cmp    $0x0,%al
je     11be <fputnam+0xae>
movsbl -0x19(%rbp),%eax
cmp    $0x27,%eax
jne    115c <fputnam+0x4c>
movsbl -0x1a(%rbp),%eax
xor    $0x1,%eax
mov    %al,-0x1a(%rbp)
jmp    117e <fputnam+0x6e>
movsbl -0x19(%rbp),%eax
cmp    $0xa,%eax
jne    1179 <fputnam+0x69>
mov    -0x10(%rbp),%rsi
mov    $0xd,%edi
mov    $0x0,%al
call   1030 <putc2@plt>
jmp    117e <fputnam+0x6e>
movsbl -0x19(%rbp),%eax
cmp    $0x5f,%eax
jne    11aa <fputnam+0x9a>
cmpb   $0x0,-0x1a(%rbp)
jne    11aa <fputnam+0x9a>
mov    -0x10(%rbp),%rsi
mov    $0x51,%edi
mov    $0x0,%al
call   1030 <putc2@plt>
jmp    11b9 <fputnam+0xa9>
movsbl -0x19(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x0,%al
call   1030 <putc2@plt>
jmp    1124 <fputnam+0x14>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fputnam>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    (%rsi),%bl
test   %bl,%bl
je     1184 <fputnam+0x74>
mov    %rsi,%rbp
mov    %rdi,%r14
add    $0x1,%rbp
xor    %r15d,%r15d
jmp    1158 <fputnam+0x48>
nopl   0x0(%rax)
mov    $0xd,%edi
mov    %r14,%rsi
xor    %eax,%eax
call   1030 <putc2@plt>
movsbl %bl,%edi
mov    %r14,%rsi
xor    %eax,%eax
call   1030 <putc2@plt>
movzbl 0x0(%rbp),%ebx
add    $0x1,%rbp
test   %bl,%bl
je     1184 <fputnam+0x74>
cmp    $0xa,%bl
je     1130 <fputnam+0x20>
cmp    $0x27,%bl
jne    1170 <fputnam+0x60>
xor    $0x1,%r15b
jmp    113f <fputnam+0x2f>
nopl   0x0(%rax,%rax,1)
cmp    $0x5f,%bl
jne    113f <fputnam+0x2f>
test   %r15b,%r15b
jne    113f <fputnam+0x2f>
xor    %r15d,%r15d
mov    $0x51,%edi
jmp    1142 <fputnam+0x32>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct._buf = type opaque
define i32 @fputnam(%struct._buf* %0, i8* nocapture %1) {
%3 = load i8, i8* %1
%4 = icmp eq i8 %3, 0
br i1 %4, label %27, label %5
5: ; preds = %2, %21
%6 = phi i8 [ %25, %21 ], [ %3, %2 ]
%7 = phi i8* [ %9, %21 ], [ %1, %2 ]
%8 = phi i8 [ %23, %21 ], [ 0, %2 ]
%9 = getelementptr inbounds i8, i8* %7, i64 1
%10 = sext i8 %6 to i32
switch i8 %6, label %15 [
i8 39, label %11
i8 10, label %13
]
11: ; preds = %5
%12 = xor i8 %8, 1
br label %19
13: ; preds = %5
%14 = tail call i32 (i32, %struct._buf*, ...) bitcast (i32 (...)* @putc2 to i32 (i32, %struct._buf*, ...)*)(i32 13, %struct._buf* %0)
br label %19
15: ; preds = %5
%16 = icmp ne i8 %6, 95
%17 = icmp ne i8 %8, 0
%18 = select i1 %16, i1 true, i1 %17
br i1 %18, label %19, label %21
19: ; preds = %11, %13, %15
%20 = phi i8 [ %8, %15 ], [ %8, %13 ], [ %12, %11 ]
br label %21
21: ; preds = %15, %19
%22 = phi i32 [ %10, %19 ], [ 81, %15 ]
%23 = phi i8 [ %20, %19 ], [ 0, %15 ]
%24 = tail call i32 (i32, %struct._buf*, ...) bitcast (i32 (...)* @putc2 to i32 (i32, %struct._buf*, ...)*)(i32 %22, %struct._buf* %0)
%25 = load i8, i8* %9
%26 = icmp eq i8 %25, 0
br i1 %26, label %27, label %5
27: ; preds = %21, %2
ret i32 undef
}