#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*/ EXIT_FAILURE;
int /*<<< orphan*/ O_RDWR;
scalar_t__ STDERR_FILENO;
scalar_t__ STDIN_FILENO;
scalar_t__ STDOUT_FILENO;
int /*<<< orphan*/ TIOCSCTTY;
int close(scalar_t__);
scalar_t__ dup2(scalar_t__, scalar_t__);
int /*<<< orphan*/ execlp(char *, char *, char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int ioctl(scalar_t__, int /*<<< orphan*/, int);
int open(char const *, int /*<<< orphan*/);
scalar_t__ setsid();
pid_t start_child(const char *ctty) {
  pid_t pid;
  pid = fork();
  if (0 != pid)
    return pid;
  if (((pid_t)-1) == setsid())
    goto terminate;
  if (-1 == close(STDIN_FILENO))
    goto terminate;
  if (-1 == open(ctty, O_RDWR))
    goto terminate;
  if (STDOUT_FILENO != dup2(STDIN_FILENO, STDOUT_FILENO))
    goto terminate;
  if (STDERR_FILENO != dup2(STDOUT_FILENO, STDERR_FILENO))
    goto terminate;
  if (-1 == ioctl(STDIN_FILENO, TIOCSCTTY, 1))
    goto terminate;
  (void)execlp("vtrc", "vtrc", (char *)NULL);
terminate:
  exit(EXIT_FAILURE);
  return (-1);
}


x86 assembly (clang, -O0):
<start_child>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x10(%rbp)
xor    %eax,%eax
cmp    -0x10(%rbp),%rax
je     11ad <start_child+0x2d>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret
mov    $0x0,%al
call   1060 <setsid@plt>
mov    %rax,%rcx
mov    $0xffffffffffffffff,%rax
cmp    %rcx,%rax
jne    11cc <start_child+0x4c>
jmp    12d1 <start_child+0x151>
mov    0x2de5(%rip),%rax
mov    (%rax),%rdi
call   1050 <close@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    11ef <start_child+0x6f>
jmp    12d1 <start_child+0x151>
mov    -0x8(%rbp),%rdi
mov    0x2de6(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1215 <start_child+0x95>
jmp    12d1 <start_child+0x151>
mov    0x2dd4(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
mov    0x2d8e(%rip),%rax
mov    (%rax),%rdi
mov    0x2dbc(%rip),%rax
mov    (%rax),%rsi
call   1030 <dup2@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
cmp    %rcx,%rax
je     1251 <start_child+0xd1>
jmp    12d1 <start_child+0x151>
mov    0x2d78(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    0x2d8a(%rip),%rax
mov    (%rax),%rdi
mov    0x2d60(%rip),%rax
mov    (%rax),%rsi
call   1030 <dup2@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
cmp    %rcx,%rax
je     128d <start_child+0x10d>
jmp    12d1 <start_child+0x151>
mov    0x2d24(%rip),%rax
mov    (%rax),%rdi
mov    0x2d22(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edx
call   1040 <ioctl@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    12be <start_child+0x13e>
jmp    12d1 <start_child+0x151>
lea    0xd3b(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
mov    %rsi,%rdi
call   1090 <execlp@plt>
mov    0x2cf0(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<start_child>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%rbx
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
je     119a <start_child+0x1a>
pop    %rbx
pop    %r14
pop    %r15
ret
xor    %eax,%eax
call   1060 <setsid@plt>
cmp    $0xffffffffffffffff,%rax
je     1235 <start_child+0xb5>
mov    0x2e06(%rip),%r14
mov    (%r14),%rdi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
je     1235 <start_child+0xb5>
mov    0x2e1a(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     1235 <start_child+0xb5>
mov    0x2e14(%rip),%r15
mov    (%r15),%rbx
mov    (%r14),%rdi
mov    %rbx,%rsi
call   1030 <dup2@plt>
cmp    %rax,%rbx
jne    1235 <start_child+0xb5>
mov    0x2dda(%rip),%rax
mov    (%rax),%rbx
mov    (%r15),%rdi
mov    %rbx,%rsi
call   1030 <dup2@plt>
cmp    %rax,%rbx
jne    1235 <start_child+0xb5>
mov    (%r14),%rdi
mov    0x2dad(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edx
call   1040 <ioctl@plt>
cmp    $0xffffffff,%eax
je     1235 <start_child+0xb5>
lea    0xdd5(%rip),%rdi
mov    %rdi,%rsi
xor    %edx,%edx
call   1090 <execlp@plt>
mov    0x2d8c(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@STDIN_FILENO = global i64 0
@O_RDWR = global i32 0
@STDOUT_FILENO = global i64 0
@STDERR_FILENO = global i64 0
@TIOCSCTTY = global i32 0
@.str = [5 x i8] c"vtrc\00"
@EXIT_FAILURE = global i32 0
define i64 @start_child(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
store i8* %0, i8** %2
%4 = call i64 (...) @fork()
store i64 %4, i64* %3
%5 = load i64, i64* %3
%6 = icmp ne i64 0, %5
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = load i64, i64* %3
ret i64 %8
9: ; preds = %1
%10 = call i64 (...) @setsid()
%11 = icmp eq i64 -1, %10
br i1 %11, label %12, label %13
12: ; preds = %9
br label %46
13: ; preds = %9
%14 = load i64, i64* @STDIN_FILENO
%15 = call i32 @close(i64 %14)
%16 = icmp eq i32 -1, %15
br i1 %16, label %17, label %18
17: ; preds = %13
br label %46
18: ; preds = %13
%19 = load i8*, i8** %2
%20 = load i32, i32* @O_RDWR
%21 = call i32 @open(i8* %19, i32 %20)
%22 = icmp eq i32 -1, %21
br i1 %22, label %23, label %24
23: ; preds = %18
br label %46
24: ; preds = %18
%25 = load i64, i64* @STDOUT_FILENO
%26 = load i64, i64* @STDIN_FILENO
%27 = load i64, i64* @STDOUT_FILENO
%28 = call i64 @dup2(i64 %26, i64 %27)
%29 = icmp ne i64 %25, %28
br i1 %29, label %30, label %31
30: ; preds = %24
br label %46
31: ; preds = %24
%32 = load i64, i64* @STDERR_FILENO
%33 = load i64, i64* @STDOUT_FILENO
%34 = load i64, i64* @STDERR_FILENO
%35 = call i64 @dup2(i64 %33, i64 %34)
%36 = icmp ne i64 %32, %35
br i1 %36, label %37, label %38
37: ; preds = %31
br label %46
38: ; preds = %31
%39 = load i64, i64* @STDIN_FILENO
%40 = load i32, i32* @TIOCSCTTY
%41 = call i32 @ioctl(i64 %39, i32 %40, i32 1)
%42 = icmp eq i32 -1, %41
br i1 %42, label %43, label %44
43: ; preds = %38
br label %46
44: ; preds = %38
%45 = call i32 @execlp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* null)
br label %46
46: ; preds = %44, %43, %37, %30, %23, %17, %12
%47 = load i32, i32* @EXIT_FAILURE
%48 = call i32 @exit(i32 %47)
unreachable
}

LLVM IR (-O2):
@STDIN_FILENO = global i64 0
@O_RDWR = global i32 0
@STDOUT_FILENO = global i64 0
@STDERR_FILENO = global i64 0
@TIOCSCTTY = global i32 0
@.str = [5 x i8] c"vtrc\00"
@EXIT_FAILURE = global i32 0
define i64 @start_child(i8* nocapture %0) {
%2 = tail call i64 (...) @fork()
%3 = icmp eq i64 %2, 0
br i1 %3, label %5, label %4
4: ; preds = %1
ret i64 %2
5: ; preds = %1
%6 = tail call i64 (...) @setsid()
%7 = icmp eq i64 %6, -1
br i1 %7, label %33, label %8
8: ; preds = %5
%9 = load i64, i64* @STDIN_FILENO
%10 = tail call i32 @close(i64 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %33, label %12
12: ; preds = %8
%13 = load i32, i32* @O_RDWR
%14 = tail call i32 @open(i8* %0, i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %33, label %16
16: ; preds = %12
%17 = load i64, i64* @STDOUT_FILENO
%18 = load i64, i64* @STDIN_FILENO
%19 = tail call i64 @dup2(i64 %18, i64 %17)
%20 = icmp eq i64 %17, %19
br i1 %20, label %21, label %33
21: ; preds = %16
%22 = load i64, i64* @STDERR_FILENO
%23 = load i64, i64* @STDOUT_FILENO
%24 = tail call i64 @dup2(i64 %23, i64 %22)
%25 = icmp eq i64 %22, %24
br i1 %25, label %26, label %33
26: ; preds = %21
%27 = load i64, i64* @STDIN_FILENO
%28 = load i32, i32* @TIOCSCTTY
%29 = tail call i32 @ioctl(i64 %27, i32 %28, i32 1)
%30 = icmp eq i32 %29, -1
br i1 %30, label %33, label %31
31: ; preds = %26
%32 = tail call i32 @execlp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* null)
br label %33
33: ; preds = %26, %21, %16, %12, %8, %5, %31
%34 = load i32, i32* @EXIT_FAILURE
%35 = tail call i32 @exit(i32 %34)
unreachable
}