#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__ ssize_t;
typedef int /*<<< orphan*/ buffer;

/* Variables and functions */
int O_APPEND;
int O_CREAT;
int O_RDONLY;
int O_WRONLY;
int /*<<< orphan*/ S_IWUSR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ err(int, char *);
int /*<<< orphan*/ execlp(char *, char *);
int fork();
int getpid();
int open(char *, int);
int /*<<< orphan*/ printf(char *, int);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ wait(int *);
scalar_t__ write(int, char *, scalar_t__);
int main(int argc, char **argv) {
  char buffer[1 << 12];
  int pid, status, fd_read, fd_write;
  ssize_t read_size;
  if (argc != 3) {
    err(1, "Exactly 2 arguments must be passed!");
  }
  pid = fork();
  if (pid == -1) {
    err(1, "Error while forking!");
  }
  if (pid > 0) {
    wait(&status);
    printf("Good work, my %d!\n", pid);
  } else {
    if ((fd_read = open(argv[1], O_RDONLY)) == -1) {
      err(1, "Error while opening the first file for reading!");
    }
    if ((fd_write = open(argv[2], O_WRONLY | O_CREAT | O_APPEND), S_IWUSR) ==
        -1) {
      close(fd_read);
      err(1, "Error while opening the second file for writing!");
    }
    while ((read_size = read(fd_read, buffer, sizeof(buffer))) > 0) {
      if (read_size != write(fd_write, buffer, read_size)) {
        err(1, "Error while writing content to the second file!");
      }
    }
    close(fd_read);
    close(fd_write);
    execlp("ls", "ls");
    printf("Father %d, i'm done!\n", getpid());
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11d4 <main+0x34>
mov    $0x1,%edi
lea    0xe31(%rip),%rsi
call   1080 <err@plt>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %eax,-0x1014(%rbp)
cmpl   $0xffffffff,-0x1014(%rbp)
jne    11ff <main+0x5f>
mov    $0x1,%edi
lea    0xe2a(%rip),%rsi
call   1080 <err@plt>
cmpl   $0x0,-0x1014(%rbp)
jle    122f <main+0x8f>
lea    -0x1018(%rbp),%rdi
call   10b0 <wait@plt>
mov    -0x1014(%rbp),%esi
lea    0xe14(%rip),%rdi
call   1050 <printf@plt>
jmp    1376 <main+0x1d6>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2da2(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x101c(%rbp)
cmp    $0xffffffff,%eax
jne    1265 <main+0xc5>
mov    $0x1,%edi
lea    0xdec(%rip),%rsi
call   1080 <err@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2d4c(%rip),%rax
mov    (%rax),%esi
mov    0x2d7b(%rip),%rax
or     (%rax),%esi
mov    0x2d32(%rip),%rax
or     (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x1020(%rbp)
mov    0x2d3e(%rip),%rax
cmpl   $0xffffffff,(%rax)
jne    12bf <main+0x11f>
mov    -0x101c(%rbp),%edi
call   1060 <close@plt>
mov    $0x1,%edi
lea    0xdc2(%rip),%rsi
call   1080 <err@plt>
jmp    12c4 <main+0x124>
mov    -0x101c(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    $0x1000,%edx
call   1070 <read@plt>
mov    %rax,-0x1028(%rbp)
cmp    $0x0,%rax
jle    133c <main+0x19c>
mov    -0x1028(%rbp),%rax
mov    %rax,-0x1030(%rbp)
mov    -0x1020(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    -0x1028(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,%rcx
mov    -0x1030(%rbp),%rax
cmp    %rcx,%rax
je     1337 <main+0x197>
mov    $0x1,%edi
lea    0xd7b(%rip),%rsi
call   1080 <err@plt>
jmp    12c4 <main+0x124>
mov    -0x101c(%rbp),%edi
call   1060 <close@plt>
mov    -0x1020(%rbp),%edi
call   1060 <close@plt>
lea    0xd84(%rip),%rsi
mov    %rsi,%rdi
call   10a0 <execlp@plt>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xd6f(%rip),%rdi
call   1050 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1000,%rsp
mov    %rsi,%rbp
cmp    $0x3,%edi
je     11c8 <main+0x28>
lea    0xe42(%rip),%rsi
mov    $0x1,%edi
call   1080 <err@plt>
xor    %eax,%eax
call   10c0 <fork@plt>
cmp    $0xffffffff,%eax
je     11f0 <main+0x50>
mov    %eax,%ebx
test   %eax,%eax
jle    1201 <main+0x61>
mov    %rsp,%rdi
call   10b0 <wait@plt>
lea    0xe50(%rip),%rdi
mov    %ebx,%esi
jmp    1309 <main+0x169>
lea    0xe2d(%rip),%rsi
mov    $0x1,%edi
call   1080 <err@plt>
mov    0x8(%rbp),%rdi
mov    0x2dd4(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,%r14d
cmp    $0xffffffff,%eax
jne    122c <main+0x8c>
lea    0xe2a(%rip),%rsi
mov    $0x1,%edi
call   1080 <err@plt>
mov    0x2d8d(%rip),%rax
mov    0x2dbe(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x10(%rbp),%rdi
mov    0x2d6f(%rip),%rax
or     (%rax),%esi
call   1090 <open@plt>
mov    %eax,%r12d
mov    0x2d7e(%rip),%rax
cmpl   $0xffffffff,(%rax)
jne    1278 <main+0xd8>
mov    %r14d,%edi
call   1060 <close@plt>
lea    0xe0e(%rip),%rsi
mov    $0x1,%edi
call   1080 <err@plt>
mov    %rsp,%rsi
mov    %r14d,%edi
mov    $0x1000,%edx
call   1070 <read@plt>
test   %rax,%rax
jle    12da <main+0x13a>
mov    %rax,%rbp
mov    %rsp,%rbx
lea    0xe13(%rip),%r15
jmp    12b8 <main+0x118>
nopl   0x0(%rax)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1070 <read@plt>
mov    %rax,%rbp
test   %rax,%rax
jle    12da <main+0x13a>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1030 <write@plt>
cmp    %rax,%rbp
je     12a0 <main+0x100>
mov    $0x1,%edi
mov    %r15,%rsi
call   1080 <err@plt>
jmp    12a0 <main+0x100>
mov    %r14d,%edi
call   1060 <close@plt>
mov    %r12d,%edi
call   1060 <close@plt>
lea    0xdec(%rip),%rdi
mov    %rdi,%rsi
call   10a0 <execlp@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xdd9(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x1000,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [36 x i8] c"Exactly 2 arguments must be passed!\00"
@.str.1 = [21 x i8] c"Error while forking!\00"
@.str.2 = [19 x i8] c"Good work, my %d!\0A\00"
@O_RDONLY = global i32 0
@.str.3 = [48 x i8] c"Error while opening the first file for reading!\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@S_IWUSR = global i32 0
@.str.4 = [49 x i8] c"Error while opening the second file for writing!\00"
@.str.5 = [48 x i8] c"Error while writing content to the second file!\00"
@.str.6 = [3 x i8] c"ls\00"
@.str.7 = [22 x i8] c"Father %d, i'm done!\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [4096 x i8]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 3
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 @err(i32 1, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %14, %2
%17 = call i32 (...) @fork()
store i32 %17, i32* %7
%18 = load i32, i32* %7
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = call i32 @err(i32 1, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %22
22: ; preds = %20, %16
%23 = load i32, i32* %7
%24 = icmp sgt i32 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %22
%26 = call i32 @wait(i32* %8)
%27 = load i32, i32* %7
%28 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %27)
br label %78
29: ; preds = %22
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 1
%32 = load i8*, i8** %31
%33 = load i32, i32* @O_RDONLY
%34 = call i32 @open(i8* %32, i32 %33)
store i32 %34, i32* %9
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %38
36: ; preds = %29
%37 = call i32 @err(i32 1, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.3, i64 0, i64 0))
br label %38
38: ; preds = %36, %29
%39 = load i8**, i8*** %5
%40 = getelementptr inbounds i8*, i8** %39, i64 2
%41 = load i8*, i8** %40
%42 = load i32, i32* @O_WRONLY
%43 = load i32, i32* @O_CREAT
%44 = or i32 %42, %43
%45 = load i32, i32* @O_APPEND
%46 = or i32 %44, %45
%47 = call i32 @open(i8* %41, i32 %46)
store i32 %47, i32* %10
%48 = load i32, i32* @S_IWUSR
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %54
50: ; preds = %38
%51 = load i32, i32* %9
%52 = call i32 @close(i32 %51)
%53 = call i32 @err(i32 1, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.4, i64 0, i64 0))
br label %54
54: ; preds = %50, %38
br label %55
55: ; preds = %69, %54
%56 = load i32, i32* %9
%57 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%58 = call i64 @read(i32 %56, i8* %57, i32 4096)
store i64 %58, i64* %11
%59 = icmp sgt i64 %58, 0
br i1 %59, label %60, label %70
60: ; preds = %55
%61 = load i64, i64* %11
%62 = load i32, i32* %10
%63 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%64 = load i64, i64* %11
%65 = call i64 @write(i32 %62, i8* %63, i64 %64)
%66 = icmp ne i64 %61, %65
br i1 %66, label %67, label %69
67: ; preds = %60
%68 = call i32 @err(i32 1, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.5, i64 0, i64 0))
br label %69
69: ; preds = %67, %60
br label %55
70: ; preds = %55
%71 = load i32, i32* %9
%72 = call i32 @close(i32 %71)
%73 = load i32, i32* %10
%74 = call i32 @close(i32 %73)
%75 = call i32 @execlp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0))
%76 = call i32 (...) @getpid()
%77 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.7, i64 0, i64 0), i32 %76)
br label %78
78: ; preds = %70, %25
%79 = load i32, i32* %3
ret i32 %79
}

LLVM IR (-O2):
@.str = [36 x i8] c"Exactly 2 arguments must be passed!\00"
@.str.1 = [21 x i8] c"Error while forking!\00"
@.str.2 = [19 x i8] c"Good work, my %d!\0A\00"
@O_RDONLY = global i32 0
@.str.3 = [48 x i8] c"Error while opening the first file for reading!\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@S_IWUSR = global i32 0
@.str.4 = [49 x i8] c"Error while opening the second file for writing!\00"
@.str.5 = [48 x i8] c"Error while writing content to the second file!\00"
@.str.6 = [3 x i8] c"ls\00"
@.str.7 = [22 x i8] c"Father %d, i'm done!\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [4096 x i8]
%4 = alloca i32
%5 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%6 = bitcast i32* %4 to i8*
%7 = icmp eq i32 %0, 3
br i1 %7, label %10, label %8
8: ; preds = %2
%9 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %8, %2
%11 = tail call i32 (...) @fork()
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %20
15: ; preds = %10
%16 = icmp sgt i32 %11, 0
br i1 %16, label %17, label %20
17: ; preds = %15
%18 = call i32 @wait(i32* %4)
%19 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %11)
br label %60
20: ; preds = %13, %15
%21 = getelementptr inbounds i8*, i8** %1, i64 1
%22 = load i8*, i8** %21
%23 = load i32, i32* @O_RDONLY
%24 = tail call i32 @open(i8* %22, i32 %23)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %28
26: ; preds = %20
%27 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.3, i64 0, i64 0))
br label %28
28: ; preds = %26, %20
%29 = getelementptr inbounds i8*, i8** %1, i64 2
%30 = load i8*, i8** %29
%31 = load i32, i32* @O_WRONLY
%32 = load i32, i32* @O_CREAT
%33 = or i32 %32, %31
%34 = load i32, i32* @O_APPEND
%35 = or i32 %33, %34
%36 = tail call i32 @open(i8* %30, i32 %35)
%37 = load i32, i32* @S_IWUSR
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %42
39: ; preds = %28
%40 = tail call i32 @close(i32 %24)
%41 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.4, i64 0, i64 0))
br label %42
42: ; preds = %39, %28
%43 = call i64 @read(i32 %24, i8* %5, i32 4096)
%44 = icmp sgt i64 %43, 0
br i1 %44, label %45, label %54
45: ; preds = %42, %51
%46 = phi i64 [ %52, %51 ], [ %43, %42 ]
%47 = call i64 @write(i32 %36, i8* %5, i64 %46)
%48 = icmp eq i64 %46, %47
br i1 %48, label %51, label %49
49: ; preds = %45
%50 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.5, i64 0, i64 0))
br label %51
51: ; preds = %49, %45
%52 = call i64 @read(i32 %24, i8* %5, i32 4096)
%53 = icmp sgt i64 %52, 0
br i1 %53, label %45, label %54
54: ; preds = %51, %42
%55 = tail call i32 @close(i32 %24)
%56 = tail call i32 @close(i32 %36)
%57 = tail call i32 @execlp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0))
%58 = tail call i32 (...) @getpid()
%59 = tail call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.7, i64 0, i64 0), i32 %58)
br label %60
60: ; preds = %54, %17
ret i32 0
}