#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 */
scalar_t__ EAGAIN;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
scalar_t__ errno;
int execlp(char *, char *, void *);
int fcntl(int, int /*<<< orphan*/, int);
int forkpty(int *, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
            int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ putchar(char);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strlen(char const *);
int /*<<< orphan*/ usleep(int);
int write(int, char const *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int fd;
  pid_t pid = forkpty(&fd, NULL, NULL, NULL);
  if (pid == -1) {
    perror("forkpty");
    return 1;
  } else if (pid == 0) {
    if (execlp("/bin/sh", "sh", (void *)0) == -1) {
      perror("execlp");
    }
    fprintf(stderr, "program exited.\n");
    return 1;
  }
  printf("Child process: %d\n", pid);
  printf("master fd: %d\n", fd);
  int flags;
  if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
    flags = 0;
  if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) {
    perror("fcntl");
    return 1;
  }
  const char *cmd = "echo $PS1\n";
  if (write(fd, cmd, strlen(cmd)) == -1) {
    perror("write");
    return 1;
  }
  char buf[255];
  while (1) {
    int nread = read(fd, buf, 254);
    if (nread == -1) {
      if (errno == EAGAIN) {
        usleep(1000);
        continue;
      }
      perror("read");
      break;
    }
    int i;
    for (i = 0; i < nread; i++) {
      putchar(buf[i]);
    }
  }
  printf("Done\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x14(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rsi
mov    %rcx,%rdx
call   10a0 <forkpty@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1201 <main+0x51>
lea    0xe10(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1404 <main+0x254>
cmpl   $0x0,-0x18(%rbp)
jne    1258 <main+0xa8>
lea    0xdf6(%rip),%rdi
lea    0xdf4(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   10c0 <execlp@plt>
cmp    $0xffffffff,%eax
jne    1237 <main+0x87>
lea    0xdde(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2d9a(%rip),%rax
mov    (%rax),%edi
lea    0xdd0(%rip),%rsi
call   1090 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1404 <main+0x254>
jmp    125d <main+0xad>
mov    -0x18(%rbp),%esi
lea    0xdc1(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x14(%rbp),%esi
lea    0xdc3(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x14(%rbp),%edi
mov    0x2d37(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <fcntl@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    12a5 <main+0xf5>
movl   $0x0,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2d21(%rip),%rax
mov    (%rax),%esi
mov    -0x1c(%rbp),%edx
mov    0x2d2d(%rip),%rax
or     (%rax),%edx
call   1040 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    12e3 <main+0x133>
lea    0xd78(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1404 <main+0x254>
lea    0xd66(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x144(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x140(%rbp)
mov    -0x28(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x144(%rbp),%edi
mov    -0x140(%rbp),%rsi
mov    %eax,%edx
call   1050 <write@plt>
cmp    $0xffffffff,%eax
jne    1340 <main+0x190>
lea    0xd2c(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1404 <main+0x254>
jmp    1345 <main+0x195>
mov    -0x14(%rbp),%edi
lea    -0x130(%rbp),%rsi
mov    $0xfe,%edx
call   1080 <read@plt>
mov    %eax,-0x134(%rbp)
cmpl   $0xffffffff,-0x134(%rbp)
jne    13a6 <main+0x1f6>
mov    0x2c55(%rip),%rax
mov    (%rax),%rax
mov    0x2c33(%rip),%rcx
cmp    (%rcx),%rax
jne    1395 <main+0x1e5>
mov    $0x3e8,%edi
call   10d0 <usleep@plt>
jmp    1345 <main+0x195>
lea    0xcc5(%rip),%rdi
call   10b0 <perror@plt>
jmp    13ef <main+0x23f>
movl   $0x0,-0x138(%rbp)
mov    -0x138(%rbp),%eax
cmp    -0x134(%rbp),%eax
jge    13ea <main+0x23a>
movslq -0x138(%rbp),%rax
movsbl -0x130(%rbp,%rax,1),%edi
ff
call   1030 <putchar@plt>
mov    -0x138(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x138(%rbp)
jmp    13b0 <main+0x200>
jmp    1345 <main+0x195>
lea    0xc70(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x150,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x110,%rsp
lea    0xc(%rsp),%rdi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   10b0 <forkpty@plt>
test   %eax,%eax
jne    122d <main+0x6d>
lea    0xe1d(%rip),%rdi
lea    0xe1b(%rip),%rsi
xor    %edx,%edx
call   10d0 <execlp@plt>
cmp    $0xffffffff,%eax
je     12a7 <main+0xe7>
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
lea    0xe05(%rip),%rsi
call   10a0 <fprintf@plt>
mov    $0x1,%eax
add    $0x110,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
cmp    $0xffffffff,%eax
jne    123e <main+0x7e>
lea    0xdc7(%rip),%rdi
jmp    12e3 <main+0x123>
lea    0xde3(%rip),%rdi
xor    %ebx,%ebx
mov    %eax,%esi
xor    %eax,%eax
call   1080 <printf@plt>
mov    0xc(%rsp),%esi
lea    0xde0(%rip),%rdi
xor    %eax,%eax
call   1080 <printf@plt>
mov    0xc(%rsp),%edi
mov    0x2d53(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <fcntl@plt>
cmp    $0xffffffff,%eax
cmovne %eax,%ebx
mov    0xc(%rsp),%edi
mov    0x2d49(%rip),%rax
mov    (%rax),%esi
mov    0x2d58(%rip),%rax
or     (%rax),%ebx
mov    %ebx,%edx
call   1050 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    12b8 <main+0xf8>
lea    0xda5(%rip),%rdi
jmp    12e3 <main+0x123>
lea    0xd62(%rip),%rdi
call   10c0 <perror@plt>
jmp    1202 <main+0x42>
mov    0xc(%rsp),%ebx
lea    0xd8d(%rip),%r14
mov    %r14,%rdi
call   1070 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1060 <write@plt>
cmp    $0xffffffff,%eax
jne    12ed <main+0x12d>
lea    0xd78(%rip),%rdi
call   10c0 <perror@plt>
jmp    1217 <main+0x57>
lea    0x10(%rsp),%r12
mov    0x2ccf(%rip),%r14
mov    0x2cb0(%rip),%r15
mov    0xc(%rsp),%edi
mov    %r12,%rsi
mov    $0xfe,%edx
call   1090 <read@plt>
cmp    $0xffffffff,%eax
je     1340 <main+0x180>
test   %eax,%eax
jle    1300 <main+0x140>
mov    %eax,%r13d
xor    %ebx,%ebx
nop
movsbl 0x10(%rsp,%rbx,1),%edi
call   1030 <putchar@plt>
add    $0x1,%rbx
cmp    %rbx,%r13
jne    1320 <main+0x160>
jmp    1300 <main+0x140>
cs nopw 0x0(%rax,%rax,1)
nop
mov    (%r14),%rax
cmp    (%r15),%rax
jne    1354 <main+0x194>
mov    $0x3e8,%edi
call   10e0 <usleep@plt>
jmp    1300 <main+0x140>
lea    0xd06(%rip),%rdi
call   10c0 <perror@plt>
lea    0xcff(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
jmp    121c <main+0x5c>

LLVM IR (-O0):
@.str = [8 x i8] c"forkpty\00"
@.str.1 = [8 x i8] c"/bin/sh\00"
@.str.2 = [3 x i8] c"sh\00"
@.str.3 = [7 x i8] c"execlp\00"
@stderr = global i32 0
@.str.4 = [17 x i8] c"program exited.\0A\00"
@.str.5 = [19 x i8] c"Child process: %d\0A\00"
@.str.6 = [15 x i8] c"master fd: %d\0A\00"
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str.7 = [6 x i8] c"fcntl\00"
@.str.8 = [11 x i8] c"echo $PS1\0A\00"
@.str.9 = [6 x i8] c"write\00"
@errno = global i64 0
@EAGAIN = global i64 0
@.str.10 = [5 x i8] c"read\00"
@.str.11 = [6 x i8] c"Done\0A\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 i8*
%10 = alloca [255 x i8]
%11 = alloca i32
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = call i32 @forkpty(i32* %6, i32* null, i32* null, i32* null)
store i32 %13, i32* %7
%14 = load i32, i32* %7
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %91
18: ; preds = %2
%19 = load i32, i32* %7
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %29
21: ; preds = %18
%22 = call i32 @execlp(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* null)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %26
24: ; preds = %21
%25 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %26
26: ; preds = %24, %21
%27 = load i32, i32* @stderr
%28 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %3
br label %91
29: ; preds = %18
br label %30
30: ; preds = %29
%31 = load i32, i32* %7
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i32 %31)
%33 = load i32, i32* %6
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0), i32 %33)
%35 = load i32, i32* %6
%36 = load i32, i32* @F_GETFL
%37 = call i32 @fcntl(i32 %35, i32 %36, i32 0)
store i32 %37, i32* %8
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %40
39: ; preds = %30
store i32 0, i32* %8
br label %40
40: ; preds = %39, %30
%41 = load i32, i32* %6
%42 = load i32, i32* @F_SETFL
%43 = load i32, i32* %8
%44 = load i32, i32* @O_NONBLOCK
%45 = or i32 %43, %44
%46 = call i32 @fcntl(i32 %41, i32 %42, i32 %45)
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %50
48: ; preds = %40
%49 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0))
store i32 1, i32* %3
br label %91
50: ; preds = %40
store i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.8, i64 0, i64 0), i8** %9
%51 = load i32, i32* %6
%52 = load i8*, i8** %9
%53 = load i8*, i8** %9
%54 = call i32 @strlen(i8* %53)
%55 = call i32 @write(i32 %51, i8* %52, i32 %54)
%56 = icmp eq i32 %55, -1
br i1 %56, label %57, label %59
57: ; preds = %50
%58 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.9, i64 0, i64 0))
store i32 1, i32* %3
br label %91
59: ; preds = %50
br label %60
60: ; preds = %59, %70, %88
%61 = load i32, i32* %6
%62 = getelementptr inbounds [255 x i8], [255 x i8]* %10, i64 0, i64 0
%63 = call i32 @read(i32 %61, i8* %62, i32 254)
store i32 %63, i32* %11
%64 = load i32, i32* %11
%65 = icmp eq i32 %64, -1
br i1 %65, label %66, label %74
66: ; preds = %60
%67 = load i64, i64* @errno
%68 = load i64, i64* @EAGAIN
%69 = icmp eq i64 %67, %68
br i1 %69, label %70, label %72
70: ; preds = %66
%71 = call i32 @usleep(i32 1000)
br label %60
72: ; preds = %66
%73 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.10, i64 0, i64 0))
br label %89
74: ; preds = %60
store i32 0, i32* %12
br label %75
75: ; preds = %85, %74
%76 = load i32, i32* %12
%77 = load i32, i32* %11
%78 = icmp slt i32 %76, %77
br i1 %78, label %79, label %88
79: ; preds = %75
%80 = load i32, i32* %12
%81 = sext i32 %80 to i64
%82 = getelementptr inbounds [255 x i8], [255 x i8]* %10, i64 0, i64 %81
%83 = load i8, i8* %82
%84 = call i32 @putchar(i8 signext %83)
br label %85
85: ; preds = %79
%86 = load i32, i32* %12
%87 = add nsw i32 %86, 1
store i32 %87, i32* %12
br label %75
88: ; preds = %75
br label %60
89: ; preds = %72
%90 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.11, i64 0, i64 0))
store i32 0, i32* %3
br label %91
91: ; preds = %89, %57, %48, %26, %16
%92 = load i32, i32* %3
ret i32 %92
}

LLVM IR (-O2):
@.str = [8 x i8] c"forkpty\00"
@.str.1 = [8 x i8] c"/bin/sh\00"
@.str.2 = [3 x i8] c"sh\00"
@.str.3 = [7 x i8] c"execlp\00"
@stderr = global i32 0
@.str.4 = [17 x i8] c"program exited.\0A\00"
@.str.5 = [19 x i8] c"Child process: %d\0A\00"
@.str.6 = [15 x i8] c"master fd: %d\0A\00"
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str.7 = [6 x i8] c"fcntl\00"
@.str.8 = [11 x i8] c"echo $PS1\0A\00"
@.str.9 = [6 x i8] c"write\00"
@errno = global i64 0
@EAGAIN = global i64 0
@.str.10 = [5 x i8] c"read\00"
@str = [5 x i8] c"Done\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = alloca [255 x i8]
%5 = bitcast i32* %3 to i8*
%6 = call i32 @forkpty(i32* %3, i32* null, i32* null, i32* null)
switch i32 %6, label %17 [
i32 -1, label %7
i32 0, label %9
]
7: ; preds = %2
%8 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %68
9: ; preds = %2
%10 = call i32 @execlp(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* null)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %14
14: ; preds = %12, %9
%15 = load i32, i32* @stderr
%16 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
br label %68
17: ; preds = %2
%18 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i32 %6)
%19 = load i32, i32* %3
%20 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0), i32 %19)
%21 = load i32, i32* %3
%22 = load i32, i32* @F_GETFL
%23 = call i32 @fcntl(i32 %21, i32 %22, i32 0)
%24 = icmp eq i32 %23, -1
%25 = select i1 %24, i32 0, i32 %23
%26 = load i32, i32* %3
%27 = load i32, i32* @F_SETFL
%28 = load i32, i32* @O_NONBLOCK
%29 = or i32 %28, %25
%30 = call i32 @fcntl(i32 %26, i32 %27, i32 %29)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %34
32: ; preds = %17
%33 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0))
br label %68
34: ; preds = %17
%35 = load i32, i32* %3
%36 = call i32 @strlen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.8, i64 0, i64 0))
%37 = call i32 @write(i32 %35, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.8, i64 0, i64 0), i32 %36)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %41
39: ; preds = %34
%40 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.9, i64 0, i64 0))
br label %68
41: ; preds = %34
%42 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 0
br label %43
43: ; preds = %49, %41
%44 = load i32, i32* %3
%45 = call i32 @read(i32 %44, i8* %42, i32 254)
%46 = icmp eq i32 %45, -1
br i1 %46, label %52, label %47
47: ; preds = %43
%48 = icmp sgt i32 %45, 0
br i1 %48, label %50, label %49
49: ; preds = %58, %47, %56
br label %43
50: ; preds = %47
%51 = zext i32 %45 to i64
br label %58
52: ; preds = %43
%53 = load i64, i64* @errno
%54 = load i64, i64* @EAGAIN
%55 = icmp eq i64 %53, %54
br i1 %55, label %56, label %65
56: ; preds = %52
%57 = call i32 @usleep(i32 1000)
br label %49
58: ; preds = %50, %58
%59 = phi i64 [ 0, %50 ], [ %63, %58 ]
%60 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 %59
%61 = load i8, i8* %60
%62 = call i32 @putchar(i8 signext %61)
%63 = add nuw nsw i64 %59, 1
%64 = icmp eq i64 %63, %51
br i1 %64, label %49, label %58
65: ; preds = %52
%66 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.10, i64 0, i64 0))
%67 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str, i64 0, i64 0))
br label %68
68: ; preds = %32, %65, %39, %14, %7
%69 = phi i32 [ 1, %7 ], [ 1, %14 ], [ 1, %32 ], [ 1, %39 ], [ 0, %65 ]
ret i32 %69
}