#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 */
struct termios {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ cfmakeraw(struct termios *);
int /*<<< orphan*/ close(int);
int getpt();
scalar_t__ grantpt(int);
int /*<<< orphan*/ printf(char *, ...);
char *ptsname(int);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ tcgetattr(int, struct termios *);
int /*<<< orphan*/ tcsetattr(int, int /*<<< orphan*/, struct termios *);
scalar_t__ unlockpt(int);
int /*<<< orphan*/ write(int, char *, int);
int main() {
  int ptm;
  char c;
  char c2 = '_';
  struct termios t;
  if ((ptm = getpt()) < 0) {
    printf("error creating pair\n");
    return -1;
  }
  if (grantpt(ptm) < 0 || unlockpt(ptm) < 0) {
    printf("error in grant/unlock\n");
    return -1;
  }
  tcgetattr(ptm, &t);
  cfmakeraw(&t);
  tcsetattr(ptm, TCSANOW, &t);
  printf("%s\n", ptsname(ptm));
  while (read(ptm, &c, 1)) {
    write(ptm, &c, 1);
    write(ptm, &c2, 1);
  }
  close(ptm);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
movb   $0x5f,-0xa(%rbp)
mov    $0x0,%al
call   1070 <getpt@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    11f0 <main+0x40>
lea    0xe23(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12bb <main+0x10b>
mov    -0x8(%rbp),%edi
call   10d0 <grantpt@plt>
cmp    $0x0,%rax
jl     1214 <main+0x64>
mov    -0x8(%rbp),%edi
call   1090 <unlockpt@plt>
cmp    $0x0,%rax
jge    122e <main+0x7e>
lea    0xdfa(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12bb <main+0x10b>
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   10b0 <tcgetattr@plt>
lea    -0x10(%rbp),%rdi
call   10a0 <cfmakeraw@plt>
mov    -0x8(%rbp),%edi
mov    0x2d9b(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   10c0 <tcsetattr@plt>
mov    -0x8(%rbp),%edi
call   1030 <ptsname@plt>
mov    %rax,%rsi
lea    0xdc2(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
lea    -0x9(%rbp),%rsi
mov    $0x1,%edx
call   1080 <read@plt>
cmp    $0x0,%rax
je     12b3 <main+0x103>
mov    -0x8(%rbp),%edi
lea    -0x9(%rbp),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    -0x8(%rbp),%edi
lea    -0xa(%rbp),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
jmp    1271 <main+0xc1>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
movb   $0x5f,0x6(%rsp)
xor    %eax,%eax
call   1070 <getpt@plt>
test   %eax,%eax
js     128d <main+0xdd>
mov    %eax,%ebx
mov    %eax,%edi
call   10d0 <grantpt@plt>
test   %rax,%rax
js     1296 <main+0xe6>
mov    %ebx,%edi
call   1090 <unlockpt@plt>
test   %rax,%rax
js     1296 <main+0xe6>
lea    0x8(%rsp),%r14
mov    %ebx,%edi
mov    %r14,%rsi
call   10b0 <tcgetattr@plt>
mov    %r14,%rdi
call   10a0 <cfmakeraw@plt>
mov    0x2ddb(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   10c0 <tcsetattr@plt>
mov    %ebx,%edi
call   1030 <ptsname@plt>
mov    %rax,%rdi
call   1040 <puts@plt>
lea    0x7(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1080 <read@plt>
test   %rax,%rax
je     1282 <main+0xd2>
lea    0x7(%rsp),%r14
lea    0x6(%rsp),%r15
nopl   0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1050 <write@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1050 <write@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1080 <read@plt>
test   %rax,%rax
jne    1250 <main+0xa0>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
jmp    12a7 <main+0xf7>
lea    0xd82(%rip),%rdi
jmp    129d <main+0xed>
lea    0xd63(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
@.str = [21 x i8] c"error creating pair\0A\00"
@.str.1 = [23 x i8] c"error in grant/unlock\0A\00"
@TCSANOW = global i32 0
@.str.2 = [4 x i8] c"%s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8
%4 = alloca i8
%5 = alloca %struct.termios
store i32 0, i32* %1
store i8 95, i8* %4
%6 = call i32 (...) @getpt()
store i32 %6, i32* %2
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %42
10: ; preds = %0
%11 = load i32, i32* %2
%12 = call i64 @grantpt(i32 %11)
%13 = icmp slt i64 %12, 0
br i1 %13, label %18, label %14
14: ; preds = %10
%15 = load i32, i32* %2
%16 = call i64 @unlockpt(i32 %15)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %14, %10
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %42
20: ; preds = %14
%21 = load i32, i32* %2
%22 = call i32 @tcgetattr(i32 %21, %struct.termios* %5)
%23 = call i32 @cfmakeraw(%struct.termios* %5)
%24 = load i32, i32* %2
%25 = load i32, i32* @TCSANOW
%26 = call i32 @tcsetattr(i32 %24, i32 %25, %struct.termios* %5)
%27 = load i32, i32* %2
%28 = call i8* @ptsname(i32 %27)
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %28)
br label %30
30: ; preds = %34, %20
%31 = load i32, i32* %2
%32 = call i64 @read(i32 %31, i8* %3, i32 1)
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %39
34: ; preds = %30
%35 = load i32, i32* %2
%36 = call i32 @write(i32 %35, i8* %3, i32 1)
%37 = load i32, i32* %2
%38 = call i32 @write(i32 %37, i8* %4, i32 1)
br label %30
39: ; preds = %30
%40 = load i32, i32* %2
%41 = call i32 @close(i32 %40)
br label %42
42: ; preds = %39, %18, %8
%43 = load i32, i32* %1
ret i32 %43
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@TCSANOW = global i32 0
@str = [22 x i8] c"error in grant/unlock\00"
@str.3 = [20 x i8] c"error creating pair\00"
define i32 @main() {
%1 = alloca i8
%2 = alloca i8
%3 = alloca %struct.termios
store i8 95, i8* %2
%4 = bitcast %struct.termios* %3 to i8*
%5 = tail call i32 (...) @getpt()
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.3, i64 0, i64 0))
br label %33
9: ; preds = %0
%10 = tail call i64 @grantpt(i32 %5)
%11 = icmp slt i64 %10, 0
br i1 %11, label %15, label %12
12: ; preds = %9
%13 = tail call i64 @unlockpt(i32 %5)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %12, %9
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
br label %33
17: ; preds = %12
%18 = call i32 @tcgetattr(i32 %5, %struct.termios* %3)
%19 = call i32 @cfmakeraw(%struct.termios* %3)
%20 = load i32, i32* @TCSANOW
%21 = call i32 @tcsetattr(i32 %5, i32 %20, %struct.termios* %3)
%22 = call i8* @ptsname(i32 %5)
%23 = call i32 @puts(i8* dereferenceable(1) %22)
%24 = call i64 @read(i32 %5, i8* %1, i32 1)
%25 = icmp eq i64 %24, 0
br i1 %25, label %31, label %26
26: ; preds = %17, %26
%27 = call i32 @write(i32 %5, i8* %1, i32 1)
%28 = call i32 @write(i32 %5, i8* %2, i32 1)
%29 = call i64 @read(i32 %5, i8* %1, i32 1)
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %26
31: ; preds = %26, %17
%32 = call i32 @close(i32 %5)
br label %33
33: ; preds = %31, %15, %7
%34 = phi i32 [ -1, %7 ], [ -1, %15 ], [ 0, %31 ]
ret i32 %34
}