#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 /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ error(char *, char const *);
int open(char const *, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ warning(char *, ...);
int write(int, char *, int);
void copy_output(int write_fd, const char *ofname) {
  int file_fd;
  char buffer[1000];
  int read_bytes;
  int res;
  file_fd = open(ofname, O_RDONLY);
  if (file_fd == -1)
    error("mail: Cannot open the output file %s", ofname);
  do {
    read_bytes = read(file_fd, buffer, 1000);
    if (read_bytes > 0) {
      res = write(write_fd, buffer, read_bytes);
      if (res == -1)
        warning("Cannot write to the mail pipe %i", write_fd);
    }
  } while (read_bytes > 0);
  if (read_bytes == -1)
    warning("Cannot read the output file %s from %i", ofname, file_fd);
}


x86 assembly (clang, -O0):
<copy_output>:
push   %rbp
mov    %rsp,%rbp
sub    $0x410,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e7b(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1191 <copy_output+0x41>
mov    -0x10(%rbp),%rsi
lea    0xe74(%rip),%rdi
call   1060 <error@plt>
jmp    1196 <copy_output+0x46>
mov    -0x14(%rbp),%edi
lea    -0x400(%rbp),%rsi
mov    $0x3e8,%edx
call   1050 <read@plt>
mov    %eax,-0x404(%rbp)
cmpl   $0x0,-0x404(%rbp)
jle    11fb <copy_output+0xab>
mov    -0x4(%rbp),%edi
lea    -0x400(%rbp),%rsi
mov    -0x404(%rbp),%edx
call   1040 <write@plt>
mov    %eax,-0x408(%rbp)
cmpl   $0xffffffff,-0x408(%rbp)
jne    11f6 <copy_output+0xa6>
mov    -0x4(%rbp),%esi
lea    0xe36(%rip),%rdi
mov    $0x0,%al
call   1030 <warning@plt>
jmp    11fb <copy_output+0xab>
jmp    1200 <copy_output+0xb0>
cmpl   $0x0,-0x404(%rbp)
jg     1196 <copy_output+0x46>
cmpl   $0xffffffff,-0x404(%rbp)
jne    122f <copy_output+0xdf>
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
lea    0xe1e(%rip),%rdi
mov    $0x0,%al
call   1030 <warning@plt>
add    $0x410,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_output>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x3f0,%rsp
mov    %rsi,%r14
mov    %edi,%ebx
mov    0x2e7d(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1070 <open@plt>
mov    %eax,%r15d
cmp    $0xffffffff,%eax
jne    118c <copy_output+0x3c>
lea    0xe7c(%rip),%rdi
mov    %r14,%rsi
call   1060 <error@plt>
mov    %rsp,%rsi
mov    %r15d,%edi
mov    $0x3e8,%edx
call   1050 <read@plt>
test   %eax,%eax
jle    11e3 <copy_output+0x93>
mov    %rsp,%rbp
lea    0xe7b(%rip),%r12
jmp    11c4 <copy_output+0x74>
nopl   0x0(%rax)
mov    %r15d,%edi
mov    %rbp,%rsi
mov    $0x3e8,%edx
call   1050 <read@plt>
test   %eax,%eax
jle    11e3 <copy_output+0x93>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
jne    11b0 <copy_output+0x60>
mov    %r12,%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1030 <warning@plt>
jmp    11b0 <copy_output+0x60>
cmp    $0xffffffff,%eax
je     11f8 <copy_output+0xa8>
add    $0x3f0,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe47(%rip),%rdi
mov    %r14,%rsi
mov    %r15d,%edx
xor    %eax,%eax
add    $0x3f0,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <warning@plt>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [37 x i8] c"mail: Cannot open the output file %s\00"
@.str.1 = [33 x i8] c"Cannot write to the mail pipe %i\00"
@.str.2 = [39 x i8] c"Cannot read the output file %s from %i\00"
define void @copy_output(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca [1000 x i8]
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %3
store i8* %1, i8** %4
%9 = load i8*, i8** %4
%10 = load i32, i32* @O_RDONLY
%11 = call i32 @open(i8* %9, i32 %10)
store i32 %11, i32* %5
%12 = load i32, i32* %5
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = load i8*, i8** %4
%16 = call i32 @error(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8* %15)
br label %17
17: ; preds = %14, %2
br label %18
18: ; preds = %36, %17
%19 = load i32, i32* %5
%20 = getelementptr inbounds [1000 x i8], [1000 x i8]* %6, i64 0, i64 0
%21 = call i32 @read(i32 %19, i8* %20, i32 1000)
store i32 %21, i32* %7
%22 = load i32, i32* %7
%23 = icmp sgt i32 %22, 0
br i1 %23, label %24, label %35
24: ; preds = %18
%25 = load i32, i32* %3
%26 = getelementptr inbounds [1000 x i8], [1000 x i8]* %6, i64 0, i64 0
%27 = load i32, i32* %7
%28 = call i32 @write(i32 %25, i8* %26, i32 %27)
store i32 %28, i32* %8
%29 = load i32, i32* %8
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %34
31: ; preds = %24
%32 = load i32, i32* %3
%33 = call i32 (i8*, ...) @warning(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %32)
br label %34
34: ; preds = %31, %24
br label %35
35: ; preds = %34, %18
br label %36
36: ; preds = %35
%37 = load i32, i32* %7
%38 = icmp sgt i32 %37, 0
br i1 %38, label %18, label %39
39: ; preds = %36
%40 = load i32, i32* %7
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %46
42: ; preds = %39
%43 = load i8*, i8** %4
%44 = load i32, i32* %5
%45 = call i32 (i8*, ...) @warning(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i8* %43, i32 %44)
br label %46
46: ; preds = %42, %39
ret void
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [37 x i8] c"mail: Cannot open the output file %s\00"
@.str.1 = [33 x i8] c"Cannot write to the mail pipe %i\00"
@.str.2 = [39 x i8] c"Cannot read the output file %s from %i\00"
define void @copy_output(i32 %0, i8* %1) {
%3 = alloca [1000 x i8]
%4 = getelementptr inbounds [1000 x i8], [1000 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i8* %1, i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 @error(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8* %1)
br label %10
10: ; preds = %8, %2
%11 = call i32 @read(i32 %6, i8* %4, i32 1000)
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %22
13: ; preds = %10, %19
%14 = phi i32 [ %20, %19 ], [ %11, %10 ]
%15 = call i32 @write(i32 %0, i8* %4, i32 %14)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %13
%18 = tail call i32 (i8*, ...) @warning(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %0)
br label %19
19: ; preds = %17, %13
%20 = call i32 @read(i32 %6, i8* %4, i32 1000)
%21 = icmp sgt i32 %20, 0
br i1 %21, label %13, label %22
22: ; preds = %19, %10
%23 = phi i32 [ %11, %10 ], [ %20, %19 ]
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = tail call i32 (i8*, ...) @warning(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i8* %1, i32 %6)
br label %27
27: ; preds = %25, %22
ret void
}