#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*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ *getenv(char *);
scalar_t__ line(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ rename(char *, char *);
int /*<<< orphan*/ run(char *, char *);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ tempfile(char *);
int /*<<< orphan*/ unlink(char *);
int main(int argc, char **argv) {
  char old[255];
  char new[255];
  char old_name[255];
  char new_name[255];
  FILE *old_fp;
  FILE *new_fp;
  if (getenv("EDITOR") == NULL) {
    printf("chen: $EDITOR is not set\n");
    exit(1);
  }
  tempfile(old);
  tempfile(new);
  run("ls -1 > %s", old);
  run("ls -1 > %s", new);
  run("$EDITOR %s", new);
  old_fp = fopen(old, "r");
  if (old_fp == NULL) {
    printf("chen: Failed to open file %s\n", old);
    exit(1);
  }
  new_fp = fopen(new, "r");
  if (new_fp == NULL) {
    printf("chen: Failed to open file %s\n", new);
    exit(1);
  }
  while (line(old_name, old_fp) && line(new_name, new_fp)) {
    if (strcmp(old_name, new_name)) {
      printf("%s -> %s\n", old_name, new_name);
      rename(old_name, new_name);
    }
  }
  fclose(old_fp);
  fclose(new_fp);
  unlink(old);
  unlink(new);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe30(%rip),%rdi
call   1030 <getenv@plt>
cmp    $0x0,%rax
jne    11f7 <main+0x47>
lea    0xe21(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    -0x110(%rbp),%rdi
call   1040 <tempfile@plt>
lea    -0x210(%rbp),%rdi
call   1040 <tempfile@plt>
lea    -0x110(%rbp),%rsi
lea    0xe04(%rip),%rdi
call   1070 <run@plt>
lea    -0x210(%rbp),%rsi
lea    0xdf1(%rip),%rdi
call   1070 <run@plt>
lea    -0x210(%rbp),%rsi
lea    0xde9(%rip),%rdi
call   1070 <run@plt>
lea    -0x110(%rbp),%rdi
lea    0xde1(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x418(%rbp)
cmpq   $0x0,-0x418(%rbp)
jne    128f <main+0xdf>
lea    -0x110(%rbp),%rsi
lea    0xdbb(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    -0x210(%rbp),%rdi
lea    0xd9a(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x420(%rbp)
cmpq   $0x0,-0x420(%rbp)
jne    12d6 <main+0x126>
lea    -0x210(%rbp),%rsi
lea    0xd74(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
jmp    12db <main+0x12b>
lea    -0x310(%rbp),%rdi
mov    -0x418(%rbp),%rsi
call   10c0 <line@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x421(%rbp)
je     1323 <main+0x173>
lea    -0x410(%rbp),%rdi
mov    -0x420(%rbp),%rsi
call   10c0 <line@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x421(%rbp)
mov    -0x421(%rbp),%al
test   $0x1,%al
jne    1336 <main+0x186>
jmp    1387 <main+0x1d7>
lea    -0x310(%rbp),%rdi
lea    -0x410(%rbp),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
je     1382 <main+0x1d2>
lea    -0x310(%rbp),%rsi
lea    -0x410(%rbp),%rdx
lea    0xcef(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
lea    -0x310(%rbp),%rdi
lea    -0x410(%rbp),%rsi
call   10b0 <rename@plt>
jmp    12db <main+0x12b>
mov    -0x418(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x420(%rbp),%rdi
call   1060 <fclose@plt>
lea    -0x110(%rbp),%rdi
call   1050 <unlink@plt>
lea    -0x210(%rbp),%rdi
call   1050 <unlink@plt>
xor    %eax,%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x400,%rsp
lea    0xe29(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1315 <main+0x155>
lea    0x100(%rsp),%r14
mov    %r14,%rdi
call   1040 <tempfile@plt>
mov    %rsp,%r15
mov    %r15,%rdi
call   1040 <tempfile@plt>
lea    0xe00(%rip),%rbx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1080 <run@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1080 <run@plt>
lea    0xdee(%rip),%rdi
mov    %r15,%rsi
call   1080 <run@plt>
lea    0xdea(%rip),%rsi
mov    %r14,%rdi
call   10b0 <fopen@plt>
test   %rax,%rax
je     132b <main+0x16b>
mov    %rax,%r14
lea    0xdcf(%rip),%rsi
mov    %rsp,%rdi
call   10b0 <fopen@plt>
test   %rax,%rax
je     133c <main+0x17c>
mov    %rax,%r15
lea    0x200(%rsp),%rdi
mov    %r14,%rsi
call   10d0 <line@plt>
test   %rax,%rax
je     12dd <main+0x11d>
lea    0x300(%rsp),%r12
lea    0x200(%rsp),%rbx
lea    0xdaf(%rip),%r13
jmp    12a0 <main+0xe0>
mov    %rbx,%rdi
mov    %r14,%rsi
call   10d0 <line@plt>
test   %rax,%rax
je     12dd <main+0x11d>
mov    %r12,%rdi
mov    %r15,%rsi
call   10d0 <line@plt>
test   %rax,%rax
je     12dd <main+0x11d>
mov    %rbx,%rdi
mov    %r12,%rsi
call   10a0 <strcmp@plt>
test   %rax,%rax
je     1290 <main+0xd0>
mov    %r13,%rdi
mov    %rbx,%rsi
mov    %r12,%rdx
xor    %eax,%eax
call   1090 <printf@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
call   10c0 <rename@plt>
jmp    1290 <main+0xd0>
mov    %r14,%rdi
call   1070 <fclose@plt>
mov    %r15,%rdi
call   1070 <fclose@plt>
lea    0x100(%rsp),%rdi
call   1050 <unlink@plt>
mov    %rsp,%rdi
call   1050 <unlink@plt>
xor    %eax,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
lea    0xd2b(%rip),%rdi
call   1060 <puts@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
lea    0xced(%rip),%rdi
lea    0x100(%rsp),%rsi
jmp    1346 <main+0x186>
lea    0xcdc(%rip),%rdi
mov    %rsp,%rsi
xor    %eax,%eax
call   1090 <printf@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"EDITOR\00"
@.str.1 = [26 x i8] c"chen: $EDITOR is not set\0A\00"
@.str.2 = [11 x i8] c"ls -1 > %s\00"
@.str.3 = [11 x i8] c"$EDITOR %s\00"
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [30 x i8] c"chen: Failed to open file %s\0A\00"
@.str.6 = [10 x i8] c"%s -> %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [255 x i8]
%7 = alloca [255 x i8]
%8 = alloca [255 x i8]
%9 = alloca [255 x i8]
%10 = alloca i32*
%11 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = call i32* @getenv(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %2
%18 = getelementptr inbounds [255 x i8], [255 x i8]* %6, i64 0, i64 0
%19 = call i32 @tempfile(i8* %18)
%20 = getelementptr inbounds [255 x i8], [255 x i8]* %7, i64 0, i64 0
%21 = call i32 @tempfile(i8* %20)
%22 = getelementptr inbounds [255 x i8], [255 x i8]* %6, i64 0, i64 0
%23 = call i32 @run(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* %22)
%24 = getelementptr inbounds [255 x i8], [255 x i8]* %7, i64 0, i64 0
%25 = call i32 @run(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* %24)
%26 = getelementptr inbounds [255 x i8], [255 x i8]* %7, i64 0, i64 0
%27 = call i32 @run(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* %26)
%28 = getelementptr inbounds [255 x i8], [255 x i8]* %6, i64 0, i64 0
%29 = call i32* @fopen(i8* %28, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %29, i32** %10
%30 = load i32*, i32** %10
%31 = icmp eq i32* %30, null
br i1 %31, label %32, label %36
32: ; preds = %17
%33 = getelementptr inbounds [255 x i8], [255 x i8]* %6, i64 0, i64 0
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0), i8* %33)
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %17
%37 = getelementptr inbounds [255 x i8], [255 x i8]* %7, i64 0, i64 0
%38 = call i32* @fopen(i8* %37, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %38, i32** %11
%39 = load i32*, i32** %11
%40 = icmp eq i32* %39, null
br i1 %40, label %41, label %45
41: ; preds = %36
%42 = getelementptr inbounds [255 x i8], [255 x i8]* %7, i64 0, i64 0
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0), i8* %42)
%44 = call i32 @exit(i32 1)
unreachable
45: ; preds = %36
br label %46
46: ; preds = %70, %45
%47 = getelementptr inbounds [255 x i8], [255 x i8]* %8, i64 0, i64 0
%48 = load i32*, i32** %10
%49 = call i64 @line(i8* %47, i32* %48)
%50 = icmp ne i64 %49, 0
br i1 %50, label %51, label %56
51: ; preds = %46
%52 = getelementptr inbounds [255 x i8], [255 x i8]* %9, i64 0, i64 0
%53 = load i32*, i32** %11
%54 = call i64 @line(i8* %52, i32* %53)
%55 = icmp ne i64 %54, 0
br label %56
56: ; preds = %51, %46
%57 = phi i1 [ false, %46 ], [ %55, %51 ]
br i1 %57, label %58, label %71
58: ; preds = %56
%59 = getelementptr inbounds [255 x i8], [255 x i8]* %8, i64 0, i64 0
%60 = getelementptr inbounds [255 x i8], [255 x i8]* %9, i64 0, i64 0
%61 = call i64 @strcmp(i8* %59, i8* %60)
%62 = icmp ne i64 %61, 0
br i1 %62, label %63, label %70
63: ; preds = %58
%64 = getelementptr inbounds [255 x i8], [255 x i8]* %8, i64 0, i64 0
%65 = getelementptr inbounds [255 x i8], [255 x i8]* %9, i64 0, i64 0
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0), i8* %64, i8* %65)
%67 = getelementptr inbounds [255 x i8], [255 x i8]* %8, i64 0, i64 0
%68 = getelementptr inbounds [255 x i8], [255 x i8]* %9, i64 0, i64 0
%69 = call i32 @rename(i8* %67, i8* %68)
br label %70
70: ; preds = %63, %58
br label %46
71: ; preds = %56
%72 = load i32*, i32** %10
%73 = call i32 @fclose(i32* %72)
%74 = load i32*, i32** %11
%75 = call i32 @fclose(i32* %74)
%76 = getelementptr inbounds [255 x i8], [255 x i8]* %6, i64 0, i64 0
%77 = call i32 @unlink(i8* %76)
%78 = getelementptr inbounds [255 x i8], [255 x i8]* %7, i64 0, i64 0
%79 = call i32 @unlink(i8* %78)
ret i32 0
}

LLVM IR (-O2):
@.str = [7 x i8] c"EDITOR\00"
@.str.2 = [11 x i8] c"ls -1 > %s\00"
@.str.3 = [11 x i8] c"$EDITOR %s\00"
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [30 x i8] c"chen: Failed to open file %s\0A\00"
@.str.6 = [10 x i8] c"%s -> %s\0A\00"
@str = [25 x i8] c"chen: $EDITOR is not set\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [255 x i8]
%4 = alloca [255 x i8]
%5 = alloca [255 x i8]
%6 = alloca [255 x i8]
%7 = getelementptr inbounds [255 x i8], [255 x i8]* %3, i64 0, i64 0
%8 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 0
%9 = getelementptr inbounds [255 x i8], [255 x i8]* %5, i64 0, i64 0
%10 = getelementptr inbounds [255 x i8], [255 x i8]* %6, i64 0, i64 0
%11 = tail call i32* @getenv(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %2
%17 = call i32 @tempfile(i8* %7)
%18 = call i32 @tempfile(i8* %8)
%19 = call i32 @run(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* %7)
%20 = call i32 @run(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* %8)
%21 = call i32 @run(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* %8)
%22 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %27
24: ; preds = %16
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0), i8* %7)
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %16
%28 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%29 = icmp eq i32* %28, null
br i1 %29, label %33, label %30
30: ; preds = %27
%31 = call i64 @line(i8* %9, i32* %22)
%32 = icmp eq i64 %31, 0
br i1 %32, label %48, label %36
33: ; preds = %27
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0), i8* %8)
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %30, %45
%37 = call i64 @line(i8* %10, i32* %28)
%38 = icmp eq i64 %37, 0
br i1 %38, label %48, label %39
39: ; preds = %36
%40 = call i64 @strcmp(i8* %9, i8* %10)
%41 = icmp eq i64 %40, 0
br i1 %41, label %45, label %42
42: ; preds = %39
%43 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0), i8* %9, i8* %10)
%44 = call i32 @rename(i8* %9, i8* %10)
br label %45
45: ; preds = %42, %39
%46 = call i64 @line(i8* %9, i32* %22)
%47 = icmp eq i64 %46, 0
br i1 %47, label %48, label %36
48: ; preds = %36, %45, %30
%49 = call i32 @fclose(i32* %22)
%50 = call i32 @fclose(i32* %28)
%51 = call i32 @unlink(i8* %7)
%52 = call i32 @unlink(i8* %8)
ret i32 0
}