#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 pid_t;

/* Variables and functions */
int fork();
int getpid();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, int *);
long syscall(int, int, int);
int main(void) {
  int choice;
  pid_t pid;
  printf("Enter (1) to fork current process or (2) to change a specific "
         "process' hidden flag. (0) to exit. ");
  scanf("%d", &choice);
  switch (choice) {
  case 0: {
    printf("Good bye...\n");
    return 0;
  } break;
  case 1: {
    pid = getpid();
    int frk = fork();
    if (frk == -1) {
      printf("Process couldn't fork succesfully. You have no permission to do "
             "this operation.\n");
      return -1;
    } else if (frk == 0) {
      printf("I'm the child process and my pid is %d\n", getpid());
      return 0;
    } else {
      printf("I'm the parent process and my pid is %d\n", pid);
      return 0;
    }
  } break;
  case 2: {
    int flag;
    printf("Please enter pid of process you want to change its flag: ");
    scanf("%d", &pid);
    printf("Please enter 'hidden' flag value: ");
    scanf("%d", &flag);
    long ret = syscall(351, pid, flag);
    if (ret != 0) {
      printf("Hidden flag couldn't changed succesfully, error code is %ld.\n",
             ret);
      return -1;
    } else {
      printf("Hidden flag has been changed succesfully.\n");
      return 0;
    }
  } break;
  default:
    return 0;
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe9a(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xeee(%rip),%rdi
lea    -0x8(%rbp),%rsi
call   1060 <scanf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x24(%rbp)
test   %eax,%eax
je     11b2 <main+0x62>
jmp    1190 <main+0x40>
mov    -0x24(%rbp),%eax
sub    $0x1,%eax
je     11cc <main+0x7c>
jmp    11a1 <main+0x51>
mov    -0x24(%rbp),%eax
sub    $0x2,%eax
je     124e <main+0xfe>
jmp    12e1 <main+0x191>
lea    0xeac(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12e8 <main+0x198>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0xc(%rbp)
mov    $0x0,%al
call   1070 <fork@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    1204 <main+0xb4>
lea    0xe81(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e8 <main+0x198>
cmpl   $0x0,-0x10(%rbp)
jne    1231 <main+0xe1>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xea5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12e8 <main+0x198>
mov    -0xc(%rbp),%esi
lea    0xeb0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12e8 <main+0x198>
lea    0xebf(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xdff(%rip),%rdi
lea    -0xc(%rbp),%rsi
call   1060 <scanf@plt>
lea    0xedb(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xde1(%rip),%rdi
lea    -0x14(%rbp),%rsi
call   1060 <scanf@plt>
mov    -0xc(%rbp),%esi
mov    -0x14(%rbp),%edx
mov    $0x15f,%edi
call   1050 <syscall@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     12c7 <main+0x177>
mov    -0x20(%rbp),%rsi
lea    0xebd(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e8 <main+0x198>
lea    0xee1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12e8 <main+0x198>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
lea    0xe93(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xee5(%rip),%rdi
lea    0x14(%rsp),%rsi
call   1070 <scanf@plt>
mov    0x14(%rsp),%eax
cmp    $0x2,%eax
je     11ad <main+0x4d>
cmp    $0x1,%eax
je     121b <main+0xbb>
test   %eax,%eax
jne    127b <main+0x11b>
lea    0x1023(%rip),%rdi
jmp    1250 <main+0xf0>
lea    0xf02(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe9e(%rip),%rbp
lea    0x10(%rsp),%rsi
mov    %rbp,%rdi
call   1070 <scanf@plt>
lea    0xf18(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xc(%rsp),%rsi
mov    %rbp,%rdi
call   1070 <scanf@plt>
mov    0x10(%rsp),%esi
mov    0xc(%rsp),%edx
mov    $0x15f,%edi
call   1060 <syscall@plt>
test   %rax,%rax
je     1249 <main+0xe9>
lea    0xf09(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xffffffff,%ebx
jmp    127b <main+0x11b>
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
je     1257 <main+0xf7>
cmp    $0xffffffff,%eax
jne    1269 <main+0x109>
lea    0xf3e(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebx
jmp    127b <main+0x11b>
lea    0xf01(%rip),%rdi
call   1030 <puts@plt>
jmp    127b <main+0x11b>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe00(%rip),%rdi
mov    %eax,%esi
jmp    1274 <main+0x114>
lea    0xe1d(%rip),%rdi
xor    %ebx,%ebx
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [98 x i8] c"Enter (1) to fork current process or (2) to change a specific process' hidden flag. (0) to exit. \00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [13 x i8] c"Good bye...\0A\00"
@.str.3 = [81 x i8] c"Process couldn't fork succesfully. You have no permission to do this operation.\0A\00"
@.str.4 = [40 x i8] c"I'm the child process and my pid is %d\0A\00"
@.str.5 = [41 x i8] c"I'm the parent process and my pid is %d\0A\00"
@.str.6 = [58 x i8] c"Please enter pid of process you want to change its flag: \00"
@.str.7 = [35 x i8] c"Please enter 'hidden' flag value: \00"
@.str.8 = [62 x i8] c"Hidden flag couldn't changed succesfully, error code is %ld.\0A\00"
@.str.9 = [43 x i8] c"Hidden flag has been changed succesfully.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64
store i32 0, i32* %1
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([98 x i8], [98 x i8]* @.str, i64 0, i64 0))
%8 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %2)
%9 = load i32, i32* %2
switch i32 %9, label %43 [
i32 0, label %10
i32 1, label %12
i32 2, label %28
]
10: ; preds = %0
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %1
br label %44
12: ; preds = %0
%13 = call i32 (...) @getpid()
store i32 %13, i32* %3
%14 = call i32 (...) @fork()
store i32 %14, i32* %4
%15 = load i32, i32* %4
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %12
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([81 x i8], [81 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %1
br label %44
19: ; preds = %12
%20 = load i32, i32* %4
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %19
%23 = call i32 (...) @getpid()
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.4, i64 0, i64 0), i32 %23)
store i32 0, i32* %1
br label %44
25: ; preds = %19
%26 = load i32, i32* %3
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.5, i64 0, i64 0), i32 %26)
store i32 0, i32* %1
br label %44
28: ; preds = %0
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.6, i64 0, i64 0))
%30 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %3)
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.7, i64 0, i64 0))
%32 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %5)
%33 = load i32, i32* %3
%34 = load i32, i32* %5
%35 = call i64 @syscall(i32 351, i32 %33, i32 %34)
store i64 %35, i64* %6
%36 = load i64, i64* %6
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %28
%39 = load i64, i64* %6
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.8, i64 0, i64 0), i64 %39)
store i32 -1, i32* %1
br label %44
41: ; preds = %28
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.9, i64 0, i64 0))
store i32 0, i32* %1
br label %44
43: ; preds = %0
store i32 0, i32* %1
br label %44
44: ; preds = %43, %41, %38, %25, %22, %17, %10
%45 = load i32, i32* %1
ret i32 %45
}

LLVM IR (-O2):
@.str = [98 x i8] c"Enter (1) to fork current process or (2) to change a specific process' hidden flag. (0) to exit. \00"
@.str.1 = [3 x i8] c"%d\00"
@.str.4 = [40 x i8] c"I'm the child process and my pid is %d\0A\00"
@.str.5 = [41 x i8] c"I'm the parent process and my pid is %d\0A\00"
@.str.6 = [58 x i8] c"Please enter pid of process you want to change its flag: \00"
@.str.7 = [35 x i8] c"Please enter 'hidden' flag value: \00"
@.str.8 = [62 x i8] c"Hidden flag couldn't changed succesfully, error code is %ld.\0A\00"
@str = [42 x i8] c"Hidden flag has been changed succesfully.\00"
@str.10 = [80 x i8] c"Process couldn't fork succesfully. You have no permission to do this operation.\00"
@str.11 = [12 x i8] c"Good bye...\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = bitcast i32* %1 to i8*
%5 = bitcast i32* %2 to i8*
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([98 x i8], [98 x i8]* @.str, i64 0, i64 0))
%7 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %1)
%8 = load i32, i32* %1
switch i32 %8, label %37 [
i32 0, label %9
i32 1, label %11
i32 2, label %21
]
9: ; preds = %0
%10 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.11, i64 0, i64 0))
br label %37
11: ; preds = %0
%12 = call i32 (...) @getpid()
%13 = call i32 (...) @fork()
switch i32 %13, label %19 [
i32 -1, label %14
i32 0, label %16
]
14: ; preds = %11
%15 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([80 x i8], [80 x i8]* @str.10, i64 0, i64 0))
br label %37
16: ; preds = %11
%17 = call i32 (...) @getpid()
%18 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.4, i64 0, i64 0), i32 %17)
br label %37
19: ; preds = %11
%20 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str.5, i64 0, i64 0), i32 %12)
br label %37
21: ; preds = %0
%22 = bitcast i32* %3 to i8*
%23 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([58 x i8], [58 x i8]* @.str.6, i64 0, i64 0))
%24 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %2)
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.7, i64 0, i64 0))
%26 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %3)
%27 = load i32, i32* %2
%28 = load i32, i32* %3
%29 = call i64 @syscall(i32 351, i32 %27, i32 %28)
%30 = icmp eq i64 %29, 0
br i1 %30, label %33, label %31
31: ; preds = %21
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([62 x i8], [62 x i8]* @.str.8, i64 0, i64 0), i64 %29)
br label %35
33: ; preds = %21
%34 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @str, i64 0, i64 0))
br label %35
35: ; preds = %33, %31
%36 = phi i32 [ -1, %31 ], [ 0, %33 ]
br label %37
37: ; preds = %0, %14, %16, %19, %35, %9
%38 = phi i32 [ %36, %35 ], [ 0, %9 ], [ -1, %14 ], [ 0, %16 ], [ 0, %19 ], [ 0, %0 ]
ret i32 %38
}