#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 pid_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ err_msg(char *, int);
int /*<<< orphan*/ err_sys(char *);
int errno;
int /*<<< orphan*/ execlp(char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int forkpty(int *, char *, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int getpgrp();
int getpid();
int getsid(int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int setsid();
char *ttyname(int);
int main(int argc, char *argv[]) {
  if (setsid() == -1)
    perror("setsid");
  int ret = 0;
  pid_t pid = getpid();
  pid_t grp = getpgrp();
  pid_t sid = getsid(0);
  printf("parent %u: %u, %u\n", sid, pid, grp);
  int pty_fd = 0;
  char name[512] = {0};
  pid_t cid = forkpty(&pty_fd, name, NULL, NULL);
  if (cid == -1) {
    err_sys("forkpty");
  } else if (cid == 0) {
    pid = getpid();
    grp = getpgrp();
    sid = getsid(0);
    printf("child %u: %u, %u\n", sid, pid, grp);
    int fd = open("/dev/tty", O_RDWR);
    if (fd == -1)
      err_msg("open default tty failed, errno = %d", errno);
    else
      err_msg("open default tty OK, fd = %d", fd);
    close(fd);
    printf("hello %d: %s\n", pty_fd, ttyname(pty_fd));
    execlp("./grpid", "grpid", NULL);
    printf("execlp failed, errno %d\n", errno);
    exit(-1);
  }
  printf("forkpty: %d, %s\n", pty_fd, name);
  char buf[1024] = {0};
  while (1) {
    ret = read(pty_fd, buf, 1024);
    if (ret > 0) {
      buf[ret] = 0;
      printf(">%s", buf);
    } else
      break;
  }
  close(pty_fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x650,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1090 <setsid@plt>
cmp    $0xffffffff,%eax
jne    1235 <main+0x35>
lea    0xdd0(%rip),%rdi
call   10f0 <perror@plt>
movl   $0x0,-0x14(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   10c0 <getpgrp@plt>
mov    %eax,-0x1c(%rbp)
xor    %edi,%edi
call   1060 <getsid@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%esi
mov    -0x18(%rbp),%edx
mov    -0x1c(%rbp),%ecx
lea    0xd9d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x24(%rbp)
lea    -0x230(%rbp),%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1070 <memset@plt>
lea    -0x230(%rbp),%rsi
lea    -0x24(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   10d0 <forkpty@plt>
mov    %eax,-0x234(%rbp)
cmpl   $0xffffffff,-0x234(%rbp)
jne    12c6 <main+0xc6>
lea    0xd5e(%rip),%rdi
call   1030 <err_sys@plt>
jmp    13cc <main+0x1cc>
cmpl   $0x0,-0x234(%rbp)
jne    13c7 <main+0x1c7>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   10c0 <getpgrp@plt>
mov    %eax,-0x1c(%rbp)
xor    %edi,%edi
call   1060 <getsid@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%esi
mov    -0x18(%rbp),%edx
mov    -0x1c(%rbp),%ecx
lea    0xd21(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2cd9(%rip),%rax
mov    (%rax),%esi
lea    0xd1c(%rip),%rdi
call   10e0 <open@plt>
mov    %eax,-0x238(%rbp)
cmpl   $0xffffffff,-0x238(%rbp)
jne    134a <main+0x14a>
mov    0x2ca1(%rip),%rax
mov    (%rax),%esi
lea    0xcfd(%rip),%rdi
call   1100 <err_msg@plt>
jmp    135c <main+0x15c>
mov    -0x238(%rbp),%esi
lea    0xd0a(%rip),%rdi
call   1100 <err_msg@plt>
mov    -0x238(%rbp),%edi
call   1080 <close@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x644(%rbp)
mov    -0x24(%rbp),%edi
call   10b0 <ttyname@plt>
mov    -0x644(%rbp),%esi
mov    %rax,%rdx
lea    0xcf6(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xcf6(%rip),%rdi
lea    0xcf1(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1120 <execlp@plt>
mov    0x2c2b(%rip),%rax
mov    (%rax),%esi
lea    0xcde(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   1110 <exit@plt>
jmp    13cc <main+0x1cc>
mov    -0x24(%rbp),%esi
lea    -0x230(%rbp),%rdx
lea    0xcd0(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x640(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
mov    -0x24(%rbp),%edi
lea    -0x640(%rbp),%rsi
mov    $0x400,%edx
call   10a0 <read@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    143e <main+0x23e>
movslq -0x14(%rbp),%rax
movb   $0x0,-0x640(%rbp,%rax,1)
lea    -0x640(%rbp),%rsi
lea    0xc8c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1443 <main+0x243>
jmp    1448 <main+0x248>
jmp    13f7 <main+0x1f7>
mov    -0x24(%rbp),%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x650,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x610,%rsp
xor    %eax,%eax
call   1090 <setsid@plt>
cmp    $0xffffffff,%eax
je     13f3 <main+0x1f3>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   10c0 <getpgrp@plt>
mov    %eax,%ebp
xor    %edi,%edi
call   1060 <getsid@plt>
lea    0xdcc(%rip),%rdi
mov    %eax,%esi
mov    %ebx,%edx
mov    %ebp,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
movl   $0x0,0xc(%rsp)
lea    0x10(%rsp),%rbx
mov    $0x200,%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <memset@plt>
lea    0xc(%rsp),%rdi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   10d0 <forkpty@plt>
cmp    $0xffffffff,%eax
je     1346 <main+0x146>
test   %eax,%eax
jne    1352 <main+0x152>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   10c0 <getpgrp@plt>
mov    %eax,%ebp
xor    %edi,%edi
call   1060 <getsid@plt>
lea    0xd7c(%rip),%rdi
mov    %eax,%esi
mov    %ebx,%edx
mov    %ebp,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2d2e(%rip),%rax
mov    (%rax),%esi
lea    0xd71(%rip),%rdi
call   10e0 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
mov    0x2d04(%rip),%rbp
mov    0x0(%rbp),%esi
cmovne %eax,%esi
lea    0xd5c(%rip),%rax
lea    0xd79(%rip),%rdi
cmove  %rax,%rdi
call   1100 <err_msg@plt>
mov    %ebx,%edi
call   1080 <close@plt>
mov    0xc(%rsp),%ebx
mov    %ebx,%edi
call   10b0 <ttyname@plt>
lea    0xd74(%rip),%rdi
mov    %ebx,%esi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xd6f(%rip),%rdi
lea    0xd6a(%rip),%rsi
xor    %edx,%edx
call   1120 <execlp@plt>
mov    0x0(%rbp),%esi
lea    0xd5f(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   1110 <exit@plt>
lea    0xccd(%rip),%rdi
call   1030 <err_sys@plt>
mov    0xc(%rsp),%esi
lea    0xd50(%rip),%rdi
lea    0x10(%rsp),%rdx
xor    %eax,%eax
call   1050 <printf@plt>
lea    0x210(%rsp),%rbx
mov    $0x400,%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <memset@plt>
mov    0xc(%rsp),%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   10a0 <read@plt>
test   %eax,%eax
jle    13dc <main+0x1dc>
lea    0xd22(%rip),%r14
lea    0x210(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %eax,%eax
movb   $0x0,0x210(%rsp,%rax,1)
mov    %r14,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0xc(%rsp),%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   10a0 <read@plt>
test   %eax,%eax
jg     13b0 <main+0x1b0>
mov    0xc(%rsp),%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x610,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xc06(%rip),%rdi
call   10f0 <perror@plt>
jmp    121b <main+0x1b>

LLVM IR (-O0):
@.str = [7 x i8] c"setsid\00"
@.str.1 = [19 x i8] c"parent %u: %u, %u\0A\00"
@.str.2 = [8 x i8] c"forkpty\00"
@.str.3 = [18 x i8] c"child %u: %u, %u\0A\00"
@.str.4 = [9 x i8] c"/dev/tty\00"
@O_RDWR = global i32 0
@.str.5 = [36 x i8] c"open default tty failed, errno = %d\00"
@errno = global i32 0
@.str.6 = [29 x i8] c"open default tty OK, fd = %d\00"
@.str.7 = [14 x i8] c"hello %d: %s\0A\00"
@.str.8 = [8 x i8] c"./grpid\00"
@.str.9 = [6 x i8] c"grpid\00"
@.str.10 = [25 x i8] c"execlp failed, errno %d\0A\00"
@.str.11 = [17 x i8] c"forkpty: %d, %s\0A\00"
@.str.12 = [4 x i8] c">%s\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca [512 x i8]
%12 = alloca i32
%13 = alloca i32
%14 = alloca [1024 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%15 = call i32 (...) @setsid()
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %2
%18 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %19
19: ; preds = %17, %2
store i32 0, i32* %6
%20 = call i32 (...) @getpid()
store i32 %20, i32* %7
%21 = call i32 (...) @getpgrp()
store i32 %21, i32* %8
%22 = call i32 @getsid(i32 0)
store i32 %22, i32* %9
%23 = load i32, i32* %9
%24 = load i32, i32* %7
%25 = load i32, i32* %8
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %23, i32 %24, i32 %25)
store i32 0, i32* %10
%27 = bitcast [512 x i8]* %11 to i8*
%28 = getelementptr inbounds [512 x i8], [512 x i8]* %11, i64 0, i64 0
%29 = call i32 @forkpty(i32* %10, i8* %28, i32* null, i32* null)
store i32 %29, i32* %12
%30 = load i32, i32* %12
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %34
32: ; preds = %19
%33 = call i32 @err_sys(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %67
34: ; preds = %19
%35 = load i32, i32* %12
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %66
37: ; preds = %34
%38 = call i32 (...) @getpid()
store i32 %38, i32* %7
%39 = call i32 (...) @getpgrp()
store i32 %39, i32* %8
%40 = call i32 @getsid(i32 0)
store i32 %40, i32* %9
%41 = load i32, i32* %9
%42 = load i32, i32* %7
%43 = load i32, i32* %8
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i32 %41, i32 %42, i32 %43)
%45 = load i32, i32* @O_RDWR
%46 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 %45)
store i32 %46, i32* %13
%47 = load i32, i32* %13
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %52
49: ; preds = %37
%50 = load i32, i32* @errno
%51 = call i32 @err_msg(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0), i32 %50)
br label %55
52: ; preds = %37
%53 = load i32, i32* %13
%54 = call i32 @err_msg(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0), i32 %53)
br label %55
55: ; preds = %52, %49
%56 = load i32, i32* %13
%57 = call i32 @close(i32 %56)
%58 = load i32, i32* %10
%59 = load i32, i32* %10
%60 = call i8* @ttyname(i32 %59)
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.7, i64 0, i64 0), i32 %58, i8* %60)
%62 = call i32 @execlp(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.9, i64 0, i64 0), i32* null)
%63 = load i32, i32* @errno
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.10, i64 0, i64 0), i32 %63)
%65 = call i32 @exit(i32 -1)
unreachable
66: ; preds = %34
br label %67
67: ; preds = %66, %32
%68 = load i32, i32* %10
%69 = getelementptr inbounds [512 x i8], [512 x i8]* %11, i64 0, i64 0
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.11, i64 0, i64 0), i32 %68, i8* %69)
%71 = bitcast [1024 x i8]* %14 to i8*
br label %72
72: ; preds = %67, %85
%73 = load i32, i32* %10
%74 = getelementptr inbounds [1024 x i8], [1024 x i8]* %14, i64 0, i64 0
%75 = call i32 @read(i32 %73, i8* %74, i32 1024)
store i32 %75, i32* %6
%76 = load i32, i32* %6
%77 = icmp sgt i32 %76, 0
br i1 %77, label %78, label %84
78: ; preds = %72
%79 = load i32, i32* %6
%80 = sext i32 %79 to i64
%81 = getelementptr inbounds [1024 x i8], [1024 x i8]* %14, i64 0, i64 %80
store i8 0, i8* %81
%82 = getelementptr inbounds [1024 x i8], [1024 x i8]* %14, i64 0, i64 0
%83 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.12, i64 0, i64 0), i8* %82)
br label %85
84: ; preds = %72
br label %86
85: ; preds = %78
br label %72
86: ; preds = %84
%87 = load i32, i32* %10
%88 = call i32 @close(i32 %87)
ret i32 0
}

LLVM IR (-O2):
@.str = [7 x i8] c"setsid\00"
@.str.1 = [19 x i8] c"parent %u: %u, %u\0A\00"
@.str.2 = [8 x i8] c"forkpty\00"
@.str.3 = [18 x i8] c"child %u: %u, %u\0A\00"
@.str.4 = [9 x i8] c"/dev/tty\00"
@O_RDWR = global i32 0
@.str.5 = [36 x i8] c"open default tty failed, errno = %d\00"
@errno = global i32 0
@.str.6 = [29 x i8] c"open default tty OK, fd = %d\00"
@.str.7 = [14 x i8] c"hello %d: %s\0A\00"
@.str.8 = [8 x i8] c"./grpid\00"
@.str.9 = [6 x i8] c"grpid\00"
@.str.10 = [25 x i8] c"execlp failed, errno %d\0A\00"
@.str.11 = [17 x i8] c"forkpty: %d, %s\0A\00"
@.str.12 = [4 x i8] c">%s\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = alloca [512 x i8]
%5 = alloca [1024 x i8]
%6 = tail call i32 (...) @setsid()
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %8, %2
%11 = tail call i32 (...) @getpid()
%12 = tail call i32 (...) @getpgrp()
%13 = tail call i32 @getsid(i32 0)
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %13, i32 %11, i32 %12)
%15 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%16 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%17 = call i32 @forkpty(i32* %3, i8* %16, i32* null, i32* null)
switch i32 %17, label %40 [
i32 -1, label %18
i32 0, label %20
]
18: ; preds = %10
%19 = call i32 @err_sys(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %40
20: ; preds = %10
%21 = call i32 (...) @getpid()
%22 = call i32 (...) @getpgrp()
%23 = call i32 @getsid(i32 0)
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i32 %23, i32 %21, i32 %22)
%25 = load i32, i32* @O_RDWR
%26 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 %25)
%27 = icmp eq i32 %26, -1
%28 = load i32, i32* @errno
%29 = select i1 %27, i32 %28, i32 %26
%30 = select i1 %27, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0)
%31 = call i32 @err_msg(i8* %30, i32 %29)
%32 = call i32 @close(i32 %26)
%33 = load i32, i32* %3
%34 = call i8* @ttyname(i32 %33)
%35 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.7, i64 0, i64 0), i32 %33, i8* %34)
%36 = call i32 @execlp(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.9, i64 0, i64 0), i32* null)
%37 = load i32, i32* @errno
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.10, i64 0, i64 0), i32 %37)
%39 = call i32 @exit(i32 -1)
unreachable
40: ; preds = %10, %18
%41 = load i32, i32* %3
%42 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.11, i64 0, i64 0), i32 %41, i8* %16)
%43 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%44 = load i32, i32* %3
%45 = call i32 @read(i32 %44, i8* %43, i32 1024)
%46 = icmp sgt i32 %45, 0
br i1 %46, label %47, label %55
47: ; preds = %40, %47
%48 = phi i32 [ %53, %47 ], [ %45, %40 ]
%49 = zext i32 %48 to i64
%50 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 %49
store i8 0, i8* %50
%51 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.12, i64 0, i64 0), i8* %43)
%52 = load i32, i32* %3
%53 = call i32 @read(i32 %52, i8* %43, i32 1024)
%54 = icmp sgt i32 %53, 0
br i1 %54, label %47, label %55
55: ; preds = %47, %40
%56 = load i32, i32* %3
%57 = call i32 @close(i32 %56)
ret i32 0
}