#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 stat {
  int st_size;
};
typedef int pid_t;
typedef int /*<<< orphan*/ fileBuf;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
char *getpid();
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int pipe(int *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, ...);
int /*<<< orphan*/ sleep(int);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
int wait(int *);
int write(int, char **, int);
int main(int argc, char *argv[]) {
  if (argc != 2) {
    printf("Nenurodytas failas!\n");
    return 0;
  }
  struct stat buffer;
  int size;
  int fd[2];
  pid_t pid, x;
  int status;
  int dskr = open(argv[1], O_RDONLY);
  if (dskr < 0) {
    perror(argv[1]);
    return 0;
  }
  if (stat(argv[1], &buffer) != 0) {
    printf("Klaida\n");
  }
  size = buffer.st_size;
  char fileBuf[size];
  read(dskr, fileBuf, size);
  close(dskr);
  if (pipe(fd) == -1) {
    fprintf(stderr, "Nepavyko sukurti programinio kanalo !\n");
    return 0;
  }
  pid = getpid();
  if (write(fd[1], &fileBuf, size) != sizeof(fileBuf)) {
    fprintf(stderr, "Klaida rasant");
    return 0;
  }
  pid = fork();
  if (pid == 0) {
    char readBuf[size];
    sleep(1);
    read(fd[0], &readBuf, size);
    printf("Failo turinys: %s\n", readBuf);
    return 1;
  } else if (pid == -1) {
    fprintf(stderr, "Nepavyko sukurti vaiko !\n");
    return 0;
  } else {
    printf("(tevas) Mano PID: %ld\n", getpid());
    x = wait(&status);
    return 1;
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     120d <main+0x3d>
lea    0xe06(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    142d <main+0x25d>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2dcc(%rip),%rax
mov    (%rax),%esi
call   10b0 <open@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    1249 <main+0x79>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10c0 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    142d <main+0x25d>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x18(%rbp),%rsi
call   10a0 <stat@plt>
cmp    $0x0,%rax
je     1272 <main+0xa2>
lea    0xdaa(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %rax,-0x70(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rsi
sub    %rcx,%rsi
mov    %rsi,-0x68(%rbp)
mov    %rsi,%rsp
mov    %rax,-0x48(%rbp)
mov    -0x34(%rbp),%edi
mov    -0x1c(%rbp),%edx
mov    $0x0,%al
call   1080 <read@plt>
mov    -0x34(%rbp),%edi
call   1060 <close@plt>
lea    -0x24(%rbp),%rdi
call   1070 <pipe@plt>
cmp    $0xffffffff,%eax
jne    12f1 <main+0x121>
mov    0x2d08(%rip),%rax
mov    (%rax),%edi
lea    0xd44(%rip),%rsi
call   1090 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x4c(%rbp)
jmp    1426 <main+0x256>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x68(%rbp),%rsi
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
mov    -0x70(%rbp),%rcx
cltq
cmp    %rcx,%rax
je     1341 <main+0x171>
mov    0x2cb8(%rip),%rax
mov    (%rax),%edi
lea    0xd1b(%rip),%rsi
call   1090 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x4c(%rbp)
jmp    1426 <main+0x256>
mov    $0x0,%al
call   10f0 <fork@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jne    13c2 <main+0x1f2>
mov    -0x1c(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x58(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x78(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x60(%rbp)
mov    $0x1,%edi
call   10d0 <sleep@plt>
mov    -0x78(%rbp),%rsi
mov    -0x24(%rbp),%edi
mov    -0x1c(%rbp),%edx
mov    $0x0,%al
call   1080 <read@plt>
mov    -0x78(%rbp),%rsi
lea    0xcb1(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x4c(%rbp)
mov    -0x58(%rbp),%rax
mov    %rax,%rsp
jmp    1426 <main+0x256>
cmpl   $0xffffffff,-0x28(%rbp)
jne    13f4 <main+0x224>
mov    0x2c05(%rip),%rax
mov    (%rax),%edi
lea    0xc89(%rip),%rsi
call   1090 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x4c(%rbp)
jmp    1426 <main+0x256>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %rax,%rsi
lea    0xc7a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x30(%rbp),%rdi
call   10e0 <wait@plt>
mov    %eax,-0x2c(%rbp)
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x4c(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
cmp    $0x2,%edi
jne    12fe <main+0x11e>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
mov    0x2de0(%rip),%rax
mov    (%rax),%esi
call   10c0 <open@plt>
mov    0x8(%rbx),%rdi
test   %eax,%eax
js     1394 <main+0x1b4>
mov    %eax,%r12d
lea    -0x38(%rbp),%rsi
call   10b0 <stat@plt>
test   %rax,%rax
je     1238 <main+0x58>
lea    0xe46(%rip),%rdi
call   1030 <puts@plt>
mov    %rsp,-0x30(%rbp)
mov    -0x38(%rbp),%r15d
mov    %rsp,%rbx
lea    0xf(%r15),%r13
and    $0xfffffffffffffff0,%r13
sub    %r13,%rbx
mov    %rbx,%rsp
xor    %r14d,%r14d
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %r15d,%edx
xor    %eax,%eax
call   1090 <read@plt>
mov    %r12d,%edi
call   1070 <close@plt>
lea    -0x40(%rbp),%rdi
call   1080 <pipe@plt>
cmp    $0xffffffff,%eax
je     130f <main+0x12f>
xor    %r14d,%r14d
xor    %eax,%eax
call   1050 <getpid@plt>
mov    -0x3c(%rbp),%edi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1040 <write@plt>
cltq
cmp    %r15,%rax
jne    1326 <main+0x146>
xor    %r14d,%r14d
xor    %eax,%eax
call   1100 <fork@plt>
cmp    $0xffffffff,%eax
je     133d <main+0x15d>
test   %eax,%eax
jne    1354 <main+0x174>
mov    %rsp,%r12
mov    %rsp,%rbx
sub    %r13,%rbx
mov    %rbx,%rsp
mov    $0x1,%r14d
mov    $0x1,%edi
call   10e0 <sleep@plt>
mov    -0x40(%rbp),%edi
mov    %rbx,%rsi
mov    %r15d,%edx
xor    %eax,%eax
call   1090 <read@plt>
lea    0xd46(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r12,%rsp
jmp    137b <main+0x19b>
lea    0xd7b(%rip),%rdi
call   1030 <puts@plt>
xor    %r14d,%r14d
jmp    1382 <main+0x1a2>
mov    0x2cc2(%rip),%rax
mov    (%rax),%edi
lea    0xce1(%rip),%rsi
call   10a0 <fprintf@plt>
jmp    137b <main+0x19b>
mov    0x2cab(%rip),%rax
mov    (%rax),%edi
lea    0xcf1(%rip),%rsi
call   10a0 <fprintf@plt>
jmp    137b <main+0x19b>
mov    0x2c94(%rip),%rax
mov    (%rax),%edi
lea    0xcfb(%rip),%rsi
call   10a0 <fprintf@plt>
jmp    137b <main+0x19b>
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xd00(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
lea    -0x44(%rbp),%rdi
call   10f0 <wait@plt>
mov    $0x1,%r14d
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    %r14d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
call   10d0 <perror@plt>
xor    %r14d,%r14d
jmp    1382 <main+0x1a2>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [21 x i8] c"Nenurodytas failas!\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [8 x i8] c"Klaida\0A\00"
@stderr = global i32 0
@.str.2 = [39 x i8] c"Nepavyko sukurti programinio kanalo !\0A\00"
@.str.3 = [14 x i8] c"Klaida rasant\00"
@.str.4 = [19 x i8] c"Failo turinys: %s\0A\00"
@.str.5 = [26 x i8] c"Nepavyko sukurti vaiko !\0A\00"
@.str.6 = [23 x i8] c"(tevas) Mano PID: %ld\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.stat
%7 = alloca i32
%8 = alloca [2 x i32]
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i8*
%14 = alloca i64
%15 = alloca i32
%16 = alloca i8*
%17 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%18 = load i32, i32* %4
%19 = icmp ne i32 %18, 2
br i1 %19, label %20, label %22
20: ; preds = %2
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %3
br label %102
22: ; preds = %2
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
%26 = load i32, i32* @O_RDONLY
%27 = call i32 @open(i8* %25, i32 %26)
store i32 %27, i32* %12
%28 = load i32, i32* %12
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %35
30: ; preds = %22
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 1
%33 = load i8*, i8** %32
%34 = call i32 @perror(i8* %33)
store i32 0, i32* %3
br label %102
35: ; preds = %22
%36 = load i8**, i8*** %5
%37 = getelementptr inbounds i8*, i8** %36, i64 1
%38 = load i8*, i8** %37
%39 = call i64 @stat(i8* %38, %struct.stat* %6)
%40 = icmp ne i64 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %35
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %43
43: ; preds = %41, %35
%44 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%45 = load i32, i32* %44
store i32 %45, i32* %7
%46 = load i32, i32* %7
%47 = zext i32 %46 to i64
%48 = call i8* @llvm.stacksave()
store i8* %48, i8** %13
%49 = alloca i8, i64 %47
store i64 %47, i64* %14
%50 = load i32, i32* %12
%51 = load i32, i32* %7
%52 = call i32 (i32, ...) @read(i32 %50, i8* %49, i32 %51)
%53 = load i32, i32* %12
%54 = call i32 @close(i32 %53)
%55 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%56 = call i32 @pipe(i32* %55)
%57 = icmp eq i32 %56, -1
br i1 %57, label %58, label %61
58: ; preds = %43
%59 = load i32, i32* @stderr
%60 = call i32 @fprintf(i32 %59, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %3
store i32 1, i32* %15
br label %100
61: ; preds = %43
%62 = call i8* (...) @getpid()
%63 = ptrtoint i8* %62 to i32
store i32 %63, i32* %9
%64 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%65 = load i32, i32* %64
%66 = bitcast i8* %49 to i8**
%67 = load i32, i32* %7
%68 = call i32 @write(i32 %65, i8** %66, i32 %67)
%69 = sext i32 %68 to i64
%70 = icmp ne i64 %69, %47
br i1 %70, label %71, label %74
71: ; preds = %61
%72 = load i32, i32* @stderr
%73 = call i32 @fprintf(i32 %72, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %3
store i32 1, i32* %15
br label %100
74: ; preds = %61
%75 = call i32 (...) @fork()
store i32 %75, i32* %9
%76 = load i32, i32* %9
%77 = icmp eq i32 %76, 0
br i1 %77, label %78, label %90
78: ; preds = %74
%79 = load i32, i32* %7
%80 = zext i32 %79 to i64
%81 = call i8* @llvm.stacksave()
store i8* %81, i8** %16
%82 = alloca i8, i64 %80
store i64 %80, i64* %17
%83 = call i32 @sleep(i32 1)
%84 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%85 = load i32, i32* %84
%86 = load i32, i32* %7
%87 = call i32 (i32, ...) @read(i32 %85, i8* %82, i32 %86)
%88 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i8* %82)
store i32 1, i32* %3
store i32 1, i32* %15
%89 = load i8*, i8** %16
br label %100
90: ; preds = %74
%91 = load i32, i32* %9
%92 = icmp eq i32 %91, -1
br i1 %92, label %93, label %96
93: ; preds = %90
%94 = load i32, i32* @stderr
%95 = call i32 @fprintf(i32 %94, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0))
store i32 0, i32* %3
store i32 1, i32* %15
br label %100
96: ; preds = %90
%97 = call i8* (...) @getpid()
%98 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0), i8* %97)
%99 = call i32 @wait(i32* %11)
store i32 %99, i32* %10
store i32 1, i32* %3
store i32 1, i32* %15
br label %100
100: ; preds = %96, %93, %78, %71, %58
%101 = load i8*, i8** %13
br label %102
102: ; preds = %100, %30, %20
%103 = load i32, i32* %3
ret i32 %103
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str.2 = [39 x i8] c"Nepavyko sukurti programinio kanalo !\0A\00"
@.str.3 = [14 x i8] c"Klaida rasant\00"
@.str.4 = [19 x i8] c"Failo turinys: %s\0A\00"
@.str.5 = [26 x i8] c"Nepavyko sukurti vaiko !\0A\00"
@.str.6 = [23 x i8] c"(tevas) Mano PID: %ld\0A\00"
@str = [7 x i8] c"Klaida\00"
@str.7 = [20 x i8] c"Nenurodytas failas!\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = alloca [2 x i32]
%5 = alloca i32
%6 = icmp eq i32 %0, 2
br i1 %6, label %9, label %7
7: ; preds = %2
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.7, i64 0, i64 0))
br label %71
9: ; preds = %2
%10 = bitcast %struct.stat* %3 to i8*
%11 = bitcast [2 x i32]* %4 to i8*
%12 = bitcast i32* %5 to i8*
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
%15 = load i32, i32* @O_RDONLY
%16 = tail call i32 @open(i8* %14, i32 %15)
%17 = icmp slt i32 %16, 0
%18 = load i8*, i8** %13
br i1 %17, label %19, label %21
19: ; preds = %9
%20 = tail call i32 @perror(i8* %18)
br label %69
21: ; preds = %9
%22 = call i64 @stat(i8* %18, %struct.stat* %3)
%23 = icmp eq i64 %22, 0
br i1 %23, label %26, label %24
24: ; preds = %21
%25 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str, i64 0, i64 0))
br label %26
26: ; preds = %24, %21
%27 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%28 = load i32, i32* %27
%29 = zext i32 %28 to i64
%30 = call i8* @llvm.stacksave()
%31 = alloca i8, i64 %29
%32 = call i32 (i32, ...) @read(i32 %16, i8* %31, i32 %28)
%33 = call i32 @close(i32 %16)
%34 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%35 = call i32 @pipe(i32* %34)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %40
37: ; preds = %26
%38 = load i32, i32* @stderr
%39 = call i32 @fprintf(i32 %38, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0))
br label %67
40: ; preds = %26
%41 = call i8* (...) @getpid()
%42 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%43 = load i32, i32* %42
%44 = bitcast i8* %31 to i8**
%45 = call i32 @write(i32 %43, i8** %44, i32 %28)
%46 = sext i32 %45 to i64
%47 = icmp eq i64 %46, %29
br i1 %47, label %51, label %48
48: ; preds = %40
%49 = load i32, i32* @stderr
%50 = call i32 @fprintf(i32 %49, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
br label %67
51: ; preds = %40
%52 = call i32 (...) @fork()
switch i32 %52, label %63 [
i32 0, label %53
i32 -1, label %60
]
53: ; preds = %51
%54 = call i8* @llvm.stacksave()
%55 = alloca i8, i64 %29
%56 = call i32 @sleep(i32 1)
%57 = load i32, i32* %34
%58 = call i32 (i32, ...) @read(i32 %57, i8* %55, i32 %28)
%59 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i8* %55)
br label %67
60: ; preds = %51
%61 = load i32, i32* @stderr
%62 = call i32 @fprintf(i32 %61, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0))
br label %67
63: ; preds = %51
%64 = call i8* (...) @getpid()
%65 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0), i8* %64)
%66 = call i32 @wait(i32* %5)
br label %67
67: ; preds = %63, %60, %53, %48, %37
%68 = phi i32 [ 0, %37 ], [ 0, %48 ], [ 1, %53 ], [ 0, %60 ], [ 1, %63 ]
br label %69
69: ; preds = %67, %19
%70 = phi i32 [ 0, %19 ], [ %68, %67 ]
br label %71
71: ; preds = %69, %7
%72 = phi i32 [ 0, %7 ], [ %70, %69 ]
ret i32 %72
}