#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*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fscanf(int /*<<< orphan*/ *, char *, double *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
scalar_t__ rand();
int system(char *);
double getFrequencyGHz() {
  int t = 0;
  for (int i = 0; i < 100000000; i++) {
    t += rand();
  }
  printf("t=%d\n", t);
  int rc = system("cpupower frequency-info | grep 'available frequency steps: "
                  "' | sed -e s'/^.*: //' | sed -e 's/ .*//' > freq.txt");
  if (rc != 0) {
    perror("system");
    exit(-1);
  }
  FILE *file = fopen("freq.txt", "r");
  if (file == 0) {
    perror("open freq.txt");
    exit(-1);
  }
  double freqGHz = 0;
  rc = fscanf(file, "%lf", &freqGHz);
  if (rc != 1) {
    perror("scanning file");
    exit(-1);
  }
  fclose(file);
  return freqGHz;
}


x86 assembly (clang, -O0):
<getFrequencyGHz>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
cmpl   $0x5f5e100,-0x8(%rbp)
jge    11c5 <getFrequencyGHz+0x45>
mov    $0x0,%al
call   10a0 <rand@plt>
mov    %rax,%rcx
movslq -0x4(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x4(%rbp)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1196 <getFrequencyGHz+0x16>
mov    -0x4(%rbp),%esi
lea    0xe31(%rip),%rdi
call   1050 <printf@plt>
lea    0xe2b(%rip),%rdi
call   1040 <system@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     1203 <getFrequencyGHz+0x83>
lea    0xe82(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
lea    0xe63(%rip),%rdi
lea    0xe6c(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    123b <getFrequencyGHz+0xbb>
lea    0xe53(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe3f(%rip),%rsi
lea    -0x20(%rbp),%rdx
call   1060 <fscanf@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x1,-0xc(%rbp)
je     127a <getFrequencyGHz+0xfa>
lea    0xe26(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
movsd  -0x20(%rbp),%xmm0
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getFrequencyGHz>:
push   %rbp
push   %rbx
push   %rax
mov    $0x5f5e100,%ebp
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
xor    %eax,%eax
call   10a0 <rand@plt>
add    %eax,%ebx
add    $0xffffffff,%ebp
jne    1190 <getFrequencyGHz+0x10>
lea    0xe5b(%rip),%rdi
mov    %ebx,%esi
call   1050 <printf@plt>
lea    0xe53(%rip),%rdi
call   1040 <system@plt>
test   %eax,%eax
jne    120a <getFrequencyGHz+0x8a>
lea    0xeaa(%rip),%rdi
lea    0xeb3(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     1213 <getFrequencyGHz+0x93>
mov    %rax,%rbx
movq   $0x0,(%rsp)
lea    0xea7(%rip),%rsi
mov    %rsp,%rdx
mov    %rax,%rdi
call   1060 <fscanf@plt>
cmp    $0x1,%eax
jne    121c <getFrequencyGHz+0x9c>
mov    %rbx,%rdi
call   1030 <fclose@plt>
movsd  (%rsp),%xmm0
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe65(%rip),%rdi
jmp    1223 <getFrequencyGHz+0xa3>
lea    0xe65(%rip),%rdi
jmp    1223 <getFrequencyGHz+0xa3>
lea    0xe6e(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [6 x i8] c"t=%d\0A\00"
@.str.1 = [112 x i8] c"cpupower frequency-info | grep 'available frequency steps: ' | sed -e s'/^.*: //' | sed -e 's/ .*//' > freq.txt\00"
@.str.2 = [7 x i8] c"system\00"
@.str.3 = [9 x i8] c"freq.txt\00"
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [14 x i8] c"open freq.txt\00"
@.str.6 = [4 x i8] c"%lf\00"
@.str.7 = [14 x i8] c"scanning file\00"
define double @getFrequencyGHz() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32*
%5 = alloca double
store i32 0, i32* %1
store i32 0, i32* %2
br label %6
6: ; preds = %15, %0
%7 = load i32, i32* %2
%8 = icmp slt i32 %7, 100000000
br i1 %8, label %9, label %18
9: ; preds = %6
%10 = call i64 (...) @rand()
%11 = load i32, i32* %1
%12 = sext i32 %11 to i64
%13 = add nsw i64 %12, %10
%14 = trunc i64 %13 to i32
store i32 %14, i32* %1
br label %15
15: ; preds = %9
%16 = load i32, i32* %2
%17 = add nsw i32 %16, 1
store i32 %17, i32* %2
br label %6
18: ; preds = %6
%19 = load i32, i32* %1
%20 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %19)
%21 = call i32 @system(i8* getelementptr inbounds ([112 x i8], [112 x i8]* @.str.1, i64 0, i64 0))
store i32 %21, i32* %3
%22 = load i32, i32* %3
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %18
%25 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%26 = call i32 @exit(i32 -1)
unreachable
27: ; preds = %18
%28 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %28, i32** %4
%29 = load i32*, i32** %4
%30 = icmp eq i32* %29, null
br i1 %30, label %31, label %34
31: ; preds = %27
%32 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
%33 = call i32 @exit(i32 -1)
unreachable
34: ; preds = %27
store double 0.000000e+00, double* %5
%35 = load i32*, i32** %4
%36 = call i32 @fscanf(i32* %35, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), double* %5)
store i32 %36, i32* %3
%37 = load i32, i32* %3
%38 = icmp ne i32 %37, 1
br i1 %38, label %39, label %42
39: ; preds = %34
%40 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.7, i64 0, i64 0))
%41 = call i32 @exit(i32 -1)
unreachable
42: ; preds = %34
%43 = load i32*, i32** %4
%44 = call i32 @fclose(i32* %43)
%45 = load double, double* %5
ret double %45
}

LLVM IR (-O2):
@.str = [6 x i8] c"t=%d\0A\00"
@.str.1 = [112 x i8] c"cpupower frequency-info | grep 'available frequency steps: ' | sed -e s'/^.*: //' | sed -e 's/ .*//' > freq.txt\00"
@.str.2 = [7 x i8] c"system\00"
@.str.3 = [9 x i8] c"freq.txt\00"
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [14 x i8] c"open freq.txt\00"
@.str.6 = [4 x i8] c"%lf\00"
@.str.7 = [14 x i8] c"scanning file\00"
define double @getFrequencyGHz() {
%1 = alloca double
br label %6
2: ; preds = %6
%3 = tail call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %11)
%4 = tail call i32 @system(i8* getelementptr inbounds ([112 x i8], [112 x i8]* @.str.1, i64 0, i64 0))
%5 = icmp eq i32 %4, 0
br i1 %5, label %17, label %14
6: ; preds = %0, %6
%7 = phi i32 [ 0, %0 ], [ %11, %6 ]
%8 = phi i32 [ 0, %0 ], [ %12, %6 ]
%9 = tail call i64 (...) @rand()
%10 = trunc i64 %9 to i32
%11 = add i32 %7, %10
%12 = add nuw nsw i32 %8, 1
%13 = icmp eq i32 %12, 100000000
br i1 %13, label %2, label %6
14: ; preds = %2
%15 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%16 = tail call i32 @exit(i32 -1)
unreachable
17: ; preds = %2
%18 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %23
20: ; preds = %17
%21 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
%22 = tail call i32 @exit(i32 -1)
unreachable
23: ; preds = %17
%24 = bitcast double* %1 to i8*
store double 0.000000e+00, double* %1
%25 = call i32 @fscanf(i32* %18, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), double* %1)
%26 = icmp eq i32 %25, 1
br i1 %26, label %30, label %27
27: ; preds = %23
%28 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.7, i64 0, i64 0))
%29 = call i32 @exit(i32 -1)
unreachable
30: ; preds = %23
%31 = call i32 @fclose(i32* %18)
%32 = load double, double* %1
ret double %32
}