#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 ssize_t;
typedef int mode_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int EXIT_SUCCESS;
int O_CREAT;
int O_RDONLY;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ SEEK_CUR;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWGRP;
int S_IWOTH;
int S_IWUSR;
int /*<<< orphan*/ helpAndLeave(char *, int /*<<< orphan*/);
int /*<<< orphan*/ lseek(int, int, int /*<<< orphan*/);
scalar_t__ memcmp(char *, char *, int);
int /*<<< orphan*/ pexit(char *);
int read(int, char *, int);
int /*<<< orphan*/ safeClose(int);
int safeOpen(char *, int, ...);
int write(int, char *, int);
int main(int argc, char *argv[]) {
  if (argc != 3) {
    helpAndLeave(argv[0], EXIT_FAILURE);
  }
  int inputFd, outputFd, inputFlags, outputFlags;
  mode_t outputMode;
  char *inputFile, *outputFile;
  ssize_t numRead;
  char buf[1024];
  char zeroes[1024];
  inputFile = argv[1];
  outputFile = argv[2];
  inputFlags = O_RDONLY;
  outputFlags = O_WRONLY | O_CREAT | O_TRUNC;
  outputMode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
  inputFd = safeOpen(inputFile, inputFlags);
  outputFd = safeOpen(outputFile, outputFlags, outputMode);
  while ((numRead = read(inputFd, buf, 1024)) > 0) {
    if (numRead == -1) {
      pexit("read");
    }
    if (memcmp(buf, zeroes, numRead) == 0) {
      lseek(outputFd, numRead, SEEK_CUR);
    } else {
      if (write(outputFd, buf, numRead) != numRead) {
        pexit("write");
      }
    }
  }
  safeClose(inputFd);
  safeClose(outputFd);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x840,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11b8 <main+0x38>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    0x2def(%rip),%rax
mov    (%rax),%esi
call   1080 <helpAndLeave@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x38(%rbp)
mov    0x2df9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2d9d(%rip),%rax
mov    (%rax),%eax
mov    0x2e0c(%rip),%rcx
or     (%rcx),%eax
mov    0x2dfb(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x20(%rbp)
mov    0x2d97(%rip),%rax
mov    (%rax),%eax
mov    0x2db6(%rip),%rcx
or     (%rcx),%eax
mov    0x2d9d(%rip),%rcx
or     (%rcx),%eax
mov    0x2dac(%rip),%rcx
or     (%rcx),%eax
mov    0x2d83(%rip),%rcx
or     (%rcx),%eax
mov    0x2d4a(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x24(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    $0x0,%al
call   10a0 <safeOpen@plt>
mov    %eax,-0x14(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%esi
mov    -0x24(%rbp),%edx
mov    $0x0,%al
call   10a0 <safeOpen@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x440(%rbp),%rsi
mov    $0x400,%edx
call   1060 <read@plt>
mov    %eax,-0x3c(%rbp)
cmp    $0x0,%eax
jle    12f8 <main+0x178>
cmpl   $0xffffffff,-0x3c(%rbp)
jne    128e <main+0x10e>
lea    0xd77(%rip),%rdi
call   1090 <pexit@plt>
lea    -0x440(%rbp),%rdi
lea    -0x840(%rbp),%rsi
mov    -0x3c(%rbp),%edx
call   1070 <memcmp@plt>
cmp    $0x0,%rax
jne    12c7 <main+0x147>
mov    -0x18(%rbp),%edi
mov    -0x3c(%rbp),%esi
mov    0x2d1d(%rip),%rax
mov    (%rax),%edx
call   1050 <lseek@plt>
jmp    12f3 <main+0x173>
mov    -0x18(%rbp),%edi
lea    -0x440(%rbp),%rsi
mov    -0x3c(%rbp),%edx
call   1030 <write@plt>
cmp    -0x3c(%rbp),%eax
je     12ee <main+0x16e>
lea    0xd1c(%rip),%rdi
call   1090 <pexit@plt>
jmp    12f3 <main+0x173>
jmp    1258 <main+0xd8>
mov    -0x14(%rbp),%edi
call   1040 <safeClose@plt>
mov    -0x18(%rbp),%edi
call   1040 <safeClose@plt>
mov    0x2c81(%rip),%rax
mov    (%rax),%eax
add    $0x840,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x808,%rsp
mov    %rsi,%r14
cmp    $0x3,%edi
je     11aa <main+0x2a>
mov    (%r14),%rdi
mov    0x2dfd(%rip),%rax
mov    (%rax),%esi
call   1080 <helpAndLeave@plt>
mov    0x2dcf(%rip),%rax
mov    0x2e40(%rip),%rcx
mov    (%rcx),%ebx
or     (%rax),%ebx
mov    0x2e2d(%rip),%rax
or     (%rax),%ebx
mov    0x2dcc(%rip),%rax
mov    0x2ded(%rip),%rcx
mov    (%rcx),%ebp
or     (%rax),%ebp
mov    0x8(%r14),%rdi
mov    0x2dce(%rip),%rax
or     (%rax),%ebp
mov    0x10(%r14),%r14
mov    0x2dd9(%rip),%rax
or     (%rax),%ebp
mov    0x2dd8(%rip),%rax
mov    0x2da9(%rip),%rcx
or     (%rcx),%ebp
mov    (%rax),%esi
mov    0x2d6e(%rip),%rax
or     (%rax),%ebp
xor    %eax,%eax
call   10a0 <safeOpen@plt>
mov    %eax,%r13d
mov    %r14,%rdi
mov    %ebx,%esi
mov    %ebp,%edx
xor    %eax,%eax
call   10a0 <safeOpen@plt>
mov    %eax,%r14d
mov    %rsp,%rsi
mov    %r13d,%edi
mov    $0x400,%edx
call   1060 <read@plt>
test   %eax,%eax
jle    12b5 <main+0x135>
mov    %eax,%ebx
mov    %rsp,%rbp
lea    0x400(%rsp),%r15
mov    0x2d89(%rip),%r12
jmp    1284 <main+0x104>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    (%r12),%edx
mov    %r14d,%edi
mov    %ebx,%esi
call   1050 <lseek@plt>
mov    %r13d,%edi
mov    %rbp,%rsi
mov    $0x400,%edx
call   1060 <read@plt>
mov    %eax,%ebx
test   %eax,%eax
jle    12b5 <main+0x135>
mov    %rbp,%rdi
mov    %r15,%rsi
mov    %ebx,%edx
call   1070 <memcmp@plt>
test   %rax,%rax
je     1260 <main+0xe0>
mov    %r14d,%edi
mov    %rbp,%rsi
mov    %ebx,%edx
call   1030 <write@plt>
cmp    %ebx,%eax
je     126e <main+0xee>
lea    0xd52(%rip),%rdi
call   1090 <pexit@plt>
jmp    126e <main+0xee>
mov    %r13d,%edi
call   1040 <safeClose@plt>
mov    %r14d,%edi
call   1040 <safeClose@plt>
mov    0x2cc4(%rip),%rax
mov    (%rax),%eax
add    $0x808,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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
@S_IROTH = global i32 0
@S_IWOTH = global i32 0
@.str = [5 x i8] c"read\00"
@SEEK_CUR = global i32 0
@.str.1 = [6 x i8] c"write\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i32
%14 = alloca [1024 x i8]
%15 = alloca [1024 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%16 = load i32, i32* %4
%17 = icmp ne i32 %16, 3
br i1 %17, label %18, label %24
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 0
%21 = load i8*, i8** %20
%22 = load i32, i32* @EXIT_FAILURE
%23 = call i32 @helpAndLeave(i8* %21, i32 %22)
br label %24
24: ; preds = %18, %2
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 1
%27 = load i8*, i8** %26
store i8* %27, i8** %11
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 2
%30 = load i8*, i8** %29
store i8* %30, i8** %12
%31 = load i32, i32* @O_RDONLY
store i32 %31, i32* %8
%32 = load i32, i32* @O_WRONLY
%33 = load i32, i32* @O_CREAT
%34 = or i32 %32, %33
%35 = load i32, i32* @O_TRUNC
%36 = or i32 %34, %35
store i32 %36, i32* %9
%37 = load i32, i32* @S_IRUSR
%38 = load i32, i32* @S_IWUSR
%39 = or i32 %37, %38
%40 = load i32, i32* @S_IRGRP
%41 = or i32 %39, %40
%42 = load i32, i32* @S_IWGRP
%43 = or i32 %41, %42
%44 = load i32, i32* @S_IROTH
%45 = or i32 %43, %44
%46 = load i32, i32* @S_IWOTH
%47 = or i32 %45, %46
store i32 %47, i32* %10
%48 = load i8*, i8** %11
%49 = load i32, i32* %8
%50 = call i32 (i8*, i32, ...) @safeOpen(i8* %48, i32 %49)
store i32 %50, i32* %6
%51 = load i8*, i8** %12
%52 = load i32, i32* %9
%53 = load i32, i32* %10
%54 = call i32 (i8*, i32, ...) @safeOpen(i8* %51, i32 %52, i32 %53)
store i32 %54, i32* %7
br label %55
55: ; preds = %86, %24
%56 = load i32, i32* %6
%57 = getelementptr inbounds [1024 x i8], [1024 x i8]* %14, i64 0, i64 0
%58 = call i32 @read(i32 %56, i8* %57, i32 1024)
store i32 %58, i32* %13
%59 = icmp sgt i32 %58, 0
br i1 %59, label %60, label %87
60: ; preds = %55
%61 = load i32, i32* %13
%62 = icmp eq i32 %61, -1
br i1 %62, label %63, label %65
63: ; preds = %60
%64 = call i32 @pexit(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %65
65: ; preds = %63, %60
%66 = getelementptr inbounds [1024 x i8], [1024 x i8]* %14, i64 0, i64 0
%67 = getelementptr inbounds [1024 x i8], [1024 x i8]* %15, i64 0, i64 0
%68 = load i32, i32* %13
%69 = call i64 @memcmp(i8* %66, i8* %67, i32 %68)
%70 = icmp eq i64 %69, 0
br i1 %70, label %71, label %76
71: ; preds = %65
%72 = load i32, i32* %7
%73 = load i32, i32* %13
%74 = load i32, i32* @SEEK_CUR
%75 = call i32 @lseek(i32 %72, i32 %73, i32 %74)
br label %86
76: ; preds = %65
%77 = load i32, i32* %7
%78 = getelementptr inbounds [1024 x i8], [1024 x i8]* %14, i64 0, i64 0
%79 = load i32, i32* %13
%80 = call i32 @write(i32 %77, i8* %78, i32 %79)
%81 = load i32, i32* %13
%82 = icmp ne i32 %80, %81
br i1 %82, label %83, label %85
83: ; preds = %76
%84 = call i32 @pexit(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %85
85: ; preds = %83, %76
br label %86
86: ; preds = %85, %71
br label %55
87: ; preds = %55
%88 = load i32, i32* %6
%89 = call i32 @safeClose(i32 %88)
%90 = load i32, i32* %7
%91 = call i32 @safeClose(i32 %90)
%92 = load i32, i32* @EXIT_SUCCESS
ret i32 %92
}

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
@S_IROTH = global i32 0
@S_IWOTH = global i32 0
@SEEK_CUR = global i32 0
@.str.1 = [6 x i8] c"write\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1024 x i8]
%4 = alloca [1024 x i8]
%5 = icmp eq i32 %0, 3
br i1 %5, label %10, label %6
6: ; preds = %2
%7 = load i8*, i8** %1
%8 = load i32, i32* @EXIT_FAILURE
%9 = tail call i32 @helpAndLeave(i8* %7, i32 %8)
br label %10
10: ; preds = %6, %2
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
%15 = getelementptr inbounds i8*, i8** %1, i64 2
%16 = load i8*, i8** %15
%17 = load i32, i32* @O_RDONLY
%18 = load i32, i32* @O_WRONLY
%19 = load i32, i32* @O_CREAT
%20 = or i32 %19, %18
%21 = load i32, i32* @O_TRUNC
%22 = or i32 %20, %21
%23 = load i32, i32* @S_IRUSR
%24 = load i32, i32* @S_IWUSR
%25 = or i32 %24, %23
%26 = load i32, i32* @S_IRGRP
%27 = or i32 %25, %26
%28 = load i32, i32* @S_IWGRP
%29 = or i32 %27, %28
%30 = load i32, i32* @S_IROTH
%31 = or i32 %29, %30
%32 = load i32, i32* @S_IWOTH
%33 = or i32 %31, %32
%34 = tail call i32 (i8*, i32, ...) @safeOpen(i8* %14, i32 %17)
%35 = tail call i32 (i8*, i32, ...) @safeOpen(i8* %16, i32 %22, i32 %33)
%36 = call i32 @read(i32 %34, i8* %11, i32 1024)
%37 = icmp sgt i32 %36, 0
br i1 %37, label %38, label %53
38: ; preds = %10, %50
%39 = phi i32 [ %51, %50 ], [ %36, %10 ]
%40 = call i64 @memcmp(i8* %11, i8* %12, i32 %39)
%41 = icmp eq i64 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %38
%43 = load i32, i32* @SEEK_CUR
%44 = call i32 @lseek(i32 %35, i32 %39, i32 %43)
br label %50
45: ; preds = %38
%46 = call i32 @write(i32 %35, i8* %11, i32 %39)
%47 = icmp eq i32 %46, %39
br i1 %47, label %50, label %48
48: ; preds = %45
%49 = call i32 @pexit(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %50
50: ; preds = %45, %48, %42
%51 = call i32 @read(i32 %34, i8* %11, i32 1024)
%52 = icmp sgt i32 %51, 0
br i1 %52, label %38, label %53
53: ; preds = %50, %10
%54 = call i32 @safeClose(i32 %34)
%55 = call i32 @safeClose(i32 %35)
%56 = load i32, i32* @EXIT_SUCCESS
ret i32 %56
}