#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 SIGTERM;
int /*<<< orphan*/ dosetpty();
scalar_t__ execvp(char *, char **);
scalar_t__ fork();
scalar_t__ forkpty(int *, char *, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
scalar_t__ getppid();
int kill(scalar_t__, int);
int /*<<< orphan*/ pipedata(int, int);
int /*<<< orphan*/ setsid();
int main(int argc, char *argv[]) {
  int fdm, n;
  pid_t pid;
  char slave_name[20];
  char buf[256];
  setsid();
  pid = forkpty(&fdm, slave_name, NULL, NULL);
  if (pid < 0)
    return -1;
  else if (pid == 0) {
    dosetpty();
    if (execvp(argv[1], &argv[1]) < 0)
      return kill(getppid(), SIGTERM);
  }
  pid = fork();
  if (pid < 0)
    return -1;
  else if (pid == 0) {
    pipedata(0, fdm);
    kill(getppid(), 9);
  } else {
    pipedata(fdm, 1);
    kill(pid, 9);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1040 <setsid@plt>
lea    -0x40(%rbp),%rsi
lea    -0x14(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1070 <forkpty@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    11cf <main+0x4f>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a1 <main+0x121>
cmpq   $0x0,-0x20(%rbp)
jne    1225 <main+0xa5>
mov    $0x0,%al
call   1050 <dosetpty@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x10(%rbp),%rsi
add    $0x8,%rsi
call   1080 <execvp@plt>
cmp    $0x0,%rax
jge    1220 <main+0xa0>
mov    $0x0,%al
call   1090 <getppid@plt>
mov    %rax,%rdi
mov    0x2dd7(%rip),%rax
mov    (%rax),%esi
call   1060 <kill@plt>
mov    %eax,-0x4(%rbp)
jmp    12a1 <main+0x121>
jmp    1225 <main+0xa5>
jmp    122a <main+0xaa>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    124c <main+0xcc>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a1 <main+0x121>
cmpq   $0x0,-0x20(%rbp)
jne    127a <main+0xfa>
mov    -0x14(%rbp),%esi
xor    %edi,%edi
call   1030 <pipedata@plt>
mov    $0x0,%al
call   1090 <getppid@plt>
mov    %rax,%rdi
mov    $0x9,%esi
call   1060 <kill@plt>
jmp    1295 <main+0x115>
mov    -0x14(%rbp),%edi
mov    $0x1,%esi
call   1030 <pipedata@plt>
mov    -0x20(%rbp),%rdi
mov    $0x9,%esi
call   1060 <kill@plt>
jmp    129a <main+0x11a>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%rbx
xor    %eax,%eax
call   1040 <setsid@plt>
lea    0xc(%rsp),%rdi
lea    0x10(%rsp),%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1070 <forkpty@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
js     123a <main+0xba>
jne    11cf <main+0x4f>
xor    %eax,%eax
call   1050 <dosetpty@plt>
mov    0x8(%rbx),%rdi
add    $0x8,%rbx
mov    %rbx,%rsi
call   1080 <execvp@plt>
test   %rax,%rax
js     1220 <main+0xa0>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
js     123a <main+0xba>
mov    0xc(%rsp),%esi
je     1201 <main+0x81>
mov    %rax,%rbx
mov    %esi,%edi
mov    $0x1,%esi
call   1030 <pipedata@plt>
mov    %rbx,%rdi
mov    $0x9,%esi
call   1060 <kill@plt>
xor    %ebp,%ebp
jmp    123a <main+0xba>
xor    %ebp,%ebp
xor    %edi,%edi
call   1030 <pipedata@plt>
xor    %eax,%eax
call   1090 <getppid@plt>
mov    %rax,%rdi
mov    $0x9,%esi
call   1060 <kill@plt>
jmp    123a <main+0xba>
xor    %eax,%eax
call   1090 <getppid@plt>
mov    0x2dba(%rip),%rcx
mov    (%rcx),%esi
mov    %rax,%rdi
call   1060 <kill@plt>
mov    %eax,%ebp
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@SIGTERM = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i64
%9 = alloca [20 x i8]
%10 = alloca [256 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = call i32 (...) @setsid()
%12 = getelementptr inbounds [20 x i8], [20 x i8]* %9, i64 0, i64 0
%13 = call i64 @forkpty(i32* %6, i8* %12, i32* null, i32* null)
store i64 %13, i64* %8
%14 = load i64, i64* %8
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %2
store i32 -1, i32* %3
br label %55
17: ; preds = %2
%18 = load i64, i64* %8
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %34
20: ; preds = %17
%21 = call i32 (...) @dosetpty()
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 1
%27 = call i64 @execvp(i8* %24, i8** %26)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %20
%30 = call i64 (...) @getppid()
%31 = load i32, i32* @SIGTERM
%32 = call i32 @kill(i64 %30, i32 %31)
store i32 %32, i32* %3
br label %55
33: ; preds = %20
br label %34
34: ; preds = %33, %17
br label %35
35: ; preds = %34
%36 = call i64 (...) @fork()
store i64 %36, i64* %8
%37 = load i64, i64* %8
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %35
store i32 -1, i32* %3
br label %55
40: ; preds = %35
%41 = load i64, i64* %8
%42 = icmp eq i64 %41, 0
br i1 %42, label %43, label %48
43: ; preds = %40
%44 = load i32, i32* %6
%45 = call i32 @pipedata(i32 0, i32 %44)
%46 = call i64 (...) @getppid()
%47 = call i32 @kill(i64 %46, i32 9)
br label %53
48: ; preds = %40
%49 = load i32, i32* %6
%50 = call i32 @pipedata(i32 %49, i32 1)
%51 = load i64, i64* %8
%52 = call i32 @kill(i64 %51, i32 9)
br label %53
53: ; preds = %48, %43
br label %54
54: ; preds = %53
store i32 0, i32* %3
br label %55
55: ; preds = %54, %39, %29, %16
%56 = load i32, i32* %3
ret i32 %56
}

LLVM IR (-O2):
@SIGTERM = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca [20 x i8]
%5 = bitcast i32* %3 to i8*
%6 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%7 = tail call i32 (...) @setsid()
%8 = call i64 @forkpty(i32* %3, i8* %6, i32* null, i32* null)
%9 = icmp slt i64 %8, 0
br i1 %9, label %35, label %10
10: ; preds = %2
%11 = icmp eq i64 %8, 0
br i1 %11, label %12, label %22
12: ; preds = %10
%13 = call i32 (...) @dosetpty()
%14 = getelementptr inbounds i8*, i8** %1, i64 1
%15 = load i8*, i8** %14
%16 = call i64 @execvp(i8* %15, i8** %14)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %12
%19 = call i64 (...) @getppid()
%20 = load i32, i32* @SIGTERM
%21 = call i32 @kill(i64 %19, i32 %20)
br label %35
22: ; preds = %10, %12
%23 = call i64 (...) @fork()
%24 = icmp slt i64 %23, 0
br i1 %24, label %35, label %25
25: ; preds = %22
%26 = icmp eq i64 %23, 0
%27 = load i32, i32* %3
br i1 %26, label %28, label %32
28: ; preds = %25
%29 = call i32 @pipedata(i32 0, i32 %27)
%30 = call i64 (...) @getppid()
%31 = call i32 @kill(i64 %30, i32 9)
br label %35
32: ; preds = %25
%33 = call i32 @pipedata(i32 %27, i32 1)
%34 = call i32 @kill(i64 %23, i32 9)
br label %35
35: ; preds = %32, %28, %22, %2, %18
%36 = phi i32 [ %21, %18 ], [ -1, %2 ], [ -1, %22 ], [ 0, %28 ], [ 0, %32 ]
ret i32 %36
}