#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_WRONLY;
int /*<<< orphan*/ my_printf(char *, int, int);
int open(char *, int);
int read(int, char *, int);
scalar_t__ write(int, char *, int);
int my_fdup(char *path, char *npath) {
  int fdf1;
  int fdf2;
  char buff[4096];
  int i;
  if (path == NULL || npath == NULL || (fdf1 = open(path, O_RDONLY)) < 0 ||
      (fdf2 = open(npath, O_CREAT | O_WRONLY)) < 0) {
    my_printf("fdf1 = %d\nfdf2 = %d\n", fdf1, fdf2);
    return (0);
  }
  while ((i = read(fdf1, buff, 4096)) > 0) {
    if (write(fdf2, buff, i) < 0)
      return (0);
  }
  return (1);
}


x86 assembly (clang, -O0):
<my_fdup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11ae <my_fdup+0x6e>
cmpq   $0x0,-0x18(%rbp)
je     11ae <my_fdup+0x6e>
mov    -0x10(%rbp),%rdi
mov    0x2e6c(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jl     11ae <my_fdup+0x6e>
mov    -0x18(%rbp),%rdi
mov    0x2e66(%rip),%rax
mov    (%rax),%esi
mov    0x2e2d(%rip),%rax
or     (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jge    11cc <my_fdup+0x8c>
mov    -0x1c(%rbp),%esi
mov    -0x20(%rbp),%edx
lea    0xe45(%rip),%rdi
call   1040 <my_printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    122b <my_fdup+0xeb>
jmp    11d1 <my_fdup+0x91>
mov    -0x1c(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
mov    %eax,-0x1024(%rbp)
cmp    $0x0,%eax
jle    1224 <my_fdup+0xe4>
mov    -0x20(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    -0x1024(%rbp),%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    121f <my_fdup+0xdf>
movl   $0x0,-0x4(%rbp)
jmp    122b <my_fdup+0xeb>
jmp    11d1 <my_fdup+0x91>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_fdup>:
push   %rbp
push   %r14
push   %rbx
sub    $0x1000,%rsp
mov    %rsi,%rbx
mov    0x2e8b(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,%r14d
mov    0x2e92(%rip),%rax
mov    0x2e5b(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
call   1060 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
js     11b7 <my_fdup+0x77>
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
test   %eax,%eax
jle    11cc <my_fdup+0x8c>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
test   %rax,%rax
jns    1190 <my_fdup+0x50>
jmp    11c8 <my_fdup+0x88>
lea    0xe42(%rip),%rdi
mov    %r14d,%esi
mov    %ebp,%edx
call   1040 <my_printf@plt>
xor    %eax,%eax
jmp    11d1 <my_fdup+0x91>
mov    $0x1,%eax
add    $0x1000,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@.str = [21 x i8] c"fdf1 = %d\0Afdf2 = %d\0A\00"
define i32 @my_fdup(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca [4096 x i8]
%9 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%10 = load i8*, i8** %4
%11 = icmp eq i8* %10, null
br i1 %11, label %27, label %12
12: ; preds = %2
%13 = load i8*, i8** %5
%14 = icmp eq i8* %13, null
br i1 %14, label %27, label %15
15: ; preds = %12
%16 = load i8*, i8** %4
%17 = load i32, i32* @O_RDONLY
%18 = call i32 @open(i8* %16, i32 %17)
store i32 %18, i32* %6
%19 = icmp slt i32 %18, 0
br i1 %19, label %27, label %20
20: ; preds = %15
%21 = load i8*, i8** %5
%22 = load i32, i32* @O_CREAT
%23 = load i32, i32* @O_WRONLY
%24 = or i32 %22, %23
%25 = call i32 @open(i8* %21, i32 %24)
store i32 %25, i32* %7
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %20, %15, %12, %2
%28 = load i32, i32* %6
%29 = load i32, i32* %7
%30 = call i32 @my_printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %28, i32 %29)
store i32 0, i32* %3
br label %46
31: ; preds = %20
br label %32
32: ; preds = %44, %31
%33 = load i32, i32* %6
%34 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%35 = call i32 @read(i32 %33, i8* %34, i32 4096)
store i32 %35, i32* %9
%36 = icmp sgt i32 %35, 0
br i1 %36, label %37, label %45
37: ; preds = %32
%38 = load i32, i32* %7
%39 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%40 = load i32, i32* %9
%41 = call i64 @write(i32 %38, i8* %39, i32 %40)
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %44
43: ; preds = %37
store i32 0, i32* %3
br label %46
44: ; preds = %37
br label %32
45: ; preds = %32
store i32 1, i32* %3
br label %46
46: ; preds = %45, %43, %27
%47 = load i32, i32* %3
ret i32 %47
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@.str = [21 x i8] c"fdf1 = %d\0Afdf2 = %d\0A\00"
define i32 @my_fdup(i8* %0, i8* %1) {
%3 = alloca [4096 x i8]
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%5 = icmp ne i8* %0, null
%6 = icmp ne i8* %1, null
tail call void @llvm.assume(i1 %5)
tail call void @llvm.assume(i1 %6)
%7 = load i32, i32* @O_RDONLY
%8 = tail call i32 @open(i8* %0, i32 %7)
%9 = icmp sgt i32 %8, -1
tail call void @llvm.assume(i1 %9)
%10 = load i32, i32* @O_CREAT
%11 = load i32, i32* @O_WRONLY
%12 = or i32 %11, %10
%13 = tail call i32 @open(i8* %1, i32 %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = tail call i32 @my_printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %8, i32 %13)
br label %23
17: ; preds = %2, %20
%18 = call i32 @read(i32 %8, i8* %4, i32 4096)
%19 = icmp sgt i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %17
%21 = call i64 @write(i32 %13, i8* %4, i32 %18)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %17
23: ; preds = %17, %20, %15
%24 = phi i32 [ 0, %15 ], [ 1, %17 ], [ 0, %20 ]
ret i32 %24
}