#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*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ copy(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ mktemp(char *);
int /*<<< orphan*/ rename(char *, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ unlink(char *);
main(int argc, char *argv[]) {
  FILE *ffp, *tfp;
  char *ffile, tfile[20];
  strcpy(tfile, "tfXXXXXX");
  mktemp(tfile);
  while (--argc) {
    if (NULL == (ffp = fopen(ffile = *++argv, "r"))) {
      fprintf(stderr, "Can't open %s\n", ffile);
      continue;
    }
    if (NULL == (tfp = fopen(tfile, "w"))) {
      fprintf(stderr, "Can't create %s\n", tfile);
      fclose(ffp);
      continue;
    }
    copy(ffp, tfp);
    fclose(ffp);
    fclose(tfp);
    unlink(ffile);
    rename(tfile, ffile);
  }
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x40(%rbp),%rdi
lea    0xe4f(%rip),%rsi
call   1040 <strcpy@plt>
lea    -0x40(%rbp),%rdi
call   10b0 <mktemp@plt>
mov    -0x8(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
je     12a7 <main+0x117>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x8,%rcx
mov    %rcx,-0x10(%rbp)
mov    0x8(%rax),%rdi
mov    %rdi,-0x28(%rbp)
lea    0xe1a(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%rcx
mov    %rcx,-0x18(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
jne    1224 <main+0x94>
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rdx
lea    0xdf1(%rip),%rsi
call   1070 <fprintf@plt>
jmp    11bf <main+0x2f>
lea    -0x40(%rbp),%rdi
lea    0xdeb(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%rcx
mov    %rcx,-0x20(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
jne    126d <main+0xdd>
mov    0x2d93(%rip),%rax
mov    (%rax),%edi
lea    -0x40(%rbp),%rdx
lea    0xdc2(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
jmp    11bf <main+0x2f>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1060 <copy@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1030 <unlink@plt>
lea    -0x40(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1090 <rename@plt>
jmp    11bf <main+0x2f>
xor    %edi,%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,0x8(%rsp)
mov    %edi,%ebp
lea    0xe54(%rip),%rsi
lea    0x10(%rsp),%r14
mov    %r14,%rdi
call   1040 <strcpy@plt>
mov    %r14,%rdi
call   10b0 <mktemp@plt>
cmp    $0x1,%ebp
jne    11cd <main+0x3d>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    %ebp,%r14d
mov    $0x1,%r13d
lea    0x10(%rsp),%rbx
jmp    1201 <main+0x71>
nopl   (%rax)
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
lea    0xe1b(%rip),%rsi
mov    %rbp,%rdx
call   1070 <fprintf@plt>
add    $0x1,%r13
cmp    %r13d,%r14d
je     11c6 <main+0x36>
mov    0x8(%rsp),%rax
mov    (%rax,%r13,8),%rbp
mov    %rbp,%rdi
lea    0xdf5(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11e0 <main+0x50>
mov    %rax,%r15
mov    %rbx,%rdi
lea    0xdef(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1270 <main+0xe0>
mov    %rax,%r12
mov    %r15,%rdi
mov    %rax,%rsi
call   1060 <copy@plt>
mov    %r15,%rdi
call   1050 <fclose@plt>
mov    %r12,%rdi
call   1050 <fclose@plt>
mov    %rbp,%rdi
call   1030 <unlink@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1090 <rename@plt>
jmp    11f8 <main+0x68>
nopl   0x0(%rax,%rax,1)
mov    0x2d69(%rip),%rax
mov    (%rax),%edi
lea    0xd9c(%rip),%rsi
mov    %rbx,%rdx
call   1070 <fprintf@plt>
mov    %r15,%rdi
call   1050 <fclose@plt>
jmp    11f8 <main+0x68>

LLVM IR (-O0):
@.str = [9 x i8] c"tfXXXXXX\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [15 x i8] c"Can't open %s\0A\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [17 x i8] c"Can't create %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i8*
%9 = alloca [20 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = getelementptr inbounds [20 x i8], [20 x i8]* %9, i64 0, i64 0
%11 = call i32 @strcpy(i8* %10, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%12 = getelementptr inbounds [20 x i8], [20 x i8]* %9, i64 0, i64 0
%13 = call i32 @mktemp(i8* %12)
br label %14
14: ; preds = %38, %32, %24, %2
%15 = load i32, i32* %4
%16 = add nsw i32 %15, -1
store i32 %16, i32* %4
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %51
18: ; preds = %14
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i32 1
store i8** %20, i8*** %5
%21 = load i8*, i8** %20
store i8* %21, i8** %8
%22 = call i32* @fopen(i8* %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %22, i32** %6
%23 = icmp eq i32* null, %22
br i1 %23, label %24, label %28
24: ; preds = %18
%25 = load i32, i32* @stderr
%26 = load i8*, i8** %8
%27 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %26)
br label %14
28: ; preds = %18
%29 = getelementptr inbounds [20 x i8], [20 x i8]* %9, i64 0, i64 0
%30 = call i32* @fopen(i8* %29, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %30, i32** %7
%31 = icmp eq i32* null, %30
br i1 %31, label %32, label %38
32: ; preds = %28
%33 = load i32, i32* @stderr
%34 = getelementptr inbounds [20 x i8], [20 x i8]* %9, i64 0, i64 0
%35 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i8* %34)
%36 = load i32*, i32** %6
%37 = call i32 @fclose(i32* %36)
br label %14
38: ; preds = %28
%39 = load i32*, i32** %6
%40 = load i32*, i32** %7
%41 = call i32 @copy(i32* %39, i32* %40)
%42 = load i32*, i32** %6
%43 = call i32 @fclose(i32* %42)
%44 = load i32*, i32** %7
%45 = call i32 @fclose(i32* %44)
%46 = load i8*, i8** %8
%47 = call i32 @unlink(i8* %46)
%48 = getelementptr inbounds [20 x i8], [20 x i8]* %9, i64 0, i64 0
%49 = load i8*, i8** %8
%50 = call i32 @rename(i8* %48, i8* %49)
br label %14
51: ; preds = %14
%52 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [9 x i8] c"tfXXXXXX\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [15 x i8] c"Can't open %s\0A\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [17 x i8] c"Can't create %s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [20 x i8]
%4 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 0
%5 = call i32 @strcpy(i8* %4, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%6 = call i32 @mktemp(i8* %4)
%7 = add nsw i32 %0, -1
%8 = icmp eq i32 %7, 0
br i1 %8, label %35, label %9
9: ; preds = %2, %19
%10 = phi i32 [ %20, %19 ], [ %7, %2 ]
%11 = phi i8** [ %12, %19 ], [ %1, %2 ]
%12 = getelementptr inbounds i8*, i8** %11, i64 1
%13 = load i8*, i8** %12
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %22
16: ; preds = %9
%17 = load i32, i32* @stderr
%18 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %13)
br label %19
19: ; preds = %16, %25, %29
%20 = add nsw i32 %10, -1
%21 = icmp eq i32 %20, 0
br i1 %21, label %35, label %9
22: ; preds = %9
%23 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %29
25: ; preds = %22
%26 = load i32, i32* @stderr
%27 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i8* %4)
%28 = call i32 @fclose(i32* %14)
br label %19
29: ; preds = %22
%30 = call i32 @copy(i32* %14, i32* %23)
%31 = call i32 @fclose(i32* %14)
%32 = call i32 @fclose(i32* %23)
%33 = call i32 @unlink(i8* %13)
%34 = call i32 @rename(i8* %4, i8* %13)
br label %19
35: ; preds = %19, %2
%36 = call i32 @exit(i32 0)
unreachable
}