#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 O_NOCTTY;
int O_RDWR;
char *calloc(int, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ grantpt(int);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
scalar_t__ ptsname(int);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ unlockpt(int);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  char *buffer = calloc(256, sizeof(char));
  int size = 0;
  int ptcl = open("/dev/ptmx", O_RDWR | O_NOCTTY);
  if (ptcl < 0) {
    perror("open /dev/ptmx");
    return -1;
  }
  grantpt(ptcl);
  unlockpt(ptcl);
  fprintf(stderr, "client device-> %s\n", (char *)ptsname(ptcl));
  int ptma = open("/dev/ptmx", O_RDWR | O_NOCTTY);
  if (ptma < 0) {
    perror("open /dev/ptmx");
    return -1;
  }
  grantpt(ptma);
  unlockpt(ptma);
  fprintf(stderr, "master device-> %s\n", (char *)ptsname(ptma));
  while (1) {
    size = read(ptcl, buffer, 256);
    if (size > 0) {
      write(ptma, buffer, size);
      fprintf(stderr, "client-> %s", buffer);
    }
    size = read(ptma, buffer, 256);
    if (size > 0) {
      write(ptcl, buffer, size);
      fprintf(stderr, "master-> %s", buffer);
    }
  }
  free(buffer);
  close(ptcl);
  close(ptma);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x100,%edi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    0x2e21(%rip),%rax
mov    (%rax),%esi
mov    0x2e08(%rip),%rax
or     (%rax),%esi
lea    0xe2c(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1203 <main+0x73>
lea    0xe0e(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1343 <main+0x1b3>
mov    -0x20(%rbp),%edi
call   10b0 <grantpt@plt>
mov    -0x20(%rbp),%edi
call   1070 <unlockpt@plt>
mov    0x2db6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%edi
call   1030 <ptsname@plt>
mov    -0x28(%rbp),%edi
mov    %rax,%rdx
lea    0xddb(%rip),%rsi
call   1080 <fprintf@plt>
mov    0x2da8(%rip),%rax
mov    (%rax),%esi
mov    0x2d8f(%rip),%rax
or     (%rax),%esi
lea    0xdb3(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    127c <main+0xec>
lea    0xd95(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1343 <main+0x1b3>
mov    -0x24(%rbp),%edi
call   10b0 <grantpt@plt>
mov    -0x24(%rbp),%edi
call   1070 <unlockpt@plt>
mov    0x2d3d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x24(%rbp),%edi
call   1030 <ptsname@plt>
mov    -0x2c(%rbp),%edi
mov    %rax,%rdx
lea    0xd76(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x20(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x100,%edx
call   1050 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    12f8 <main+0x168>
mov    -0x24(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1040 <write@plt>
mov    0x2cea(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xd44(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x24(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x100,%edx
call   1050 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    133e <main+0x1ae>
mov    -0x20(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1040 <write@plt>
mov    0x2ca4(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xd0a(%rip),%rsi
call   1080 <fprintf@plt>
jmp    12b2 <main+0x122>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0x100,%edi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,%rbx
mov    0x2e34(%rip),%r15
mov    0x2e1d(%rip),%r12
mov    (%r12),%esi
or     (%r15),%esi
lea    0xe3c(%rip),%rdi
call   1090 <open@plt>
test   %eax,%eax
js     12d0 <main+0x140>
mov    %eax,%r14d
mov    %eax,%edi
call   10b0 <grantpt@plt>
mov    %r14d,%edi
call   1070 <unlockpt@plt>
mov    0x2de1(%rip),%r13
mov    0x0(%r13),%ebp
mov    %r14d,%edi
call   1030 <ptsname@plt>
lea    0xe0d(%rip),%rsi
mov    %ebp,%edi
mov    %rax,%rdx
call   1080 <fprintf@plt>
mov    (%r12),%esi
or     (%r15),%esi
lea    0xdeb(%rip),%rdi
call   1090 <open@plt>
test   %eax,%eax
js     12d0 <main+0x140>
mov    %eax,%ebp
mov    %eax,%edi
call   10b0 <grantpt@plt>
mov    %ebp,%edi
call   1070 <unlockpt@plt>
mov    0x0(%r13),%r15d
mov    %ebp,%edi
call   1030 <ptsname@plt>
lea    0xdda(%rip),%rsi
mov    %r15d,%edi
mov    %rax,%rdx
call   1080 <fprintf@plt>
lea    0xddc(%rip),%r15
lea    0xde1(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x100,%edx
call   1050 <read@plt>
test   %eax,%eax
jle    129f <main+0x10f>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    0x0(%r13),%edi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1080 <fprintf@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x100,%edx
call   1050 <read@plt>
test   %eax,%eax
jle    1270 <main+0xe0>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    0x0(%r13),%edi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1080 <fprintf@plt>
jmp    1270 <main+0xe0>
lea    0xd29(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"/dev/ptmx\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@.str.1 = [15 x i8] c"open /dev/ptmx\00"
@stderr = global i32 0
@.str.2 = [20 x i8] c"client device-> %s\0A\00"
@.str.3 = [20 x i8] c"master device-> %s\0A\00"
@.str.4 = [12 x i8] c"client-> %s\00"
@.str.5 = [12 x i8] c"master-> %s\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = call i8* @calloc(i32 256, i32 1)
store i8* %10, i8** %6
store i32 0, i32* %7
%11 = load i32, i32* @O_RDWR
%12 = load i32, i32* @O_NOCTTY
%13 = or i32 %11, %12
%14 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %13)
store i32 %14, i32* %8
%15 = load i32, i32* %8
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %2
%18 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %76
19: ; preds = %2
%20 = load i32, i32* %8
%21 = call i32 @grantpt(i32 %20)
%22 = load i32, i32* %8
%23 = call i32 @unlockpt(i32 %22)
%24 = load i32, i32* @stderr
%25 = load i32, i32* %8
%26 = call i64 @ptsname(i32 %25)
%27 = inttoptr i64 %26 to i8*
%28 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %27)
%29 = load i32, i32* @O_RDWR
%30 = load i32, i32* @O_NOCTTY
%31 = or i32 %29, %30
%32 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %31)
store i32 %32, i32* %9
%33 = load i32, i32* %9
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %19
%36 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %76
37: ; preds = %19
%38 = load i32, i32* %9
%39 = call i32 @grantpt(i32 %38)
%40 = load i32, i32* %9
%41 = call i32 @unlockpt(i32 %40)
%42 = load i32, i32* @stderr
%43 = load i32, i32* %9
%44 = call i64 @ptsname(i32 %43)
%45 = inttoptr i64 %44 to i8*
%46 = call i32 @fprintf(i32 %42, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %45)
br label %47
47: ; preds = %37, %75
%48 = load i32, i32* %8
%49 = load i8*, i8** %6
%50 = call i32 @read(i32 %48, i8* %49, i32 256)
store i32 %50, i32* %7
%51 = load i32, i32* %7
%52 = icmp sgt i32 %51, 0
br i1 %52, label %53, label %61
53: ; preds = %47
%54 = load i32, i32* %9
%55 = load i8*, i8** %6
%56 = load i32, i32* %7
%57 = call i32 @write(i32 %54, i8* %55, i32 %56)
%58 = load i32, i32* @stderr
%59 = load i8*, i8** %6
%60 = call i32 @fprintf(i32 %58, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i8* %59)
br label %61
61: ; preds = %53, %47
%62 = load i32, i32* %9
%63 = load i8*, i8** %6
%64 = call i32 @read(i32 %62, i8* %63, i32 256)
store i32 %64, i32* %7
%65 = load i32, i32* %7
%66 = icmp sgt i32 %65, 0
br i1 %66, label %67, label %75
67: ; preds = %61
%68 = load i32, i32* %8
%69 = load i8*, i8** %6
%70 = load i32, i32* %7
%71 = call i32 @write(i32 %68, i8* %69, i32 %70)
%72 = load i32, i32* @stderr
%73 = load i8*, i8** %6
%74 = call i32 @fprintf(i32 %72, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i8* %73)
br label %75
75: ; preds = %67, %61
br label %47
76: ; preds = %35, %17
%77 = load i32, i32* %3
ret i32 %77
}

LLVM IR (-O2):
@.str = [10 x i8] c"/dev/ptmx\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@.str.1 = [15 x i8] c"open /dev/ptmx\00"
@stderr = global i32 0
@.str.2 = [20 x i8] c"client device-> %s\0A\00"
@.str.3 = [20 x i8] c"master device-> %s\0A\00"
@.str.4 = [12 x i8] c"client-> %s\00"
@.str.5 = [12 x i8] c"master-> %s\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call dereferenceable_or_null(256) i8* @calloc(i32 256, i32 1)
%4 = load i32, i32* @O_RDWR
%5 = load i32, i32* @O_NOCTTY
%6 = or i32 %5, %4
%7 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %43, label %9
9: ; preds = %2
%10 = tail call i32 @grantpt(i32 %7)
%11 = tail call i32 @unlockpt(i32 %7)
%12 = load i32, i32* @stderr
%13 = tail call i64 @ptsname(i32 %7)
%14 = inttoptr i64 %13 to i8*
%15 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %14)
%16 = load i32, i32* @O_RDWR
%17 = load i32, i32* @O_NOCTTY
%18 = or i32 %17, %16
%19 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %18)
%20 = icmp slt i32 %19, 0
br i1 %20, label %43, label %21
21: ; preds = %9
%22 = tail call i32 @grantpt(i32 %19)
%23 = tail call i32 @unlockpt(i32 %19)
%24 = load i32, i32* @stderr
%25 = tail call i64 @ptsname(i32 %19)
%26 = inttoptr i64 %25 to i8*
%27 = tail call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %26)
br label %28
28: ; preds = %42, %21
%29 = tail call i32 @read(i32 %7, i8* %3, i32 256)
%30 = icmp sgt i32 %29, 0
br i1 %30, label %31, label %35
31: ; preds = %28
%32 = tail call i32 @write(i32 %19, i8* %3, i32 %29)
%33 = load i32, i32* @stderr
%34 = tail call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i8* %3)
br label %35
35: ; preds = %31, %28
%36 = tail call i32 @read(i32 %19, i8* %3, i32 256)
%37 = icmp sgt i32 %36, 0
br i1 %37, label %38, label %42
38: ; preds = %35
%39 = tail call i32 @write(i32 %7, i8* %3, i32 %36)
%40 = load i32, i32* @stderr
%41 = tail call i32 @fprintf(i32 %40, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i8* %3)
br label %42
42: ; preds = %38, %35
br label %28
43: ; preds = %9, %2
%44 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
ret i32 -1
}