#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 /*<<< orphan*/ st_mode;
};
typedef int ssize_t;
typedef int /*<<< orphan*/ b;

/* Variables and functions */
int O_CREAT;
int O_RDONLY;
int O_WRONLY;
int /*<<< orphan*/ S_ISREG(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int open(char const *, int, ...);
int read(int, char *, int);
int stat(char const *, struct stat *);
int write(int, char *, int);
int vh_file_copy(const char *src, const char *dst) {
  struct stat st_src, st_dst;
  int fd_src = 0, fd_dst = 0;
  ssize_t r, w;
  char b[4096];
  int err, res = 1;
  if (!src || !dst)
    goto end;
  err = stat(src, &st_src);
  if (err || !S_ISREG(st_src.st_mode))
    goto end;
  err = stat(dst, &st_dst);
  if (!err)
    goto end;
  fd_src = open(src, O_RDONLY | (0));
  if (fd_src == -1)
    goto end;
  fd_dst = open(dst, O_CREAT | O_WRONLY | (0), 0644);
  if (fd_dst == -1)
    goto end;
  while ((r = read(fd_src, b, sizeof(b))) > 0) {
    w = write(fd_dst, b, r);
    if (w == -1)
      goto end;
  }
  if (!r)
    res = 0;
end:
  if (fd_src)
    close(fd_src);
  if (fd_dst)
    close(fd_dst);
  return res;
}


x86 assembly (clang, -O0):
<vh_file_copy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x1,-0x1038(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     11a1 <vh_file_copy+0x41>
cmpq   $0x0,-0x10(%rbp)
jne    11a6 <vh_file_copy+0x46>
jmp    12c8 <vh_file_copy+0x168>
mov    -0x8(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1060 <stat@plt>
mov    %eax,-0x1034(%rbp)
cmpl   $0x0,-0x1034(%rbp)
jne    11d7 <vh_file_copy+0x77>
mov    -0x18(%rbp),%edi
call   1070 <S_ISREG@plt>
cmp    $0x0,%eax
jne    11dc <vh_file_copy+0x7c>
jmp    12c8 <vh_file_copy+0x168>
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1060 <stat@plt>
mov    %eax,-0x1034(%rbp)
cmpl   $0x0,-0x1034(%rbp)
jne    1201 <vh_file_copy+0xa1>
jmp    12c8 <vh_file_copy+0x168>
mov    -0x8(%rbp),%rdi
mov    0x2dd4(%rip),%rax
mov    (%rax),%esi
or     $0x0,%esi
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    122a <vh_file_copy+0xca>
jmp    12c8 <vh_file_copy+0x168>
mov    -0x10(%rbp),%rdi
mov    0x2dc3(%rip),%rax
mov    (%rax),%esi
mov    0x2d8a(%rip),%rax
or     (%rax),%esi
or     $0x0,%esi
mov    $0x1a4,%edx
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    1261 <vh_file_copy+0x101>
jmp    12c8 <vh_file_copy+0x168>
jmp    1266 <vh_file_copy+0x106>
mov    -0x24(%rbp),%edi
lea    -0x1030(%rbp),%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jle    12af <vh_file_copy+0x14f>
mov    -0x28(%rbp),%edi
lea    -0x1030(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0xffffffff,-0x30(%rbp)
jne    12aa <vh_file_copy+0x14a>
jmp    12c8 <vh_file_copy+0x168>
jmp    1266 <vh_file_copy+0x106>
cmpl   $0x0,-0x2c(%rbp)
jne    12c3 <vh_file_copy+0x163>
movl   $0x0,-0x1038(%rbp)
jmp    12c8 <vh_file_copy+0x168>
cmpl   $0x0,-0x24(%rbp)
je     12da <vh_file_copy+0x17a>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
cmpl   $0x0,-0x28(%rbp)
je     12ec <vh_file_copy+0x18c>
mov    -0x28(%rbp),%edi
call   1040 <close@plt>
mov    -0x1038(%rbp),%eax
add    $0x1040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vh_file_copy>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1010,%rsp
mov    $0x1,%r12d
test   %rdi,%rdi
je     1191 <vh_file_copy+0x31>
mov    %rsi,%rbp
test   %rsi,%rsi
je     1191 <vh_file_copy+0x31>
mov    %rdi,%rbx
mov    %rsp,%rsi
call   1060 <stat@plt>
test   %eax,%eax
je     11a4 <vh_file_copy+0x44>
mov    %r12d,%eax
add    $0x1010,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%rsp),%edi
call   1070 <S_ISREG@plt>
test   %eax,%eax
je     1191 <vh_file_copy+0x31>
lea    0x8(%rsp),%rsi
mov    %rbp,%rdi
call   1060 <stat@plt>
test   %eax,%eax
je     1191 <vh_file_copy+0x31>
mov    0x2e18(%rip),%rax
mov    (%rax),%esi
xor    %r15d,%r15d
mov    %rbx,%rdi
xor    %eax,%eax
call   1080 <open@plt>
mov    %eax,%r14d
cmp    $0xffffffff,%eax
je     1248 <vh_file_copy+0xe8>
mov    0x2e12(%rip),%rax
mov    0x2ddb(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbp,%rdi
mov    $0x1a4,%edx
xor    %eax,%eax
call   1080 <open@plt>
mov    %eax,%r15d
mov    $0x1,%r12d
cmp    $0xffffffff,%eax
je     1257 <vh_file_copy+0xf7>
lea    0x10(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
test   %eax,%eax
jle    1250 <vh_file_copy+0xf0>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    1220 <vh_file_copy+0xc0>
jmp    1257 <vh_file_copy+0xf7>
mov    $0x1,%r12d
jmp    125c <vh_file_copy+0xfc>
setne  %al
movzbl %al,%r12d
test   %r14d,%r14d
je     1264 <vh_file_copy+0x104>
mov    %r14d,%edi
call   1040 <close@plt>
test   %r15d,%r15d
je     1191 <vh_file_copy+0x31>
mov    %r15d,%edi
call   1040 <close@plt>
jmp    1191 <vh_file_copy+0x31>

LLVM IR (-O0):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
define i32 @vh_file_copy(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca %struct.stat
%6 = alloca %struct.stat
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca [4096 x i8]
%12 = alloca i32
%13 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
store i32 0, i32* %7
store i32 0, i32* %8
store i32 1, i32* %13
%14 = load i8*, i8** %3
%15 = icmp ne i8* %14, null
br i1 %15, label %16, label %19
16: ; preds = %2
%17 = load i8*, i8** %4
%18 = icmp ne i8* %17, null
br i1 %18, label %20, label %19
19: ; preds = %16, %2
br label %75
20: ; preds = %16
%21 = load i8*, i8** %3
%22 = call i32 @stat(i8* %21, %struct.stat* %5)
store i32 %22, i32* %12
%23 = load i32, i32* %12
%24 = icmp ne i32 %23, 0
br i1 %24, label %30, label %25
25: ; preds = %20
%26 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%27 = load i32, i32* %26
%28 = call i32 @S_ISREG(i32 %27)
%29 = icmp ne i32 %28, 0
br i1 %29, label %31, label %30
30: ; preds = %25, %20
br label %75
31: ; preds = %25
%32 = load i8*, i8** %4
%33 = call i32 @stat(i8* %32, %struct.stat* %6)
store i32 %33, i32* %12
%34 = load i32, i32* %12
%35 = icmp ne i32 %34, 0
br i1 %35, label %37, label %36
36: ; preds = %31
br label %75
37: ; preds = %31
%38 = load i8*, i8** %3
%39 = load i32, i32* @O_RDONLY
%40 = or i32 %39, 0
%41 = call i32 (i8*, i32, ...) @open(i8* %38, i32 %40)
store i32 %41, i32* %7
%42 = load i32, i32* %7
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %45
44: ; preds = %37
br label %75
45: ; preds = %37
%46 = load i8*, i8** %4
%47 = load i32, i32* @O_CREAT
%48 = load i32, i32* @O_WRONLY
%49 = or i32 %47, %48
%50 = or i32 %49, 0
%51 = call i32 (i8*, i32, ...) @open(i8* %46, i32 %50, i32 420)
store i32 %51, i32* %8
%52 = load i32, i32* %8
%53 = icmp eq i32 %52, -1
br i1 %53, label %54, label %55
54: ; preds = %45
br label %75
55: ; preds = %45
br label %56
56: ; preds = %69, %55
%57 = load i32, i32* %7
%58 = getelementptr inbounds [4096 x i8], [4096 x i8]* %11, i64 0, i64 0
%59 = call i32 @read(i32 %57, i8* %58, i32 4096)
store i32 %59, i32* %9
%60 = icmp sgt i32 %59, 0
br i1 %60, label %61, label %70
61: ; preds = %56
%62 = load i32, i32* %8
%63 = getelementptr inbounds [4096 x i8], [4096 x i8]* %11, i64 0, i64 0
%64 = load i32, i32* %9
%65 = call i32 @write(i32 %62, i8* %63, i32 %64)
store i32 %65, i32* %10
%66 = load i32, i32* %10
%67 = icmp eq i32 %66, -1
br i1 %67, label %68, label %69
68: ; preds = %61
br label %75
69: ; preds = %61
br label %56
70: ; preds = %56
%71 = load i32, i32* %9
%72 = icmp ne i32 %71, 0
br i1 %72, label %74, label %73
73: ; preds = %70
store i32 0, i32* %13
br label %74
74: ; preds = %73, %70
br label %75
75: ; preds = %74, %68, %54, %44, %36, %30, %19
%76 = load i32, i32* %7
%77 = icmp ne i32 %76, 0
br i1 %77, label %78, label %81
78: ; preds = %75
%79 = load i32, i32* %7
%80 = call i32 @close(i32 %79)
br label %81
81: ; preds = %78, %75
%82 = load i32, i32* %8
%83 = icmp ne i32 %82, 0
br i1 %83, label %84, label %87
84: ; preds = %81
%85 = load i32, i32* %8
%86 = call i32 @close(i32 %85)
br label %87
87: ; preds = %84, %81
%88 = load i32, i32* %13
ret i32 %88
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
define i32 @vh_file_copy(i8* %0, i8* %1) {
%3 = alloca %struct.stat
%4 = alloca %struct.stat
%5 = alloca [4096 x i8]
%6 = bitcast %struct.stat* %3 to i8*
%7 = bitcast %struct.stat* %4 to i8*
%8 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%9 = icmp ne i8* %0, null
%10 = icmp ne i8* %1, null
%11 = and i1 %9, %10
br i1 %11, label %12, label %55
12: ; preds = %2
%13 = call i32 @stat(i8* %0, %struct.stat* %3)
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %55
15: ; preds = %12
%16 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%17 = load i32, i32* %16
%18 = tail call i32 @S_ISREG(i32 %17)
%19 = icmp eq i32 %18, 0
br i1 %19, label %55, label %20
20: ; preds = %15
%21 = call i32 @stat(i8* %1, %struct.stat* %4)
%22 = icmp eq i32 %21, 0
br i1 %22, label %55, label %23
23: ; preds = %20
%24 = load i32, i32* @O_RDONLY
%25 = tail call i32 (i8*, i32, ...) @open(i8* %0, i32 %24)
%26 = icmp eq i32 %25, -1
br i1 %26, label %45, label %27
27: ; preds = %23
%28 = load i32, i32* @O_CREAT
%29 = load i32, i32* @O_WRONLY
%30 = or i32 %29, %28
%31 = tail call i32 (i8*, i32, ...) @open(i8* %1, i32 %30, i32 420)
%32 = icmp eq i32 %31, -1
br i1 %32, label %42, label %33
33: ; preds = %27, %36
%34 = call i32 @read(i32 %25, i8* %8, i32 4096)
%35 = icmp sgt i32 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %33
%37 = call i32 @write(i32 %31, i8* %8, i32 %34)
%38 = icmp eq i32 %37, -1
br i1 %38, label %42, label %33
39: ; preds = %33
%40 = icmp ne i32 %34, 0
%41 = zext i1 %40 to i32
br label %42
42: ; preds = %36, %39, %27
%43 = phi i32 [ 1, %27 ], [ %41, %39 ], [ 1, %36 ]
%44 = icmp eq i32 %25, 0
br i1 %44, label %49, label %45
45: ; preds = %23, %42
%46 = phi i32 [ %43, %42 ], [ 1, %23 ]
%47 = phi i32 [ %31, %42 ], [ 0, %23 ]
%48 = tail call i32 @close(i32 %25)
br label %49
49: ; preds = %45, %42
%50 = phi i32 [ %46, %45 ], [ %43, %42 ]
%51 = phi i32 [ %47, %45 ], [ %31, %42 ]
%52 = icmp eq i32 %51, 0
br i1 %52, label %55, label %53
53: ; preds = %49
%54 = tail call i32 @close(i32 %51)
br label %55
55: ; preds = %12, %20, %15, %2, %53, %49
%56 = phi i32 [ %50, %53 ], [ %50, %49 ], [ 1, %2 ], [ 1, %15 ], [ 1, %20 ], [ 1, %12 ]
ret i32 %56
}