#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 NULL;
  }
  *sptr = 0;
  sptr = strchr(str, '"');
  if (sptr == NULL) {
    fprintf(stderr, "File format error: invalid string\n");
    return NULL;
  }
  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>
movq   $0x0,-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>
movq   $0x0,-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     1232 <format_string+0xe2>
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     1232 <format_string+0xe2>
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>
xor    %r14d,%r14d
mov    %r14,%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 %12
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))
store i8* null, i8** %2
br label %63
12: ; preds = %1
%13 = load i8*, i8** %4
store i8 0, i8* %13
%14 = load i8*, i8** %3
%15 = call i8* @strchr(i8* %14, i8 signext 34)
store i8* %15, i8** %4
%16 = load i8*, i8** %4
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %21
18: ; preds = %12
%19 = load i32, i32* @stderr
%20 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %2
br label %63
21: ; preds = %12
%22 = load i8*, i8** %4
%23 = getelementptr inbounds i8, i8* %22, i32 1
store i8* %23, i8** %4
%24 = load i8*, i8** %4
store i8* %24, i8** %3
%25 = load i8*, i8** %3
%26 = call i8* @strchr(i8* %25, i8 signext 92)
store i8* %26, i8** %4
br label %27
27: ; preds = %58, %21
%28 = load i8*, i8** %4
%29 = icmp ne i8* %28, null
br i1 %29, label %30, label %61
30: ; preds = %27
%31 = load i8*, i8** %4
%32 = getelementptr inbounds i8, i8* %31, i64 1
%33 = load i8, i8* %32
%34 = sext i8 %33 to i32
%35 = icmp eq i32 %34, 110
br i1 %35, label %36, label %53
36: ; preds = %30
%37 = load i8*, i8** %4
store i8 10, i8* %37
%38 = load i8*, i8** %4
%39 = getelementptr inbounds i8, i8* %38, i32 1
store i8* %39, i8** %4
%40 = load i8*, i8** %4
store i8 0, i8* %40
%41 = load i8*, i8** %4
%42 = getelementptr inbounds i8, i8* %41, i32 1
store i8* %42, i8** %4
%43 = load i8*, i8** %3
%44 = load i8*, i8** %3
%45 = call i32 @strlen(i8* %44)
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i8, i8* %43, i64 %46
%48 = load i8*, i8** %4
%49 = load i8*, i8** %4
%50 = call i32 @strlen(i8* %49)
%51 = add nsw i32 %50, 1
%52 = call i32 @memmove(i8* %47, i8* %48, i32 %51)
br label %58
53: ; preds = %30
%54 = load i32, i32* @stderr
%55 = call i32 @fprintf(i32 %54, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
%56 = load i8*, i8** %4
%57 = getelementptr inbounds i8, i8* %56, i64 2
store i8* %57, i8** %4
br label %58
58: ; preds = %53, %36
%59 = load i8*, i8** %4
%60 = call i8* @strchr(i8* %59, i8 signext 92)
store i8* %60, i8** %4
br label %27
61: ; preds = %27
%62 = load i8*, i8** %3
store i8* %62, i8** %2
br label %63
63: ; preds = %61, %18, %9
%64 = load i8*, i8** %2
ret i8* %64
}

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 ], [ null, %32 ], [ %8, %28 ]
ret i8* %36
}