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

/* Variables and functions */
int /*<<< orphan*/ memset(char *, char, int);
int /*<<< orphan*/ openpty(int *, int *, int /*<<< orphan*/ *,
                           int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char **argv) {
  int amaster;
  int aslave;
  openpty(&amaster, &aslave, NULL, NULL, NULL);
  printf("Amaster fd[%i], aslave fd[%i]\n", amaster, aslave);
  printf("Sending ls to pty\n");
  write(aslave, "ls", 2);
  printf("Reading pty output\n");
  int readed_bytes;
  char buffer[256];
  do {
    memset(buffer, '\0', 256);
    readed_bytes = read(amaster, buffer, 255);
    printf("%s", buffer);
  } while (readed_bytes == 255);
  printf("***PTY output end");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x14(%rbp),%rdi
lea    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   1070 <openpty@plt>
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
lea    0xe72(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe83(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%edi
lea    0xe85(%rip),%rsi
mov    $0x2,%edx
call   1030 <write@plt>
lea    0xe77(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x120(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1050 <memset@plt>
mov    -0x14(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    $0xff,%edx
call   1060 <read@plt>
mov    %eax,-0x1c(%rbp)
lea    -0x120(%rbp),%rsi
lea    0xe4c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0xff,-0x1c(%rbp)
je     11c5 <main+0x75>
lea    0xe34(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x110,%rsp
lea    0xc(%rsp),%rdi
lea    0x8(%rsp),%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1080 <openpty@plt>
mov    0xc(%rsp),%esi
mov    0x8(%rsp),%edx
lea    0xe70(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe99(%rip),%rdi
call   1030 <puts@plt>
mov    0x8(%rsp),%edi
lea    0xe71(%rip),%rsi
mov    $0x2,%edx
call   1040 <write@plt>
lea    0xe8a(%rip),%rdi
call   1030 <puts@plt>
lea    0x10(%rsp),%rbx
lea    0xe52(%rip),%r14
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1060 <memset@plt>
mov    0xc(%rsp),%edi
mov    %rbx,%rsi
mov    $0xff,%edx
call   1070 <read@plt>
mov    %eax,%ebp
mov    %r14,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
cmp    $0xff,%ebp
je     11d0 <main+0x70>
lea    0xe17(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x110,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [31 x i8] c"Amaster fd[%i], aslave fd[%i]\0A\00"
@.str.1 = [19 x i8] c"Sending ls to pty\0A\00"
@.str.2 = [3 x i8] c"ls\00"
@.str.3 = [20 x i8] c"Reading pty output\0A\00"
@.str.4 = [3 x i8] c"%s\00"
@.str.5 = [18 x i8] c"***PTY output end\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 [256 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = call i32 @openpty(i32* %6, i32* %7, i32* null, i32* null, i32* null)
%11 = load i32, i32* %6
%12 = load i32, i32* %7
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i32 %11, i32 %12)
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%15 = load i32, i32* %7
%16 = call i32 @write(i32 %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 2)
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
br label %18
18: ; preds = %26, %2
%19 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%20 = call i32 @memset(i8* %19, i8 signext 0, i32 256)
%21 = load i32, i32* %6
%22 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%23 = call i32 @read(i32 %21, i8* %22, i32 255)
store i32 %23, i32* %8
%24 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %24)
br label %26
26: ; preds = %18
%27 = load i32, i32* %8
%28 = icmp eq i32 %27, 255
br i1 %28, label %18, label %29
29: ; preds = %26
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str = [31 x i8] c"Amaster fd[%i], aslave fd[%i]\0A\00"
@.str.2 = [3 x i8] c"ls\00"
@.str.4 = [3 x i8] c"%s\00"
@.str.5 = [18 x i8] c"***PTY output end\00"
@str = [18 x i8] c"Sending ls to pty\00"
@str.6 = [19 x i8] c"Reading pty output\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca [256 x i8]
%6 = bitcast i32* %3 to i8*
%7 = bitcast i32* %4 to i8*
%8 = call i32 @openpty(i32* %3, i32* %4, i32* null, i32* null, i32* null)
%9 = load i32, i32* %3
%10 = load i32, i32* %4
%11 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i32 %9, i32 %10)
%12 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
%13 = load i32, i32* %4
%14 = call i32 @write(i32 %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 2)
%15 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.6, i64 0, i64 0))
%16 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
br label %17
17: ; preds = %17, %2
%18 = call i32 @memset(i8* %16, i8 signext 0, i32 256)
%19 = load i32, i32* %3
%20 = call i32 @read(i32 %19, i8* %16, i32 255)
%21 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %16)
%22 = icmp eq i32 %20, 255
br i1 %22, label %17, label %23
23: ; preds = %17
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0))
ret i32 0
}