#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 atoi(char *);
int /*<<< orphan*/ execl(char *, char *, char *, int /*<<< orphan*/ *);
int getpid();
int getppid();
int /*<<< orphan*/ printf(char *, int, int, int);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int main(int argc, char *argv[]) {
  if (argc == 2) {
    int arg = atoi(argv[1]);
    pid_t pid = getpid();
    pid_t ppid = getppid();
    printf("PID: %d\t PPID: %d\t ARG: %d\n", pid, ppid, arg);
    if (arg > 0) {
      --arg;
      char msg[3];
      sprintf(msg, "%d", arg);
      execl("/home/martynas/Projects/os-assignments/LD4.1/a.out", "a.out", msg,
            NULL);
    }
  } else {
    puts("invalid number of args. expected only 1.");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jne    120f <main+0x9f>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1060 <atoi@plt>
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1080 <getppid@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%esi
mov    -0x1c(%rbp),%edx
mov    -0x14(%rbp),%ecx
lea    0xe3c(%rip),%rdi
call   1050 <printf@plt>
cmpl   $0x0,-0x14(%rbp)
jle    120a <main+0x9a>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
lea    -0x1f(%rbp),%rdi
mov    -0x14(%rbp),%edx
lea    0xe32(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x1f(%rbp),%rdx
lea    0xe25(%rip),%rdi
lea    0xe4b(%rip),%rsi
xor    %eax,%eax
mov    %eax,%ecx
call   1090 <execl@plt>
jmp    121b <main+0xab>
lea    0xe3c(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
cmp    $0x2,%edi
jne    11dc <main+0x6c>
mov    0x8(%rsi),%rdi
call   1060 <atoi@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1080 <getppid@plt>
lea    0xe66(%rip),%rdi
mov    %ebp,%esi
mov    %eax,%edx
mov    %ebx,%ecx
call   1050 <printf@plt>
test   %ebx,%ebx
jle    11e8 <main+0x78>
add    $0xffffffff,%ebx
lea    0xe69(%rip),%rsi
lea    0x5(%rsp),%rbp
mov    %rbp,%rdi
mov    %ebx,%edx
call   1070 <sprintf@plt>
lea    0xe56(%rip),%rdi
lea    0xe7c(%rip),%rsi
mov    %rbp,%rdx
xor    %ecx,%ecx
call   1090 <execl@plt>
jmp    11e8 <main+0x78>
lea    0xe6f(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [28 x i8] c"PID: %d\09 PPID: %d\09 ARG: %d\0A\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [51 x i8] c"/home/martynas/Projects/os-assignments/LD4.1/a.out\00"
@.str.3 = [6 x i8] c"a.out\00"
@.str.4 = [41 x i8] c"invalid number of args. expected only 1.\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [3 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp eq i32 %10, 2
br i1 %11, label %12, label %34
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %14
%16 = call i32 @atoi(i8* %15)
store i32 %16, i32* %6
%17 = call i32 (...) @getpid()
store i32 %17, i32* %7
%18 = call i32 (...) @getppid()
store i32 %18, i32* %8
%19 = load i32, i32* %7
%20 = load i32, i32* %8
%21 = load i32, i32* %6
%22 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %19, i32 %20, i32 %21)
%23 = load i32, i32* %6
%24 = icmp sgt i32 %23, 0
br i1 %24, label %25, label %33
25: ; preds = %12
%26 = load i32, i32* %6
%27 = add nsw i32 %26, -1
store i32 %27, i32* %6
%28 = getelementptr inbounds [3 x i8], [3 x i8]* %9, i64 0, i64 0
%29 = load i32, i32* %6
%30 = call i32 @sprintf(i8* %28, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %29)
%31 = getelementptr inbounds [3 x i8], [3 x i8]* %9, i64 0, i64 0
%32 = call i32 @execl(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %31, i32* null)
br label %33
33: ; preds = %25, %12
br label %36
34: ; preds = %2
%35 = call i32 @puts(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0))
br label %36
36: ; preds = %34, %33
ret i32 0
}

LLVM IR (-O2):
@.str = [28 x i8] c"PID: %d\09 PPID: %d\09 ARG: %d\0A\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [51 x i8] c"/home/martynas/Projects/os-assignments/LD4.1/a.out\00"
@.str.3 = [6 x i8] c"a.out\00"
@.str.4 = [41 x i8] c"invalid number of args. expected only 1.\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [3 x i8]
%4 = icmp eq i32 %0, 2
br i1 %4, label %5, label %18
5: ; preds = %2
%6 = getelementptr inbounds i8*, i8** %1, i64 1
%7 = load i8*, i8** %6
%8 = tail call i32 @atoi(i8* %7)
%9 = tail call i32 (...) @getpid()
%10 = tail call i32 (...) @getppid()
%11 = tail call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %9, i32 %10, i32 %8)
%12 = icmp sgt i32 %8, 0
br i1 %12, label %13, label %20
13: ; preds = %5
%14 = add nsw i32 %8, -1
%15 = getelementptr inbounds [3 x i8], [3 x i8]* %3, i64 0, i64 0
%16 = call i32 @sprintf(i8* %15, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %14)
%17 = call i32 @execl(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %15, i32* null)
br label %20
18: ; preds = %2
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0))
br label %20
20: ; preds = %5, %13, %18
ret i32 0
}