#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*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int posix_openpt(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *);
int ptsname_r(int, char *, int);
int /*<<< orphan*/ stdout;
int ptym_open(char *pts_name) {
  int fdm;
  int ret;
  if ((fdm = posix_openpt(O_RDWR)) < 0) {
    perror("ut_wrapper : posix_openpt:");
    return -1;
  }
  ret = ptsname_r(fdm, pts_name, 32);
  if (ret) {
    perror("ut_wrapper : ptsname");
    close(fdm);
    return -1;
  }
  printf("PTS %s\n", pts_name);
  fflush(stdout);
  return fdm;
}


x86 assembly (clang, -O0):
<ptym_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e75(%rip),%rax
mov    (%rax),%edi
call   1080 <posix_openpt@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    119e <ptym_open+0x3e>
lea    0xe73(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1200 <ptym_open+0xa0>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x20,%edx
call   1050 <ptsname_r@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     11dc <ptym_open+0x7c>
lea    0xe58(%rip),%rdi
call   1070 <perror@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1200 <ptym_open+0xa0>
mov    -0x10(%rbp),%rsi
lea    0xe49(%rip),%rdi
call   1030 <printf@plt>
mov    0x2de5(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ptym_open>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2e7a(%rip),%rax
mov    (%rax),%edi
call   1080 <posix_openpt@plt>
test   %eax,%eax
js     11b5 <ptym_open+0x55>
mov    %eax,%ebx
mov    %eax,%edi
mov    %r14,%rsi
mov    $0x20,%edx
call   1050 <ptsname_r@plt>
test   %eax,%eax
jne    11c8 <ptym_open+0x68>
lea    0xe9b(%rip),%rdi
mov    %r14,%rsi
call   1030 <printf@plt>
mov    0x2e34(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe44(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%ebx
jmp    11ab <ptym_open+0x4b>
lea    0xe4c(%rip),%rdi
call   1070 <perror@plt>
mov    %ebx,%edi
call   1040 <close@plt>
mov    $0xffffffff,%ebx
jmp    11ab <ptym_open+0x4b>

LLVM IR (-O0):
@O_RDWR = global i32 0
@.str = [27 x i8] c"ut_wrapper : posix_openpt:\00"
@.str.1 = [21 x i8] c"ut_wrapper : ptsname\00"
@.str.2 = [8 x i8] c"PTS %s\0A\00"
@stdout = global i32 0
define i32 @ptym_open(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %3
%6 = load i32, i32* @O_RDWR
%7 = call i32 @posix_openpt(i32 %6)
store i32 %7, i32* %4
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %27
11: ; preds = %1
%12 = load i32, i32* %4
%13 = load i8*, i8** %3
%14 = call i32 @ptsname_r(i32 %12, i8* %13, i32 32)
store i32 %14, i32* %5
%15 = load i32, i32* %5
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %11
%18 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%19 = load i32, i32* %4
%20 = call i32 @close(i32 %19)
store i32 -1, i32* %2
br label %27
21: ; preds = %11
%22 = load i8*, i8** %3
%23 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* %22)
%24 = load i32, i32* @stdout
%25 = call i32 @fflush(i32 %24)
%26 = load i32, i32* %4
store i32 %26, i32* %2
br label %27
27: ; preds = %21, %17, %9
%28 = load i32, i32* %2
ret i32 %28
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@.str = [27 x i8] c"ut_wrapper : posix_openpt:\00"
@.str.1 = [21 x i8] c"ut_wrapper : ptsname\00"
@.str.2 = [8 x i8] c"PTS %s\0A\00"
@stdout = global i32 0
define i32 @ptym_open(i8* %0) {
%2 = load i32, i32* @O_RDWR
%3 = tail call i32 @posix_openpt(i32 %2)
%4 = icmp slt i32 %3, 0
br i1 %4, label %5, label %7
5: ; preds = %1
%6 = tail call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
br label %17
7: ; preds = %1
%8 = tail call i32 @ptsname_r(i32 %3, i8* %0, i32 32)
%9 = icmp eq i32 %8, 0
br i1 %9, label %13, label %10
10: ; preds = %7
%11 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%12 = tail call i32 @close(i32 %3)
br label %17
13: ; preds = %7
%14 = tail call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%15 = load i32, i32* @stdout
%16 = tail call i32 @fflush(i32 %15)
br label %17
17: ; preds = %13, %10, %5
%18 = phi i32 [ -1, %5 ], [ -1, %10 ], [ %3, %13 ]
ret i32 %18
}