#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 scalar_t__ pid_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
scalar_t__ EINVAL;
int EXIT_FAILURE;
int EXIT_SUCCESS;
int O_CLOEXEC;
int O_NOCTTY;
int O_RDWR;
int STDIN_FILENO;
int /*<<< orphan*/ _exit(int);
int /*<<< orphan*/ close(int);
scalar_t__ errno;
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int login_pty(int);
int open(char *, int);
int resolve_procfd_symlink(int, char *, int);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
int /*<<< orphan*/ strlen(char *);
scalar_t__ strncmp(char *, char *, int /*<<< orphan*/);
int unlockpt(int);
int wait_for_pid(scalar_t__);
int do_tiocgptpeer(char *ptmx, char *expected_procfd_contents) {
  int ret;
  int master = -1, slave = -1, fret = -1;
  master = open(ptmx, O_RDWR | O_NOCTTY | O_CLOEXEC);
  if (master < 0) {
    fprintf(stderr, "Failed to open \"%s\": %s\n", ptmx, strerror(errno));
    return -1;
  }
  ret = unlockpt(master);
  if (ret < 0) {
    fprintf(stderr, "Failed to unlock terminal\n");
    goto do_cleanup;
  }
  if (slave < 0) {
    if (errno == EINVAL) {
      fprintf(stderr, "TIOCGPTPEER is not supported. "
                      "Skipping test.\n");
      fret = EXIT_SUCCESS;
    }
    fprintf(stderr, "Failed to perform TIOCGPTPEER ioctl\n");
    goto do_cleanup;
  }
  pid_t pid = fork();
  if (pid < 0)
    goto do_cleanup;
  if (pid == 0) {
    char buf[4096];
    ret = login_pty(slave);
    if (ret < 0) {
      fprintf(stderr, "Failed to setup terminal\n");
      _exit(EXIT_FAILURE);
    }
    ret = resolve_procfd_symlink(STDIN_FILENO, buf, sizeof(buf));
    if (ret < 0) {
      fprintf(stderr, "Failed to retrieve pathname of pts "
                      "slave file descriptor\n");
      _exit(EXIT_FAILURE);
    }
    if (strncmp(expected_procfd_contents, buf,
                strlen(expected_procfd_contents)) != 0) {
      fprintf(stderr,
              "Received invalid contents for "
              "\"/proc/<pid>/fd/%d\" symlink: %s\n",
              STDIN_FILENO, buf);
      _exit(-1);
    }
    fprintf(stderr,
            "Contents of \"/proc/<pid>/fd/%d\" "
            "symlink are valid: %s\n",
            STDIN_FILENO, buf);
    _exit(EXIT_SUCCESS);
  }
  ret = wait_for_pid(pid);
  if (ret < 0)
    goto do_cleanup;
  fret = EXIT_SUCCESS;
do_cleanup:
  if (master >= 0)
    close(master);
  if (slave >= 0)
    close(slave);
  return fret;
}


x86 assembly (clang, -O0):
<do_tiocgptpeer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1050,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0xffffffff,-0x20(%rbp)
movl   $0xffffffff,-0x24(%rbp)
movl   $0xffffffff,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2df5(%rip),%rax
mov    (%rax),%esi
mov    0x2ddc(%rip),%rax
or     (%rax),%esi
mov    0x2d93(%rip),%rax
or     (%rax),%esi
call   10c0 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    126c <do_tiocgptpeer+0xac>
mov    0x2db0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x103c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x1038(%rbp)
mov    0x2d8e(%rip),%rax
mov    (%rax),%rdi
call   10d0 <strerror@plt>
mov    -0x103c(%rbp),%edi
mov    -0x1038(%rbp),%rdx
mov    %rax,%rcx
lea    0xda7(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    14a4 <do_tiocgptpeer+0x2e4>
mov    -0x20(%rbp),%edi
call   1090 <unlockpt@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    129d <do_tiocgptpeer+0xdd>
mov    0x2d48(%rip),%rax
mov    (%rax),%edi
lea    0xd88(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
jmp    147a <do_tiocgptpeer+0x2ba>
cmpl   $0x0,-0x24(%rbp)
jge    1300 <do_tiocgptpeer+0x140>
mov    0x2d1a(%rip),%rax
mov    (%rax),%rax
mov    0x2d00(%rip),%rcx
cmp    (%rcx),%rax
jne    12e4 <do_tiocgptpeer+0x124>
mov    0x2d08(%rip),%rax
mov    (%rax),%edi
lea    0xd63(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
mov    0x2cd1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2ce5(%rip),%rax
mov    (%rax),%edi
lea    0xd6e(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
jmp    147a <do_tiocgptpeer+0x2ba>
mov    $0x0,%al
call   10e0 <fork@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jge    131b <do_tiocgptpeer+0x15b>
jmp    147a <do_tiocgptpeer+0x2ba>
cmpq   $0x0,-0x30(%rbp)
jne    1453 <do_tiocgptpeer+0x293>
mov    -0x24(%rbp),%edi
call   10a0 <login_pty@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1360 <do_tiocgptpeer+0x1a0>
mov    0x2c8e(%rip),%rax
mov    (%rax),%edi
lea    0xd3c(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
mov    0x2c67(%rip),%rax
mov    (%rax),%edi
call   1050 <_exit@plt>
mov    0x2c41(%rip),%rax
mov    (%rax),%edi
lea    -0x1030(%rbp),%rsi
mov    $0x1000,%edx
call   1060 <resolve_procfd_symlink@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    13ac <do_tiocgptpeer+0x1ec>
mov    0x2c42(%rip),%rax
mov    (%rax),%edi
lea    0xd0a(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
mov    0x2c1b(%rip),%rax
mov    (%rax),%edi
call   1050 <_exit@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x1050(%rbp)
lea    -0x1030(%rbp),%rax
mov    %rax,-0x1048(%rbp)
mov    -0x18(%rbp),%rdi
call   1070 <strlen@plt>
mov    -0x1050(%rbp),%rdi
mov    -0x1048(%rbp),%rsi
mov    %eax,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
je     141e <do_tiocgptpeer+0x25e>
mov    0x2bdc(%rip),%rax
mov    (%rax),%edi
mov    0x2bab(%rip),%rax
mov    (%rax),%edx
lea    -0x1030(%rbp),%rcx
lea    0xcce(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
mov    $0xffffffff,%edi
call   1050 <_exit@plt>
mov    0x2bab(%rip),%rax
mov    (%rax),%edi
mov    0x2b7a(%rip),%rax
mov    (%rax),%edx
lea    -0x1030(%rbp),%rcx
lea    0xcdc(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
mov    0x2b64(%rip),%rax
mov    (%rax),%edi
call   1050 <_exit@plt>
mov    -0x30(%rbp),%rdi
call   1030 <wait_for_pid@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    146e <do_tiocgptpeer+0x2ae>
jmp    147a <do_tiocgptpeer+0x2ba>
mov    0x2b3b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x20(%rbp)
jl     148c <do_tiocgptpeer+0x2cc>
mov    -0x20(%rbp),%edi
call   1080 <close@plt>
cmpl   $0x0,-0x24(%rbp)
jl     149e <do_tiocgptpeer+0x2de>
mov    -0x24(%rbp),%edi
call   1080 <close@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1050,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_tiocgptpeer>:
push   %rbp
push   %r14
push   %rbx
mov    0x2e8d(%rip),%rax
mov    0x2e76(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rdi,%rbp
mov    0x2e38(%rip),%rax
or     (%rax),%esi
call   1060 <open@plt>
test   %eax,%eax
js     11cc <do_tiocgptpeer+0x7c>
mov    %eax,%ebx
mov    %eax,%edi
call   1040 <unlockpt@plt>
mov    $0xffffffff,%ebp
test   %eax,%eax
js     1201 <do_tiocgptpeer+0xb1>
mov    0x2e34(%rip),%rax
mov    (%rax),%rax
mov    0x2e22(%rip),%rcx
lea    0xebd(%rip),%r14
cmp    (%rcx),%rax
jne    1208 <do_tiocgptpeer+0xb8>
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
lea    0xe7a(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x2df0(%rip),%rax
mov    (%rax),%ebp
jmp    1208 <do_tiocgptpeer+0xb8>
mov    0x2dfd(%rip),%rax
mov    (%rax),%ebx
mov    0x2dec(%rip),%rax
mov    (%rax),%rdi
call   1070 <strerror@plt>
lea    0xe15(%rip),%rsi
mov    %ebx,%edi
mov    %rbp,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0xffffffff,%ebp
jmp    1222 <do_tiocgptpeer+0xd2>
lea    0xe11(%rip),%r14
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %ebx,%edi
call   1030 <close@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_CLOEXEC = global i32 0
@stderr = global i32 0
@.str = [25 x i8] c"Failed to open \22%s\22: %s\0A\00"
@errno = global i64 0
@.str.1 = [27 x i8] c"Failed to unlock terminal\0A\00"
@EINVAL = global i64 0
@.str.2 = [46 x i8] c"TIOCGPTPEER is not supported. Skipping test.\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.3 = [37 x i8] c"Failed to perform TIOCGPTPEER ioctl\0A\00"
@.str.4 = [26 x i8] c"Failed to setup terminal\0A\00"
@EXIT_FAILURE = global i32 0
@STDIN_FILENO = global i32 0
@.str.5 = [58 x i8] c"Failed to retrieve pathname of pts slave file descriptor\0A\00"
@.str.6 = [63 x i8] c"Received invalid contents for \22/proc/<pid>/fd/%d\22 symlink: %s\0A\00"
@.str.7 = [55 x i8] c"Contents of \22/proc/<pid>/fd/%d\22 symlink are valid: %s\0A\00"
define i32 @do_tiocgptpeer(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i64
%11 = alloca [4096 x i8]
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 -1, i32* %7
store i32 -1, i32* %8
store i32 -1, i32* %9
%12 = load i8*, i8** %4
%13 = load i32, i32* @O_RDWR
%14 = load i32, i32* @O_NOCTTY
%15 = or i32 %13, %14
%16 = load i32, i32* @O_CLOEXEC
%17 = or i32 %15, %16
%18 = call i32 @open(i8* %12, i32 %17)
store i32 %18, i32* %7
%19 = load i32, i32* %7
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %27
21: ; preds = %2
%22 = load i32, i32* @stderr
%23 = load i8*, i8** %4
%24 = load i64, i64* @errno
%25 = call i8* @strerror(i64 %24)
%26 = call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %23, i8* %25)
store i32 -1, i32* %3
br label %120
27: ; preds = %2
%28 = load i32, i32* %7
%29 = call i32 @unlockpt(i32 %28)
store i32 %29, i32* %6
%30 = load i32, i32* %6
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %27
%33 = load i32, i32* @stderr
%34 = call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
br label %106
35: ; preds = %27
%36 = load i32, i32* %8
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %49
38: ; preds = %35
%39 = load i64, i64* @errno
%40 = load i64, i64* @EINVAL
%41 = icmp eq i64 %39, %40
br i1 %41, label %42, label %46
42: ; preds = %38
%43 = load i32, i32* @stderr
%44 = call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0))
%45 = load i32, i32* @EXIT_SUCCESS
store i32 %45, i32* %9
br label %46
46: ; preds = %42, %38
%47 = load i32, i32* @stderr
%48 = call i32 (i32, i8*, ...) @fprintf(i32 %47, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0))
br label %106
49: ; preds = %35
%50 = call i64 (...) @fork()
store i64 %50, i64* %10
%51 = load i64, i64* %10
%52 = icmp slt i64 %51, 0
br i1 %52, label %53, label %54
53: ; preds = %49
br label %106
54: ; preds = %49
%55 = load i64, i64* %10
%56 = icmp eq i64 %55, 0
br i1 %56, label %57, label %98
57: ; preds = %54
%58 = load i32, i32* %8
%59 = call i32 @login_pty(i32 %58)
store i32 %59, i32* %6
%60 = load i32, i32* %6
%61 = icmp slt i32 %60, 0
br i1 %61, label %62, label %67
62: ; preds = %57
%63 = load i32, i32* @stderr
%64 = call i32 (i32, i8*, ...) @fprintf(i32 %63, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
%65 = load i32, i32* @EXIT_FAILURE
%66 = call i32 @_exit(i32 %65)
unreachable
67: ; preds = %57
%68 = load i32, i32* @STDIN_FILENO
%69 = getelementptr inbounds [4096 x i8], [4096 x i8]* %11, i64 0, i64 0
%70 = call i32 @resolve_procfd_symlink(i32 %68, i8* %69, i32 4096)
store i32 %70, i32* %6
%71 = load i32, i32* %6
%72 = icmp slt i32 %71, 0
br i1 %72, label %73, label %78
73: ; preds = %67
%74 = load i32, i32* @stderr
%75 = call i32 (i32, i8*, ...) @fprintf(i32 %74, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.5, i64 0, i64 0))
%76 = load i32, i32* @EXIT_FAILURE
%77 = call i32 @_exit(i32 %76)
unreachable
78: ; preds = %67
%79 = load i8*, i8** %5
%80 = getelementptr inbounds [4096 x i8], [4096 x i8]* %11, i64 0, i64 0
%81 = load i8*, i8** %5
%82 = call i32 @strlen(i8* %81)
%83 = call i64 @strncmp(i8* %79, i8* %80, i32 %82)
%84 = icmp ne i64 %83, 0
br i1 %84, label %85, label %91
85: ; preds = %78
%86 = load i32, i32* @stderr
%87 = load i32, i32* @STDIN_FILENO
%88 = getelementptr inbounds [4096 x i8], [4096 x i8]* %11, i64 0, i64 0
%89 = call i32 (i32, i8*, ...) @fprintf(i32 %86, i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.6, i64 0, i64 0), i32 %87, i8* %88)
%90 = call i32 @_exit(i32 -1)
unreachable
91: ; preds = %78
%92 = load i32, i32* @stderr
%93 = load i32, i32* @STDIN_FILENO
%94 = getelementptr inbounds [4096 x i8], [4096 x i8]* %11, i64 0, i64 0
%95 = call i32 (i32, i8*, ...) @fprintf(i32 %92, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.7, i64 0, i64 0), i32 %93, i8* %94)
%96 = load i32, i32* @EXIT_SUCCESS
%97 = call i32 @_exit(i32 %96)
unreachable
98: ; preds = %54
%99 = load i64, i64* %10
%100 = call i32 @wait_for_pid(i64 %99)
store i32 %100, i32* %6
%101 = load i32, i32* %6
%102 = icmp slt i32 %101, 0
br i1 %102, label %103, label %104
103: ; preds = %98
br label %106
104: ; preds = %98
%105 = load i32, i32* @EXIT_SUCCESS
store i32 %105, i32* %9
br label %106
106: ; preds = %104, %103, %53, %46, %32
%107 = load i32, i32* %7
%108 = icmp sge i32 %107, 0
br i1 %108, label %109, label %112
109: ; preds = %106
%110 = load i32, i32* %7
%111 = call i32 @close(i32 %110)
br label %112
112: ; preds = %109, %106
%113 = load i32, i32* %8
%114 = icmp sge i32 %113, 0
br i1 %114, label %115, label %118
115: ; preds = %112
%116 = load i32, i32* %8
%117 = call i32 @close(i32 %116)
br label %118
118: ; preds = %115, %112
%119 = load i32, i32* %9
store i32 %119, i32* %3
br label %120
120: ; preds = %118, %21
%121 = load i32, i32* %3
ret i32 %121
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_CLOEXEC = global i32 0
@stderr = global i32 0
@.str = [25 x i8] c"Failed to open \22%s\22: %s\0A\00"
@errno = global i64 0
@.str.1 = [27 x i8] c"Failed to unlock terminal\0A\00"
@EINVAL = global i64 0
@.str.2 = [46 x i8] c"TIOCGPTPEER is not supported. Skipping test.\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.3 = [37 x i8] c"Failed to perform TIOCGPTPEER ioctl\0A\00"
@EXIT_FAILURE = global i32 0
@STDIN_FILENO = global i32 0
define i32 @do_tiocgptpeer(i8* %0, i8* nocapture readnone %1) {
%3 = load i32, i32* @O_RDWR
%4 = load i32, i32* @O_NOCTTY
%5 = or i32 %4, %3
%6 = load i32, i32* @O_CLOEXEC
%7 = or i32 %5, %6
%8 = tail call i32 @open(i8* %0, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %15
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = load i64, i64* @errno
%13 = tail call i8* @strerror(i64 %12)
%14 = tail call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %13)
br label %32
15: ; preds = %2
%16 = tail call i32 @unlockpt(i32 %8)
%17 = icmp slt i32 %16, 0
br i1 %17, label %26, label %18
18: ; preds = %15
%19 = load i64, i64* @errno
%20 = load i64, i64* @EINVAL
%21 = icmp eq i64 %19, %20
br i1 %21, label %22, label %26
22: ; preds = %18
%23 = load i32, i32* @stderr
%24 = tail call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0))
%25 = load i32, i32* @EXIT_SUCCESS
br label %26
26: ; preds = %18, %22, %15
%27 = phi i8* [ getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), %15 ], [ getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), %22 ], [ getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), %18 ]
%28 = phi i32 [ -1, %15 ], [ %25, %22 ], [ -1, %18 ]
%29 = load i32, i32* @stderr
%30 = tail call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* %27)
%31 = tail call i32 @close(i32 %8)
br label %32
32: ; preds = %26, %10
%33 = phi i32 [ -1, %10 ], [ %28, %26 ]
ret i32 %33
}