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

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int openpty(int *, int *, char *, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int main(int argc, char *argv[]) {
  int mpty = 0, spty = 0;
  int rv = 0, n = 0;
  char spty_name[128] = {0};
  char buf[1024] = {0};
  fd_set rfds;
  rv = openpty(&mpty, &spty, spty_name, NULL, NULL);
  if (-1 == rv) {
    perror("Failed to get a pty");
    return -1;
  }
  printf("Get a pty pair, FD -- master[%d], slave[%d]\n", mpty, spty);
  printf("Slave name is %s\n", spty_name);
  FD_ZERO(&rfds);
  FD_SET(mpty, &rfds);
  while (1) {
    rv = select(mpty + 1, &rfds, &rfds, NULL, NULL);
    if (rv < 0) {
      perror("Failed to select");
      return -1;
    }
    if (FD_ISSET(mpty, &rfds)) {
      n = read(mpty, buf, 1024);
      if (n > 0) {
        printf("recv [%d] bytes:[%s]\n", n, buf);
      } else if (n == 0) {
        printf("Slave closed\n");
        break;
      } else {
        if (errno == EINTR) {
          continue;
        }
        perror("Failed to read the master\n");
        return -1;
      }
    }
  }
  close(mpty);
  close(spty);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x4b0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
lea    -0xa0(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1050 <memset@plt>
lea    -0x4a0(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
lea    -0xa0(%rbp),%rdx
lea    -0x14(%rbp),%rdi
lea    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rcx
call   10a0 <openpty@plt>
mov    %eax,-0x1c(%rbp)
mov    $0xffffffff,%eax
cmp    -0x1c(%rbp),%eax
jne    1240 <main+0xa0>
lea    0xdd1(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1392 <main+0x1f2>
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
lea    0xdc7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0xa0(%rbp),%rsi
lea    0xddf(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x4a4(%rbp),%rdi
call   1080 <FD_ZERO@plt>
mov    -0x14(%rbp),%edi
lea    -0x4a4(%rbp),%rsi
call   10c0 <FD_SET@plt>
mov    -0x14(%rbp),%edi
add    $0x1,%edi
lea    -0x4a4(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%r8d
mov    %rdx,%rsi
mov    %r8,%rcx
call   1090 <select@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    12c6 <main+0x126>
lea    0xd9e(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1392 <main+0x1f2>
mov    -0x14(%rbp),%edi
lea    -0x4a4(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1376 <main+0x1d6>
mov    -0x14(%rbp),%edi
lea    -0x4a0(%rbp),%rsi
mov    $0x400,%edx
call   1070 <read@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jle    131d <main+0x17d>
mov    -0x20(%rbp),%esi
lea    -0x4a0(%rbp),%rdx
lea    0xd53(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1371 <main+0x1d1>
cmpl   $0x0,-0x20(%rbp)
jne    133a <main+0x19a>
lea    0xd4c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    137b <main+0x1db>
mov    0x2c9f(%rip),%rax
mov    (%rax),%rax
mov    0x2c8d(%rip),%rcx
cmp    (%rcx),%rax
jne    1359 <main+0x1b9>
jmp    1284 <main+0xe4>
lea    0xd28(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1392 <main+0x1f2>
jmp    1376 <main+0x1d6>
jmp    1284 <main+0xe4>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x4b0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x490,%rsp
movl   $0x0,0x4(%rsp)
movl   $0x0,0x8(%rsp)
xorps  %xmm0,%xmm0
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
lea    0x90(%rsp),%rdi
mov    $0x400,%edx
xor    %esi,%esi
call   1060 <memset@plt>
lea    0x4(%rsp),%rdi
lea    0x8(%rsp),%rsi
lea    0x10(%rsp),%rdx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   10b0 <openpty@plt>
cmp    $0xffffffff,%eax
je     1369 <main+0x1b9>
mov    0x4(%rsp),%esi
mov    0x8(%rsp),%edx
lea    0xdd1(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xdf0(%rip),%rdi
lea    0x10(%rsp),%rsi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1090 <FD_ZERO@plt>
mov    0x4(%rsp),%edi
mov    %rbx,%rsi
call   10d0 <FD_SET@plt>
mov    0x4(%rsp),%edi
add    $0x1,%edi
mov    %rbx,%rsi
mov    %rbx,%rdx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   10a0 <select@plt>
test   %eax,%eax
js     1347 <main+0x197>
lea    0xc(%rsp),%rbx
lea    0x90(%rsp),%r14
lea    0xdbb(%rip),%r15
mov    0x2d30(%rip),%r12
mov    0x2d21(%rip),%r13
jmp    12ea <main+0x13a>
nopl   0x0(%rax)
mov    %r15,%rdi
mov    %eax,%esi
mov    %r14,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x4(%rsp),%edi
add    $0x1,%edi
mov    %rbx,%rsi
mov    %rbx,%rdx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   10a0 <select@plt>
test   %eax,%eax
js     1347 <main+0x197>
mov    0x4(%rsp),%edi
mov    %rbx,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     12cf <main+0x11f>
mov    0x4(%rsp),%edi
mov    %r14,%rsi
mov    $0x400,%edx
call   1080 <read@plt>
test   %eax,%eax
jg     12c0 <main+0x110>
je     1325 <main+0x175>
mov    (%r12),%rax
cmp    0x0(%r13),%rax
je     12cf <main+0x11f>
lea    0xd57(%rip),%rdi
jmp    134e <main+0x19e>
lea    0xd69(%rip),%rdi
call   1030 <puts@plt>
mov    0x4(%rsp),%edi
call   1070 <close@plt>
mov    0x8(%rsp),%edi
call   1070 <close@plt>
xor    %eax,%eax
jmp    1358 <main+0x1a8>
lea    0xd05(%rip),%rdi
call   10c0 <perror@plt>
mov    $0xffffffff,%eax
add    $0x490,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
lea    0xc90(%rip),%rdi
jmp    134e <main+0x19e>

LLVM IR (-O0):
@.str = [20 x i8] c"Failed to get a pty\00"
@.str.1 = [45 x i8] c"Get a pty pair, FD -- master[%d], slave[%d]\0A\00"
@.str.2 = [18 x i8] c"Slave name is %s\0A\00"
@.str.3 = [17 x i8] c"Failed to select\00"
@.str.4 = [22 x i8] c"recv [%d] bytes:[%s]\0A\00"
@.str.5 = [14 x i8] c"Slave closed\0A\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.6 = [27 x i8] c"Failed to read the master\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 i32
%10 = alloca [128 x i8]
%11 = alloca [1024 x i8]
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
store i32 0, i32* %7
store i32 0, i32* %8
store i32 0, i32* %9
%13 = bitcast [128 x i8]* %10 to i8*
%14 = bitcast [1024 x i8]* %11 to i8*
%15 = getelementptr inbounds [128 x i8], [128 x i8]* %10, i64 0, i64 0
%16 = call i32 @openpty(i32* %6, i32* %7, i8* %15, i32* null, i32* null)
store i32 %16, i32* %8
%17 = load i32, i32* %8
%18 = icmp eq i32 -1, %17
br i1 %18, label %19, label %21
19: ; preds = %2
%20 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %71
21: ; preds = %2
%22 = load i32, i32* %6
%23 = load i32, i32* %7
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i32 %22, i32 %23)
%25 = getelementptr inbounds [128 x i8], [128 x i8]* %10, i64 0, i64 0
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %25)
%27 = call i32 @FD_ZERO(i32* %12)
%28 = load i32, i32* %6
%29 = call i32 @FD_SET(i32 %28, i32* %12)
br label %30
30: ; preds = %21, %61, %65
%31 = load i32, i32* %6
%32 = add nsw i32 %31, 1
%33 = call i32 @select(i32 %32, i32* %12, i32* %12, i32* null, i32* null)
store i32 %33, i32* %8
%34 = load i32, i32* %8
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %30
%37 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %3
br label %71
38: ; preds = %30
%39 = load i32, i32* %6
%40 = call i64 @FD_ISSET(i32 %39, i32* %12)
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %65
42: ; preds = %38
%43 = load i32, i32* %6
%44 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%45 = call i32 @read(i32 %43, i8* %44, i32 1024)
store i32 %45, i32* %9
%46 = load i32, i32* %9
%47 = icmp sgt i32 %46, 0
br i1 %47, label %48, label %52
48: ; preds = %42
%49 = load i32, i32* %9
%50 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i32 %49, i8* %50)
br label %64
52: ; preds = %42
%53 = load i32, i32* %9
%54 = icmp eq i32 %53, 0
br i1 %54, label %55, label %57
55: ; preds = %52
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
br label %66
57: ; preds = %52
%58 = load i64, i64* @errno
%59 = load i64, i64* @EINTR
%60 = icmp eq i64 %58, %59
br i1 %60, label %61, label %62
61: ; preds = %57
br label %30
62: ; preds = %57
%63 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0))
store i32 -1, i32* %3
br label %71
64: ; preds = %48
br label %65
65: ; preds = %64, %38
br label %30
66: ; preds = %55
%67 = load i32, i32* %6
%68 = call i32 @close(i32 %67)
%69 = load i32, i32* %7
%70 = call i32 @close(i32 %69)
store i32 0, i32* %3
br label %71
71: ; preds = %66, %62, %36, %19
%72 = load i32, i32* %3
ret i32 %72
}

LLVM IR (-O2):
@.str = [20 x i8] c"Failed to get a pty\00"
@.str.1 = [45 x i8] c"Get a pty pair, FD -- master[%d], slave[%d]\0A\00"
@.str.2 = [18 x i8] c"Slave name is %s\0A\00"
@.str.3 = [17 x i8] c"Failed to select\00"
@.str.4 = [22 x i8] c"recv [%d] bytes:[%s]\0A\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.6 = [27 x i8] c"Failed to read the master\0A\00"
@str = [13 x i8] c"Slave closed\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca [128 x i8]
%6 = alloca [1024 x i8]
%7 = alloca i32
%8 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%9 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%10 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%12 = bitcast i32* %7 to i8*
%13 = call i32 @openpty(i32* %3, i32* %4, i8* %10, i32* null, i32* null)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %60
17: ; preds = %2
%18 = load i32, i32* %3
%19 = load i32, i32* %4
%20 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i32 %18, i32 %19)
%21 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %10)
%22 = call i32 @FD_ZERO(i32* %7)
%23 = load i32, i32* %3
%24 = call i32 @FD_SET(i32 %23, i32* %7)
%25 = load i32, i32* %3
%26 = add nsw i32 %25, 1
%27 = call i32 @select(i32 %26, i32* %7, i32* %7, i32* null, i32* null)
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %55, %17
%30 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
br label %60
31: ; preds = %17, %55
%32 = load i32, i32* %3
%33 = call i64 @FD_ISSET(i32 %32, i32* %7)
%34 = icmp eq i64 %33, 0
br i1 %34, label %55, label %35
35: ; preds = %31
%36 = load i32, i32* %3
%37 = call i32 @read(i32 %36, i8* %11, i32 1024)
%38 = icmp sgt i32 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %35
%40 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i32 %37, i8* %11)
br label %55
41: ; preds = %35
%42 = icmp eq i32 %37, 0
br i1 %42, label %43, label %49
43: ; preds = %41
%44 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
%45 = load i32, i32* %3
%46 = call i32 @close(i32 %45)
%47 = load i32, i32* %4
%48 = call i32 @close(i32 %47)
br label %60
49: ; preds = %41
%50 = load i64, i64* @errno
%51 = load i64, i64* @EINTR
%52 = icmp eq i64 %50, %51
br i1 %52, label %55, label %53
53: ; preds = %49
%54 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0))
br label %60
55: ; preds = %31, %39, %49
%56 = load i32, i32* %3
%57 = add nsw i32 %56, 1
%58 = call i32 @select(i32 %57, i32* %7, i32* %7, i32* null, i32* null)
%59 = icmp slt i32 %58, 0
br i1 %59, label %29, label %31
60: ; preds = %43, %53, %29, %15
%61 = phi i32 [ -1, %15 ], [ -1, %29 ], [ 0, %43 ], [ -1, %53 ]
ret i32 %61
}