#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_NONBLOCK;
int O_RDWR;
int /*<<< orphan*/ close(int);
scalar_t__ grantpt(int);
int posix_openpt(int);
char *ptsname(int);
int /*<<< orphan*/ strncpy(char *, char *, int);
scalar_t__ unlockpt(int);
int ptym_open(char *pts_name, char *pts_name_s, int pts_namesz) {
  char *ptr;
  int fdm;
  strncpy(pts_name, "/dev/ptmx", pts_namesz);
  pts_name[pts_namesz - 1] = '\0';
  fdm = posix_openpt(O_RDWR | O_NONBLOCK);
  if (fdm < 0)
    return (-1);
  if (grantpt(fdm) < 0) {
    close(fdm);
    return (-2);
  }
  if (unlockpt(fdm) < 0) {
    close(fdm);
    return (-3);
  }
  if ((ptr = ptsname(fdm)) == NULL) {
    close(fdm);
    return (-4);
  }
  strncpy(pts_name_s, ptr, pts_namesz);
  pts_name[pts_namesz - 1] = '\0';
  return (fdm);
}


x86 assembly (clang, -O0):
<ptym_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%edx
lea    0xe7f(%rip),%rsi
call   1040 <strncpy@plt>
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
mov    0x2e39(%rip),%rax
or     (%rax),%edi
call   1080 <posix_openpt@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    11c7 <ptym_open+0x67>
movl   $0xffffffff,-0x4(%rbp)
jmp    1264 <ptym_open+0x104>
mov    -0x2c(%rbp),%edi
call   1070 <grantpt@plt>
cmp    $0x0,%rax
jge    11ed <ptym_open+0x8d>
mov    -0x2c(%rbp),%edi
call   1050 <close@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1264 <ptym_open+0x104>
mov    -0x2c(%rbp),%edi
call   1060 <unlockpt@plt>
cmp    $0x0,%rax
jge    1213 <ptym_open+0xb3>
mov    -0x2c(%rbp),%edi
call   1050 <close@plt>
movl   $0xfffffffd,-0x4(%rbp)
jmp    1264 <ptym_open+0x104>
mov    -0x2c(%rbp),%edi
call   1030 <ptsname@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    123d <ptym_open+0xdd>
mov    -0x2c(%rbp),%edi
call   1050 <close@plt>
movl   $0xfffffffc,-0x4(%rbp)
jmp    1264 <ptym_open+0x104>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1040 <strncpy@plt>
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ptym_open>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %rsi,%r15
mov    %rdi,%r14
lea    0xe89(%rip),%rsi
call   1040 <strncpy@plt>
movslq %ebx,%r12
movb   $0x0,-0x1(%r12,%r14,1)
mov    0x2e5c(%rip),%rax
mov    0x2e4d(%rip),%rcx
mov    (%rcx),%edi
or     (%rax),%edi
call   1080 <posix_openpt@plt>
test   %eax,%eax
js     11dc <ptym_open+0x7c>
mov    %eax,%ebx
mov    %eax,%edi
call   1070 <grantpt@plt>
test   %rax,%rax
js     11e3 <ptym_open+0x83>
mov    %ebx,%edi
call   1060 <unlockpt@plt>
test   %rax,%rax
js     11f1 <ptym_open+0x91>
mov    %ebx,%edi
call   1030 <ptsname@plt>
test   %rax,%rax
je     11ff <ptym_open+0x9f>
mov    %r15,%rdi
mov    %rax,%rsi
mov    %r12d,%edx
call   1040 <strncpy@plt>
movb   $0x0,-0x1(%r14,%r12,1)
jmp    120b <ptym_open+0xab>
mov    $0xffffffff,%ebx
jmp    120b <ptym_open+0xab>
mov    %ebx,%edi
call   1050 <close@plt>
mov    $0xfffffffe,%ebx
jmp    120b <ptym_open+0xab>
mov    %ebx,%edi
call   1050 <close@plt>
mov    $0xfffffffd,%ebx
jmp    120b <ptym_open+0xab>
mov    %ebx,%edi
call   1050 <close@plt>
mov    $0xfffffffc,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [10 x i8] c"/dev/ptmx\00"
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
define i32 @ptym_open(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = load i8*, i8** %5
%11 = load i32, i32* %7
%12 = call i32 @strncpy(i8* %10, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = load i8*, i8** %5
%14 = load i32, i32* %7
%15 = sub nsw i32 %14, 1
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %13, i64 %16
store i8 0, i8* %17
%18 = load i32, i32* @O_RDWR
%19 = load i32, i32* @O_NONBLOCK
%20 = or i32 %18, %19
%21 = call i32 @posix_openpt(i32 %20)
store i32 %21, i32* %9
%22 = load i32, i32* %9
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %3
store i32 -1, i32* %4
br label %57
25: ; preds = %3
%26 = load i32, i32* %9
%27 = call i64 @grantpt(i32 %26)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %25
%30 = load i32, i32* %9
%31 = call i32 @close(i32 %30)
store i32 -2, i32* %4
br label %57
32: ; preds = %25
%33 = load i32, i32* %9
%34 = call i64 @unlockpt(i32 %33)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %32
%37 = load i32, i32* %9
%38 = call i32 @close(i32 %37)
store i32 -3, i32* %4
br label %57
39: ; preds = %32
%40 = load i32, i32* %9
%41 = call i8* @ptsname(i32 %40)
store i8* %41, i8** %8
%42 = icmp eq i8* %41, null
br i1 %42, label %43, label %46
43: ; preds = %39
%44 = load i32, i32* %9
%45 = call i32 @close(i32 %44)
store i32 -4, i32* %4
br label %57
46: ; preds = %39
%47 = load i8*, i8** %6
%48 = load i8*, i8** %8
%49 = load i32, i32* %7
%50 = call i32 @strncpy(i8* %47, i8* %48, i32 %49)
%51 = load i8*, i8** %5
%52 = load i32, i32* %7
%53 = sub nsw i32 %52, 1
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds i8, i8* %51, i64 %54
store i8 0, i8* %55
%56 = load i32, i32* %9
store i32 %56, i32* %4
br label %57
57: ; preds = %46, %43, %36, %29, %24
%58 = load i32, i32* %4
ret i32 %58
}

LLVM IR (-O2):
@.str = [10 x i8] c"/dev/ptmx\00"
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
define i32 @ptym_open(i8* %0, i8* %1, i32 %2) {
%4 = tail call i32 @strncpy(i8* %0, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %2)
%5 = add nsw i32 %2, -1
%6 = sext i32 %5 to i64
%7 = getelementptr inbounds i8, i8* %0, i64 %6
store i8 0, i8* %7
%8 = load i32, i32* @O_RDWR
%9 = load i32, i32* @O_NONBLOCK
%10 = or i32 %9, %8
%11 = tail call i32 @posix_openpt(i32 %10)
%12 = icmp slt i32 %11, 0
br i1 %12, label %30, label %13
13: ; preds = %3
%14 = tail call i64 @grantpt(i32 %11)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = tail call i32 @close(i32 %11)
br label %30
18: ; preds = %13
%19 = tail call i64 @unlockpt(i32 %11)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = tail call i32 @close(i32 %11)
br label %30
23: ; preds = %18
%24 = tail call i8* @ptsname(i32 %11)
%25 = icmp eq i8* %24, null
br i1 %25, label %26, label %28
26: ; preds = %23
%27 = tail call i32 @close(i32 %11)
br label %30
28: ; preds = %23
%29 = tail call i32 @strncpy(i8* %1, i8* %24, i32 %2)
store i8 0, i8* %7
br label %30
30: ; preds = %3, %28, %26, %21, %16
%31 = phi i32 [ -2, %16 ], [ -3, %21 ], [ -4, %26 ], [ %11, %28 ], [ -1, %3 ]
ret i32 %31
}