#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_EXCL;
int /*<<< orphan*/ O_RDONLY;
int O_WRONLY;
int S_IROTH;
int S_IRWXG;
int S_IRWXU;
int S_IXOTH;
char *basename(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ copyBytes(int, int);
char *dirname(char *);
int /*<<< orphan*/ free(char *);
char *malloc(scalar_t__);
int /*<<< orphan*/ mkdirp(char *, int);
int origopen(char const *, int, ...);
char *realpath(char const *, int /*<<< orphan*/ *);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char const *);
char *strdup(char *);
scalar_t__ strlen(char const *);
int copyfile(const char *origpathname) {
  int ofd, dfd;
  const char *bckpath = "/tmp/back";
  char *realp1 = realpath(origpathname, NULL);
  char *realp2 = strdup(realp1);
  char *dirnm = dirname(realp1);
  char *filenm = basename(realp2);
  char *destDirPath = malloc(strlen(dirnm) + strlen(bckpath) + 2);
  strcpy(destDirPath, bckpath);
  strcat(destDirPath, dirnm);
  strcat(destDirPath, "/");
  mkdirp(destDirPath, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
  char *destFilePath = malloc(strlen(destDirPath) + strlen(filenm) + 1);
  strcpy(destFilePath, destDirPath);
  strcat(destFilePath, filenm);
  ofd = origopen(origpathname, O_RDONLY);
  dfd = origopen(destFilePath, O_CREAT | O_EXCL | O_WRONLY,
                 S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
  if (dfd <= 0) {
    close(ofd);
    goto release_buffers;
  }
  copyBytes(ofd, dfd);
  close(ofd);
  close(dfd);
release_buffers:
  free(realp1);
  free(realp2);
  free(destDirPath);
  free(destFilePath);
  return 0;
}


x86 assembly (clang, -O0):
<copyfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
lea    0xe1d(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   10b0 <realpath@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   10f0 <strdup@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1060 <dirname@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
call   10a0 <basename@plt>
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x58(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x58(%rbp),%rdi
add    %rax,%rdi
add    $0x2,%rdi
call   10c0 <malloc@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x40(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   10e0 <strcat@plt>
mov    -0x40(%rbp),%rdi
lea    0xd9c(%rip),%rsi
call   10e0 <strcat@plt>
mov    -0x40(%rbp),%rdi
mov    0x2d62(%rip),%rax
mov    (%rax),%esi
mov    0x2d49(%rip),%rax
or     (%rax),%esi
mov    0x2d20(%rip),%rax
or     (%rax),%esi
mov    0x2d3f(%rip),%rax
or     (%rax),%esi
call   1070 <mkdirp@plt>
mov    -0x40(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x50(%rbp)
mov    -0x38(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x50(%rbp),%rdi
add    %rax,%rdi
add    $0x1,%rdi
call   10c0 <malloc@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rdi
mov    -0x40(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x48(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   10e0 <strcat@plt>
mov    -0x8(%rbp),%rdi
mov    0x2cd9(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   10d0 <origopen@plt>
mov    %eax,-0xc(%rbp)
mov    -0x48(%rbp),%rdi
mov    0x2cf2(%rip),%rax
mov    (%rax),%esi
mov    0x2ca9(%rip),%rax
or     (%rax),%esi
mov    0x2c88(%rip),%rax
or     (%rax),%esi
mov    0x2cbf(%rip),%rax
mov    (%rax),%edx
mov    0x2ca6(%rip),%rax
or     (%rax),%edx
mov    0x2c7d(%rip),%rax
or     (%rax),%edx
mov    0x2c9c(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   10d0 <origopen@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jg     135f <copyfile+0x18f>
mov    -0xc(%rbp),%edi
call   1080 <close@plt>
jmp    137a <copyfile+0x1aa>
mov    -0xc(%rbp),%edi
mov    -0x10(%rbp),%esi
call   1090 <copyBytes@plt>
mov    -0xc(%rbp),%edi
call   1080 <close@plt>
mov    -0x10(%rbp),%edi
call   1080 <close@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copyfile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r12
xor    %esi,%esi
call   10b0 <realpath@plt>
mov    %rax,%r15
mov    %rax,%rdi
call   10f0 <strdup@plt>
mov    %rax,%rbp
mov    %r15,%rdi
call   1060 <dirname@plt>
mov    %rax,%rbx
mov    %rbp,(%rsp)
mov    %rbp,%rdi
call   10a0 <basename@plt>
mov    %rax,%rbp
mov    %rbx,%rdi
call   1050 <strlen@plt>
lea    0xb(%rax),%rdi
call   10c0 <malloc@plt>
mov    %rax,%r14
lea    0xdd8(%rip),%rsi
mov    %rax,%rdi
call   1040 <strcpy@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   10e0 <strcat@plt>
lea    0xdc8(%rip),%rsi
mov    %r14,%rdi
call   10e0 <strcat@plt>
mov    0x2d8f(%rip),%rax
mov    0x2d78(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2d4d(%rip),%rax
or     (%rax),%esi
mov    0x2d6c(%rip),%rax
or     (%rax),%esi
mov    %r14,%rdi
call   1070 <mkdirp@plt>
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %rax,%rbx
mov    %rbp,%rdi
call   1050 <strlen@plt>
lea    (%rbx,%rax,1),%rdi
add    $0x1,%rdi
call   10c0 <malloc@plt>
mov    %rax,%r13
mov    %rax,%rdi
mov    %r14,%rsi
call   1040 <strcpy@plt>
mov    %r13,%rdi
mov    %rbp,%rsi
call   10e0 <strcat@plt>
mov    0x2d12(%rip),%rax
mov    (%rax),%esi
mov    %r12,%rdi
xor    %eax,%eax
call   10d0 <origopen@plt>
mov    0x2d2f(%rip),%rcx
mov    0x2ce8(%rip),%rdx
mov    (%rdx),%esi
or     (%rcx),%esi
mov    0x2cc5(%rip),%rcx
or     (%rcx),%esi
mov    %eax,%ebx
mov    0x2cea(%rip),%rax
mov    (%rax),%edx
mov    0x2cf1(%rip),%rax
or     (%rax),%edx
mov    0x2cb8(%rip),%rax
or     (%rax),%edx
mov    0x2cd7(%rip),%rax
or     (%rax),%edx
mov    %r13,%rdi
xor    %eax,%eax
call   10d0 <origopen@plt>
test   %eax,%eax
jle    1325 <copyfile+0x155>
mov    %eax,%ebp
mov    %ebx,%edi
mov    %eax,%esi
call   1090 <copyBytes@plt>
mov    %ebx,%edi
call   1080 <close@plt>
mov    %ebp,%ebx
mov    %ebx,%edi
call   1080 <close@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    (%rsp),%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r13,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"/tmp/back\00"
@.str.1 = [2 x i8] c"/\00"
@S_IRWXU = global i32 0
@S_IRWXG = global i32 0
@S_IROTH = global i32 0
@S_IXOTH = global i32 0
@O_RDONLY = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@O_WRONLY = global i32 0
define i32 @copyfile(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
store i8* %0, i8** %2
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8** %5
%12 = load i8*, i8** %2
%13 = call i8* @realpath(i8* %12, i32* null)
store i8* %13, i8** %6
%14 = load i8*, i8** %6
%15 = call i8* @strdup(i8* %14)
store i8* %15, i8** %7
%16 = load i8*, i8** %6
%17 = call i8* @dirname(i8* %16)
store i8* %17, i8** %8
%18 = load i8*, i8** %7
%19 = call i8* @basename(i8* %18)
store i8* %19, i8** %9
%20 = load i8*, i8** %8
%21 = call i64 @strlen(i8* %20)
%22 = load i8*, i8** %5
%23 = call i64 @strlen(i8* %22)
%24 = add nsw i64 %21, %23
%25 = add nsw i64 %24, 2
%26 = call i8* @malloc(i64 %25)
store i8* %26, i8** %10
%27 = load i8*, i8** %10
%28 = load i8*, i8** %5
%29 = call i32 @strcpy(i8* %27, i8* %28)
%30 = load i8*, i8** %10
%31 = load i8*, i8** %8
%32 = call i32 @strcat(i8* %30, i8* %31)
%33 = load i8*, i8** %10
%34 = call i32 @strcat(i8* %33, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%35 = load i8*, i8** %10
%36 = load i32, i32* @S_IRWXU
%37 = load i32, i32* @S_IRWXG
%38 = or i32 %36, %37
%39 = load i32, i32* @S_IROTH
%40 = or i32 %38, %39
%41 = load i32, i32* @S_IXOTH
%42 = or i32 %40, %41
%43 = call i32 @mkdirp(i8* %35, i32 %42)
%44 = load i8*, i8** %10
%45 = call i64 @strlen(i8* %44)
%46 = load i8*, i8** %9
%47 = call i64 @strlen(i8* %46)
%48 = add nsw i64 %45, %47
%49 = add nsw i64 %48, 1
%50 = call i8* @malloc(i64 %49)
store i8* %50, i8** %11
%51 = load i8*, i8** %11
%52 = load i8*, i8** %10
%53 = call i32 @strcpy(i8* %51, i8* %52)
%54 = load i8*, i8** %11
%55 = load i8*, i8** %9
%56 = call i32 @strcat(i8* %54, i8* %55)
%57 = load i8*, i8** %2
%58 = load i32, i32* @O_RDONLY
%59 = call i32 (i8*, i32, ...) @origopen(i8* %57, i32 %58)
store i32 %59, i32* %3
%60 = load i8*, i8** %11
%61 = load i32, i32* @O_CREAT
%62 = load i32, i32* @O_EXCL
%63 = or i32 %61, %62
%64 = load i32, i32* @O_WRONLY
%65 = or i32 %63, %64
%66 = load i32, i32* @S_IRWXU
%67 = load i32, i32* @S_IRWXG
%68 = or i32 %66, %67
%69 = load i32, i32* @S_IROTH
%70 = or i32 %68, %69
%71 = load i32, i32* @S_IXOTH
%72 = or i32 %70, %71
%73 = call i32 (i8*, i32, ...) @origopen(i8* %60, i32 %65, i32 %72)
store i32 %73, i32* %4
%74 = load i32, i32* %4
%75 = icmp sle i32 %74, 0
br i1 %75, label %76, label %79
76: ; preds = %1
%77 = load i32, i32* %3
%78 = call i32 @close(i32 %77)
br label %87
79: ; preds = %1
%80 = load i32, i32* %3
%81 = load i32, i32* %4
%82 = call i32 @copyBytes(i32 %80, i32 %81)
%83 = load i32, i32* %3
%84 = call i32 @close(i32 %83)
%85 = load i32, i32* %4
%86 = call i32 @close(i32 %85)
br label %87
87: ; preds = %79, %76
%88 = load i8*, i8** %6
%89 = call i32 @free(i8* %88)
%90 = load i8*, i8** %7
%91 = call i32 @free(i8* %90)
%92 = load i8*, i8** %10
%93 = call i32 @free(i8* %92)
%94 = load i8*, i8** %11
%95 = call i32 @free(i8* %94)
ret i32 0
}

LLVM IR (-O2):
@.str = [10 x i8] c"/tmp/back\00"
@.str.1 = [2 x i8] c"/\00"
@S_IRWXU = global i32 0
@S_IRWXG = global i32 0
@S_IROTH = global i32 0
@S_IXOTH = global i32 0
@O_RDONLY = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@O_WRONLY = global i32 0
define i32 @copyfile(i8* %0) {
%2 = tail call i8* @realpath(i8* %0, i32* null)
%3 = tail call i8* @strdup(i8* %2)
%4 = tail call i8* @dirname(i8* %2)
%5 = tail call i8* @basename(i8* %3)
%6 = tail call i64 @strlen(i8* dereferenceable(1) %4)
%7 = add nsw i64 %6, 11
%8 = tail call i8* @malloc(i64 %7)
%9 = tail call i32 @strcpy(i8* %8, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%10 = tail call i32 @strcat(i8* %8, i8* %4)
%11 = tail call i32 @strcat(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%12 = load i32, i32* @S_IRWXU
%13 = load i32, i32* @S_IRWXG
%14 = or i32 %13, %12
%15 = load i32, i32* @S_IROTH
%16 = or i32 %14, %15
%17 = load i32, i32* @S_IXOTH
%18 = or i32 %16, %17
%19 = tail call i32 @mkdirp(i8* %8, i32 %18)
%20 = tail call i64 @strlen(i8* dereferenceable(1) %8)
%21 = tail call i64 @strlen(i8* dereferenceable(1) %5)
%22 = add i64 %20, 1
%23 = add i64 %22, %21
%24 = tail call i8* @malloc(i64 %23)
%25 = tail call i32 @strcpy(i8* %24, i8* %8)
%26 = tail call i32 @strcat(i8* %24, i8* %5)
%27 = load i32, i32* @O_RDONLY
%28 = tail call i32 (i8*, i32, ...) @origopen(i8* %0, i32 %27)
%29 = load i32, i32* @O_CREAT
%30 = load i32, i32* @O_EXCL
%31 = or i32 %30, %29
%32 = load i32, i32* @O_WRONLY
%33 = or i32 %31, %32
%34 = load i32, i32* @S_IRWXU
%35 = load i32, i32* @S_IRWXG
%36 = or i32 %35, %34
%37 = load i32, i32* @S_IROTH
%38 = or i32 %36, %37
%39 = load i32, i32* @S_IXOTH
%40 = or i32 %38, %39
%41 = tail call i32 (i8*, i32, ...) @origopen(i8* %24, i32 %33, i32 %40)
%42 = icmp slt i32 %41, 1
br i1 %42, label %46, label %43
43: ; preds = %1
%44 = tail call i32 @copyBytes(i32 %28, i32 %41)
%45 = tail call i32 @close(i32 %28)
br label %46
46: ; preds = %1, %43
%47 = phi i32 [ %41, %43 ], [ %28, %1 ]
%48 = tail call i32 @close(i32 %47)
%49 = tail call i32 @free(i8* %2)
%50 = tail call i32 @free(i8* %3)
%51 = tail call i32 @free(i8* %8)
%52 = tail call i32 @free(i8* %24)
ret i32 0
}