#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 /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ memmove(char *, char *, int);
int /*<<< orphan*/ stderr;
char *strchr(char *, char);
int strlen(char *);
char *strrchr(char *, char);
char *format_string(char *str) {
  char *sptr;
  sptr = strrchr(str, '"');
  if (sptr == NULL) {
    fprintf(stderr, "File format error: invalid string\n");
    return str;
  }
  *sptr = 0;
  sptr = strchr(str, '"');
  if (sptr == NULL) {
    fprintf(stderr, "File format error: invalid string\n");
    return str;
  }
  sptr++;
  str = sptr;
  sptr = strchr(str, '\\');
  while (sptr != NULL) {
    if (sptr[1] == 'n') {
      *sptr = '\n';
      sptr++;
      *sptr = 0;
      sptr++;
      memmove(str + strlen(str), sptr, strlen(sptr) + 1);
    } else {
      fprintf(stderr, "Unrecognized escape sequence in string\n");
      sptr += 2;
    }
    sptr = strchr(sptr, '\\');
  }
  return str;
}


x86 assembly (clang, -O0):
<format_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x22,%esi
call   1050 <strrchr@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    119b <format_string+0x4b>
mov    0x2e60(%rip),%rax
mov    (%rax),%edi
lea    0xe77(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12d2 <format_string+0x182>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    $0x22,%esi
call   1040 <strchr@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11e1 <format_string+0x91>
mov    0x2e1a(%rip),%rax
mov    (%rax),%edi
lea    0xe31(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12d2 <format_string+0x182>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x5c,%esi
call   1040 <strchr@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     12ca <format_string+0x17a>
mov    -0x18(%rbp),%rax
movsbl 0x1(%rax),%eax
cmp    $0x6e,%eax
jne    1292 <format_string+0x142>
mov    -0x18(%rbp),%rax
movb   $0xa,(%rax)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    %eax,%edx
add    $0x1,%edx
call   1070 <memmove@plt>
jmp    12b3 <format_string+0x163>
mov    0x2d47(%rip),%rax
mov    (%rax),%edi
lea    0xd81(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x18(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x5c,%esi
call   1040 <strchr@plt>
mov    %rax,-0x18(%rbp)
jmp    1207 <format_string+0xb7>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<format_string>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdi,%rbx
mov    $0x22,%esi
call   1050 <strrchr@plt>
test   %rax,%rax
je     121a <format_string+0xca>
movb   $0x0,(%rax)
mov    %rbx,%rdi
mov    $0x22,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     121a <format_string+0xca>
mov    %rax,%r14
add    $0x1,%r14
mov    %r14,%rdi
mov    $0x5c,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     1231 <format_string+0xe1>
mov    %rax,%rbx
mov    0x2e31(%rip),%r13
lea    0xe6d(%rip),%r15
jmp    11e5 <format_string+0x95>
nopl   0x0(%rax,%rax,1)
mov    0x0(%r13),%edi
mov    %r15,%rsi
call   1060 <fprintf@plt>
add    $0x2,%rbx
mov    %rbx,%rdi
mov    $0x5c,%esi
call   1040 <strchr@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1231 <format_string+0xe1>
cmpb   $0x6e,0x1(%rbx)
jne    11c0 <format_string+0x70>
movw   $0xa,(%rbx)
add    $0x2,%rbx
mov    %r14,%rdi
call   1030 <strlen@plt>
movslq %eax,%r12
add    %r14,%r12
mov    %rbx,%rdi
call   1030 <strlen@plt>
lea    0x1(%rax),%edx
mov    %r12,%rdi
mov    %rbx,%rsi
call   1070 <memmove@plt>
jmp    11d0 <format_string+0x80>
mov    0x2dbf(%rip),%rax
mov    (%rax),%edi
lea    0xdd6(%rip),%rsi
call   1060 <fprintf@plt>
jmp    1234 <format_string+0xe4>
mov    %r14,%rbx
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [35 x i8] c"File format error: invalid string\0A\00"
@.str.1 = [40 x i8] c"Unrecognized escape sequence in string\0A\00"
define i8* @format_string(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = call i8* @strrchr(i8* %5, i8 signext 34)
store i8* %6, i8** %4
%7 = load i8*, i8** %4
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %13
9: ; preds = %1
%10 = load i32, i32* @stderr
%11 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
%12 = load i8*, i8** %3
store i8* %12, i8** %2
br label %65
13: ; preds = %1
%14 = load i8*, i8** %4
store i8 0, i8* %14
%15 = load i8*, i8** %3
%16 = call i8* @strchr(i8* %15, i8 signext 34)
store i8* %16, i8** %4
%17 = load i8*, i8** %4
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %23
19: ; preds = %13
%20 = load i32, i32* @stderr
%21 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
%22 = load i8*, i8** %3
store i8* %22, i8** %2
br label %65
23: ; preds = %13
%24 = load i8*, i8** %4
%25 = getelementptr inbounds i8, i8* %24, i32 1
store i8* %25, i8** %4
%26 = load i8*, i8** %4
store i8* %26, i8** %3
%27 = load i8*, i8** %3
%28 = call i8* @strchr(i8* %27, i8 signext 92)
store i8* %28, i8** %4
br label %29
29: ; preds = %60, %23
%30 = load i8*, i8** %4
%31 = icmp ne i8* %30, null
br i1 %31, label %32, label %63
32: ; preds = %29
%33 = load i8*, i8** %4
%34 = getelementptr inbounds i8, i8* %33, i64 1
%35 = load i8, i8* %34
%36 = sext i8 %35 to i32
%37 = icmp eq i32 %36, 110
br i1 %37, label %38, label %55
38: ; preds = %32
%39 = load i8*, i8** %4
store i8 10, i8* %39
%40 = load i8*, i8** %4
%41 = getelementptr inbounds i8, i8* %40, i32 1
store i8* %41, i8** %4
%42 = load i8*, i8** %4
store i8 0, i8* %42
%43 = load i8*, i8** %4
%44 = getelementptr inbounds i8, i8* %43, i32 1
store i8* %44, i8** %4
%45 = load i8*, i8** %3
%46 = load i8*, i8** %3
%47 = call i32 @strlen(i8* %46)
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i8, i8* %45, i64 %48
%50 = load i8*, i8** %4
%51 = load i8*, i8** %4
%52 = call i32 @strlen(i8* %51)
%53 = add nsw i32 %52, 1
%54 = call i32 @memmove(i8* %49, i8* %50, i32 %53)
br label %60
55: ; preds = %32
%56 = load i32, i32* @stderr
%57 = call i32 @fprintf(i32 %56, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
%58 = load i8*, i8** %4
%59 = getelementptr inbounds i8, i8* %58, i64 2
store i8* %59, i8** %4
br label %60
60: ; preds = %55, %38
%61 = load i8*, i8** %4
%62 = call i8* @strchr(i8* %61, i8 signext 92)
store i8* %62, i8** %4
br label %29
63: ; preds = %29
%64 = load i8*, i8** %3
store i8* %64, i8** %2
br label %65
65: ; preds = %63, %19, %9
%66 = load i8*, i8** %2
ret i8* %66
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [35 x i8] c"File format error: invalid string\0A\00"
@.str.1 = [40 x i8] c"Unrecognized escape sequence in string\0A\00"
define i8* @format_string(i8* %0) {
%2 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 34)
%3 = icmp eq i8* %2, null
br i1 %3, label %32, label %4
4: ; preds = %1
store i8 0, i8* %2
%5 = tail call i8* @strchr(i8* dereferenceable(1) %0, i8 signext 34)
%6 = icmp eq i8* %5, null
br i1 %6, label %32, label %7
7: ; preds = %4
%8 = getelementptr inbounds i8, i8* %5, i64 1
%9 = tail call i8* @strchr(i8* %8, i8 signext 92)
%10 = icmp eq i8* %9, null
br i1 %10, label %35, label %11
11: ; preds = %7, %28
%12 = phi i8* [ %30, %28 ], [ %9, %7 ]
%13 = getelementptr inbounds i8, i8* %12, i64 1
%14 = load i8, i8* %13
%15 = icmp eq i8 %14, 110
br i1 %15, label %16, label %24
16: ; preds = %11
store i8 10, i8* %12
store i8 0, i8* %13
%17 = getelementptr inbounds i8, i8* %12, i64 2
%18 = tail call i32 @strlen(i8* %8)
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %8, i64 %19
%21 = tail call i32 @strlen(i8* %17)
%22 = add nsw i32 %21, 1
%23 = tail call i32 @memmove(i8* %20, i8* %17, i32 %22)
br label %28
24: ; preds = %11
%25 = load i32, i32* @stderr
%26 = tail call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
%27 = getelementptr inbounds i8, i8* %12, i64 2
br label %28
28: ; preds = %24, %16
%29 = phi i8* [ %17, %16 ], [ %27, %24 ]
%30 = tail call i8* @strchr(i8* %29, i8 signext 92)
%31 = icmp eq i8* %30, null
br i1 %31, label %35, label %11
32: ; preds = %4, %1
%33 = load i32, i32* @stderr
%34 = tail call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
br label %35
35: ; preds = %28, %32, %7
%36 = phi i8* [ %8, %7 ], [ %0, %32 ], [ %8, %28 ]
ret i8* %36
}