#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*/ O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ grantpt(int);
int open(char *, int /*<<< orphan*/);
int posix_openpt(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *);
char *ptsname(int);
int /*<<< orphan*/ raw(int);
int /*<<< orphan*/ readwrite(int /*<<< orphan*/, int, int, int);
int /*<<< orphan*/ reset(int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
scalar_t__ symlink(char *, char const *);
int /*<<< orphan*/ unlink(char const *);
scalar_t__ unlockpt(int);
int main(int argc, char *argv[]) {
  int fd;
  int slv;
  const char *ptylink;
  ptylink = NULL;
  if (argc > 1)
    ptylink = argv[1];
  fd = posix_openpt(O_RDWR);
  if (fd < 0)
    return 1;
  if (grantpt(fd) < 0)
    return 2;
  if (unlockpt(fd) < 0)
    return 3;
  if (ptylink) {
    unlink(ptylink);
    if (symlink(ptsname(fd), ptylink) < 0)
      fprintf(stderr, "Error: %s\n", strerror(errno));
  }
  printf("%s\n", ptsname(fd));
  slv = open(ptsname(fd), O_RDWR);
  if (slv < 0)
    return 4;
  raw(slv);
  close(slv);
  raw(0);
  readwrite(0, 1, fd, fd);
  close(fd);
  reset(0);
  if (ptylink)
    unlink(ptylink);
  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)
movq   $0x0,-0x20(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    1214 <main+0x34>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    0x2dcd(%rip),%rax
mov    (%rax),%edi
call   1100 <posix_openpt@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    123b <main+0x5b>
movl   $0x1,-0x4(%rbp)
jmp    1376 <main+0x196>
mov    -0x14(%rbp),%edi
call   10e0 <grantpt@plt>
cmp    $0x0,%rax
jge    1259 <main+0x79>
movl   $0x2,-0x4(%rbp)
jmp    1376 <main+0x196>
mov    -0x14(%rbp),%edi
call   10a0 <unlockpt@plt>
cmp    $0x0,%rax
jge    1277 <main+0x97>
movl   $0x3,-0x4(%rbp)
jmp    1376 <main+0x196>
cmpq   $0x0,-0x20(%rbp)
je     12da <main+0xfa>
mov    -0x20(%rbp),%rdi
call   1040 <unlink@plt>
mov    -0x14(%rbp),%edi
call   1030 <ptsname@plt>
mov    %rax,%rdi
mov    -0x20(%rbp),%rsi
call   1080 <symlink@plt>
cmp    $0x0,%rax
jge    12d5 <main+0xf5>
mov    0x2d28(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2d14(%rip),%rax
mov    (%rax),%edi
call   10f0 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    %rax,%rdx
lea    0xd30(%rip),%rsi
call   10b0 <fprintf@plt>
jmp    12da <main+0xfa>
mov    -0x14(%rbp),%edi
call   1030 <ptsname@plt>
mov    %rax,%rsi
lea    0xd1b(%rip),%rdi
call   1060 <printf@plt>
mov    -0x14(%rbp),%edi
call   1030 <ptsname@plt>
mov    %rax,%rdi
mov    0x2ce5(%rip),%rax
mov    (%rax),%esi
call   10d0 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1323 <main+0x143>
movl   $0x4,-0x4(%rbp)
jmp    1376 <main+0x196>
mov    -0x18(%rbp),%edi
call   1070 <raw@plt>
mov    -0x18(%rbp),%edi
call   1090 <close@plt>
xor    %edi,%edi
call   1070 <raw@plt>
mov    -0x14(%rbp),%edx
mov    -0x14(%rbp),%ecx
xor    %edi,%edi
mov    $0x1,%esi
call   1050 <readwrite@plt>
mov    -0x14(%rbp),%edi
call   1090 <close@plt>
xor    %edi,%edi
call   10c0 <reset@plt>
cmpq   $0x0,-0x20(%rbp)
je     136f <main+0x18f>
mov    -0x20(%rbp),%rdi
call   1040 <unlink@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
cmp    $0x2,%edi
jl     11f2 <main+0x12>
mov    0x8(%rsi),%r14
jmp    11f5 <main+0x15>
xor    %r14d,%r14d
mov    0x2dec(%rip),%rbx
mov    (%rbx),%edi
call   1100 <posix_openpt@plt>
test   %eax,%eax
js     12ec <main+0x10c>
mov    %eax,%ebp
mov    %eax,%edi
call   10e0 <grantpt@plt>
test   %rax,%rax
js     12f3 <main+0x113>
mov    %ebp,%edi
call   10a0 <unlockpt@plt>
test   %rax,%rax
js     12fa <main+0x11a>
test   %r14,%r14
je     127b <main+0x9b>
mov    %r14,%rdi
call   1040 <unlink@plt>
mov    %ebp,%edi
call   1030 <ptsname@plt>
mov    %rax,%rdi
mov    %r14,%rsi
call   1080 <symlink@plt>
test   %rax,%rax
jns    127b <main+0x9b>
mov    0x2d80(%rip),%rax
mov    (%rax),%r15d
mov    0x2d6e(%rip),%rax
mov    (%rax),%edi
call   10f0 <strerror@plt>
lea    0xd90(%rip),%rsi
mov    %r15d,%edi
mov    %rax,%rdx
call   10b0 <fprintf@plt>
mov    %ebp,%edi
call   1030 <ptsname@plt>
lea    0xd7e(%rip),%rdi
mov    %rax,%rsi
call   1060 <printf@plt>
mov    %ebp,%edi
call   1030 <ptsname@plt>
mov    (%rbx),%esi
mov    %rax,%rdi
call   10d0 <open@plt>
test   %eax,%eax
js     1301 <main+0x121>
mov    %eax,%ebx
mov    %eax,%edi
call   1070 <raw@plt>
mov    %ebx,%edi
call   1090 <close@plt>
xor    %ebx,%ebx
xor    %edi,%edi
call   1070 <raw@plt>
xor    %edi,%edi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %ebp,%ecx
call   1050 <readwrite@plt>
mov    %ebp,%edi
call   1090 <close@plt>
xor    %edi,%edi
call   10c0 <reset@plt>
test   %r14,%r14
je     1306 <main+0x126>
mov    %r14,%rdi
call   1040 <unlink@plt>
jmp    1306 <main+0x126>
mov    $0x1,%ebx
jmp    1306 <main+0x126>
mov    $0x2,%ebx
jmp    1306 <main+0x126>
mov    $0x3,%ebx
jmp    1306 <main+0x126>
mov    $0x4,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDWR = global i32 0
@stderr = global i32 0
@.str = [11 x i8] c"Error: %s\0A\00"
@errno = global i32 0
@.str.1 = [4 x i8] c"%s\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 i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* null, i8** %8
%9 = load i32, i32* %4
%10 = icmp sgt i32 %9, 1
br i1 %10, label %11, label %15
11: ; preds = %2
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 1
%14 = load i8*, i8** %13
store i8* %14, i8** %8
br label %15
15: ; preds = %11, %2
%16 = load i32, i32* @O_RDWR
%17 = call i32 @posix_openpt(i32 %16)
store i32 %17, i32* %6
%18 = load i32, i32* %6
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %15
store i32 1, i32* %3
br label %77
21: ; preds = %15
%22 = load i32, i32* %6
%23 = call i64 @grantpt(i32 %22)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %21
store i32 2, i32* %3
br label %77
26: ; preds = %21
%27 = load i32, i32* %6
%28 = call i64 @unlockpt(i32 %27)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %26
store i32 3, i32* %3
br label %77
31: ; preds = %26
%32 = load i8*, i8** %8
%33 = icmp ne i8* %32, null
br i1 %33, label %34, label %48
34: ; preds = %31
%35 = load i8*, i8** %8
%36 = call i32 @unlink(i8* %35)
%37 = load i32, i32* %6
%38 = call i8* @ptsname(i32 %37)
%39 = load i8*, i8** %8
%40 = call i64 @symlink(i8* %38, i8* %39)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %47
42: ; preds = %34
%43 = load i32, i32* @stderr
%44 = load i32, i32* @errno
%45 = call i8* @strerror(i32 %44)
%46 = call i32 @fprintf(i32 %43, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %45)
br label %47
47: ; preds = %42, %34
br label %48
48: ; preds = %47, %31
%49 = load i32, i32* %6
%50 = call i8* @ptsname(i32 %49)
%51 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %50)
%52 = load i32, i32* %6
%53 = call i8* @ptsname(i32 %52)
%54 = load i32, i32* @O_RDWR
%55 = call i32 @open(i8* %53, i32 %54)
store i32 %55, i32* %7
%56 = load i32, i32* %7
%57 = icmp slt i32 %56, 0
br i1 %57, label %58, label %59
58: ; preds = %48
store i32 4, i32* %3
br label %77
59: ; preds = %48
%60 = load i32, i32* %7
%61 = call i32 @raw(i32 %60)
%62 = load i32, i32* %7
%63 = call i32 @close(i32 %62)
%64 = call i32 @raw(i32 0)
%65 = load i32, i32* %6
%66 = load i32, i32* %6
%67 = call i32 @readwrite(i32 0, i32 1, i32 %65, i32 %66)
%68 = load i32, i32* %6
%69 = call i32 @close(i32 %68)
%70 = call i32 @reset(i32 0)
%71 = load i8*, i8** %8
%72 = icmp ne i8* %71, null
br i1 %72, label %73, label %76
73: ; preds = %59
%74 = load i8*, i8** %8
%75 = call i32 @unlink(i8* %74)
br label %76
76: ; preds = %73, %59
store i32 0, i32* %3
br label %77
77: ; preds = %76, %58, %30, %25, %20
%78 = load i32, i32* %3
ret i32 %78
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@stderr = global i32 0
@.str = [11 x i8] c"Error: %s\0A\00"
@errno = global i32 0
@.str.1 = [4 x i8] c"%s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp sgt i32 %0, 1
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
br label %7
7: ; preds = %4, %2
%8 = phi i8* [ %6, %4 ], [ null, %2 ]
%9 = load i32, i32* @O_RDWR
%10 = tail call i32 @posix_openpt(i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %46, label %12
12: ; preds = %7
%13 = tail call i64 @grantpt(i32 %10)
%14 = icmp slt i64 %13, 0
br i1 %14, label %46, label %15
15: ; preds = %12
%16 = tail call i64 @unlockpt(i32 %10)
%17 = icmp slt i64 %16, 0
br i1 %17, label %46, label %18
18: ; preds = %15
%19 = icmp eq i8* %8, null
br i1 %19, label %30, label %20
20: ; preds = %18
%21 = tail call i32 @unlink(i8* %8)
%22 = tail call i8* @ptsname(i32 %10)
%23 = tail call i64 @symlink(i8* %22, i8* %8)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %30
25: ; preds = %20
%26 = load i32, i32* @stderr
%27 = load i32, i32* @errno
%28 = tail call i8* @strerror(i32 %27)
%29 = tail call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %28)
br label %30
30: ; preds = %20, %25, %18
%31 = tail call i8* @ptsname(i32 %10)
%32 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %31)
%33 = tail call i8* @ptsname(i32 %10)
%34 = load i32, i32* @O_RDWR
%35 = tail call i32 @open(i8* %33, i32 %34)
%36 = icmp slt i32 %35, 0
br i1 %36, label %46, label %37
37: ; preds = %30
%38 = tail call i32 @raw(i32 %35)
%39 = tail call i32 @close(i32 %35)
%40 = tail call i32 @raw(i32 0)
%41 = tail call i32 @readwrite(i32 0, i32 1, i32 %10, i32 %10)
%42 = tail call i32 @close(i32 %10)
%43 = tail call i32 @reset(i32 0)
br i1 %19, label %46, label %44
44: ; preds = %37
%45 = tail call i32 @unlink(i8* %8)
br label %46
46: ; preds = %37, %44, %30, %15, %12, %7
%47 = phi i32 [ 1, %7 ], [ 2, %12 ], [ 3, %15 ], [ 4, %30 ], [ 0, %44 ], [ 0, %37 ]
ret i32 %47
}