#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*/ slavepath;

/* Variables and functions */
int O_NOCTTY;
int O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ execvp(char const *, char *const *);
int fork();
int getpt();
scalar_t__ grantpt(int);
int open(char *, int);
scalar_t__ ptsname_r(int, char *, int);
int /*<<< orphan*/ setenv(char *, char *, int);
int /*<<< orphan*/ setsid();
scalar_t__ unlockpt(int);
int spawn_process_with_pts(const char *execparam, char *const argv[],
                           int *pid) {
  int r = getpt();
  if (r <= 0)
    return -1;
  if (grantpt(r))
    return -2;
  if (unlockpt(r))
    return -3;
  char slavepath[64];
  if (ptsname_r(r, slavepath, sizeof(slavepath)) < 0) {
    return -4;
  }
  int rforkv = fork();
  if (rforkv == 0) {
    close(r);
    close(0);
    close(1);
    close(2);
    r = open(slavepath, O_RDWR | O_NOCTTY);
    dup2(r, 0);
    dup2(r, 1);
    dup2(r, 2);
    setsid();
    setenv("TERM", "xterm", 1);
    execvp(execparam, argv);
  } else {
    if (rforkv < 0) {
      close(r);
      return -10;
    }
    *pid = rforkv;
    return r;
  }
}


x86 assembly (clang, -O0):
<spawn_process_with_pts>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    $0x0,%al
call   1080 <getpt@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jg     11e7 <spawn_process_with_pts+0x37>
movl   $0xffffffff,-0x4(%rbp)
jmp    1321 <spawn_process_with_pts+0x171>
mov    -0x24(%rbp),%edi
call   10c0 <grantpt@plt>
cmp    $0x0,%rax
je     1205 <spawn_process_with_pts+0x55>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1321 <spawn_process_with_pts+0x171>
mov    -0x24(%rbp),%edi
call   1090 <unlockpt@plt>
cmp    $0x0,%rax
je     1223 <spawn_process_with_pts+0x73>
movl   $0xfffffffd,-0x4(%rbp)
jmp    1321 <spawn_process_with_pts+0x171>
mov    -0x24(%rbp),%edi
lea    -0x70(%rbp),%rsi
mov    $0x40,%edx
call   1060 <ptsname_r@plt>
cmp    $0x0,%rax
jge    124a <spawn_process_with_pts+0x9a>
movl   $0xfffffffc,-0x4(%rbp)
jmp    1321 <spawn_process_with_pts+0x171>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %eax,-0x74(%rbp)
cmpl   $0x0,-0x74(%rbp)
jne    12f4 <spawn_process_with_pts+0x144>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
xor    %edi,%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   1050 <close@plt>
mov    $0x2,%edi
call   1050 <close@plt>
lea    -0x70(%rbp),%rdi
mov    0x2d5c(%rip),%rax
mov    (%rax),%esi
mov    0x2d43(%rip),%rax
or     (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
xor    %esi,%esi
call   1040 <dup2@plt>
mov    -0x24(%rbp),%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    -0x24(%rbp),%edi
mov    $0x2,%esi
call   1040 <dup2@plt>
mov    $0x0,%al
call   1070 <setsid@plt>
lea    0xd2f(%rip),%rdi
lea    0xd2d(%rip),%rsi
mov    $0x1,%edx
call   1030 <setenv@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   10b0 <execvp@plt>
jmp    1321 <spawn_process_with_pts+0x171>
cmpl   $0x0,-0x74(%rbp)
jge    1312 <spawn_process_with_pts+0x162>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movl   $0xfffffff6,-0x4(%rbp)
jmp    1321 <spawn_process_with_pts+0x171>
mov    -0x74(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spawn_process_with_pts>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x48,%rsp
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %rdi,%r15
xor    %eax,%eax
call   1080 <getpt@plt>
test   %eax,%eax
jle    1224 <spawn_process_with_pts+0x74>
mov    %eax,%ebp
mov    %eax,%edi
call   10c0 <grantpt@plt>
mov    %rax,%rcx
mov    $0xfffffffe,%eax
test   %rcx,%rcx
jne    1229 <spawn_process_with_pts+0x79>
mov    %ebp,%edi
call   1090 <unlockpt@plt>
mov    %rax,%rcx
mov    $0xfffffffd,%eax
test   %rcx,%rcx
jne    1229 <spawn_process_with_pts+0x79>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x40,%edx
call   1060 <ptsname_r@plt>
test   %rax,%rax
js     1234 <spawn_process_with_pts+0x84>
xor    %eax,%eax
call   10d0 <fork@plt>
test   %eax,%eax
je     123e <spawn_process_with_pts+0x8e>
js     12c9 <spawn_process_with_pts+0x119>
mov    %eax,(%rbx)
jmp    12d5 <spawn_process_with_pts+0x125>
mov    $0xffffffff,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xfffffffc,%ebp
jmp    12d5 <spawn_process_with_pts+0x125>
mov    %ebp,%edi
call   1050 <close@plt>
xor    %edi,%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   1050 <close@plt>
mov    $0x2,%edi
call   1050 <close@plt>
mov    0x2d81(%rip),%rax
mov    0x2d6a(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rsp,%rdi
call   10a0 <open@plt>
mov    %eax,%ebx
mov    %eax,%edi
xor    %esi,%esi
call   1040 <dup2@plt>
mov    %ebx,%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    %ebx,%edi
mov    $0x2,%esi
call   1040 <dup2@plt>
xor    %eax,%eax
call   1070 <setsid@plt>
lea    0xd55(%rip),%rdi
lea    0xd53(%rip),%rsi
mov    $0x1,%edx
call   1030 <setenv@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   10b0 <execvp@plt>
jmp    12d5 <spawn_process_with_pts+0x125>
mov    %ebp,%edi
call   1050 <close@plt>
mov    $0xfffffff6,%ebp
mov    %ebp,%eax
jmp    1229 <spawn_process_with_pts+0x79>

LLVM IR (-O0):
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@.str = [5 x i8] c"TERM\00"
@.str.1 = [6 x i8] c"xterm\00"
define i32 @spawn_process_with_pts(i8* %0, i8** %1, i32* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8**
%7 = alloca i32*
%8 = alloca i32
%9 = alloca [64 x i8]
%10 = alloca i32
store i8* %0, i8** %5
store i8** %1, i8*** %6
store i32* %2, i32** %7
%11 = call i32 (...) @getpt()
store i32 %11, i32* %8
%12 = load i32, i32* %8
%13 = icmp sle i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %3
store i32 -1, i32* %4
br label %67
15: ; preds = %3
%16 = load i32, i32* %8
%17 = call i64 @grantpt(i32 %16)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %15
store i32 -2, i32* %4
br label %67
20: ; preds = %15
%21 = load i32, i32* %8
%22 = call i64 @unlockpt(i32 %21)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %20
store i32 -3, i32* %4
br label %67
25: ; preds = %20
%26 = load i32, i32* %8
%27 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%28 = call i64 @ptsname_r(i32 %26, i8* %27, i32 64)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %25
store i32 -4, i32* %4
br label %67
31: ; preds = %25
%32 = call i32 (...) @fork()
store i32 %32, i32* %10
%33 = load i32, i32* %10
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %57
35: ; preds = %31
%36 = load i32, i32* %8
%37 = call i32 @close(i32 %36)
%38 = call i32 @close(i32 0)
%39 = call i32 @close(i32 1)
%40 = call i32 @close(i32 2)
%41 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%42 = load i32, i32* @O_RDWR
%43 = load i32, i32* @O_NOCTTY
%44 = or i32 %42, %43
%45 = call i32 @open(i8* %41, i32 %44)
store i32 %45, i32* %8
%46 = load i32, i32* %8
%47 = call i32 @dup2(i32 %46, i32 0)
%48 = load i32, i32* %8
%49 = call i32 @dup2(i32 %48, i32 1)
%50 = load i32, i32* %8
%51 = call i32 @dup2(i32 %50, i32 2)
%52 = call i32 (...) @setsid()
%53 = call i32 @setenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 1)
%54 = load i8*, i8** %5
%55 = load i8**, i8*** %6
%56 = call i32 @execvp(i8* %54, i8** %55)
br label %67
57: ; preds = %31
%58 = load i32, i32* %10
%59 = icmp slt i32 %58, 0
br i1 %59, label %60, label %63
60: ; preds = %57
%61 = load i32, i32* %8
%62 = call i32 @close(i32 %61)
store i32 -10, i32* %4
br label %67
63: ; preds = %57
%64 = load i32, i32* %10
%65 = load i32*, i32** %7
store i32 %64, i32* %65
%66 = load i32, i32* %8
store i32 %66, i32* %4
br label %67
67: ; preds = %14, %19, %24, %30, %60, %63, %35
%68 = load i32, i32* %4
ret i32 %68
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@.str = [5 x i8] c"TERM\00"
@.str.1 = [6 x i8] c"xterm\00"
define i32 @spawn_process_with_pts(i8* %0, i8** %1, i32* nocapture writeonly %2) {
%4 = alloca [64 x i8]
%5 = tail call i32 (...) @getpt()
%6 = icmp slt i32 %5, 1
br i1 %6, label %42, label %7
7: ; preds = %3
%8 = tail call i64 @grantpt(i32 %5)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %42
10: ; preds = %7
%11 = tail call i64 @unlockpt(i32 %5)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %42
13: ; preds = %10
%14 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%15 = call i64 @ptsname_r(i32 %5, i8* %14, i32 64)
%16 = icmp slt i64 %15, 0
br i1 %16, label %40, label %17
17: ; preds = %13
%18 = call i32 (...) @fork()
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %35
20: ; preds = %17
%21 = call i32 @close(i32 %5)
%22 = call i32 @close(i32 0)
%23 = call i32 @close(i32 1)
%24 = call i32 @close(i32 2)
%25 = load i32, i32* @O_RDWR
%26 = load i32, i32* @O_NOCTTY
%27 = or i32 %26, %25
%28 = call i32 @open(i8* %14, i32 %27)
%29 = call i32 @dup2(i32 %28, i32 0)
%30 = call i32 @dup2(i32 %28, i32 1)
%31 = call i32 @dup2(i32 %28, i32 2)
%32 = call i32 (...) @setsid()
%33 = call i32 @setenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 1)
%34 = call i32 @execvp(i8* %0, i8** %1)
br label %40
35: ; preds = %17
%36 = icmp slt i32 %18, 0
br i1 %36, label %37, label %39
37: ; preds = %35
%38 = call i32 @close(i32 %5)
br label %40
39: ; preds = %35
store i32 %18, i32* %2
br label %40
40: ; preds = %37, %39, %20, %13
%41 = phi i32 [ -4, %13 ], [ undef, %20 ], [ -10, %37 ], [ %5, %39 ]
br label %42
42: ; preds = %10, %7, %3, %40
%43 = phi i32 [ %41, %40 ], [ -1, %3 ], [ -2, %7 ], [ -3, %10 ]
ret i32 %43
}