#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 scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ O_CLOEXEC;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dprintf(int, char *, char *, char *, char *);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ execvp(char *, char **);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
int mkstemp(char *);
int movefile(char *, char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ pipe2(int *, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(char *, char *);
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ unlink(char *);
int /*<<< orphan*/ wait(int *);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  if (argc < 3) {
    fprintf(stderr, "Usage: %s file cmd [args]...\n", argv[0]);
    return 2;
  }
  int pipefd[2];
  pipe2(pipefd, O_CLOEXEC);
  char filename[18] = "apply_XXXXXX";
  int tmpfd = mkstemp(filename);
  if (tmpfd < 0) {
    strcpy(filename, "/tmp/apply_XXXXXX");
    tmpfd = mkstemp(filename);
  }
  if (tmpfd < 0) {
    fprintf(stderr, "%s: error creating temporary file: %s\n", argv[0],
            strerror(errno));
    return 1;
  }
  pid_t pid = fork();
  if (pid < 0) {
    fprintf(stderr, "%s: fork: %s\n", argv[0], strerror(errno));
    close(tmpfd);
    unlink(filename);
    return 1;
  } else if (pid > 0) {
    close(pipefd[1]);
    close(tmpfd);
    char readbuf[64];
    int success = 1;
    int count;
    while (count = read(pipefd[0], readbuf, 64)) {
      success = 0;
      write(2, readbuf, count);
    }
    if (success) {
      int retval;
      wait(&retval);
      if (retval == 0) {
        int movefile_result = movefile(filename, argv[1]);
        if (movefile_result < 0) {
          const char *guilty_filename =
              (movefile_result == -1) ? filename : argv[1];
          fprintf(stderr, "%s: %s: %s\n", argv[0], guilty_filename,
                  strerror(errno));
          unlink(filename);
          return 1;
        }
      } else {
        unlink(filename);
      }
      return retval;
    } else {
      close(tmpfd);
      unlink(filename);
      return 1;
    }
  } else {
    close(pipefd[0]);
    int infd = open(argv[1], O_RDONLY);
    if (infd < 0) {
      dprintf(pipefd[1], "%s: %s: %s\n", argv[0], argv[1], strerror(errno));
      return 1;
    }
    dup2(infd, 0);
    close(infd);
    dup2(tmpfd, 1);
    close(tmpfd);
    execvp(argv[2], argv + 2);
    dprintf(pipefd[1], "%s: %s: %s\n", argv[0], argv[2], strerror(errno));
    close(pipefd[1]);
    return 1;
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x110,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    124d <main+0x4d>
mov    0x2dae(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xdc6(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1661 <main+0x461>
lea    -0x18(%rbp),%rdi
mov    0x2d68(%rip),%rax
mov    (%rax),%esi
call   10f0 <pipe2@plt>
mov    0xe1a(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xe17(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0xe14(%rip),%ax
mov    %ax,-0x20(%rbp)
lea    -0x30(%rbp),%rdi
call   10c0 <mkstemp@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    12b2 <main+0xb2>
lea    -0x30(%rbp),%rdi
lea    0xd7d(%rip),%rsi
call   1040 <strcpy@plt>
lea    -0x30(%rbp),%rdi
call   10c0 <mkstemp@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    1311 <main+0x111>
mov    0x2d15(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xac(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0xa8(%rbp)
mov    0x2cf0(%rip),%rax
mov    (%rax),%edi
call   1100 <strerror@plt>
mov    -0xac(%rbp),%edi
mov    -0xa8(%rbp),%rdx
mov    %rax,%rcx
lea    0xd32(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1661 <main+0x461>
mov    $0x0,%al
call   1120 <fork@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jge    138d <main+0x18d>
mov    0x2caa(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xbc(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0xb8(%rbp)
mov    0x2c85(%rip),%rax
mov    (%rax),%edi
call   1100 <strerror@plt>
mov    -0xbc(%rbp),%edi
mov    -0xb8(%rbp),%rdx
mov    %rax,%rcx
lea    0xcee(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
mov    -0x34(%rbp),%edi
call   1080 <close@plt>
lea    -0x30(%rbp),%rdi
call   1030 <unlink@plt>
movl   $0x1,-0x4(%rbp)
jmp    1661 <main+0x461>
cmpq   $0x0,-0x40(%rbp)
jle    1525 <main+0x325>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
mov    -0x34(%rbp),%edi
call   1080 <close@plt>
movl   $0x1,-0x84(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x80(%rbp),%rsi
mov    $0x40,%edx
call   10a0 <read@plt>
mov    %eax,-0x88(%rbp)
cmp    $0x0,%eax
je     13f5 <main+0x1f5>
movl   $0x0,-0x84(%rbp)
lea    -0x80(%rbp),%rsi
mov    -0x88(%rbp),%edx
mov    $0x2,%edi
call   1050 <write@plt>
jmp    13b2 <main+0x1b2>
cmpl   $0x0,-0x84(%rbp)
je     1508 <main+0x308>
lea    -0x8c(%rbp),%rdi
call   1110 <wait@plt>
cmpl   $0x0,-0x8c(%rbp)
jne    14f1 <main+0x2f1>
lea    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
call   1060 <movefile@plt>
mov    %eax,-0x90(%rbp)
cmpl   $0x0,-0x90(%rbp)
jge    14ec <main+0x2ec>
cmpl   $0xffffffff,-0x90(%rbp)
jne    145c <main+0x25c>
lea    -0x30(%rbp),%rax
mov    %rax,-0xc8(%rbp)
jmp    146b <main+0x26b>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0xc8(%rbp)
mov    -0xc8(%rbp),%rax
mov    %rax,-0x98(%rbp)
mov    0x2b58(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xdc(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0xd8(%rbp)
mov    -0x98(%rbp),%rax
mov    %rax,-0xd0(%rbp)
mov    0x2b25(%rip),%rax
mov    (%rax),%edi
call   1100 <strerror@plt>
mov    -0xdc(%rbp),%edi
mov    -0xd8(%rbp),%rdx
mov    -0xd0(%rbp),%rcx
mov    %rax,%r8
lea    0xb95(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
lea    -0x30(%rbp),%rdi
call   1030 <unlink@plt>
movl   $0x1,-0x4(%rbp)
jmp    1661 <main+0x461>
jmp    14fa <main+0x2fa>
lea    -0x30(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x8c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1661 <main+0x461>
mov    -0x34(%rbp),%edi
call   1080 <close@plt>
lea    -0x30(%rbp),%rdi
call   1030 <unlink@plt>
movl   $0x1,-0x4(%rbp)
jmp    1661 <main+0x461>
mov    -0x18(%rbp),%edi
call   1080 <close@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2aac(%rip),%rax
mov    (%rax),%esi
call   10d0 <open@plt>
mov    %eax,-0x9c(%rbp)
cmpl   $0x0,-0x9c(%rbp)
jge    15b9 <main+0x3b9>
mov    -0x14(%rbp),%eax
mov    %eax,-0xf4(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0xf0(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0xe8(%rbp)
mov    0x2a4d(%rip),%rax
mov    (%rax),%edi
call   1100 <strerror@plt>
mov    -0xf4(%rbp),%edi
mov    -0xf0(%rbp),%rdx
mov    -0xe8(%rbp),%rcx
mov    %rax,%r8
lea    0xabd(%rip),%rsi
call   1090 <dprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1661 <main+0x461>
mov    -0x9c(%rbp),%edi
xor    %esi,%esi
call   1070 <dup2@plt>
mov    -0x9c(%rbp),%edi
call   1080 <close@plt>
mov    -0x34(%rbp),%edi
mov    $0x1,%esi
call   1070 <dup2@plt>
mov    -0x34(%rbp),%edi
call   1080 <close@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    -0x10(%rbp),%rsi
add    $0x10,%rsi
call   10e0 <execvp@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x10c(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x108(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x100(%rbp)
mov    0x29a8(%rip),%rax
mov    (%rax),%edi
call   1100 <strerror@plt>
mov    -0x10c(%rbp),%edi
mov    -0x108(%rbp),%rdx
mov    -0x100(%rbp),%rcx
mov    %rax,%r8
lea    0xa18(%rip),%rsi
call   1090 <dprintf@plt>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x110,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x78,%rsp
mov    %rsi,%r14
cmp    $0x2,%edi
jg     1236 <main+0x36>
mov    0x2dbf(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
lea    0xddb(%rip),%rsi
xor    %eax,%eax
call   10b0 <fprintf@plt>
mov    $0x2,%ebx
jmp    148d <main+0x28d>
mov    0x2d83(%rip),%rax
mov    (%rax),%esi
lea    0x4(%rsp),%rdi
call   10f0 <pipe2@plt>
movaps 0xe30(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movw   $0x0,0x20(%rsp)
lea    0x10(%rsp),%rdi
call   10c0 <mkstemp@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1292 <main+0x92>
lea    0xdab(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   1040 <strcpy@plt>
mov    %rbx,%rdi
call   10c0 <mkstemp@plt>
mov    %eax,%ebp
test   %eax,%eax
js     13e9 <main+0x1e9>
xor    %eax,%eax
call   1120 <fork@plt>
test   %rax,%rax
js     1306 <main+0x106>
je     1351 <main+0x151>
mov    0x8(%rsp),%edi
call   1080 <close@plt>
mov    %ebp,%edi
call   1080 <close@plt>
mov    0x4(%rsp),%edi
lea    0x30(%rsp),%rsi
mov    $0x40,%edx
call   10a0 <read@plt>
test   %eax,%eax
je     1420 <main+0x220>
lea    0x30(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    $0x2,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1050 <write@plt>
mov    0x4(%rsp),%edi
mov    %rbx,%rsi
mov    $0x40,%edx
call   10a0 <read@plt>
test   %eax,%eax
jne    12e0 <main+0xe0>
jmp    1336 <main+0x136>
mov    0x2ccb(%rip),%rax
mov    (%rax),%ebx
mov    (%r14),%r14
mov    0x2cb7(%rip),%rax
mov    (%rax),%edi
call   1100 <strerror@plt>
lea    0xd30(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   10b0 <fprintf@plt>
mov    %ebp,%edi
call   1080 <close@plt>
lea    0x10(%rsp),%rdi
call   1030 <unlink@plt>
mov    $0x1,%ebx
jmp    148d <main+0x28d>
mov    0x4(%rsp),%edi
call   1080 <close@plt>
mov    0x8(%r14),%rdi
mov    0x2c83(%rip),%rax
mov    (%rax),%esi
call   10d0 <open@plt>
test   %eax,%eax
js     143d <main+0x23d>
mov    %eax,%ebx
mov    %eax,%edi
xor    %esi,%esi
call   1070 <dup2@plt>
mov    %ebx,%edi
call   1080 <close@plt>
mov    $0x1,%ebx
mov    %ebp,%edi
mov    $0x1,%esi
call   1070 <dup2@plt>
mov    %ebp,%edi
call   1080 <close@plt>
lea    0x10(%r14),%rsi
mov    0x10(%r14),%rdi
call   10e0 <execvp@plt>
mov    0x8(%rsp),%ebp
mov    (%r14),%r15
mov    0x10(%r14),%r14
mov    0x2c13(%rip),%rax
mov    (%rax),%edi
call   1100 <strerror@plt>
lea    0xc9a(%rip),%rsi
mov    %ebp,%edi
mov    %r15,%rdx
mov    %r14,%rcx
mov    %rax,%r8
call   1090 <dprintf@plt>
mov    0x8(%rsp),%edi
call   1080 <close@plt>
jmp    148d <main+0x28d>
mov    0x2be8(%rip),%rax
mov    (%rax),%ebx
mov    (%r14),%rbp
mov    0x2bd4(%rip),%rax
mov    (%rax),%edi
call   1100 <strerror@plt>
lea    0xc26(%rip),%rsi
mov    %ebx,%edi
mov    %rbp,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   10b0 <fprintf@plt>
mov    $0x1,%ebx
jmp    148d <main+0x28d>
lea    0xc(%rsp),%rdi
call   1110 <wait@plt>
cmpl   $0x0,0xc(%rsp)
je     1474 <main+0x274>
lea    0x10(%rsp),%rdi
call   1030 <unlink@plt>
jmp    1489 <main+0x289>
mov    0x8(%rsp),%ebx
mov    (%r14),%rbp
mov    0x8(%r14),%r14
mov    0x2b81(%rip),%rax
mov    (%rax),%edi
call   1100 <strerror@plt>
lea    0xc08(%rip),%rsi
mov    %ebx,%edi
mov    %rbp,%rdx
mov    %r14,%rcx
mov    %rax,%r8
call   1090 <dprintf@plt>
mov    $0x1,%ebx
jmp    148d <main+0x28d>
mov    0x8(%r14),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   1060 <movefile@plt>
test   %eax,%eax
js     149a <main+0x29a>
mov    0xc(%rsp),%ebx
mov    %ebx,%eax
add    $0x78,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
cmp    $0xffffffff,%eax
je     14a3 <main+0x2a3>
mov    0x8(%r14),%rbx
mov    0x2b2e(%rip),%rax
mov    (%rax),%ebp
mov    (%r14),%r14
mov    0x2b1a(%rip),%rax
mov    (%rax),%edi
call   1100 <strerror@plt>
lea    0xba1(%rip),%rsi
mov    %ebp,%edi
mov    %r14,%rdx
mov    %rbx,%rcx
mov    %rax,%r8
xor    %eax,%eax
call   10b0 <fprintf@plt>
jmp    133d <main+0x13d>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [30 x i8] c"Usage: %s file cmd [args]...\0A\00"
@O_CLOEXEC = global i32 0
@__const.main.filename = [18 x i8] c"apply_XXXXXX\00\00\00\00\00\00"
@.str.1 = [18 x i8] c"/tmp/apply_XXXXXX\00"
@.str.2 = [39 x i8] c"%s: error creating temporary file: %s\0A\00"
@errno = global i32 0
@.str.3 = [14 x i8] c"%s: fork: %s\0A\00"
@.str.4 = [12 x i8] c"%s: %s: %s\0A\00"
@O_RDONLY = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [2 x i32]
%7 = alloca [18 x i8]
%8 = alloca i32
%9 = alloca i64
%10 = alloca [64 x i8]
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i8*
%16 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%17 = load i32, i32* %4
%18 = icmp slt i32 %17, 3
br i1 %18, label %19, label %25
19: ; preds = %2
%20 = load i32, i32* @stderr
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 0
%23 = load i8*, i8** %22
%24 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %23)
store i32 2, i32* %3
br label %184
25: ; preds = %2
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%27 = load i32, i32* @O_CLOEXEC
%28 = call i32 @pipe2(i32* %26, i32 %27)
%29 = bitcast [18 x i8]* %7 to i8*
%30 = getelementptr inbounds [18 x i8], [18 x i8]* %7, i64 0, i64 0
%31 = call i32 @mkstemp(i8* %30)
store i32 %31, i32* %8
%32 = load i32, i32* %8
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %39
34: ; preds = %25
%35 = getelementptr inbounds [18 x i8], [18 x i8]* %7, i64 0, i64 0
%36 = call i32 @strcpy(i8* %35, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%37 = getelementptr inbounds [18 x i8], [18 x i8]* %7, i64 0, i64 0
%38 = call i32 @mkstemp(i8* %37)
store i32 %38, i32* %8
br label %39
39: ; preds = %34, %25
%40 = load i32, i32* %8
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %50
42: ; preds = %39
%43 = load i32, i32* @stderr
%44 = load i8**, i8*** %5
%45 = getelementptr inbounds i8*, i8** %44, i64 0
%46 = load i8*, i8** %45
%47 = load i32, i32* @errno
%48 = call i8* @strerror(i32 %47)
%49 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i8* %46, i8* %48)
store i32 1, i32* %3
br label %184
50: ; preds = %39
%51 = call i64 (...) @fork()
store i64 %51, i64* %9
%52 = load i64, i64* %9
%53 = icmp slt i64 %52, 0
br i1 %53, label %54, label %66
54: ; preds = %50
%55 = load i32, i32* @stderr
%56 = load i8**, i8*** %5
%57 = getelementptr inbounds i8*, i8** %56, i64 0
%58 = load i8*, i8** %57
%59 = load i32, i32* @errno
%60 = call i8* @strerror(i32 %59)
%61 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %55, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i8* %58, i8* %60)
%62 = load i32, i32* %8
%63 = call i32 @close(i32 %62)
%64 = getelementptr inbounds [18 x i8], [18 x i8]* %7, i64 0, i64 0
%65 = call i32 @unlink(i8* %64)
store i32 1, i32* %3
br label %184
66: ; preds = %50
%67 = load i64, i64* %9
%68 = icmp sgt i64 %67, 0
br i1 %68, label %69, label %132
69: ; preds = %66
%70 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%71 = load i32, i32* %70
%72 = call i32 @close(i32 %71)
%73 = load i32, i32* %8
%74 = call i32 @close(i32 %73)
store i32 1, i32* %11
br label %75
75: ; preds = %81, %69
%76 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%77 = load i32, i32* %76
%78 = getelementptr inbounds [64 x i8], [64 x i8]* %10, i64 0, i64 0
%79 = call i32 @read(i32 %77, i8* %78, i32 64)
store i32 %79, i32* %12
%80 = icmp ne i32 %79, 0
br i1 %80, label %81, label %85
81: ; preds = %75
store i32 0, i32* %11
%82 = getelementptr inbounds [64 x i8], [64 x i8]* %10, i64 0, i64 0
%83 = load i32, i32* %12
%84 = call i32 @write(i32 2, i8* %82, i32 %83)
br label %75
85: ; preds = %75
%86 = load i32, i32* %11
%87 = icmp ne i32 %86, 0
br i1 %87, label %88, label %127
88: ; preds = %85
%89 = call i32 @wait(i32* %13)
%90 = load i32, i32* %13
%91 = icmp eq i32 %90, 0
br i1 %91, label %92, label %122
92: ; preds = %88
%93 = getelementptr inbounds [18 x i8], [18 x i8]* %7, i64 0, i64 0
%94 = load i8**, i8*** %5
%95 = getelementptr inbounds i8*, i8** %94, i64 1
%96 = load i8*, i8** %95
%97 = call i32 @movefile(i8* %93, i8* %96)
store i32 %97, i32* %14
%98 = load i32, i32* %14
%99 = icmp slt i32 %98, 0
br i1 %99, label %100, label %121
100: ; preds = %92
%101 = load i32, i32* %14
%102 = icmp eq i32 %101, -1
br i1 %102, label %103, label %105
103: ; preds = %100
%104 = getelementptr inbounds [18 x i8], [18 x i8]* %7, i64 0, i64 0
br label %109
105: ; preds = %100
%106 = load i8**, i8*** %5
%107 = getelementptr inbounds i8*, i8** %106, i64 1
%108 = load i8*, i8** %107
br label %109
109: ; preds = %105, %103
%110 = phi i8* [ %104, %103 ], [ %108, %105 ]
store i8* %110, i8** %15
%111 = load i32, i32* @stderr
%112 = load i8**, i8*** %5
%113 = getelementptr inbounds i8*, i8** %112, i64 0
%114 = load i8*, i8** %113
%115 = load i8*, i8** %15
%116 = load i32, i32* @errno
%117 = call i8* @strerror(i32 %116)
%118 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %111, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i8* %114, i8* %115, i8* %117)
%119 = getelementptr inbounds [18 x i8], [18 x i8]* %7, i64 0, i64 0
%120 = call i32 @unlink(i8* %119)
store i32 1, i32* %3
br label %184
121: ; preds = %92
br label %125
122: ; preds = %88
%123 = getelementptr inbounds [18 x i8], [18 x i8]* %7, i64 0, i64 0
%124 = call i32 @unlink(i8* %123)
br label %125
125: ; preds = %122, %121
%126 = load i32, i32* %13
store i32 %126, i32* %3
br label %184
127: ; preds = %85
%128 = load i32, i32* %8
%129 = call i32 @close(i32 %128)
%130 = getelementptr inbounds [18 x i8], [18 x i8]* %7, i64 0, i64 0
%131 = call i32 @unlink(i8* %130)
store i32 1, i32* %3
br label %184
132: ; preds = %66
%133 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%134 = load i32, i32* %133
%135 = call i32 @close(i32 %134)
%136 = load i8**, i8*** %5
%137 = getelementptr inbounds i8*, i8** %136, i64 1
%138 = load i8*, i8** %137
%139 = load i32, i32* @O_RDONLY
%140 = call i32 @open(i8* %138, i32 %139)
store i32 %140, i32* %16
%141 = load i32, i32* %16
%142 = icmp slt i32 %141, 0
br i1 %142, label %143, label %155
143: ; preds = %132
%144 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%145 = load i32, i32* %144
%146 = load i8**, i8*** %5
%147 = getelementptr inbounds i8*, i8** %146, i64 0
%148 = load i8*, i8** %147
%149 = load i8**, i8*** %5
%150 = getelementptr inbounds i8*, i8** %149, i64 1
%151 = load i8*, i8** %150
%152 = load i32, i32* @errno
%153 = call i8* @strerror(i32 %152)
%154 = call i32 @dprintf(i32 %145, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i8* %148, i8* %151, i8* %153)
store i32 1, i32* %3
br label %184
155: ; preds = %132
%156 = load i32, i32* %16
%157 = call i32 @dup2(i32 %156, i32 0)
%158 = load i32, i32* %16
%159 = call i32 @close(i32 %158)
%160 = load i32, i32* %8
%161 = call i32 @dup2(i32 %160, i32 1)
%162 = load i32, i32* %8
%163 = call i32 @close(i32 %162)
%164 = load i8**, i8*** %5
%165 = getelementptr inbounds i8*, i8** %164, i64 2
%166 = load i8*, i8** %165
%167 = load i8**, i8*** %5
%168 = getelementptr inbounds i8*, i8** %167, i64 2
%169 = call i32 @execvp(i8* %166, i8** %168)
%170 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%171 = load i32, i32* %170
%172 = load i8**, i8*** %5
%173 = getelementptr inbounds i8*, i8** %172, i64 0
%174 = load i8*, i8** %173
%175 = load i8**, i8*** %5
%176 = getelementptr inbounds i8*, i8** %175, i64 2
%177 = load i8*, i8** %176
%178 = load i32, i32* @errno
%179 = call i8* @strerror(i32 %178)
%180 = call i32 @dprintf(i32 %171, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i8* %174, i8* %177, i8* %179)
%181 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%182 = load i32, i32* %181
%183 = call i32 @close(i32 %182)
store i32 1, i32* %3
br label %184
184: ; preds = %155, %143, %127, %125, %109, %54, %42, %19
%185 = load i32, i32* %3
ret i32 %185
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [30 x i8] c"Usage: %s file cmd [args]...\0A\00"
@O_CLOEXEC = global i32 0
@__const.main.filename = [18 x i8] c"apply_XXXXXX\00\00\00\00\00\00"
@.str.1 = [18 x i8] c"/tmp/apply_XXXXXX\00"
@.str.2 = [39 x i8] c"%s: error creating temporary file: %s\0A\00"
@errno = global i32 0
@.str.3 = [14 x i8] c"%s: fork: %s\0A\00"
@.str.4 = [12 x i8] c"%s: %s: %s\0A\00"
@O_RDONLY = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca [2 x i32]
%4 = alloca [18 x i8]
%5 = alloca [64 x i8]
%6 = alloca i32
%7 = icmp slt i32 %0, 3
br i1 %7, label %8, label %12
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = load i8*, i8** %1
%11 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %10)
br label %127
12: ; preds = %2
%13 = bitcast [2 x i32]* %3 to i8*
%14 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%15 = load i32, i32* @O_CLOEXEC
%16 = call i32 @pipe2(i32* %14, i32 %15)
%17 = getelementptr inbounds [18 x i8], [18 x i8]* %4, i64 0, i64 0
%18 = call i32 @mkstemp(i8* %17)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %30
20: ; preds = %12
%21 = call i32 @strcpy(i8* %17, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%22 = call i32 @mkstemp(i8* %17)
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %30
24: ; preds = %20
%25 = load i32, i32* @stderr
%26 = load i8*, i8** %1
%27 = load i32, i32* @errno
%28 = call i8* @strerror(i32 %27)
%29 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i8* %26, i8* %28)
br label %125
30: ; preds = %12, %20
%31 = phi i32 [ %22, %20 ], [ %18, %12 ]
%32 = call i64 (...) @fork()
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %42
34: ; preds = %30
%35 = load i32, i32* @stderr
%36 = load i8*, i8** %1
%37 = load i32, i32* @errno
%38 = call i8* @strerror(i32 %37)
%39 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i8* %36, i8* %38)
%40 = call i32 @close(i32 %31)
%41 = call i32 @unlink(i8* %17)
br label %125
42: ; preds = %30
%43 = icmp eq i64 %32, 0
br i1 %43, label %92, label %44
44: ; preds = %42
%45 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%46 = load i32, i32* %45
%47 = call i32 @close(i32 %46)
%48 = call i32 @close(i32 %31)
%49 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%50 = load i32, i32* %14
%51 = call i32 @read(i32 %50, i8* %49, i32 64)
%52 = icmp eq i32 %51, 0
br i1 %52, label %59, label %53
53: ; preds = %44, %53
%54 = phi i32 [ %57, %53 ], [ %51, %44 ]
%55 = call i32 @write(i32 2, i8* %49, i32 %54)
%56 = load i32, i32* %14
%57 = call i32 @read(i32 %56, i8* %49, i32 64)
%58 = icmp eq i32 %57, 0
br i1 %58, label %87, label %53
59: ; preds = %44
%60 = bitcast i32* %6 to i8*
%61 = call i32 @wait(i32* %6)
%62 = load i32, i32* %6
%63 = icmp eq i32 %62, 0
br i1 %63, label %64, label %81
64: ; preds = %59
%65 = getelementptr inbounds i8*, i8** %1, i64 1
%66 = load i8*, i8** %65
%67 = call i32 @movefile(i8* %17, i8* %66)
%68 = icmp slt i32 %67, 0
br i1 %68, label %69, label %83
69: ; preds = %64
%70 = icmp eq i32 %67, -1
br i1 %70, label %73, label %71
71: ; preds = %69
%72 = load i8*, i8** %65
br label %73
73: ; preds = %71, %69
%74 = phi i8* [ %72, %71 ], [ %17, %69 ]
%75 = load i32, i32* @stderr
%76 = load i8*, i8** %1
%77 = load i32, i32* @errno
%78 = call i8* @strerror(i32 %77)
%79 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %75, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i8* %76, i8* %74, i8* %78)
%80 = call i32 @unlink(i8* %17)
br label %85
81: ; preds = %59
%82 = call i32 @unlink(i8* %17)
br label %83
83: ; preds = %64, %81
%84 = load i32, i32* %6
br label %85
85: ; preds = %73, %83
%86 = phi i32 [ %84, %83 ], [ 1, %73 ]
br label %90
87: ; preds = %53
%88 = call i32 @close(i32 %31)
%89 = call i32 @unlink(i8* %17)
br label %90
90: ; preds = %87, %85
%91 = phi i32 [ %86, %85 ], [ 1, %87 ]
br label %125
92: ; preds = %42
%93 = load i32, i32* %14
%94 = call i32 @close(i32 %93)
%95 = getelementptr inbounds i8*, i8** %1, i64 1
%96 = load i8*, i8** %95
%97 = load i32, i32* @O_RDONLY
%98 = call i32 @open(i8* %96, i32 %97)
%99 = icmp slt i32 %98, 0
br i1 %99, label %100, label %108
100: ; preds = %92
%101 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%102 = load i32, i32* %101
%103 = load i8*, i8** %1
%104 = load i8*, i8** %95
%105 = load i32, i32* @errno
%106 = call i8* @strerror(i32 %105)
%107 = call i32 @dprintf(i32 %102, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i8* %103, i8* %104, i8* %106)
br label %125
108: ; preds = %92
%109 = call i32 @dup2(i32 %98, i32 0)
%110 = call i32 @close(i32 %98)
%111 = call i32 @dup2(i32 %31, i32 1)
%112 = call i32 @close(i32 %31)
%113 = getelementptr inbounds i8*, i8** %1, i64 2
%114 = load i8*, i8** %113
%115 = call i32 @execvp(i8* %114, i8** %113)
%116 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%117 = load i32, i32* %116
%118 = load i8*, i8** %1
%119 = load i8*, i8** %113
%120 = load i32, i32* @errno
%121 = call i8* @strerror(i32 %120)
%122 = call i32 @dprintf(i32 %117, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i8* %118, i8* %119, i8* %121)
%123 = load i32, i32* %116
%124 = call i32 @close(i32 %123)
br label %125
125: ; preds = %34, %90, %108, %100, %24
%126 = phi i32 [ 1, %24 ], [ 1, %34 ], [ %91, %90 ], [ 1, %108 ], [ 1, %100 ]
br label %127
127: ; preds = %125, %8
%128 = phi i32 [ 2, %8 ], [ %126, %125 ]
ret i32 %128
}