#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 */

/* Variables and functions */
int O_CREAT;
int O_RDONLY;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ asprintf(char **, char *, char *, char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fork();
int /*<<< orphan*/ free(char *);
int open(char *, int);
int read(int, char *, int);
int /*<<< orphan*/ wait(int *);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  char c;
  int read_count;
  for (int i = 1; i < argc - 1; i++) {
    if (!fork()) {
      int fd1 = open(argv[i], O_RDONLY);
      char *dest_dir;
      asprintf(&dest_dir, "%s%s", argv[argc - 1], argv[i]);
      int fd2 = open(dest_dir, O_WRONLY | O_TRUNC | O_CREAT);
      while ((read_count = read(fd1, &c, 1))) {
        write(fd2, &c, 1);
      }
      free(dest_dir);
      close(fd1);
      close(fd2);
    }
  }
  for (int i = 1; i < argc - 1; i++) {
    int status;
    wait(&status);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    -0x8(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    128e <main+0x10e>
mov    $0x0,%al
call   10a0 <fork@plt>
cmp    $0x0,%eax
jne    127b <main+0xfb>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    0x2e07(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rdx
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rcx
lea    -0x28(%rbp),%rdi
lea    0xdfd(%rip),%rsi
call   1070 <asprintf@plt>
mov    -0x28(%rbp),%rdi
mov    0x2dad(%rip),%rax
mov    (%rax),%esi
mov    0x2dd4(%rip),%rax
or     (%rax),%esi
mov    0x2dd3(%rip),%rax
or     (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x20(%rbp),%edi
lea    -0x11(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
je     1262 <main+0xe2>
mov    -0x2c(%rbp),%edi
lea    -0x11(%rbp),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
jmp    122f <main+0xaf>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
mov    -0x2c(%rbp),%edi
call   1050 <close@plt>
jmp    1280 <main+0x100>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    119d <main+0x1d>
movl   $0x1,-0x30(%rbp)
mov    -0x30(%rbp),%eax
mov    -0x8(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    12bd <main+0x13d>
lea    -0x34(%rbp),%rdi
call   1090 <wait@plt>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    1295 <main+0x115>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,0x18(%rsp)
cmp    $0x3,%edi
jl     129e <main+0x11e>
mov    %edi,%r13d
lea    -0x1(%r13),%eax
mov    %eax,%r12d
mov    $0x1,%r14d
lea    0xf(%rsp),%rbx
jmp    11e6 <main+0x66>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    0x10(%rsp),%rdi
call   1030 <free@plt>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %r15d,%edi
call   1050 <close@plt>
add    $0x1,%r14
cmp    %r12,%r14
je     1279 <main+0xf9>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
jne    11d9 <main+0x59>
mov    0x18(%rsp),%r15
mov    (%r15,%r14,8),%rdi
mov    0x2dd7(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,%ebp
mov    (%r15,%r12,8),%rdx
mov    (%r15,%r14,8),%rcx
lea    0x10(%rsp),%rdi
lea    0xde2(%rip),%rsi
call   1070 <asprintf@plt>
mov    0x10(%rsp),%rdi
mov    0x2dc1(%rip),%rax
mov    (%rax),%esi
mov    0x2d88(%rip),%rax
or     (%rax),%esi
mov    0x2db7(%rip),%rax
or     (%rax),%esi
call   1080 <open@plt>
mov    %eax,%r15d
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1060 <read@plt>
test   %eax,%eax
je     11c0 <main+0x40>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
jmp    1250 <main+0xd0>
cmp    $0x3,%r13d
jl     129e <main+0x11e>
add    $0xfffffffe,%r13d
lea    0x24(%rsp),%rbx
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1090 <wait@plt>
add    $0xffffffff,%r13d
jne    1290 <main+0x110>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [5 x i8] c"%s%s\00"
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@O_CREAT = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 1, i32* %8
br label %14
14: ; preds = %64, %2
%15 = load i32, i32* %8
%16 = load i32, i32* %4
%17 = sub nsw i32 %16, 1
%18 = icmp slt i32 %15, %17
br i1 %18, label %19, label %67
19: ; preds = %14
%20 = call i32 (...) @fork()
%21 = icmp ne i32 %20, 0
br i1 %21, label %63, label %22
22: ; preds = %19
%23 = load i8**, i8*** %5
%24 = load i32, i32* %8
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8*, i8** %23, i64 %25
%27 = load i8*, i8** %26
%28 = load i32, i32* @O_RDONLY
%29 = call i32 @open(i8* %27, i32 %28)
store i32 %29, i32* %9
%30 = load i8**, i8*** %5
%31 = load i32, i32* %4
%32 = sub nsw i32 %31, 1
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8*, i8** %30, i64 %33
%35 = load i8*, i8** %34
%36 = load i8**, i8*** %5
%37 = load i32, i32* %8
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8*, i8** %36, i64 %38
%40 = load i8*, i8** %39
%41 = call i32 @asprintf(i8** %10, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %35, i8* %40)
%42 = load i8*, i8** %10
%43 = load i32, i32* @O_WRONLY
%44 = load i32, i32* @O_TRUNC
%45 = or i32 %43, %44
%46 = load i32, i32* @O_CREAT
%47 = or i32 %45, %46
%48 = call i32 @open(i8* %42, i32 %47)
store i32 %48, i32* %11
br label %49
49: ; preds = %53, %22
%50 = load i32, i32* %9
%51 = call i32 @read(i32 %50, i8* %6, i32 1)
store i32 %51, i32* %7
%52 = icmp ne i32 %51, 0
br i1 %52, label %53, label %56
53: ; preds = %49
%54 = load i32, i32* %11
%55 = call i32 @write(i32 %54, i8* %6, i32 1)
br label %49
56: ; preds = %49
%57 = load i8*, i8** %10
%58 = call i32 @free(i8* %57)
%59 = load i32, i32* %9
%60 = call i32 @close(i32 %59)
%61 = load i32, i32* %11
%62 = call i32 @close(i32 %61)
br label %63
63: ; preds = %56, %19
br label %64
64: ; preds = %63
%65 = load i32, i32* %8
%66 = add nsw i32 %65, 1
store i32 %66, i32* %8
br label %14
67: ; preds = %14
store i32 1, i32* %12
br label %68
68: ; preds = %75, %67
%69 = load i32, i32* %12
%70 = load i32, i32* %4
%71 = sub nsw i32 %70, 1
%72 = icmp slt i32 %69, %71
br i1 %72, label %73, label %78
73: ; preds = %68
%74 = call i32 @wait(i32* %13)
br label %75
75: ; preds = %73
%76 = load i32, i32* %12
%77 = add nsw i32 %76, 1
store i32 %77, i32* %12
br label %68
78: ; preds = %68
ret i32 0
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [5 x i8] c"%s%s\00"
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@O_CREAT = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8
%4 = alloca i8*
%5 = alloca i32
%6 = add i32 %0, -1
%7 = icmp sgt i32 %0, 2
br i1 %7, label %8, label %49
8: ; preds = %2
%9 = bitcast i8** %4 to i8*
%10 = zext i32 %6 to i64
%11 = getelementptr inbounds i8*, i8** %1, i64 %10
%12 = zext i32 %6 to i64
br label %16
13: ; preds = %46
br i1 %7, label %14, label %49
14: ; preds = %13
%15 = bitcast i32* %5 to i8*
br label %50
16: ; preds = %8, %46
%17 = phi i64 [ 1, %8 ], [ %47, %46 ]
%18 = call i32 (...) @fork()
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %46
20: ; preds = %16
%21 = getelementptr inbounds i8*, i8** %1, i64 %17
%22 = load i8*, i8** %21
%23 = load i32, i32* @O_RDONLY
%24 = call i32 @open(i8* %22, i32 %23)
%25 = load i8*, i8** %11
%26 = load i8*, i8** %21
%27 = call i32 @asprintf(i8** %4, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %25, i8* %26)
%28 = load i8*, i8** %4
%29 = load i32, i32* @O_WRONLY
%30 = load i32, i32* @O_TRUNC
%31 = or i32 %30, %29
%32 = load i32, i32* @O_CREAT
%33 = or i32 %31, %32
%34 = call i32 @open(i8* %28, i32 %33)
%35 = call i32 @read(i32 %24, i8* %3, i32 1)
%36 = icmp eq i32 %35, 0
br i1 %36, label %41, label %37
37: ; preds = %20, %37
%38 = call i32 @write(i32 %34, i8* %3, i32 1)
%39 = call i32 @read(i32 %24, i8* %3, i32 1)
%40 = icmp eq i32 %39, 0
br i1 %40, label %41, label %37
41: ; preds = %37, %20
%42 = load i8*, i8** %4
%43 = call i32 @free(i8* %42)
%44 = call i32 @close(i32 %24)
%45 = call i32 @close(i32 %34)
br label %46
46: ; preds = %16, %41
%47 = add nuw nsw i64 %17, 1
%48 = icmp eq i64 %47, %12
br i1 %48, label %13, label %16
49: ; preds = %50, %2, %13
ret i32 0
50: ; preds = %14, %50
%51 = phi i32 [ 1, %14 ], [ %53, %50 ]
%52 = call i32 @wait(i32* %5)
%53 = add nuw nsw i32 %51, 1
%54 = icmp eq i32 %53, %6
br i1 %54, label %49, label %50
}