#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 */
typedef int /*<<< orphan*/ mode_t;

/* Variables and functions */
scalar_t__ EINTR;
int /*<<< orphan*/ EXIT_FAILURE;
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_WRONLY;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, int, char *,
                           char *);
int open(char *, int, ...);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
int /*<<< orphan*/ write(int, char *, int);
void cp_file(char *src, char *dest, mode_t mode) {
  int fdout;
  int fdin;
  int ret;
  char buf[1024 + 1] = {};
  fdin = open(src, O_RDONLY);
  fdout = open(dest, O_WRONLY | O_CREAT, mode);
  if (fdout == -1 || fdin == -1)
    do {
      fprintf(stderr, "[%s][%d]:%s %s\n",
              "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
              "2022-01-22/02-19-57/repos/drsin/linuxCoreLearn/refs/heads/"
              "master/apue/day03/mycp.c",
              29, "open", strerror(errno));
      exit(EXIT_FAILURE);
    } while (0);
  while (1) {
    while ((ret = read(fdin, buf, 1024)) == -1) {
      if (errno == EINTR)
        continue;
      break;
    }
    if (ret == 0)
      break;
    write(fdout, buf, ret);
  }
  if (ret == -1)
    do {
      fprintf(stderr, "[%s][%d]:%s %s\n",
              "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
              "2022-01-22/02-19-57/repos/drsin/linuxCoreLearn/refs/heads/"
              "master/apue/day03/mycp.c",
              43, "read", strerror(errno));
      exit(EXIT_FAILURE);
    } while (0);
  close(fdin);
  close(fdout);
}


x86 assembly (clang, -O0):
<cp_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
lea    -0x430(%rbp),%rdi
xor    %esi,%esi
mov    $0x401,%edx
call   1040 <memset@plt>
mov    -0x8(%rbp),%rdi
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2ddd(%rip),%rax
mov    (%rax),%esi
mov    0x2e24(%rip),%rax
or     (%rax),%esi
mov    -0x14(%rbp),%edx
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
je     11f7 <cp_file+0x77>
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1250 <cp_file+0xd0>
jmp    11fc <cp_file+0x7c>
mov    0x2dcd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x434(%rbp)
mov    0x2db6(%rip),%rax
mov    (%rax),%rdi
call   10a0 <strerror@plt>
mov    -0x434(%rbp),%edi
mov    %rax,%r9
lea    0xdd6(%rip),%rsi
lea    0xddf(%rip),%rdx
mov    $0x1d,%ecx
lea    0xe66(%rip),%r8
call   1070 <fprintf@plt>
mov    0x2d77(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
jmp    1255 <cp_file+0xd5>
jmp    125a <cp_file+0xda>
mov    -0x1c(%rbp),%edi
lea    -0x430(%rbp),%rsi
mov    $0x400,%edx
call   1060 <read@plt>
mov    %eax,-0x20(%rbp)
cmp    $0xffffffff,%eax
jne    129e <cp_file+0x11e>
mov    0x2d47(%rip),%rax
mov    (%rax),%rax
mov    0x2d2d(%rip),%rcx
cmp    (%rcx),%rax
jne    1299 <cp_file+0x119>
jmp    125a <cp_file+0xda>
jmp    129e <cp_file+0x11e>
cmpl   $0x0,-0x20(%rbp)
jne    12ad <cp_file+0x12d>
jmp    12c4 <cp_file+0x144>
mov    -0x18(%rbp),%edi
lea    -0x430(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   1030 <write@plt>
jmp    1255 <cp_file+0xd5>
cmpl   $0xffffffff,-0x20(%rbp)
jne    1327 <cp_file+0x1a7>
jmp    12d3 <cp_file+0x153>
mov    0x2cf6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x438(%rbp)
mov    0x2cdf(%rip),%rax
mov    (%rax),%rdi
call   10a0 <strerror@plt>
mov    -0x438(%rbp),%edi
mov    %rax,%r9
lea    0xcff(%rip),%rsi
lea    0xd08(%rip),%rdx
mov    $0x2b,%ecx
lea    0xd94(%rip),%r8
call   1070 <fprintf@plt>
mov    0x2ca0(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cp_file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x410,%rsp
mov    %edx,%r14d
mov    %rsi,%rbp
mov    %rdi,%rbx
mov    %rsp,%rdi
mov    $0x401,%edx
xor    %esi,%esi
call   1040 <memset@plt>
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
xor    %eax,%eax
call   1080 <open@plt>
mov    %eax,%ebx
mov    0x2de5(%rip),%rax
mov    0x2e2e(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbp,%rdi
mov    %r14d,%edx
xor    %eax,%eax
call   1080 <open@plt>
cmp    $0xffffffff,%ebx
je     124e <cp_file+0xce>
mov    %eax,%r14d
cmp    $0xffffffff,%eax
je     124e <cp_file+0xce>
mov    %rsp,%rbp
mov    0x2dd6(%rip),%r15
mov    0x2dbf(%rip),%r12
jmp    1211 <cp_file+0x91>
nopl   0x0(%rax,%rax,1)
test   %eax,%eax
je     1230 <cp_file+0xb0>
mov    %r14d,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x400,%edx
call   1060 <read@plt>
cmp    $0xffffffff,%eax
jne    1200 <cp_file+0x80>
mov    (%r15),%rcx
cmp    (%r12),%rcx
je     1211 <cp_file+0x91>
jmp    1204 <cp_file+0x84>
mov    %ebx,%edi
call   1050 <close@plt>
mov    %r14d,%edi
add    $0x410,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <close@plt>
mov    0x2d7b(%rip),%rax
mov    (%rax),%ebx
mov    0x2d6a(%rip),%rax
mov    (%rax),%rdi
call   10a0 <strerror@plt>
lea    0xd93(%rip),%rsi
lea    0xd9c(%rip),%rdx
lea    0xe28(%rip),%r8
mov    %ebx,%edi
mov    $0x1d,%ecx
mov    %rax,%r9
call   1070 <fprintf@plt>
mov    0x2d2f(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@stderr = global i32 0
@.str = [16 x i8] c"[%s][%d]:%s %s\0A\00"
@.str.1 = [147 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/drsin/linuxCoreLearn/refs/heads/master/apue/day03/mycp.c\00"
@.str.2 = [5 x i8] c"open\00"
@errno = global i64 0
@EXIT_FAILURE = global i32 0
@EINTR = global i64 0
@.str.3 = [5 x i8] c"read\00"
define void @cp_file(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca [1025 x i8]
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%11 = bitcast [1025 x i8]* %10 to i8*
%12 = load i8*, i8** %4
%13 = load i32, i32* @O_RDONLY
%14 = call i32 (i8*, i32, ...) @open(i8* %12, i32 %13)
store i32 %14, i32* %8
%15 = load i8*, i8** %5
%16 = load i32, i32* @O_WRONLY
%17 = load i32, i32* @O_CREAT
%18 = or i32 %16, %17
%19 = load i32, i32* %6
%20 = call i32 (i8*, i32, ...) @open(i8* %15, i32 %18, i32 %19)
store i32 %20, i32* %7
%21 = load i32, i32* %7
%22 = icmp eq i32 %21, -1
br i1 %22, label %26, label %23
23: ; preds = %3
%24 = load i32, i32* %8
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %35
26: ; preds = %23, %3
br label %27
27: ; preds = %26
%28 = load i32, i32* @stderr
%29 = load i64, i64* @errno
%30 = call i8* @strerror(i64 %29)
%31 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([147 x i8], [147 x i8]* @.str.1, i64 0, i64 0), i32 29, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %30)
%32 = load i32, i32* @EXIT_FAILURE
%33 = call i32 @exit(i32 %32)
unreachable
34: ; No predecessors!
br label %35
35: ; preds = %34, %23
br label %36
36: ; preds = %35, %52
br label %37
37: ; preds = %46, %36
%38 = load i32, i32* %8
%39 = getelementptr inbounds [1025 x i8], [1025 x i8]* %10, i64 0, i64 0
%40 = call i32 @read(i32 %38, i8* %39, i32 1024)
store i32 %40, i32* %9
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %48
42: ; preds = %37
%43 = load i64, i64* @errno
%44 = load i64, i64* @EINTR
%45 = icmp eq i64 %43, %44
br i1 %45, label %46, label %47
46: ; preds = %42
br label %37
47: ; preds = %42
br label %48
48: ; preds = %47, %37
%49 = load i32, i32* %9
%50 = icmp eq i32 %49, 0
br i1 %50, label %51, label %52
51: ; preds = %48
br label %57
52: ; preds = %48
%53 = load i32, i32* %7
%54 = getelementptr inbounds [1025 x i8], [1025 x i8]* %10, i64 0, i64 0
%55 = load i32, i32* %9
%56 = call i32 @write(i32 %53, i8* %54, i32 %55)
br label %36
57: ; preds = %51
%58 = load i32, i32* %9
%59 = icmp eq i32 %58, -1
br i1 %59, label %60, label %69
60: ; preds = %57
br label %61
61: ; preds = %60
%62 = load i32, i32* @stderr
%63 = load i64, i64* @errno
%64 = call i8* @strerror(i64 %63)
%65 = call i32 @fprintf(i32 %62, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([147 x i8], [147 x i8]* @.str.1, i64 0, i64 0), i32 43, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* %64)
%66 = load i32, i32* @EXIT_FAILURE
%67 = call i32 @exit(i32 %66)
unreachable
68: ; No predecessors!
br label %69
69: ; preds = %68, %57
%70 = load i32, i32* %8
%71 = call i32 @close(i32 %70)
%72 = load i32, i32* %7
%73 = call i32 @close(i32 %72)
ret void
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@stderr = global i32 0
@.str = [16 x i8] c"[%s][%d]:%s %s\0A\00"
@.str.1 = [147 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/drsin/linuxCoreLearn/refs/heads/master/apue/day03/mycp.c\00"
@.str.2 = [5 x i8] c"open\00"
@errno = global i64 0
@EXIT_FAILURE = global i32 0
@EINTR = global i64 0
define void @cp_file(i8* nocapture %0, i8* nocapture %1, i32 %2) {
%4 = alloca [1025 x i8]
%5 = getelementptr inbounds [1025 x i8], [1025 x i8]* %4, i64 0, i64 0
%6 = load i32, i32* @O_RDONLY
%7 = tail call i32 (i8*, i32, ...) @open(i8* %0, i32 %6)
%8 = load i32, i32* @O_WRONLY
%9 = load i32, i32* @O_CREAT
%10 = or i32 %9, %8
%11 = tail call i32 (i8*, i32, ...) @open(i8* %1, i32 %10, i32 %2)
%12 = icmp eq i32 %11, -1
%13 = icmp eq i32 %7, -1
%14 = or i1 %13, %12
br i1 %14, label %15, label %22
15: ; preds = %3
%16 = load i32, i32* @stderr
%17 = load i64, i64* @errno
%18 = tail call i8* @strerror(i64 %17)
%19 = tail call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([147 x i8], [147 x i8]* @.str.1, i64 0, i64 0), i32 29, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %18)
%20 = load i32, i32* @EXIT_FAILURE
%21 = tail call i32 @exit(i32 %20)
unreachable
22: ; preds = %3, %28
%23 = call i32 @read(i32 %7, i8* %5, i32 1024)
switch i32 %23, label %29 [
i32 -1, label %24
i32 0, label %31
]
24: ; preds = %22
%25 = load i64, i64* @errno
%26 = load i64, i64* @EINTR
%27 = icmp eq i64 %25, %26
br i1 %27, label %28, label %29
28: ; preds = %24, %29
br label %22
29: ; preds = %24, %22
%30 = call i32 @write(i32 %11, i8* %5, i32 %23)
br label %28
31: ; preds = %22
%32 = tail call i32 @close(i32 %7)
%33 = tail call i32 @close(i32 %11)
ret void
}