#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 pid_t;

/* Variables and functions */
int EPERM;
int O_NOCTTY;
int O_RDWR;
int atoi(char *);
int attach_child(int, char *, int);
int /*<<< orphan*/ check_yama_ptrace_scope();
int /*<<< orphan*/ die(char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, char *);
scalar_t__ grantpt(int);
int open(char *, int);
int /*<<< orphan*/ printf(char *, char *);
char *ptsname(int);
int /*<<< orphan*/ stderr;
char *strerror(int);
scalar_t__ unlockpt(int);
int /*<<< orphan*/ usage(char *);
int main(int argc, char **argv) {
  int pty;
  int do_attach = 1;
  if (argc != 4) {
    usage(argv[0]);
    return 2;
  }
  if ((pty = open("/dev/ptmx", O_RDWR | O_NOCTTY)) < 0)
    die("Unable to open /dev/ptmx: %m");
  if (unlockpt(pty) < 0)
    die("Unable to unlockpt: %m");
  if (grantpt(pty) < 0)
    die("Unable to unlockpt: %m");
  if (do_attach) {
    pid_t child = atoi(argv[1]);
    int err;
    if ((err = attach_child(child, argv[3], atoi(argv[2])))) {
      fprintf(stderr, "Unable to attach to pid %d: %s\n", child, strerror(err));
      if (err == EPERM) {
        check_yama_ptrace_scope();
      }
      return 1;
    }
  } else {
    printf("Opened a new pty: %s\n", ptsname(pty));
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1,-0x18(%rbp)
cmpl   $0x4,-0x8(%rbp)
je     11ff <main+0x3f>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1080 <usage@plt>
movl   $0x2,-0x4(%rbp)
jmp    133b <main+0x17b>
mov    0x2de2(%rip),%rax
mov    (%rax),%esi
mov    0x2dc9(%rip),%rax
or     (%rax),%esi
lea    0xde8(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1235 <main+0x75>
lea    0xdda(%rip),%rdi
call   10b0 <die@plt>
mov    -0x14(%rbp),%edi
call   1060 <unlockpt@plt>
cmp    $0x0,%rax
jge    1253 <main+0x93>
lea    0xdd9(%rip),%rdi
call   10b0 <die@plt>
mov    -0x14(%rbp),%edi
call   10d0 <grantpt@plt>
cmp    $0x0,%rax
jge    1271 <main+0xb1>
lea    0xdbb(%rip),%rdi
call   10b0 <die@plt>
cmpl   $0x0,-0x18(%rbp)
je     131d <main+0x15d>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10c0 <atoi@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10c0 <atoi@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    %eax,%edx
call   1090 <attach_child@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
je     1318 <main+0x158>
mov    0x2d05(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x20(%rbp),%edi
call   10e0 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%edx
mov    %rax,%rcx
lea    0xd50(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x20(%rbp),%eax
mov    0x2cc3(%rip),%rcx
cmp    (%rcx),%eax
jne    130c <main+0x14c>
mov    $0x0,%al
call   1040 <check_yama_ptrace_scope@plt>
movl   $0x1,-0x4(%rbp)
jmp    133b <main+0x17b>
jmp    1334 <main+0x174>
mov    -0x14(%rbp),%edi
call   1030 <ptsname@plt>
mov    %rax,%rsi
lea    0xd2f(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
cmp    $0x4,%edi
jne    1278 <main+0xd8>
mov    0x2e31(%rip),%rax
mov    0x2e1a(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe37(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    11e0 <main+0x40>
lea    0xe2f(%rip),%rdi
call   1090 <die@plt>
mov    %ebp,%edi
call   1040 <unlockpt@plt>
test   %rax,%rax
jns    11f8 <main+0x58>
lea    0xe34(%rip),%rdi
call   1090 <die@plt>
mov    %ebp,%edi
call   10b0 <grantpt@plt>
test   %rax,%rax
jns    1210 <main+0x70>
lea    0xe1c(%rip),%rdi
call   1090 <die@plt>
mov    0x8(%rbx),%rdi
call   10a0 <atoi@plt>
mov    %eax,%ebp
mov    0x10(%rbx),%rdi
mov    0x18(%rbx),%rbx
call   10a0 <atoi@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1070 <attach_child@plt>
test   %eax,%eax
je     1287 <main+0xe7>
mov    %eax,%ebx
mov    0x2d8f(%rip),%rax
mov    (%rax),%r14d
mov    %ebx,%edi
call   10c0 <strerror@plt>
lea    0xdec(%rip),%rsi
mov    %r14d,%edi
mov    %ebp,%edx
mov    %rax,%rcx
call   1050 <fprintf@plt>
mov    0x2d5a(%rip),%rax
mov    $0x1,%ebp
cmp    (%rax),%ebx
jne    1289 <main+0xe9>
xor    %eax,%eax
call   1030 <check_yama_ptrace_scope@plt>
jmp    1289 <main+0xe9>
mov    (%rbx),%rdi
call   1060 <usage@plt>
mov    $0x2,%ebp
jmp    1289 <main+0xe9>
xor    %ebp,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
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 = [29 x i8] c"Unable to open /dev/ptmx: %m\00"
@.str.2 = [23 x i8] c"Unable to unlockpt: %m\00"
@stderr = global i32 0
@.str.3 = [32 x i8] c"Unable to attach to pid %d: %s\0A\00"
@EPERM = global i32 0
@.str.4 = [22 x i8] c"Opened a new pty: %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 i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 1, i32* %7
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 4
br i1 %11, label %12, label %17
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 @usage(i8* %15)
store i32 2, i32* %3
br label %73
17: ; preds = %2
%18 = load i32, i32* @O_RDWR
%19 = load i32, i32* @O_NOCTTY
%20 = or i32 %18, %19
%21 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %20)
store i32 %21, i32* %6
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %17
%24 = call i32 @die(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
br label %25
25: ; preds = %23, %17
%26 = load i32, i32* %6
%27 = call i64 @unlockpt(i32 %26)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %25
%30 = call i32 @die(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
br label %31
31: ; preds = %29, %25
%32 = load i32, i32* %6
%33 = call i64 @grantpt(i32 %32)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %31
%36 = call i32 @die(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
br label %37
37: ; preds = %35, %31
%38 = load i32, i32* %7
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %68
40: ; preds = %37
%41 = load i8**, i8*** %5
%42 = getelementptr inbounds i8*, i8** %41, i64 1
%43 = load i8*, i8** %42
%44 = call i32 @atoi(i8* %43)
store i32 %44, i32* %8
%45 = load i32, i32* %8
%46 = load i8**, i8*** %5
%47 = getelementptr inbounds i8*, i8** %46, i64 3
%48 = load i8*, i8** %47
%49 = load i8**, i8*** %5
%50 = getelementptr inbounds i8*, i8** %49, i64 2
%51 = load i8*, i8** %50
%52 = call i32 @atoi(i8* %51)
%53 = call i32 @attach_child(i32 %45, i8* %48, i32 %52)
store i32 %53, i32* %9
%54 = icmp ne i32 %53, 0
br i1 %54, label %55, label %67
55: ; preds = %40
%56 = load i32, i32* @stderr
%57 = load i32, i32* %8
%58 = load i32, i32* %9
%59 = call i8* @strerror(i32 %58)
%60 = call i32 @fprintf(i32 %56, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i32 %57, i8* %59)
%61 = load i32, i32* %9
%62 = load i32, i32* @EPERM
%63 = icmp eq i32 %61, %62
br i1 %63, label %64, label %66
64: ; preds = %55
%65 = call i32 (...) @check_yama_ptrace_scope()
br label %66
66: ; preds = %64, %55
store i32 1, i32* %3
br label %73
67: ; preds = %40
br label %72
68: ; preds = %37
%69 = load i32, i32* %6
%70 = call i8* @ptsname(i32 %69)
%71 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i8* %70)
br label %72
72: ; preds = %68, %67
store i32 0, i32* %3
br label %73
73: ; preds = %72, %66, %12
%74 = load i32, i32* %3
ret i32 %74
}

LLVM IR (-O2):
@.str = [10 x i8] c"/dev/ptmx\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@.str.1 = [29 x i8] c"Unable to open /dev/ptmx: %m\00"
@.str.2 = [23 x i8] c"Unable to unlockpt: %m\00"
@stderr = global i32 0
@.str.3 = [32 x i8] c"Unable to attach to pid %d: %s\0A\00"
@EPERM = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 4
br i1 %3, label %7, label %4
4: ; preds = %2
%5 = load i8*, i8** %1
%6 = tail call i32 @usage(i8* %5)
br label %44
7: ; preds = %2
%8 = load i32, i32* @O_RDWR
%9 = load i32, i32* @O_NOCTTY
%10 = or i32 %9, %8
%11 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %7
%14 = tail call i32 @die(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
br label %15
15: ; preds = %13, %7
%16 = tail call i64 @unlockpt(i32 %11)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = tail call i32 @die(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
br label %20
20: ; preds = %18, %15
%21 = tail call i64 @grantpt(i32 %11)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = tail call i32 @die(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
br label %25
25: ; preds = %20, %23
%26 = getelementptr inbounds i8*, i8** %1, i64 1
%27 = load i8*, i8** %26
%28 = tail call i32 @atoi(i8* %27)
%29 = getelementptr inbounds i8*, i8** %1, i64 3
%30 = load i8*, i8** %29
%31 = getelementptr inbounds i8*, i8** %1, i64 2
%32 = load i8*, i8** %31
%33 = tail call i32 @atoi(i8* %32)
%34 = tail call i32 @attach_child(i32 %28, i8* %30, i32 %33)
%35 = icmp eq i32 %34, 0
br i1 %35, label %44, label %36
36: ; preds = %25
%37 = load i32, i32* @stderr
%38 = tail call i8* @strerror(i32 %34)
%39 = tail call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i32 %28, i8* %38)
%40 = load i32, i32* @EPERM
%41 = icmp eq i32 %34, %40
br i1 %41, label %42, label %44
42: ; preds = %36
%43 = tail call i32 (...) @check_yama_ptrace_scope()
br label %44
44: ; preds = %42, %36, %25, %4
%45 = phi i32 [ 2, %4 ], [ 0, %25 ], [ 1, %36 ], [ 1, %42 ]
ret i32 %45
}