#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 scalar_t__ pid_t;
typedef int intmax_t;

/* Variables and functions */
int O_APPEND;
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ execl(char *, char *, int /*<<< orphan*/ *);
scalar_t__ fork();
int getpid();
int open(char *, int);
int /*<<< orphan*/ printf(char *, ...);
int rand();
int /*<<< orphan*/ srand(int /*<<< orphan*/);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int /*<<< orphan*/ writePid(int, int);
int main(int argc, char *argv[]) {
  if (argc < 2) {
    printf("Wymagany jeden argument (plik do zapisu pid-u; plik nie musi "
           "sitniec)\n\n\n");
    return 1;
  } else {
    srand(time(NULL));
    pid_t pid;
    pid = fork();
    intmax_t a = rand() * getpid();
    int b = getpid() % 10;
    printf("Moj pid:%d\n", (intmax_t)getpid());
    printf("Moja zmienna[a:%d b: %d]\n", a, b);
    if (!pid) {
      printf("\nJestem potomkiem :D\n\n");
      printf("\nNazwa systemu to \n");
      execl("/bin/uname", "uname -a", NULL);
    } else
      printf("\nNie jestem potomkiem xD\n\n");
    wait(NULL);
    printf("\nPaPa\n");
    int localDescr = open(argv[1], O_RDWR | O_CREAT | O_APPEND);
    for (int i = 0; i < 10; i++) {
      if (pid)
        pid = fork();
    }
    if (!writePid(getpid(), localDescr))
      printf("Blad zapisu\n");
    return 0;
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11da <main+0x3a>
lea    0xe39(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    134a <main+0x1aa>
xor    %eax,%eax
mov    %eax,%edi
call   1070 <time@plt>
mov    %eax,%edi
call   1060 <srand@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   10c0 <rand@plt>
mov    %eax,-0x2c(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%ecx
mov    -0x2c(%rbp),%eax
imul   %ecx,%eax
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    %edx,-0x20(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xe16(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x1c(%rbp),%esi
mov    -0x20(%rbp),%edx
lea    0xe0e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
cmpq   $0x0,-0x18(%rbp)
jne    1291 <main+0xf1>
lea    0xe0f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe18(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe1e(%rip),%rdi
lea    0xe22(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1090 <execl@plt>
jmp    129f <main+0xff>
lea    0xe16(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10a0 <wait@plt>
lea    0xe1a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2d1b(%rip),%rax
mov    (%rax),%esi
mov    0x2d2a(%rip),%rax
or     (%rax),%esi
mov    0x2cf1(%rip),%rax
or     (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
cmpl   $0xa,-0x28(%rbp)
jge    131b <main+0x17b>
cmpq   $0x0,-0x18(%rbp)
je     1308 <main+0x168>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x18(%rbp)
jmp    130d <main+0x16d>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    12e8 <main+0x148>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%edi
mov    -0x24(%rbp),%esi
call   1030 <writePid@plt>
cmp    $0x0,%eax
jne    1343 <main+0x1a3>
lea    0xd94(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
cmp    $0x2,%edi
jge    11cd <main+0x1d>
mov    $0x1,%ebx
lea    0xec7(%rip),%rdi
jmp    1359 <main+0x1a9>
mov    %rsi,%r14
xor    %edi,%edi
call   1080 <time@plt>
mov    %eax,%edi
call   1070 <srand@plt>
xor    %eax,%eax
call   10c0 <fork@plt>
mov    %rax,%r15
xor    %eax,%eax
call   10d0 <rand@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1050 <getpid@plt>
imul   %eax,%ebx
xor    %eax,%eax
call   1050 <getpid@plt>
movslq %eax,%rbp
imul   $0x66666667,%rbp,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x22,%rax
add    %ecx,%eax
add    %eax,%eax
lea    (%rax,%rax,4),%eax
sub    %eax,%ebp
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xdd2(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xdce(%rip),%rdi
mov    %ebx,%esi
mov    %ebp,%edx
xor    %eax,%eax
call   1060 <printf@plt>
test   %r15,%r15
je     125c <main+0xac>
lea    0xe20(%rip),%rdi
call   1040 <puts@plt>
jmp    1289 <main+0xd9>
lea    0xdd7(%rip),%rdi
call   1040 <puts@plt>
lea    0xde1(%rip),%rdi
call   1040 <puts@plt>
lea    0xdab(%rip),%rdi
lea    0xdaf(%rip),%rsi
xor    %edx,%edx
call   10a0 <execl@plt>
xor    %ebx,%ebx
xor    %edi,%edi
call   10b0 <wait@plt>
lea    0xdca(%rip),%rdi
call   1040 <puts@plt>
mov    0x2d3b(%rip),%rax
mov    0x2d4c(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x8(%r14),%rdi
mov    0x2d0d(%rip),%rax
or     (%rax),%esi
call   1090 <open@plt>
mov    %eax,%r14d
test   %r15,%r15
je     133d <main+0x18d>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
je     133d <main+0x18d>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
je     133d <main+0x18d>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
je     133d <main+0x18d>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
je     133d <main+0x18d>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
je     133d <main+0x18d>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
je     133d <main+0x18d>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
je     133d <main+0x18d>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
je     133d <main+0x18d>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
je     133d <main+0x18d>
xor    %eax,%eax
call   10c0 <fork@plt>
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %eax,%edi
mov    %r14d,%esi
call   1030 <writePid@plt>
test   %eax,%eax
jne    135e <main+0x1ae>
lea    0xd10(%rip),%rdi
call   1040 <puts@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [73 x i8] c"Wymagany jeden argument (plik do zapisu pid-u; plik nie musi sitniec)\0A\0A\0A\00"
@.str.1 = [12 x i8] c"Moj pid:%d\0A\00"
@.str.2 = [26 x i8] c"Moja zmienna[a:%d b: %d]\0A\00"
@.str.3 = [23 x i8] c"\0AJestem potomkiem :D\0A\0A\00"
@.str.4 = [20 x i8] c"\0ANazwa systemu to \0A\00"
@.str.5 = [11 x i8] c"/bin/uname\00"
@.str.6 = [9 x i8] c"uname -a\00"
@.str.7 = [27 x i8] c"\0ANie jestem potomkiem xD\0A\0A\00"
@.str.8 = [7 x i8] c"\0APaPa\0A\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@.str.9 = [13 x i8] c"Blad zapisu\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 2
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([73 x i8], [73 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %69
15: ; preds = %2
%16 = call i32 @time(i32* null)
%17 = call i32 @srand(i32 %16)
%18 = call i64 (...) @fork()
store i64 %18, i64* %6
%19 = call i32 (...) @rand()
%20 = call i32 (...) @getpid()
%21 = mul nsw i32 %19, %20
store i32 %21, i32* %7
%22 = call i32 (...) @getpid()
%23 = srem i32 %22, 10
store i32 %23, i32* %8
%24 = call i32 (...) @getpid()
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %24)
%26 = load i32, i32* %7
%27 = load i32, i32* %8
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %26, i32 %27)
%29 = load i64, i64* %6
%30 = icmp ne i64 %29, 0
br i1 %30, label %35, label %31
31: ; preds = %15
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
%34 = call i32 @execl(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0), i32* null)
br label %37
35: ; preds = %15
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.7, i64 0, i64 0))
br label %37
37: ; preds = %35, %31
%38 = call i32 @wait(i32* null)
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.8, i64 0, i64 0))
%40 = load i8**, i8*** %5
%41 = getelementptr inbounds i8*, i8** %40, i64 1
%42 = load i8*, i8** %41
%43 = load i32, i32* @O_RDWR
%44 = load i32, i32* @O_CREAT
%45 = or i32 %43, %44
%46 = load i32, i32* @O_APPEND
%47 = or i32 %45, %46
%48 = call i32 @open(i8* %42, i32 %47)
store i32 %48, i32* %9
store i32 0, i32* %10
br label %49
49: ; preds = %58, %37
%50 = load i32, i32* %10
%51 = icmp slt i32 %50, 10
br i1 %51, label %52, label %61
52: ; preds = %49
%53 = load i64, i64* %6
%54 = icmp ne i64 %53, 0
br i1 %54, label %55, label %57
55: ; preds = %52
%56 = call i64 (...) @fork()
store i64 %56, i64* %6
br label %57
57: ; preds = %55, %52
br label %58
58: ; preds = %57
%59 = load i32, i32* %10
%60 = add nsw i32 %59, 1
store i32 %60, i32* %10
br label %49
61: ; preds = %49
%62 = call i32 (...) @getpid()
%63 = load i32, i32* %9
%64 = call i32 @writePid(i32 %62, i32 %63)
%65 = icmp ne i32 %64, 0
br i1 %65, label %68, label %66
66: ; preds = %61
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.9, i64 0, i64 0))
br label %68
68: ; preds = %66, %61
store i32 0, i32* %3
br label %69
69: ; preds = %68, %13
%70 = load i32, i32* %3
ret i32 %70
}

LLVM IR (-O2):
@.str.1 = [12 x i8] c"Moj pid:%d\0A\00"
@.str.2 = [26 x i8] c"Moja zmienna[a:%d b: %d]\0A\00"
@.str.5 = [11 x i8] c"/bin/uname\00"
@.str.6 = [9 x i8] c"uname -a\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@str = [22 x i8] c"\0AJestem potomkiem :D\0A\00"
@str.10 = [19 x i8] c"\0ANazwa systemu to \00"
@str.11 = [6 x i8] c"\0APaPa\00"
@str.12 = [12 x i8] c"Blad zapisu\00"
@str.13 = [26 x i8] c"\0ANie jestem potomkiem xD\0A\00"
@str.14 = [72 x i8] c"Wymagany jeden argument (plik do zapisu pid-u; plik nie musi sitniec)\0A\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %67, label %4
4: ; preds = %2
%5 = tail call i32 @time(i32* null)
%6 = tail call i32 @srand(i32 %5)
%7 = tail call i64 (...) @fork()
%8 = tail call i32 (...) @rand()
%9 = tail call i32 (...) @getpid()
%10 = mul nsw i32 %9, %8
%11 = tail call i32 (...) @getpid()
%12 = srem i32 %11, 10
%13 = tail call i32 (...) @getpid()
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %13)
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %10, i32 %12)
%16 = icmp eq i64 %7, 0
br i1 %16, label %17, label %21
17: ; preds = %4
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.10, i64 0, i64 0))
%20 = tail call i32 @execl(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0), i32* null)
br label %23
21: ; preds = %4
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.13, i64 0, i64 0))
br label %23
23: ; preds = %21, %17
%24 = tail call i32 @wait(i32* null)
%25 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str.11, i64 0, i64 0))
%26 = getelementptr inbounds i8*, i8** %1, i64 1
%27 = load i8*, i8** %26
%28 = load i32, i32* @O_RDWR
%29 = load i32, i32* @O_CREAT
%30 = or i32 %29, %28
%31 = load i32, i32* @O_APPEND
%32 = or i32 %30, %31
%33 = tail call i32 @open(i8* %27, i32 %32)
br i1 %16, label %63, label %34
34: ; preds = %23
%35 = tail call i64 (...) @fork()
%36 = icmp eq i64 %35, 0
br i1 %36, label %63, label %37
37: ; preds = %34
%38 = tail call i64 (...) @fork()
%39 = icmp eq i64 %38, 0
br i1 %39, label %63, label %40
40: ; preds = %37
%41 = tail call i64 (...) @fork()
%42 = icmp eq i64 %41, 0
br i1 %42, label %63, label %43
43: ; preds = %40
%44 = tail call i64 (...) @fork()
%45 = icmp eq i64 %44, 0
br i1 %45, label %63, label %46
46: ; preds = %43
%47 = tail call i64 (...) @fork()
%48 = icmp eq i64 %47, 0
br i1 %48, label %63, label %49
49: ; preds = %46
%50 = tail call i64 (...) @fork()
%51 = icmp eq i64 %50, 0
br i1 %51, label %63, label %52
52: ; preds = %49
%53 = tail call i64 (...) @fork()
%54 = icmp eq i64 %53, 0
br i1 %54, label %63, label %55
55: ; preds = %52
%56 = tail call i64 (...) @fork()
%57 = icmp eq i64 %56, 0
br i1 %57, label %63, label %58
58: ; preds = %55
%59 = tail call i64 (...) @fork()
%60 = icmp eq i64 %59, 0
br i1 %60, label %63, label %61
61: ; preds = %58
%62 = tail call i64 (...) @fork()
br label %63
63: ; preds = %23, %34, %37, %40, %43, %46, %49, %52, %55, %61, %58
%64 = tail call i32 (...) @getpid()
%65 = tail call i32 @writePid(i32 %64, i32 %33)
%66 = icmp eq i32 %65, 0
br i1 %66, label %67, label %71
67: ; preds = %63, %2
%68 = phi i8* [ getelementptr inbounds ([72 x i8], [72 x i8]* @str.14, i64 0, i64 0), %2 ], [ getelementptr inbounds ([12 x i8], [12 x i8]* @str.12, i64 0, i64 0), %63 ]
%69 = phi i32 [ 1, %2 ], [ 0, %63 ]
%70 = tail call i32 @puts(i8* dereferenceable(1) %68)
br label %71
71: ; preds = %67, %63
%72 = phi i32 [ 0, %63 ], [ %69, %67 ]
ret i32 %72
}