#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*/ pid_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ die(char *);
int /*<<< orphan*/ execlp(char *, char *, char *, char *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fork();
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
char *getenv(char *);
scalar_t__ isatty(int /*<<< orphan*/);
int /*<<< orphan*/ *raw_open(char *, char *);
int /*<<< orphan*/ release(int /*<<< orphan*/ *);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ waitpid(int /*<<< orphan*/, int /*<<< orphan*/ *,
                           int /*<<< orphan*/);
void ntx_editor(char *file) {
  if (isatty(STDIN_FILENO)) {
    char *editor = getenv("EDITOR");
    pid_t child;
    if (editor) {
      if ((child = fork()) == 0)
        execlp(editor, editor, file, (char *)NULL);
      else
        waitpid(child, NULL, 0);
    } else
      die("The environment variable EDITOR is unset.\n");
  } else {
    FILE *f = raw_open(file, "w");
    char buffer[8096];
    while (fgets(buffer, 8096, stdin))
      fputs(buffer, f);
    release(f);
  }
}


x86 assembly (clang, -O0):
<ntx_editor>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1fc0,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
call   1040 <isatty@plt>
cmp    $0x0,%rax
je     1233 <ntx_editor+0x93>
lea    0xe32(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1222 <ntx_editor+0x82>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jne    120f <ntx_editor+0x6f>
mov    -0x10(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x8(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   10b0 <execlp@plt>
jmp    121d <ntx_editor+0x7d>
mov    -0x14(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1080 <waitpid@plt>
jmp    122e <ntx_editor+0x8e>
lea    0xdde(%rip),%rdi
call   1090 <die@plt>
jmp    1289 <ntx_editor+0xe9>
mov    -0x8(%rbp),%rdi
lea    0xdf4(%rip),%rsi
call   1070 <raw_open@plt>
mov    %rax,-0x20(%rbp)
lea    -0x1fc0(%rbp),%rdi
mov    0x2d93(%rip),%rax
mov    (%rax),%edx
mov    $0x1fa0,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     1280 <ntx_editor+0xe0>
lea    -0x1fc0(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1050 <fputs@plt>
jmp    1247 <ntx_editor+0xa7>
mov    -0x20(%rbp),%rdi
call   10a0 <release@plt>
add    $0x1fc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ntx_editor>:
push   %r15
push   %r14
push   %rbx
sub    $0x1fa0,%rsp
mov    %rdi,%r14
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
call   1040 <isatty@plt>
test   %rax,%rax
je     1200 <ntx_editor+0x60>
lea    0xe37(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1275 <ntx_editor+0xd5>
mov    %rax,%rbx
xor    %eax,%eax
call   10c0 <fork@plt>
test   %eax,%eax
je     128d <ntx_editor+0xed>
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
add    $0x1fa0,%rsp
pop    %rbx
pop    %r14
pop    %r15
jmp    1080 <waitpid@plt>
lea    0xe2b(%rip),%rsi
mov    %r14,%rdi
call   1070 <raw_open@plt>
mov    %rax,%r14
mov    0x2dcf(%rip),%r15
mov    (%r15),%edx
mov    %rsp,%rdi
mov    $0x1fa0,%esi
call   1060 <fgets@plt>
test   %rax,%rax
je     1260 <ntx_editor+0xc0>
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r14,%rsi
call   1050 <fputs@plt>
mov    (%r15),%edx
mov    %rbx,%rdi
mov    $0x1fa0,%esi
call   1060 <fgets@plt>
test   %rax,%rax
jne    1240 <ntx_editor+0xa0>
mov    %r14,%rdi
call   10a0 <release@plt>
add    $0x1fa0,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xd8b(%rip),%rdi
add    $0x1fa0,%rsp
pop    %rbx
pop    %r14
pop    %r15
jmp    1090 <die@plt>
mov    %rbx,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
xor    %ecx,%ecx
add    $0x1fa0,%rsp
pop    %rbx
pop    %r14
pop    %r15
jmp    10b0 <execlp@plt>

LLVM IR (-O0):
@STDIN_FILENO = global i32 0
@.str = [7 x i8] c"EDITOR\00"
@.str.1 = [43 x i8] c"The environment variable EDITOR is unset.\0A\00"
@.str.2 = [2 x i8] c"w\00"
@stdin = global i32 0
define void @ntx_editor(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32*
%6 = alloca [8096 x i8]
store i8* %0, i8** %2
%7 = load i32, i32* @STDIN_FILENO
%8 = call i64 @isatty(i32 %7)
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %29
10: ; preds = %1
%11 = call i8* @getenv(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i8* %11, i8** %3
%12 = load i8*, i8** %3
%13 = icmp ne i8* %12, null
br i1 %13, label %14, label %26
14: ; preds = %10
%15 = call i32 (...) @fork()
store i32 %15, i32* %4
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %22
17: ; preds = %14
%18 = load i8*, i8** %3
%19 = load i8*, i8** %3
%20 = load i8*, i8** %2
%21 = call i32 @execlp(i8* %18, i8* %19, i8* %20, i8* null)
br label %25
22: ; preds = %14
%23 = load i32, i32* %4
%24 = call i32 @waitpid(i32 %23, i32* null, i32 0)
br label %25
25: ; preds = %22, %17
br label %28
26: ; preds = %10
%27 = call i32 @die(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0))
br label %28
28: ; preds = %26, %25
br label %44
29: ; preds = %1
%30 = load i8*, i8** %2
%31 = call i32* @raw_open(i8* %30, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %31, i32** %5
br label %32
32: ; preds = %37, %29
%33 = getelementptr inbounds [8096 x i8], [8096 x i8]* %6, i64 0, i64 0
%34 = load i32, i32* @stdin
%35 = call i64 @fgets(i8* %33, i32 8096, i32 %34)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %41
37: ; preds = %32
%38 = getelementptr inbounds [8096 x i8], [8096 x i8]* %6, i64 0, i64 0
%39 = load i32*, i32** %5
%40 = call i32 @fputs(i8* %38, i32* %39)
br label %32
41: ; preds = %32
%42 = load i32*, i32** %5
%43 = call i32 @release(i32* %42)
br label %44
44: ; preds = %41, %28
ret void
}

LLVM IR (-O2):
@STDIN_FILENO = global i32 0
@.str = [7 x i8] c"EDITOR\00"
@.str.1 = [43 x i8] c"The environment variable EDITOR is unset.\0A\00"
@.str.2 = [2 x i8] c"w\00"
@stdin = global i32 0
define void @ntx_editor(i8* %0) {
%2 = alloca [8096 x i8]
%3 = load i32, i32* @STDIN_FILENO
%4 = tail call i64 @isatty(i32 %3)
%5 = icmp eq i64 %4, 0
br i1 %5, label %18, label %6
6: ; preds = %1
%7 = tail call i8* @getenv(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i8* %7, null
br i1 %8, label %16, label %9
9: ; preds = %6
%10 = tail call i32 (...) @fork()
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = tail call i32 @execlp(i8* %7, i8* %7, i8* %0, i8* null)
br label %31
14: ; preds = %9
%15 = tail call i32 @waitpid(i32 %10, i32* null, i32 0)
br label %31
16: ; preds = %6
%17 = tail call i32 @die(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0))
br label %31
18: ; preds = %1
%19 = tail call i32* @raw_open(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%20 = getelementptr inbounds [8096 x i8], [8096 x i8]* %2, i64 0, i64 0
%21 = load i32, i32* @stdin
%22 = call i64 @fgets(i8* %20, i32 8096, i32 %21)
%23 = icmp eq i64 %22, 0
br i1 %23, label %29, label %24
24: ; preds = %18, %24
%25 = call i32 @fputs(i8* %20, i32* %19)
%26 = load i32, i32* @stdin
%27 = call i64 @fgets(i8* %20, i32 8096, i32 %26)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %24
29: ; preds = %24, %18
%30 = call i32 @release(i32* %19)
br label %31
31: ; preds = %16, %14, %12, %29
ret void
}