#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 */
int /*<<< orphan*/ EXIT_FAILURE;
int EXIT_SUCCESS;
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_TRUNC;
int O_WRONLY;
int S_IRGRP;
int S_IRUSR;
int S_IWGRP;
int S_IWUSR;
scalar_t__ close(int);
scalar_t__ copy(int, int);
int /*<<< orphan*/ error();
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int open(char *, int, ...);
int /*<<< orphan*/ usage();
int main(int argc, char *argv[]) {
  char *src_filename, *dest_filename;
  int src_fd = -1, dest_fd = -1;
  if (argc != 3) {
    usage();
    exit(EXIT_FAILURE);
  }
  src_filename = argv[1];
  dest_filename = argv[2];
  src_fd = open(src_filename, O_RDONLY);
  if (src_fd == -1) {
    goto fail;
  }
  dest_fd = open(dest_filename, O_WRONLY | O_CREAT | O_TRUNC,
                 (mode_t)(S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP));
  if (dest_fd == -1) {
    goto fail;
  }
  if (copy(src_fd, dest_fd) != 0) {
    goto fail;
  }
  if (close(dest_fd) != 0) {
    dest_fd = -1;
    goto fail;
  }
  if (close(src_fd) != 0) {
    src_fd = -1;
    goto fail;
  }
  return EXIT_SUCCESS;
fail:
  if (src_fd != -1) {
    close(src_fd);
  }
  error();
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x24(%rbp)
movl   $0xffffffff,-0x28(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11a3 <main+0x43>
mov    $0x0,%al
call   1050 <usage@plt>
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11e1 <main+0x81>
jmp    12a4 <main+0x144>
mov    -0x20(%rbp),%rdi
mov    0x2da4(%rip),%rax
mov    (%rax),%esi
mov    0x2e03(%rip),%rax
or     (%rax),%esi
mov    0x2df2(%rip),%rax
or     (%rax),%esi
mov    0x2da1(%rip),%rax
mov    (%rax),%edx
mov    0x2db8(%rip),%rax
or     (%rax),%edx
mov    0x2d9f(%rip),%rax
or     (%rax),%edx
mov    0x2dae(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    123d <main+0xdd>
jmp    12a4 <main+0x144>
mov    -0x24(%rbp),%edi
mov    -0x28(%rbp),%esi
call   1040 <copy@plt>
cmp    $0x0,%rax
je     1257 <main+0xf7>
jmp    12a4 <main+0x144>
mov    -0x28(%rbp),%edi
call   1030 <close@plt>
cmp    $0x0,%rax
je     1275 <main+0x115>
movl   $0xffffffff,-0x28(%rbp)
jmp    12a4 <main+0x144>
mov    -0x24(%rbp),%edi
call   1030 <close@plt>
cmp    $0x0,%rax
je     1293 <main+0x133>
movl   $0xffffffff,-0x24(%rbp)
jmp    12a4 <main+0x144>
mov    0x2d06(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12bd <main+0x15d>
cmpl   $0xffffffff,-0x24(%rbp)
je     12b6 <main+0x156>
mov    -0x24(%rbp),%edi
call   1030 <close@plt>
mov    $0x0,%al
call   1060 <error@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
cmp    $0x3,%edi
jne    1227 <main+0xc7>
mov    0x8(%rsi),%rdi
mov    0x10(%rsi),%rbx
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
xor    %r14d,%r14d
xor    %eax,%eax
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     1200 <main+0xa0>
mov    %eax,%ebp
mov    0x2dfa(%rip),%rax
mov    0x2e5b(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e48(%rip),%rax
or     (%rax),%esi
mov    0x2df7(%rip),%rax
mov    0x2e10(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2df5(%rip),%rax
or     (%rax),%edx
mov    0x2e04(%rip),%rax
or     (%rax),%edx
mov    %rbx,%rdi
xor    %eax,%eax
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11f9 <main+0x99>
mov    %eax,%ebx
mov    %ebp,%edi
mov    %eax,%esi
call   1040 <copy@plt>
test   %rax,%rax
jne    11f9 <main+0x99>
mov    %ebx,%edi
call   1030 <close@plt>
test   %rax,%rax
je     120f <main+0xaf>
mov    %ebp,%edi
call   1030 <close@plt>
xor    %eax,%eax
call   1060 <error@plt>
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    %ebp,%edi
call   1030 <close@plt>
test   %rax,%rax
jne    1200 <main+0xa0>
mov    0x2d7e(%rip),%rax
mov    (%rax),%r14d
jmp    1207 <main+0xa7>
xor    %eax,%eax
call   1050 <usage@plt>
mov    0x2d7b(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@EXIT_FAILURE = global i32 0
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 -1, i32* %8
store i32 -1, i32* %9
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 3
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = call i32 (...) @usage()
%14 = load i32, i32* @EXIT_FAILURE
%15 = call i32 @exit(i32 %14)
unreachable
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
store i8* %19, i8** %6
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 2
%22 = load i8*, i8** %21
store i8* %22, i8** %7
%23 = load i8*, i8** %6
%24 = load i32, i32* @O_RDONLY
%25 = call i32 (i8*, i32, ...) @open(i8* %23, i32 %24)
store i32 %25, i32* %8
%26 = load i32, i32* %8
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %29
28: ; preds = %16
br label %65
29: ; preds = %16
%30 = load i8*, i8** %7
%31 = load i32, i32* @O_WRONLY
%32 = load i32, i32* @O_CREAT
%33 = or i32 %31, %32
%34 = load i32, i32* @O_TRUNC
%35 = or i32 %33, %34
%36 = load i32, i32* @S_IRUSR
%37 = load i32, i32* @S_IWUSR
%38 = or i32 %36, %37
%39 = load i32, i32* @S_IRGRP
%40 = or i32 %38, %39
%41 = load i32, i32* @S_IWGRP
%42 = or i32 %40, %41
%43 = call i32 (i8*, i32, ...) @open(i8* %30, i32 %35, i32 %42)
store i32 %43, i32* %9
%44 = load i32, i32* %9
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %47
46: ; preds = %29
br label %65
47: ; preds = %29
%48 = load i32, i32* %8
%49 = load i32, i32* %9
%50 = call i64 @copy(i32 %48, i32 %49)
%51 = icmp ne i64 %50, 0
br i1 %51, label %52, label %53
52: ; preds = %47
br label %65
53: ; preds = %47
%54 = load i32, i32* %9
%55 = call i64 @close(i32 %54)
%56 = icmp ne i64 %55, 0
br i1 %56, label %57, label %58
57: ; preds = %53
store i32 -1, i32* %9
br label %65
58: ; preds = %53
%59 = load i32, i32* %8
%60 = call i64 @close(i32 %59)
%61 = icmp ne i64 %60, 0
br i1 %61, label %62, label %63
62: ; preds = %58
store i32 -1, i32* %8
br label %65
63: ; preds = %58
%64 = load i32, i32* @EXIT_SUCCESS
store i32 %64, i32* %3
br label %73
65: ; preds = %62, %57, %52, %46, %28
%66 = load i32, i32* %8
%67 = icmp ne i32 %66, -1
br i1 %67, label %68, label %71
68: ; preds = %65
%69 = load i32, i32* %8
%70 = call i64 @close(i32 %69)
br label %71
71: ; preds = %68, %65
%72 = call i32 (...) @error()
br label %73
73: ; preds = %71, %63
%74 = load i32, i32* %3
ret i32 %74
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 3
br i1 %3, label %8, label %4
4: ; preds = %2
%5 = tail call i32 (...) @usage()
%6 = load i32, i32* @EXIT_FAILURE
%7 = tail call i32 @exit(i32 %6)
unreachable
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = getelementptr inbounds i8*, i8** %1, i64 2
%12 = load i8*, i8** %11
%13 = load i32, i32* @O_RDONLY
%14 = tail call i32 (i8*, i32, ...) @open(i8* %10, i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %44, label %16
16: ; preds = %8
%17 = load i32, i32* @O_WRONLY
%18 = load i32, i32* @O_CREAT
%19 = or i32 %18, %17
%20 = load i32, i32* @O_TRUNC
%21 = or i32 %19, %20
%22 = load i32, i32* @S_IRUSR
%23 = load i32, i32* @S_IWUSR
%24 = or i32 %23, %22
%25 = load i32, i32* @S_IRGRP
%26 = or i32 %24, %25
%27 = load i32, i32* @S_IWGRP
%28 = or i32 %26, %27
%29 = tail call i32 (i8*, i32, ...) @open(i8* %12, i32 %21, i32 %28)
%30 = icmp eq i32 %29, -1
br i1 %30, label %42, label %31
31: ; preds = %16
%32 = tail call i64 @copy(i32 %14, i32 %29)
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %42
34: ; preds = %31
%35 = tail call i64 @close(i32 %29)
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %42
37: ; preds = %34
%38 = tail call i64 @close(i32 %14)
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %44
40: ; preds = %37
%41 = load i32, i32* @EXIT_SUCCESS
br label %46
42: ; preds = %16, %31, %34
%43 = tail call i64 @close(i32 %14)
br label %44
44: ; preds = %8, %37, %42
%45 = tail call i32 (...) @error()
br label %46
46: ; preds = %44, %40
%47 = phi i32 [ 0, %44 ], [ %41, %40 ]
ret i32 %47
}