#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*/ EOVERFLOW;
int O_CLOEXEC;
int O_RDWR;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int posix_openpt(int);
int /*<<< orphan*/ pthread_exit(int /*<<< orphan*/ *);
char *ptsname(int);
int set_socket_to_non_block(int);
int /*<<< orphan*/ stderr;
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
int /*<<< orphan*/ unlockpt(int);
int create_pty(char *pty_slave_name) {
  int master_fd;
  char *p;
  if ((master_fd = posix_openpt(O_RDWR | O_CLOEXEC)) == -1) {
    fprintf(stderr, "Server: Could not create pseudoterminal.");
    return -1;
  }
  if (set_socket_to_non_block(master_fd) == -1) {
    fprintf(stderr, "Server: Unable to set client socket to non-blocking");
    pthread_exit(NULL);
  }
  unlockpt(master_fd);
  p = ptsname(master_fd);
  if (p == NULL) {
    return -1;
  }
  if (strlen(p) < 1000) {
    strncpy(pty_slave_name, p, 1000);
  } else {
    errno = EOVERFLOW;
    return -1;
  }
  return master_fd;
}


x86 assembly (clang, -O0):
<create_pty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e4d(%rip),%rax
mov    (%rax),%edi
mov    0x2e1c(%rip),%rax
or     (%rax),%edi
call   10a0 <posix_openpt@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    11d0 <create_pty+0x50>
mov    0x2e1a(%rip),%rax
mov    (%rax),%edi
lea    0xe41(%rip),%rsi
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1279 <create_pty+0xf9>
mov    -0x14(%rbp),%edi
call   1040 <set_socket_to_non_block@plt>
cmp    $0xffffffff,%eax
jne    11ff <create_pty+0x7f>
mov    0x2de8(%rip),%rax
mov    (%rax),%edi
lea    0xe38(%rip),%rsi
call   1080 <fprintf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1090 <pthread_exit@plt>
mov    -0x14(%rbp),%edi
call   1070 <unlockpt@plt>
mov    -0x14(%rbp),%edi
call   1030 <ptsname@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    122a <create_pty+0xaa>
movl   $0xffffffff,-0x4(%rbp)
jmp    1279 <create_pty+0xf9>
mov    -0x20(%rbp),%rdi
call   1060 <strlen@plt>
cmp    $0x3e8,%eax
jge    1255 <create_pty+0xd5>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    $0x3e8,%edx
call   1050 <strncpy@plt>
jmp    1273 <create_pty+0xf3>
mov    0x2d94(%rip),%rax
mov    (%rax),%ecx
mov    0x2d63(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1279 <create_pty+0xf9>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_pty>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
mov    0x2e51(%rip),%rax
mov    0x2e22(%rip),%rcx
mov    (%rcx),%edi
or     (%rax),%edi
call   10a0 <posix_openpt@plt>
cmp    $0xffffffff,%eax
je     1205 <create_pty+0x85>
mov    %eax,%ebx
mov    %eax,%edi
call   1040 <set_socket_to_non_block@plt>
cmp    $0xffffffff,%eax
jne    11ce <create_pty+0x4e>
mov    0x2e17(%rip),%rax
mov    (%rax),%edi
lea    0xe67(%rip),%rsi
call   1080 <fprintf@plt>
xor    %edi,%edi
call   1090 <pthread_exit@plt>
mov    %ebx,%edi
call   1070 <unlockpt@plt>
mov    %ebx,%edi
call   1030 <ptsname@plt>
test   %rax,%rax
je     122e <create_pty+0xae>
mov    %rax,%r15
mov    %rax,%rdi
call   1060 <strlen@plt>
cmp    $0x3e7,%eax
jg     121c <create_pty+0x9c>
mov    %r14,%rdi
mov    %r15,%rsi
mov    $0x3e8,%edx
call   1050 <strncpy@plt>
jmp    1233 <create_pty+0xb3>
mov    0x2dc4(%rip),%rax
mov    (%rax),%edi
lea    0xdeb(%rip),%rsi
call   1080 <fprintf@plt>
jmp    122e <create_pty+0xae>
mov    0x2dcd(%rip),%rax
mov    (%rax),%eax
mov    0x2d9c(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@O_RDWR = global i32 0
@O_CLOEXEC = global i32 0
@stderr = global i32 0
@.str = [41 x i8] c"Server: Could not create pseudoterminal.\00"
@.str.1 = [52 x i8] c"Server: Unable to set client socket to non-blocking\00"
@EOVERFLOW = global i32 0
@errno = global i32 0
define i32 @create_pty(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
store i8* %0, i8** %3
%6 = load i32, i32* @O_RDWR
%7 = load i32, i32* @O_CLOEXEC
%8 = or i32 %6, %7
%9 = call i32 @posix_openpt(i32 %8)
store i32 %9, i32* %4
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %14
11: ; preds = %1
%12 = load i32, i32* @stderr
%13 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %42
14: ; preds = %1
%15 = load i32, i32* %4
%16 = call i32 @set_socket_to_non_block(i32 %15)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %22
18: ; preds = %14
%19 = load i32, i32* @stderr
%20 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.1, i64 0, i64 0))
%21 = call i32 @pthread_exit(i32* null)
br label %22
22: ; preds = %18, %14
%23 = load i32, i32* %4
%24 = call i32 @unlockpt(i32 %23)
%25 = load i32, i32* %4
%26 = call i8* @ptsname(i32 %25)
store i8* %26, i8** %5
%27 = load i8*, i8** %5
%28 = icmp eq i8* %27, null
br i1 %28, label %29, label %30
29: ; preds = %22
store i32 -1, i32* %2
br label %42
30: ; preds = %22
%31 = load i8*, i8** %5
%32 = call i32 @strlen(i8* %31)
%33 = icmp slt i32 %32, 1000
br i1 %33, label %34, label %38
34: ; preds = %30
%35 = load i8*, i8** %3
%36 = load i8*, i8** %5
%37 = call i32 @strncpy(i8* %35, i8* %36, i32 1000)
br label %40
38: ; preds = %30
%39 = load i32, i32* @EOVERFLOW
store i32 %39, i32* @errno
store i32 -1, i32* %2
br label %42
40: ; preds = %34
%41 = load i32, i32* %4
store i32 %41, i32* %2
br label %42
42: ; preds = %40, %38, %29, %11
%43 = load i32, i32* %2
ret i32 %43
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@O_CLOEXEC = global i32 0
@stderr = global i32 0
@.str = [41 x i8] c"Server: Could not create pseudoterminal.\00"
@.str.1 = [52 x i8] c"Server: Unable to set client socket to non-blocking\00"
@EOVERFLOW = global i32 0
@errno = global i32 0
define i32 @create_pty(i8* %0) {
%2 = load i32, i32* @O_RDWR
%3 = load i32, i32* @O_CLOEXEC
%4 = or i32 %3, %2
%5 = tail call i32 @posix_openpt(i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = load i32, i32* @stderr
%9 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
br label %28
10: ; preds = %1
%11 = tail call i32 @set_socket_to_non_block(i32 %5)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %17
13: ; preds = %10
%14 = load i32, i32* @stderr
%15 = tail call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.1, i64 0, i64 0))
%16 = tail call i32 @pthread_exit(i32* null)
br label %17
17: ; preds = %13, %10
%18 = tail call i32 @unlockpt(i32 %5)
%19 = tail call i8* @ptsname(i32 %5)
%20 = icmp eq i8* %19, null
br i1 %20, label %28, label %21
21: ; preds = %17
%22 = tail call i32 @strlen(i8* %19)
%23 = icmp slt i32 %22, 1000
br i1 %23, label %24, label %26
24: ; preds = %21
%25 = tail call i32 @strncpy(i8* %0, i8* %19, i32 1000)
br label %28
26: ; preds = %21
%27 = load i32, i32* @EOVERFLOW
store i32 %27, i32* @errno
br label %28
28: ; preds = %17, %24, %26, %7
%29 = phi i32 [ -1, %7 ], [ %5, %24 ], [ -1, %26 ], [ -1, %17 ]
ret i32 %29
}