#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 */
scalar_t__ execlp(char *, char *, char *, char *, int /*<<< orphan*/ *);
int fork();
int /*<<< orphan*/ perror(char *);
int wait(int *);
void copy_file(char fname[], char dname[]) {
  int pid1, pid2, status;
  pid1 = fork();
  if (pid1 < 0)
    perror("cp_fork");
  else if (pid1 == 0)
    if (execlp("cp", "cp", fname, dname, NULL) < 0)
      perror("cp_exec");
    else {
      pid2 = wait(&status);
      if (pid2 < 0)
        perror("cp_wait");
    }
}


x86 assembly (clang, -O0):
<copy_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1060 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1175 <copy_file+0x35>
lea    0xe95(%rip),%rdi
call   1030 <perror@plt>
jmp    11e7 <copy_file+0xa7>
cmpl   $0x0,-0x14(%rbp)
jne    11e2 <copy_file+0xa2>
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xe7a(%rip),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %rsi,%rdi
call   1040 <execlp@plt>
cmp    $0x0,%rax
jge    11b6 <copy_file+0x76>
lea    0xe5f(%rip),%rdi
call   1030 <perror@plt>
jmp    11dd <copy_file+0x9d>
lea    -0x1c(%rbp),%rdi
call   1050 <wait@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11d8 <copy_file+0x98>
lea    0xe40(%rip),%rdi
call   1030 <perror@plt>
jmp    11dd <copy_file+0x9d>
jmp    11e2 <copy_file+0xa2>
jmp    11e7 <copy_file+0xa7>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_file>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
xor    %eax,%eax
call   1060 <fork@plt>
test   %eax,%eax
js     118a <copy_file+0x4a>
jne    1182 <copy_file+0x42>
lea    0xeaa(%rip),%rdi
mov    %rdi,%rsi
mov    %rbx,%rdx
mov    %r14,%rcx
xor    %r8d,%r8d
call   1040 <execlp@plt>
test   %rax,%rax
js     1193 <copy_file+0x53>
lea    0x4(%rsp),%rdi
call   1050 <wait@plt>
test   %eax,%eax
js     11a6 <copy_file+0x66>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe6f(%rip),%rdi
jmp    119a <copy_file+0x5a>
lea    0xe71(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1030 <perror@plt>
lea    0xe66(%rip),%rdi
call   1030 <perror@plt>
jmp    1182 <copy_file+0x42>

LLVM IR (-O0):
@.str = [8 x i8] c"cp_fork\00"
@.str.1 = [3 x i8] c"cp\00"
@.str.2 = [8 x i8] c"cp_exec\00"
@.str.3 = [8 x i8] c"cp_wait\00"
define void @copy_file(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%8 = call i32 (...) @fork()
store i32 %8, i32* %5
%9 = load i32, i32* %5
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %32
13: ; preds = %2
%14 = load i32, i32* %5
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %31
16: ; preds = %13
%17 = load i8*, i8** %3
%18 = load i8*, i8** %4
%19 = call i64 @execlp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %17, i8* %18, i32* null)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %16
%22 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %30
23: ; preds = %16
%24 = call i32 @wait(i32* %7)
store i32 %24, i32* %6
%25 = load i32, i32* %6
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %23
%28 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
br label %29
29: ; preds = %27, %23
br label %30
30: ; preds = %29, %21
br label %31
31: ; preds = %30, %13
br label %32
32: ; preds = %31, %11
ret void
}

LLVM IR (-O2):
@.str = [8 x i8] c"cp_fork\00"
@.str.1 = [3 x i8] c"cp\00"
@.str.2 = [8 x i8] c"cp_exec\00"
@.str.3 = [8 x i8] c"cp_wait\00"
define void @copy_file(i8* %0, i8* %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = tail call i32 (...) @fork()
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %21
9: ; preds = %2
%10 = icmp eq i32 %5, 0
br i1 %10, label %11, label %21
11: ; preds = %9
%12 = tail call i64 @execlp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %1, i32* null)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %21
16: ; preds = %11
%17 = call i32 @wait(i32* %3)
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %16
%20 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
br label %21
21: ; preds = %9, %16, %19, %14, %7
ret void
}