#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 */
size_t find_extension(char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int save_A800state(char *, char *);
int save_snapshot(char *, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcmp(char *, char *);
int templateType(char *);
int save_state(char *fin, char *fname) {
  int tp = templateType(fin);
  fname[find_extension(fname)] = 0;
  if (tp == 1) {
    strcat(fname, ".a8s");
  } else if (tp == 2) {
    strcat(fname, ".state");
  } else
    return 0;
  if (!strcmp(fin, fname)) {
    fprintf(
        stderr,
        "Error: template state file and save state file cannot be the same!\n");
    return 0;
  }
  if (tp == 1)
    return save_A800state(fin, fname);
  return save_snapshot(fin, fname);
}


x86 assembly (clang, -O0):
<save_state>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <templateType@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1080 <find_extension@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax,%rcx,1)
cmpl   $0x1,-0x1c(%rbp)
jne    11c7 <save_state+0x57>
mov    -0x18(%rbp),%rdi
lea    0xe43(%rip),%rsi
call   1090 <strcat@plt>
jmp    11f7 <save_state+0x87>
cmpl   $0x2,-0x1c(%rbp)
jne    11e6 <save_state+0x76>
mov    -0x18(%rbp),%rdi
lea    0xe29(%rip),%rsi
call   1090 <strcat@plt>
jmp    11f2 <save_state+0x82>
movl   $0x0,-0x4(%rbp)
jmp    125d <save_state+0xed>
jmp    11f7 <save_state+0x87>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%eax
jne    122e <save_state+0xbe>
mov    0x2dcc(%rip),%rax
mov    (%rax),%edi
lea    0xdef(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    125d <save_state+0xed>
cmpl   $0x1,-0x1c(%rbp)
jne    124d <save_state+0xdd>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <save_A800state@plt>
mov    %eax,-0x4(%rbp)
jmp    125d <save_state+0xed>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <save_snapshot@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<save_state>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %rdi,%r14
call   1030 <templateType@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1080 <find_extension@plt>
movb   $0x0,(%rbx,%rax,1)
cmp    $0x2,%ebp
je     11c4 <save_state+0x54>
cmp    $0x1,%ebp
jne    1206 <save_state+0x96>
lea    0xe62(%rip),%rsi
mov    %rbx,%rdi
call   1090 <strcat@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <strcmp@plt>
test   %eax,%eax
je     11f1 <save_state+0x81>
mov    %r14,%rdi
mov    %rbx,%rsi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1070 <save_A800state@plt>
lea    0xe3a(%rip),%rsi
mov    %rbx,%rdi
call   1090 <strcat@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <strcmp@plt>
test   %eax,%eax
je     11f1 <save_state+0x81>
mov    %r14,%rdi
mov    %rbx,%rsi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1060 <save_snapshot@plt>
mov    0x2de8(%rip),%rax
mov    (%rax),%edi
lea    0xe0b(%rip),%rsi
call   1050 <fprintf@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c".a8s\00"
@.str.1 = [7 x i8] c".state\00"
@stderr = global i32 0
@.str.2 = [68 x i8] c"Error: template state file and save state file cannot be the same!\0A\00"
define i32 @save_state(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%7 = load i8*, i8** %4
%8 = call i32 @templateType(i8* %7)
store i32 %8, i32* %6
%9 = load i8*, i8** %5
%10 = load i8*, i8** %5
%11 = call i64 @find_extension(i8* %10)
%12 = getelementptr inbounds i8, i8* %9, i64 %11
store i8 0, i8* %12
%13 = load i32, i32* %6
%14 = icmp eq i32 %13, 1
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = load i8*, i8** %5
%17 = call i32 @strcat(i8* %16, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %26
18: ; preds = %2
%19 = load i32, i32* %6
%20 = icmp eq i32 %19, 2
br i1 %20, label %21, label %24
21: ; preds = %18
%22 = load i8*, i8** %5
%23 = call i32 @strcat(i8* %22, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %25
24: ; preds = %18
store i32 0, i32* %3
br label %45
25: ; preds = %21
br label %26
26: ; preds = %25, %15
%27 = load i8*, i8** %4
%28 = load i8*, i8** %5
%29 = call i32 @strcmp(i8* %27, i8* %28)
%30 = icmp ne i32 %29, 0
br i1 %30, label %34, label %31
31: ; preds = %26
%32 = load i32, i32* @stderr
%33 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %3
br label %45
34: ; preds = %26
%35 = load i32, i32* %6
%36 = icmp eq i32 %35, 1
br i1 %36, label %37, label %41
37: ; preds = %34
%38 = load i8*, i8** %4
%39 = load i8*, i8** %5
%40 = call i32 @save_A800state(i8* %38, i8* %39)
store i32 %40, i32* %3
br label %45
41: ; preds = %34
%42 = load i8*, i8** %4
%43 = load i8*, i8** %5
%44 = call i32 @save_snapshot(i8* %42, i8* %43)
store i32 %44, i32* %3
br label %45
45: ; preds = %41, %37, %31, %24
%46 = load i32, i32* %3
ret i32 %46
}

LLVM IR (-O2):
@.str = [5 x i8] c".a8s\00"
@.str.1 = [7 x i8] c".state\00"
@stderr = global i32 0
@.str.2 = [68 x i8] c"Error: template state file and save state file cannot be the same!\0A\00"
define i32 @save_state(i8* %0, i8* %1) {
%3 = tail call i32 @templateType(i8* %0)
%4 = tail call i64 @find_extension(i8* %1)
%5 = getelementptr inbounds i8, i8* %1, i64 %4
store i8 0, i8* %5
switch i32 %3, label %21 [
i32 1, label %6
i32 2, label %10
]
6: ; preds = %2
%7 = tail call i32 @strcat(i8* %1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 @strcmp(i8* dereferenceable(1) %0, i8* dereferenceable(1) %1)
%9 = icmp eq i32 %8, 0
br i1 %9, label %14, label %17
10: ; preds = %2
%11 = tail call i32 @strcat(i8* %1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%12 = tail call i32 @strcmp(i8* dereferenceable(1) %0, i8* dereferenceable(1) %1)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %19
14: ; preds = %10, %6
%15 = load i32, i32* @stderr
%16 = tail call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str.2, i64 0, i64 0))
br label %21
17: ; preds = %6
%18 = tail call i32 @save_A800state(i8* %0, i8* %1)
br label %21
19: ; preds = %10
%20 = tail call i32 @save_snapshot(i8* %0, i8* %1)
br label %21
21: ; preds = %2, %19, %17, %14
%22 = phi i32 [ %18, %17 ], [ %20, %19 ], [ 0, %14 ], [ 0, %2 ]
ret i32 %22
}