#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*/ fprintf(int /*<<< orphan*/, char *, char *);
int fscanf(int /*<<< orphan*/ *, char *, float *, float *, unsigned int *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
int /*<<< orphan*/ stderr;
int open_status_file(char *cwd, char *sim_name, int format) {
  FILE *fp;
  char file[1000];
  sprintf(file, "%s/output/%s.status.dat", cwd, sim_name);
  if ((fp = fopen(file, "rt"))) {
    float time_current;
    float time_percentage;
    unsigned int file_nr;
    if (fscanf(fp, "%f%f%d", &time_current, &time_percentage, &file_nr) != 3) {
      fprintf(stderr, "Error: could not parse file %s\n", file);
      return 1;
    }
    if (format == 1) {
      printf("%6.2fs / %3.0f%% / %5d", time_current, time_percentage, file_nr);
    } else {
      printf("Reading %s:\n"
             " - Current simulation time:  %f s\n"
             " - Percentage completed:     %f %%\n"
             " - Latest output file:       %s.output%05d.bin\n",
             file, time_current, time_percentage, sim_name, file_nr);
    }
    fclose(fp);
    return 0;
  } else {
    fprintf(stderr,
            "Error: Could not open file %s\n"
            "Run this program with `--help` flag for help.\n",
            file);
    return 1;
  }
}


x86 assembly (clang, -O0):
<open_status_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
lea    -0x410(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    0xe74(%rip),%rsi
call   1080 <sprintf@plt>
lea    -0x410(%rbp),%rdi
lea    0xe79(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     128f <open_status_file+0x12f>
mov    -0x28(%rbp),%rdi
lea    0xe5e(%rip),%rsi
lea    -0x414(%rbp),%rdx
lea    -0x418(%rbp),%rcx
lea    -0x41c(%rbp),%r8
call   1050 <fscanf@plt>
cmp    $0x3,%eax
je     1208 <open_status_file+0xa8>
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
lea    -0x410(%rbp),%rdx
lea    0xe2b(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12b2 <open_status_file+0x152>
cmpl   $0x1,-0x1c(%rbp)
jne    1243 <open_status_file+0xe3>
movss  -0x414(%rbp),%xmm0
ff
cvtss2sd %xmm0,%xmm0
movss  -0x418(%rbp),%xmm1
ff
cvtss2sd %xmm1,%xmm1
mov    -0x41c(%rbp),%esi
lea    0xe0b(%rip),%rdi
mov    $0x2,%al
call   1040 <printf@plt>
jmp    127a <open_status_file+0x11a>
lea    -0x410(%rbp),%rsi
movss  -0x414(%rbp),%xmm0
ff
cvtss2sd %xmm0,%xmm0
movss  -0x418(%rbp),%xmm1
ff
cvtss2sd %xmm1,%xmm1
mov    -0x18(%rbp),%rdx
mov    -0x41c(%rbp),%ecx
lea    0xde6(%rip),%rdi
mov    $0x2,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    12b2 <open_status_file+0x152>
mov    0x2d4a(%rip),%rax
mov    (%rax),%edi
lea    -0x410(%rbp),%rdx
lea    0xe34(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_status_file>:
push   %rbp
push   %r14
push   %rbx
sub    $0x400,%rsp
mov    %edx,%ebp
mov    %rsi,%r14
mov    %rdi,%rdx
lea    0xe86(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    %r14,%rcx
call   1080 <sprintf@plt>
lea    0xe87(%rip),%rsi
mov    %rbx,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     11f3 <open_status_file+0x93>
mov    %rax,%rbx
lea    0xe73(%rip),%rsi
lea    0xc(%rsp),%rdx
lea    0x8(%rsp),%rcx
lea    0x4(%rsp),%r8
mov    %rax,%rdi
call   1050 <fscanf@plt>
cmp    $0x3,%eax
jne    1205 <open_status_file+0xa5>
movss  0xc(%rsp),%xmm0
cvtss2sd %xmm0,%xmm0
movss  0x8(%rsp),%xmm1
cvtss2sd %xmm1,%xmm1
mov    0x4(%rsp),%ecx
cmp    $0x1,%ebp
jne    1226 <open_status_file+0xc6>
lea    0xe5a(%rip),%rdi
mov    %ecx,%esi
mov    $0x2,%al
call   1040 <printf@plt>
jmp    123c <open_status_file+0xdc>
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
lea    0xed7(%rip),%rsi
jmp    1215 <open_status_file+0xb5>
mov    0x2dd4(%rip),%rax
mov    (%rax),%edi
lea    0xe0d(%rip),%rsi
lea    0x10(%rsp),%rdx
call   1060 <fprintf@plt>
mov    $0x1,%eax
jmp    1246 <open_status_file+0xe6>
lea    0xe2c(%rip),%rdi
lea    0x10(%rsp),%rsi
mov    %r14,%rdx
mov    $0x2,%al
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [24 x i8] c"%s/output/%s.status.dat\00"
@.str.1 = [3 x i8] c"rt\00"
@.str.2 = [7 x i8] c"%f%f%d\00"
@stderr = global i32 0
@.str.3 = [32 x i8] c"Error: could not parse file %s\0A\00"
@.str.4 = [23 x i8] c"%6.2fs / %3.0f%% / %5d\00"
@.str.5 = [129 x i8] c"Reading %s:\0A - Current simulation time: %f s\0A - Percentage completed: %f %%\0A - Latest output file: %s.output%05d.bin\0A\00"
@.str.6 = [77 x i8] c"Error: Could not open file %s\0ARun this program with `--help` flag for help.\0A\00"
define i32 @open_status_file(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca [1000 x i8]
%10 = alloca float
%11 = alloca float
%12 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%13 = getelementptr inbounds [1000 x i8], [1000 x i8]* %9, i64 0, i64 0
%14 = load i8*, i8** %5
%15 = load i8*, i8** %6
%16 = call i32 @sprintf(i8* %13, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %14, i8* %15)
%17 = getelementptr inbounds [1000 x i8], [1000 x i8]* %9, i64 0, i64 0
%18 = call i32* @fopen(i8* %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %18, i32** %8
%19 = icmp ne i32* %18, null
br i1 %19, label %20, label %50
20: ; preds = %3
%21 = load i32*, i32** %8
%22 = call i32 @fscanf(i32* %21, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), float* %10, float* %11, i32* %12)
%23 = icmp ne i32 %22, 3
br i1 %23, label %24, label %28
24: ; preds = %20
%25 = load i32, i32* @stderr
%26 = getelementptr inbounds [1000 x i8], [1000 x i8]* %9, i64 0, i64 0
%27 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i8* %26)
store i32 1, i32* %4
br label %54
28: ; preds = %20
%29 = load i32, i32* %7
%30 = icmp eq i32 %29, 1
br i1 %30, label %31, label %38
31: ; preds = %28
%32 = load float, float* %10
%33 = fpext float %32 to double
%34 = load float, float* %11
%35 = fpext float %34 to double
%36 = load i32, i32* %12
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), double %33, double %35, i32 %36)
br label %47
38: ; preds = %28
%39 = getelementptr inbounds [1000 x i8], [1000 x i8]* %9, i64 0, i64 0
%40 = load float, float* %10
%41 = fpext float %40 to double
%42 = load float, float* %11
%43 = fpext float %42 to double
%44 = load i8*, i8** %6
%45 = load i32, i32* %12
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([129 x i8], [129 x i8]* @.str.5, i64 0, i64 0), i8* %39, double %41, double %43, i8* %44, i32 %45)
br label %47
47: ; preds = %38, %31
%48 = load i32*, i32** %8
%49 = call i32 @fclose(i32* %48)
store i32 0, i32* %4
br label %54
50: ; preds = %3
%51 = load i32, i32* @stderr
%52 = getelementptr inbounds [1000 x i8], [1000 x i8]* %9, i64 0, i64 0
%53 = call i32 @fprintf(i32 %51, i8* getelementptr inbounds ([77 x i8], [77 x i8]* @.str.6, i64 0, i64 0), i8* %52)
store i32 1, i32* %4
br label %54
54: ; preds = %50, %47, %24
%55 = load i32, i32* %4
ret i32 %55
}

LLVM IR (-O2):
@.str = [24 x i8] c"%s/output/%s.status.dat\00"
@.str.1 = [3 x i8] c"rt\00"
@.str.2 = [7 x i8] c"%f%f%d\00"
@stderr = global i32 0
@.str.3 = [32 x i8] c"Error: could not parse file %s\0A\00"
@.str.4 = [23 x i8] c"%6.2fs / %3.0f%% / %5d\00"
@.str.5 = [129 x i8] c"Reading %s:\0A - Current simulation time: %f s\0A - Percentage completed: %f %%\0A - Latest output file: %s.output%05d.bin\0A\00"
@.str.6 = [77 x i8] c"Error: Could not open file %s\0ARun this program with `--help` flag for help.\0A\00"
define i32 @open_status_file(i8* %0, i8* %1, i32 %2) {
%4 = alloca [1000 x i8]
%5 = alloca float
%6 = alloca float
%7 = alloca i32
%8 = getelementptr inbounds [1000 x i8], [1000 x i8]* %4, i64 0, i64 0
%9 = call i32 @sprintf(i8* %8, i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1)
%10 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %36, label %12
12: ; preds = %3
%13 = bitcast float* %5 to i8*
%14 = bitcast float* %6 to i8*
%15 = bitcast i32* %7 to i8*
%16 = call i32 @fscanf(i32* %10, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), float* %5, float* %6, i32* %7)
%17 = icmp eq i32 %16, 3
br i1 %17, label %21, label %18
18: ; preds = %12
%19 = load i32, i32* @stderr
%20 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i8* %8)
br label %34
21: ; preds = %12
%22 = icmp eq i32 %2, 1
%23 = load float, float* %5
%24 = fpext float %23 to double
%25 = load float, float* %6
%26 = fpext float %25 to double
%27 = load i32, i32* %7
br i1 %22, label %28, label %30
28: ; preds = %21
%29 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), double %24, double %26, i32 %27)
br label %32
30: ; preds = %21
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([129 x i8], [129 x i8]* @.str.5, i64 0, i64 0), i8* %8, double %24, double %26, i8* %1, i32 %27)
br label %32
32: ; preds = %30, %28
%33 = call i32 @fclose(i32* %10)
br label %34
34: ; preds = %32, %18
%35 = phi i32 [ 1, %18 ], [ 0, %32 ]
br label %39
36: ; preds = %3
%37 = load i32, i32* @stderr
%38 = call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([77 x i8], [77 x i8]* @.str.6, i64 0, i64 0), i8* %8)
br label %39
39: ; preds = %36, %34
%40 = phi i32 [ %35, %34 ], [ 1, %36 ]
ret i32 %40
}