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

/* Variables and functions */
int EINTR;
int /*<<< orphan*/ SIGCHLD;
int /*<<< orphan*/ SIGHUP;
int /*<<< orphan*/ SIGINT;
int /*<<< orphan*/ SIGQUIT;
int /*<<< orphan*/ SIG_DFL;
int /*<<< orphan*/ SIG_IGN;
int WEXITSTATUS(int);
int /*<<< orphan*/ WIFEXITED(int);
char *_PATH_VI;
int /*<<< orphan*/ _exit(int);
int errno;
int /*<<< orphan*/ execlp(char const *, char const *, char const *, char *);
int fork();
char *getenv(char *);
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int waitpid(int, int *, int /*<<< orphan*/);
int editit(const char *pathname) {
  sig_t sighup, sigint, sigquit, sigchld;
  pid_t pid;
  int saved_errno, st, ret = -1;
  const char *ed;
  ed = getenv("VISUAL");
  if (ed == NULL)
    ed = getenv("EDITOR");
  if (ed == NULL)
    ed = _PATH_VI;
  sighup = signal(SIGHUP, SIG_IGN);
  sigint = signal(SIGINT, SIG_IGN);
  sigquit = signal(SIGQUIT, SIG_IGN);
  sigchld = signal(SIGCHLD, SIG_DFL);
  if ((pid = fork()) == -1)
    goto fail;
  if (pid == 0) {
    execlp(ed, ed, pathname, (char *)NULL);
    _exit(127);
  }
  while (waitpid(pid, &st, 0) == -1)
    if (errno != EINTR)
      goto fail;
  if (!WIFEXITED(st))
    errno = EINTR;
  else
    ret = WEXITSTATUS(st);
fail:
  saved_errno = errno;
  (void)signal(SIGHUP, sighup);
  (void)signal(SIGINT, sigint);
  (void)signal(SIGQUIT, sigquit);
  (void)signal(SIGCHLD, sigchld);
  errno = saved_errno;
  return (ret);
}


x86 assembly (clang, -O0):
<editit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0xffffffff,-0x28(%rbp)
lea    0xe66(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11be <editit+0x3e>
lea    0xe52(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11d7 <editit+0x57>
mov    0x2df0(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    0x2e12(%rip),%rax
mov    (%rax),%edi
mov    0x2de1(%rip),%rax
mov    (%rax),%esi
call   1050 <signal@plt>
mov    %eax,-0xc(%rbp)
mov    0x2de8(%rip),%rax
mov    (%rax),%edi
mov    0x2dc7(%rip),%rax
mov    (%rax),%esi
call   1050 <signal@plt>
mov    %eax,-0x10(%rbp)
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
mov    0x2dad(%rip),%rax
mov    (%rax),%esi
call   1050 <signal@plt>
mov    %eax,-0x14(%rbp)
mov    0x2da4(%rip),%rax
mov    (%rax),%edi
mov    0x2d6b(%rip),%rax
mov    (%rax),%esi
call   1050 <signal@plt>
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    1257 <editit+0xd7>
jmp    12f6 <editit+0x176>
cmpl   $0x0,-0x1c(%rbp)
jne    1280 <editit+0x100>
mov    -0x30(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x8(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   1090 <execlp@plt>
mov    $0x7f,%edi
call   1040 <_exit@plt>
jmp    1285 <editit+0x105>
mov    -0x1c(%rbp),%edi
lea    -0x24(%rbp),%rsi
xor    %edx,%edx
call   1070 <waitpid@plt>
cmp    $0xffffffff,%eax
jne    12be <editit+0x13e>
mov    0x2d0d(%rip),%rax
mov    (%rax),%eax
mov    0x2cfc(%rip),%rcx
cmp    (%rcx),%eax
je     12b9 <editit+0x139>
jmp    12f6 <editit+0x176>
jmp    1285 <editit+0x105>
mov    -0x24(%rbp),%edi
call   1080 <WIFEXITED@plt>
cmp    $0x0,%eax
jne    12e6 <editit+0x166>
mov    0x2cd2(%rip),%rax
mov    (%rax),%ecx
mov    0x2cd1(%rip),%rax
mov    %ecx,(%rax)
jmp    12f1 <editit+0x171>
mov    -0x24(%rbp),%edi
call   1060 <WEXITSTATUS@plt>
mov    %eax,-0x28(%rbp)
jmp    12f6 <editit+0x176>
mov    0x2cb3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2ce7(%rip),%rax
mov    (%rax),%edi
mov    -0xc(%rbp),%esi
call   1050 <signal@plt>
mov    0x2cc6(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%esi
call   1050 <signal@plt>
mov    0x2cad(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%esi
call   1050 <signal@plt>
mov    0x2c94(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%esi
call   1050 <signal@plt>
mov    -0x20(%rbp),%ecx
mov    0x2c60(%rip),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<editit>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdi,0x20(%rsp)
lea    0xe66(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%r12
test   %rax,%rax
jne    11b6 <editit+0x36>
lea    0xe59(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%r12
mov    0x2e03(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x18(%rsp)
mov    0x2e24(%rip),%r13
mov    0x0(%r13),%edi
mov    0x2df1(%rip),%rbx
mov    (%rbx),%esi
call   1050 <signal@plt>
mov    %eax,%r15d
mov    0x2df8(%rip),%r14
mov    (%r14),%edi
mov    (%rbx),%esi
call   1050 <signal@plt>
mov    %eax,%ebp
mov    0x2ddd(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%esi
call   1050 <signal@plt>
mov    %eax,0xc(%rsp)
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
mov    0x2d88(%rip),%rax
mov    (%rax),%esi
call   1050 <signal@plt>
mov    %eax,0x8(%rsp)
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
mov    %r15d,0x14(%rsp)
mov    %ebp,0x10(%rsp)
je     12a2 <editit+0x122>
test   %ebx,%ebx
je     12fd <editit+0x17d>
lea    0x4(%rsp),%rbp
mov    0x2d62(%rip),%r12
mov    0x2d53(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebx,%edi
mov    %rbp,%rsi
xor    %edx,%edx
call   1070 <waitpid@plt>
cmp    $0xffffffff,%eax
jne    127c <editit+0xfc>
mov    (%r12),%eax
cmp    (%r15),%eax
je     1260 <editit+0xe0>
jmp    129d <editit+0x11d>
mov    0x4(%rsp),%edi
call   1080 <WIFEXITED@plt>
test   %eax,%eax
je     1296 <editit+0x116>
mov    0x4(%rsp),%edi
call   1060 <WEXITSTATUS@plt>
mov    %eax,%ebx
jmp    12a2 <editit+0x122>
mov    (%r15),%eax
mov    %eax,(%r12)
mov    $0xffffffff,%ebx
mov    0x2d07(%rip),%r15
mov    (%r15),%ebp
mov    0x0(%r13),%edi
mov    0x14(%rsp),%esi
call   1050 <signal@plt>
mov    (%r14),%edi
mov    0x10(%rsp),%esi
call   1050 <signal@plt>
mov    0x2d0c(%rip),%rax
mov    (%rax),%edi
mov    0xc(%rsp),%esi
call   1050 <signal@plt>
mov    0x2cf2(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rsp),%esi
call   1050 <signal@plt>
mov    %ebp,(%r15)
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
test   %r12,%r12
mov    0x18(%rsp),%rdi
cmovne %r12,%rdi
mov    %rdi,%rsi
mov    0x20(%rsp),%rdx
xor    %ecx,%ecx
call   1090 <execlp@plt>
mov    $0x7f,%edi
call   1040 <_exit@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"VISUAL\00"
@.str.1 = [7 x i8] c"EDITOR\00"
@_PATH_VI = global i8* null
@SIGHUP = global i32 0
@SIG_IGN = global i32 0
@SIGINT = global i32 0
@SIGQUIT = global i32 0
@SIGCHLD = global i32 0
@SIG_DFL = global i32 0
@errno = global i32 0
@EINTR = global i32 0
define i32 @editit(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
store i8* %0, i8** %2
store i32 -1, i32* %10
%12 = call i8* @getenv(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i8* %12, i8** %11
%13 = load i8*, i8** %11
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %17
15: ; preds = %1
%16 = call i8* @getenv(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
store i8* %16, i8** %11
br label %17
17: ; preds = %15, %1
%18 = load i8*, i8** %11
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = load i8*, i8** @_PATH_VI
store i8* %21, i8** %11
br label %22
22: ; preds = %20, %17
%23 = load i32, i32* @SIGHUP
%24 = load i32, i32* @SIG_IGN
%25 = call i32 @signal(i32 %23, i32 %24)
store i32 %25, i32* %3
%26 = load i32, i32* @SIGINT
%27 = load i32, i32* @SIG_IGN
%28 = call i32 @signal(i32 %26, i32 %27)
store i32 %28, i32* %4
%29 = load i32, i32* @SIGQUIT
%30 = load i32, i32* @SIG_IGN
%31 = call i32 @signal(i32 %29, i32 %30)
store i32 %31, i32* %5
%32 = load i32, i32* @SIGCHLD
%33 = load i32, i32* @SIG_DFL
%34 = call i32 @signal(i32 %32, i32 %33)
store i32 %34, i32* %6
%35 = call i32 (...) @fork()
store i32 %35, i32* %7
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %38
37: ; preds = %22
br label %68
38: ; preds = %22
%39 = load i32, i32* %7
%40 = icmp eq i32 %39, 0
br i1 %40, label %41, label %47
41: ; preds = %38
%42 = load i8*, i8** %11
%43 = load i8*, i8** %11
%44 = load i8*, i8** %2
%45 = call i32 @execlp(i8* %42, i8* %43, i8* %44, i8* null)
%46 = call i32 @_exit(i32 127)
unreachable
47: ; preds = %38
br label %48
48: ; preds = %57, %47
%49 = load i32, i32* %7
%50 = call i32 @waitpid(i32 %49, i32* %9, i32 0)
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %58
52: ; preds = %48
%53 = load i32, i32* @errno
%54 = load i32, i32* @EINTR
%55 = icmp ne i32 %53, %54
br i1 %55, label %56, label %57
56: ; preds = %52
br label %68
57: ; preds = %52
br label %48
58: ; preds = %48
%59 = load i32, i32* %9
%60 = call i32 @WIFEXITED(i32 %59)
%61 = icmp ne i32 %60, 0
br i1 %61, label %64, label %62
62: ; preds = %58
%63 = load i32, i32* @EINTR
store i32 %63, i32* @errno
br label %67
64: ; preds = %58
%65 = load i32, i32* %9
%66 = call i32 @WEXITSTATUS(i32 %65)
store i32 %66, i32* %10
br label %67
67: ; preds = %64, %62
br label %68
68: ; preds = %67, %56, %37
%69 = load i32, i32* @errno
store i32 %69, i32* %8
%70 = load i32, i32* @SIGHUP
%71 = load i32, i32* %3
%72 = call i32 @signal(i32 %70, i32 %71)
%73 = load i32, i32* @SIGINT
%74 = load i32, i32* %4
%75 = call i32 @signal(i32 %73, i32 %74)
%76 = load i32, i32* @SIGQUIT
%77 = load i32, i32* %5
%78 = call i32 @signal(i32 %76, i32 %77)
%79 = load i32, i32* @SIGCHLD
%80 = load i32, i32* %6
%81 = call i32 @signal(i32 %79, i32 %80)
%82 = load i32, i32* %8
store i32 %82, i32* @errno
%83 = load i32, i32* %10
ret i32 %83
}

LLVM IR (-O2):
@.str = [7 x i8] c"VISUAL\00"
@.str.1 = [7 x i8] c"EDITOR\00"
@_PATH_VI = global i8* null
@SIGHUP = global i32 0
@SIG_IGN = global i32 0
@SIGINT = global i32 0
@SIGQUIT = global i32 0
@SIGCHLD = global i32 0
@SIG_DFL = global i32 0
@errno = global i32 0
@EINTR = global i32 0
define i32 @editit(i8* %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = tail call i8* @getenv(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %8
6: ; preds = %1
%7 = tail call i8* @getenv(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %8
8: ; preds = %6, %1
%9 = phi i8* [ %7, %6 ], [ %4, %1 ]
%10 = load i8*, i8** @_PATH_VI
%11 = load i32, i32* @SIGHUP
%12 = load i32, i32* @SIG_IGN
%13 = tail call i32 @signal(i32 %11, i32 %12)
%14 = load i32, i32* @SIGINT
%15 = load i32, i32* @SIG_IGN
%16 = tail call i32 @signal(i32 %14, i32 %15)
%17 = load i32, i32* @SIGQUIT
%18 = load i32, i32* @SIG_IGN
%19 = tail call i32 @signal(i32 %17, i32 %18)
%20 = load i32, i32* @SIGCHLD
%21 = load i32, i32* @SIG_DFL
%22 = tail call i32 @signal(i32 %20, i32 %21)
%23 = tail call i32 (...) @fork()
switch i32 %23, label %29 [
i32 -1, label %45
i32 0, label %24
]
24: ; preds = %8
%25 = icmp eq i8* %9, null
%26 = select i1 %25, i8* %10, i8* %9
%27 = tail call i32 @execlp(i8* %26, i8* %26, i8* %0, i8* null)
%28 = tail call i32 @_exit(i32 127)
unreachable
29: ; preds = %8, %32
%30 = call i32 @waitpid(i32 %23, i32* %2, i32 0)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %36
32: ; preds = %29
%33 = load i32, i32* @errno
%34 = load i32, i32* @EINTR
%35 = icmp eq i32 %33, %34
br i1 %35, label %29, label %45
36: ; preds = %29
%37 = load i32, i32* %2
%38 = call i32 @WIFEXITED(i32 %37)
%39 = icmp eq i32 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %36
%41 = load i32, i32* @EINTR
store i32 %41, i32* @errno
br label %45
42: ; preds = %36
%43 = load i32, i32* %2
%44 = call i32 @WEXITSTATUS(i32 %43)
br label %45
45: ; preds = %32, %8, %40, %42
%46 = phi i32 [ %23, %8 ], [ %44, %42 ], [ -1, %40 ], [ -1, %32 ]
%47 = load i32, i32* @errno
%48 = load i32, i32* @SIGHUP
%49 = call i32 @signal(i32 %48, i32 %13)
%50 = load i32, i32* @SIGINT
%51 = call i32 @signal(i32 %50, i32 %16)
%52 = load i32, i32* @SIGQUIT
%53 = call i32 @signal(i32 %52, i32 %19)
%54 = load i32, i32* @SIGCHLD
%55 = call i32 @signal(i32 %54, i32 %22)
store i32 %47, i32* @errno
ret i32 %46
}