#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 */
struct timespec {
  int dummy;
};
struct sched_param {
  int sched_priority;
};
typedef scalar_t__ clock_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ SCHED_RR;
scalar_t__ clock();
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ getpid();
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ sched_setscheduler(int /*<<< orphan*/, int /*<<< orphan*/,
                              struct sched_param *);
int /*<<< orphan*/ syscall(int);
int main(void) {
  int i = 0;
  struct timespec *time_pai =
      (struct timespec *)malloc(sizeof(struct timespec));
  struct timespec *time_filho =
      (struct timespec *)malloc(sizeof(struct timespec));
  struct sched_param param;
  clock_t begin, end;
  if (fork()) {
    param.sched_priority = 99;
    if (sched_setscheduler(getpid(), SCHED_RR, &param) != 0) {
      printf("\n\nHelp me! Setting scheduler failed\n\n");
      exit(EXIT_FAILURE);
    }
    while (1) {
      begin = clock();
      printf("Child process here! I'll get out soon\n");
      end = clock();
      printf("\n\nGetting out! Elapsed time (in clocks): %lf \n\n",
             (double)(end - begin));
      syscall(161);
      syscall(158);
    }
  } else {
    param.sched_priority = 99;
    if (sched_setscheduler(getpid(), SCHED_RR, &param) != 0) {
      printf("\n\nHelp me! Setting scheduler failed\n\n");
      exit(EXIT_FAILURE);
    }
    while (1) {
      for (i = 0; i < 10; i++) {
        printf("Parent process here!\n");
      }
      syscall(158);
    }
  }
  while (1) {
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
mov    $0x4,%edi
call   1080 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    $0x4,%edi
call   1080 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   10a0 <fork@plt>
cmp    $0x0,%rax
je     126c <main+0xec>
movl   $0x63,-0x20(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%edi
mov    0x2df6(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1060 <sched_setscheduler@plt>
cmp    $0x0,%rax
je     120b <main+0x8b>
lea    0xe0a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
jmp    1210 <main+0x90>
mov    $0x0,%al
call   1040 <clock@plt>
mov    %rax,-0x28(%rbp)
lea    0xe04(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   1040 <clock@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
sub    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
cvtsi2sd %rax,%xmm0
lea    0xe01(%rip),%rdi
mov    $0x1,%al
call   1050 <printf@plt>
mov    $0xa1,%edi
call   1070 <syscall@plt>
mov    $0x9e,%edi
call   1070 <syscall@plt>
jmp    1210 <main+0x90>
movl   $0x63,-0x20(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%edi
mov    0x2d4d(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1060 <sched_setscheduler@plt>
cmp    $0x0,%rax
je     12b4 <main+0x134>
lea    0xd61(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2d33(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
jmp    12b9 <main+0x139>
movl   $0x0,-0x8(%rbp)
cmpl   $0xa,-0x8(%rbp)
jge    12e6 <main+0x166>
lea    0xdac(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    12c0 <main+0x140>
mov    $0x9e,%edi
call   1070 <syscall@plt>
jmp    12b9 <main+0x139>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    $0x4,%edi
call   1090 <malloc@plt>
mov    $0x4,%edi
call   1090 <malloc@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
mov    %rax,%rbx
movl   $0x63,0x8(%rsp)
xor    %eax,%eax
call   1040 <getpid@plt>
mov    0x2e03(%rip),%rcx
mov    (%rcx),%esi
lea    0x8(%rsp),%rdx
mov    %eax,%edi
call   1070 <sched_setscheduler@plt>
test   %rbx,%rbx
je     1244 <main+0xb4>
test   %rax,%rax
jne    12ac <main+0x11c>
lea    0xe55(%rip),%r14
lea    0xe09(%rip),%r15
nopw   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1050 <clock@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1050 <clock@plt>
sub    %rbx,%rax
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
mov    %r15,%rdi
mov    $0x1,%al
call   1060 <printf@plt>
mov    $0xa1,%edi
call   1080 <syscall@plt>
mov    $0x9e,%edi
call   1080 <syscall@plt>
jmp    1200 <main+0x70>
test   %rax,%rax
jne    12ac <main+0x11c>
lea    0xde0(%rip),%rbx
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    $0x9e,%edi
call   1080 <syscall@plt>
jmp    1250 <main+0xc0>
lea    0xdb8(%rip),%rdi
call   1030 <puts@plt>
mov    0x2d21(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.timespec = type { i32 }
%struct.sched_param = type { i32 }
@SCHED_RR = global i32 0
@.str = [38 x i8] c"\0A\0AHelp me! Setting scheduler failed\0A\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [39 x i8] c"Child process here! I'll get out soon\0A\00"
@.str.2 = [48 x i8] c"\0A\0AGetting out! Elapsed time (in clocks): %lf \0A\0A\00"
@.str.3 = [22 x i8] c"Parent process here!\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.timespec*
%4 = alloca %struct.timespec*
%5 = alloca %struct.sched_param
%6 = alloca i64
%7 = alloca i64
store i32 0, i32* %1
store i32 0, i32* %2
%8 = call i64 @malloc(i32 4)
%9 = inttoptr i64 %8 to %struct.timespec*
store %struct.timespec* %9, %struct.timespec** %3
%10 = call i64 @malloc(i32 4)
%11 = inttoptr i64 %10 to %struct.timespec*
store %struct.timespec* %11, %struct.timespec** %4
%12 = call i64 (...) @fork()
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %36
14: ; preds = %0
%15 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %5, i32 0, i32 0
store i32 99, i32* %15
%16 = call i32 (...) @getpid()
%17 = load i32, i32* @SCHED_RR
%18 = call i64 @sched_setscheduler(i32 %16, i32 %17, %struct.sched_param* %5)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %14
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
%22 = load i32, i32* @EXIT_FAILURE
%23 = call i32 @exit(i32 %22)
unreachable
24: ; preds = %14
br label %25
25: ; preds = %24, %25
%26 = call i64 (...) @clock()
store i64 %26, i64* %6
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
%28 = call i64 (...) @clock()
store i64 %28, i64* %7
%29 = load i64, i64* %7
%30 = load i64, i64* %6
%31 = sub nsw i64 %29, %30
%32 = sitofp i64 %31 to double
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0), double %32)
%34 = call i32 @syscall(i32 161)
%35 = call i32 @syscall(i32 158)
br label %25
36: ; preds = %0
%37 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %5, i32 0, i32 0
store i32 99, i32* %37
%38 = call i32 (...) @getpid()
%39 = load i32, i32* @SCHED_RR
%40 = call i64 @sched_setscheduler(i32 %38, i32 %39, %struct.sched_param* %5)
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %46
42: ; preds = %36
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
%44 = load i32, i32* @EXIT_FAILURE
%45 = call i32 @exit(i32 %44)
unreachable
46: ; preds = %36
br label %47
47: ; preds = %46, %56
store i32 0, i32* %2
br label %48
48: ; preds = %53, %47
%49 = load i32, i32* %2
%50 = icmp slt i32 %49, 10
br i1 %50, label %51, label %56
51: ; preds = %48
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
br label %53
53: ; preds = %51
%54 = load i32, i32* %2
%55 = add nsw i32 %54, 1
store i32 %55, i32* %2
br label %48
56: ; preds = %48
%57 = call i32 @syscall(i32 158)
br label %47
}

LLVM IR (-O2):
%struct.sched_param = type { i32 }
@SCHED_RR = global i32 0
@EXIT_FAILURE = global i32 0
@.str.2 = [48 x i8] c"\0A\0AGetting out! Elapsed time (in clocks): %lf \0A\0A\00"
@str = [21 x i8] c"Parent process here!\00"
@str.5 = [38 x i8] c"Child process here! I'll get out soon\00"
@str.6 = [37 x i8] c"\0A\0AHelp me! Setting scheduler failed\0A\00"
define i32 @main() {
%1 = alloca %struct.sched_param
%2 = tail call i64 @malloc(i32 4)
%3 = tail call i64 @malloc(i32 4)
%4 = bitcast %struct.sched_param* %1 to i8*
%5 = tail call i64 (...) @fork()
%6 = icmp eq i64 %5, 0
%7 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %1, i64 0, i32 0
store i32 99, i32* %7
%8 = tail call i32 (...) @getpid()
%9 = load i32, i32* @SCHED_RR
%10 = call i64 @sched_setscheduler(i32 %8, i32 %9, %struct.sched_param* %1)
%11 = icmp eq i64 %10, 0
br i1 %6, label %26, label %12
12: ; preds = %0
br i1 %11, label %17, label %13
13: ; preds = %12
%14 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @str.6, i64 0, i64 0))
%15 = load i32, i32* @EXIT_FAILURE
%16 = call i32 @exit(i32 %15)
unreachable
17: ; preds = %12, %17
%18 = call i64 (...) @clock()
%19 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str.5, i64 0, i64 0))
%20 = call i64 (...) @clock()
%21 = sub nsw i64 %20, %18
%22 = sitofp i64 %21 to double
%23 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0), double %22)
%24 = call i32 @syscall(i32 161)
%25 = call i32 @syscall(i32 158)
br label %17
26: ; preds = %0
br i1 %11, label %31, label %27
27: ; preds = %26
%28 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @str.6, i64 0, i64 0))
%29 = load i32, i32* @EXIT_FAILURE
%30 = call i32 @exit(i32 %29)
unreachable
31: ; preds = %26, %31
%32 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%33 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%34 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%35 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%36 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%37 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%38 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%39 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%40 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%41 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%42 = call i32 @syscall(i32 158)
br label %31
}