#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputs(char const *, int /*<<< orphan*/ *);
void create_script() {
  const char *script =
      ""
      "#!/usr/bin/perl\n"
      "use strict;\n"
      "\n"
      "my $stat_file_name = "
      "get_stat_file_name('/usr/local/pharos/conf/named.conf');\n"
      "# my @label_arr = ('++ Socket I/O Statistics ++', '++ Incoming Requests "
      "++', '++ Incoming Queries ++', '++ Name Server Statistics ++');\n"
      "my @label_arr = ('++ Incoming Requests ++', '++ Name Server Statistics "
      "++');\n"
      "# my %display_key_hash = ('QUERY' => 'query', 'A' => 'address', 'CNAME' "
      "=> 'cname', 'NS' => 'ns', 'SOA' => 'soa');\n"
      "my %display_key_hash = ('QUERY' => 'qps', 'response time 0ms to 5ms' => "
      "'rt_05', 'response time 5ms to 10ms' => 'rt_10', 'response time 10ms to "
      "20ms' => 'rt_20', 'response time 20ms to 50ms' => 'rt_50',  'response "
      "time 50ms to Xms' => 'rt_50+');\n"
      "my %result = ();\n"
      "\n"
      "foreach my $label (@label_arr)\n"
      "{\n"
      "   my @stat_arr = get_stat_section($stat_file_name, $label);\n"
      "   foreach my $stat_result (@stat_arr)\n"
      "   {\n"
      "       $stat_result =~ m|^\\s*(\\d+)\\s+(.+?)\\s*$|;\n"
      "       my $display_key = get_display_key($2);\n"
      "       $result{$display_key} = $1 if $display_key;\n"
      "   }\n"
      "}\n"
      "foreach my $k (keys %display_key_hash)\n"
      "{\n"
      "   $result{$display_key_hash{$k}} = 0 if not "
      "defined($result{$display_key_hash{$k}});\n"
      "}\n"
      "\n"
      "foreach my $k (sort keys %result)\n"
      "{\n"
      "   my $v = $result{$k};\n"
      "   print \"$k,$v\\n\";\n"
      "}\n"
      "\n"
      "sub get_display_key\n"
      "{\n"
      "   my $key = shift;\n"
      "   if ($display_key_hash{$key})\n"
      "   {\n"
      "       return $display_key_hash{$key};\n"
      "   }\n"
      "   else\n"
      "   {\n"
      "       # my $ret = '';\n"
      "       # map { $ret .= lc(substr($_, 0, 1)); } split /\\s+/, $key;\n"
      "       # return $ret;\n"
      "       return '';\n"
      "   }\n"
      "}\n"
      "\n"
      "\n"
      "sub get_stat_file_name\n"
      "{\n"
      "   my $conf_file = shift;\n"
      "   open FH, $conf_file or die \"error open file: $conf_file\\n\";\n"
      "   my @arr = <FH>;\n"
      "   close FH;\n"
      "   @arr = grep /\\bstatistics-file\\b/, @arr;\n"
      "   $arr[0] =~ m|\\s*statistics-file\\s+[\"'](.+)[\"']\\s*;|;\n"
      "   return $1 ? $1 : '';\n"
      "}\n"
      "\n"
      "\n"
      "sub get_stat_section\n"
      "{\n"
      "   my $stat_file = shift;\n"
      "   my $seperator = shift;\n"
      "\n"
      "   open FH, $stat_file or die \"error open file: $stat_file\\n\";\n"
      "   my @stat_arr = <FH>;\n"
      "   close FH;\n"
      "   my $stat_str = join \"\", @stat_arr;\n"
      "   $stat_str =~ s/\\r/\\n/g;\n"
      "   $stat_str =~ m/\\Q$seperator\\E\\n+((.|\\n)*?)\\n+\\+\\+/;\n"
      "   my $val = $1;\n"
      "   $val =~ s/\\n\\s+/\\n/g;\n"
      "   $val =~ s/^\\s+//g;\n"
      "   return split /\\n/, $val;\n"
      "}\n";
  FILE *fp = fopen("/tmp/rndc_tsar.pl", "w");
  if (!fp) {
    return;
  }
  fputs(script, fp);
  fclose(fp);
}


x86 assembly (clang, -O0):
<create_script>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    0xec1(%rip),%rax
mov    %rax,-0x8(%rbp)
lea    0x1714(%rip),%rdi
lea    0x171f(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    116a <create_script+0x3a>
jmp    1180 <create_script+0x50>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <fputs@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_script>:
push   %rbx
lea    0x1726(%rip),%rdi
lea    0x1731(%rip),%rsi
call   1040 <fopen@plt>
test   %rax,%rax
je     116e <create_script+0x3e>
mov    %rax,%rbx
lea    0xead(%rip),%rdi
mov    $0x85d,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1050 <fwrite@plt>
mov    %rbx,%rdi
pop    %rbx
jmp    1030 <fclose@plt>
pop    %rbx
ret

LLVM IR (-O0):
@.str = [2142 x i8] c"#!/usr/bin/perl\0Ause strict;\0A\0Amy $stat_file_name = get_stat_file_name('/usr/local/pharos/conf/named.conf');\0A# my @label_arr = ('++ Socket I/O Statistics ++', '++ Incoming Requests ++', '++ Incoming Queries ++', '++ Name Server Statistics ++');\0Amy @label_arr = ('++ Incoming Requests ++', '++ Name Server Statistics ++');\0A# my %display_key_hash = ('QUERY' => 'query', 'A' => 'address', 'CNAME' => 'cname', 'NS' => 'ns', 'SOA' => 'soa');\0Amy %display_key_hash = ('QUERY' => 'qps', 'response time 0ms to 5ms' => 'rt_05', 'response time 5ms to 10ms' => 'rt_10', 'response time 10ms to 20ms' => 'rt_20', 'response time 20ms to 50ms' => 'rt_50', 'response time 50ms to Xms' => 'rt_50+');\0Amy %result = ();\0A\0Aforeach my $label (@label_arr)\0A{\0A my @stat_arr = get_stat_section($stat_file_name, $label);\0A foreach my $stat_result (@stat_arr)\0A {\0A $stat_result =~ m|^\\s*(\\d+)\\s+(.+?)\\s*$|;\0A my $display_key = get_display_key($2);\0A $result{$display_key} = $1 if $display_key;\0A }\0A}\0Aforeach my $k (keys %display_key_hash)\0A{\0A $result{$display_key_hash{$k}} = 0 if not defined($result{$display_key_hash{$k}});\0A}\0A\0Aforeach my $k (sort keys %result)\0A{\0A my $v = $result{$k};\0A print \22$k,$v\\n\22;\0A}\0A\0Asub get_display_key\0A{\0A my $key = shift;\0A if ($display_key_hash{$key})\0A {\0A return $display_key_hash{$key};\0A }\0A else\0A {\0A # my $ret = '';\0A # map { $ret .= lc(substr($_, 0, 1)); } split /\\s+/, $key;\0A # return $ret;\0A return '';\0A }\0A}\0A\0A\0Asub get_stat_file_name\0A{\0A my $conf_file = shift;\0A open FH, $conf_file or die \22error open file: $conf_file\\n\22;\0A my @arr = <FH>;\0A close FH;\0A @arr = grep /\\bstatistics-file\\b/, @arr;\0A $arr[0] =~ m|\\s*statistics-file\\s+[\22'](.+)[\22']\\s*;|;\0A return $1 ? $1 : '';\0A}\0A\0A\0Asub get_stat_section\0A{\0A my $stat_file = shift;\0A my $seperator = shift;\0A\0A open FH, $stat_file or die \22error open file: $stat_file\\n\22;\0A my @stat_arr = <FH>;\0A close FH;\0A my $stat_str = join \22\22, @stat_arr;\0A $stat_str =~ s/\\r/\\n/g;\0A $stat_str =~ m/\\Q$seperator\\E\\n+((.|\\n)*?)\\n+\\+\\+/;\0A my $val = $1;\0A $val =~ s/\\n\\s+/\\n/g;\0A $val =~ s/^\\s+//g;\0A return split /\\n/, $val;\0A}\0A\00"
@.str.1 = [18 x i8] c"/tmp/rndc_tsar.pl\00"
@.str.2 = [2 x i8] c"w\00"
define void @create_script() {
%1 = alloca i8*
%2 = alloca i32*
store i8* getelementptr inbounds ([2142 x i8], [2142 x i8]* @.str, i64 0, i64 0), i8** %1
%3 = call i32* @fopen(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %3, i32** %2
%4 = load i32*, i32** %2
%5 = icmp ne i32* %4, null
br i1 %5, label %7, label %6
6: ; preds = %0
br label %13
7: ; preds = %0
%8 = load i8*, i8** %1
%9 = load i32*, i32** %2
%10 = call i32 @fputs(i8* %8, i32* %9)
%11 = load i32*, i32** %2
%12 = call i32 @fclose(i32* %11)
br label %13
13: ; preds = %7, %6
ret void
}

LLVM IR (-O2):
@.str = [2142 x i8] c"#!/usr/bin/perl\0Ause strict;\0A\0Amy $stat_file_name = get_stat_file_name('/usr/local/pharos/conf/named.conf');\0A# my @label_arr = ('++ Socket I/O Statistics ++', '++ Incoming Requests ++', '++ Incoming Queries ++', '++ Name Server Statistics ++');\0Amy @label_arr = ('++ Incoming Requests ++', '++ Name Server Statistics ++');\0A# my %display_key_hash = ('QUERY' => 'query', 'A' => 'address', 'CNAME' => 'cname', 'NS' => 'ns', 'SOA' => 'soa');\0Amy %display_key_hash = ('QUERY' => 'qps', 'response time 0ms to 5ms' => 'rt_05', 'response time 5ms to 10ms' => 'rt_10', 'response time 10ms to 20ms' => 'rt_20', 'response time 20ms to 50ms' => 'rt_50', 'response time 50ms to Xms' => 'rt_50+');\0Amy %result = ();\0A\0Aforeach my $label (@label_arr)\0A{\0A my @stat_arr = get_stat_section($stat_file_name, $label);\0A foreach my $stat_result (@stat_arr)\0A {\0A $stat_result =~ m|^\\s*(\\d+)\\s+(.+?)\\s*$|;\0A my $display_key = get_display_key($2);\0A $result{$display_key} = $1 if $display_key;\0A }\0A}\0Aforeach my $k (keys %display_key_hash)\0A{\0A $result{$display_key_hash{$k}} = 0 if not defined($result{$display_key_hash{$k}});\0A}\0A\0Aforeach my $k (sort keys %result)\0A{\0A my $v = $result{$k};\0A print \22$k,$v\\n\22;\0A}\0A\0Asub get_display_key\0A{\0A my $key = shift;\0A if ($display_key_hash{$key})\0A {\0A return $display_key_hash{$key};\0A }\0A else\0A {\0A # my $ret = '';\0A # map { $ret .= lc(substr($_, 0, 1)); } split /\\s+/, $key;\0A # return $ret;\0A return '';\0A }\0A}\0A\0A\0Asub get_stat_file_name\0A{\0A my $conf_file = shift;\0A open FH, $conf_file or die \22error open file: $conf_file\\n\22;\0A my @arr = <FH>;\0A close FH;\0A @arr = grep /\\bstatistics-file\\b/, @arr;\0A $arr[0] =~ m|\\s*statistics-file\\s+[\22'](.+)[\22']\\s*;|;\0A return $1 ? $1 : '';\0A}\0A\0A\0Asub get_stat_section\0A{\0A my $stat_file = shift;\0A my $seperator = shift;\0A\0A open FH, $stat_file or die \22error open file: $stat_file\\n\22;\0A my @stat_arr = <FH>;\0A close FH;\0A my $stat_str = join \22\22, @stat_arr;\0A $stat_str =~ s/\\r/\\n/g;\0A $stat_str =~ m/\\Q$seperator\\E\\n+((.|\\n)*?)\\n+\\+\\+/;\0A my $val = $1;\0A $val =~ s/\\n\\s+/\\n/g;\0A $val =~ s/^\\s+//g;\0A return split /\\n/, $val;\0A}\0A\00"
@.str.1 = [18 x i8] c"/tmp/rndc_tsar.pl\00"
@.str.2 = [2 x i8] c"w\00"
define void @create_script() {
%1 = tail call i32* @fopen(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%2 = icmp eq i32* %1, null
br i1 %2, label %6, label %3
3: ; preds = %0
%4 = tail call i64 @fwrite(i8* getelementptr inbounds ([2142 x i8], [2142 x i8]* @.str, i64 0, i64 0), i64 2141, i64 1, i32* %1)
%5 = tail call i32 @fclose(i32* %1)
br label %6
6: ; preds = %0, %3
ret void
}