#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 */
struct group {
  int gr_gid;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int S_IRUSR;
int S_IWGRP;
int S_IWUSR;
int /*<<< orphan*/ chmod(char *, int);
int /*<<< orphan*/ chown(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ close(int);
struct group *getgrnam(char *);
int /*<<< orphan*/ getuid();
int open(char *, int /*<<< orphan*/);
int ptys_open(int fdm, char *pts_name) {
  int fds;
  int gid;
  struct group *grptr;
  grptr = getgrnam("tty");
  if (grptr != NULL)
    gid = grptr->gr_gid;
  else
    gid = -1;
  chown(pts_name, getuid(), gid);
  chmod(pts_name, S_IRUSR | S_IWUSR | S_IWGRP);
  fds = open(pts_name, O_RDWR);
  if (fds < 0) {
    close(fdm);
    return -1;
  }
  return fds;
}


x86 assembly (clang, -O0):
<ptys_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe8a(%rip),%rdi
call   1050 <getgrnam@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1198 <ptys_open+0x38>
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
jmp    119f <ptys_open+0x3f>
movl   $0xffffffff,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   1030 <getuid@plt>
mov    -0x28(%rbp),%rdi
mov    %eax,%esi
mov    -0x18(%rbp),%edx
call   1060 <chown@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e01(%rip),%rax
mov    (%rax),%esi
mov    0x2e08(%rip),%rax
or     (%rax),%esi
mov    0x2e07(%rip),%rax
or     (%rax),%esi
call   1070 <chmod@plt>
mov    -0x10(%rbp),%rdi
mov    0x2dfd(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1213 <ptys_open+0xb3>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1219 <ptys_open+0xb9>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ptys_open>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %edi,%r14d
lea    0xe8f(%rip),%rdi
call   1050 <getgrnam@plt>
test   %rax,%rax
je     117f <ptys_open+0x1f>
mov    (%rax),%ebp
jmp    1184 <ptys_open+0x24>
mov    $0xffffffff,%ebp
xor    %eax,%eax
call   1030 <getuid@plt>
mov    %rbx,%rdi
mov    %eax,%esi
mov    %ebp,%edx
call   1060 <chown@plt>
mov    0x2e2a(%rip),%rax
mov    0x2e33(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e30(%rip),%rax
or     (%rax),%esi
mov    %rbx,%rdi
call   1070 <chmod@plt>
mov    0x2e27(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1080 <open@plt>
test   %eax,%eax
jns    11dc <ptys_open+0x7c>
mov    %r14d,%edi
call   1040 <close@plt>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.group = type { i32 }
@.str = [4 x i8] c"tty\00"
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IWGRP = global i32 0
@O_RDWR = global i32 0
define i32 @ptys_open(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.group*
store i32 %0, i32* %4
store i8* %1, i8** %5
%9 = call %struct.group* @getgrnam(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store %struct.group* %9, %struct.group** %8
%10 = load %struct.group*, %struct.group** %8
%11 = icmp ne %struct.group* %10, null
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = load %struct.group*, %struct.group** %8
%14 = getelementptr inbounds %struct.group, %struct.group* %13, i32 0, i32 0
%15 = load i32, i32* %14
store i32 %15, i32* %7
br label %17
16: ; preds = %2
store i32 -1, i32* %7
br label %17
17: ; preds = %16, %12
%18 = load i8*, i8** %5
%19 = call i32 (...) @getuid()
%20 = load i32, i32* %7
%21 = call i32 @chown(i8* %18, i32 %19, i32 %20)
%22 = load i8*, i8** %5
%23 = load i32, i32* @S_IRUSR
%24 = load i32, i32* @S_IWUSR
%25 = or i32 %23, %24
%26 = load i32, i32* @S_IWGRP
%27 = or i32 %25, %26
%28 = call i32 @chmod(i8* %22, i32 %27)
%29 = load i8*, i8** %5
%30 = load i32, i32* @O_RDWR
%31 = call i32 @open(i8* %29, i32 %30)
store i32 %31, i32* %6
%32 = load i32, i32* %6
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %17
%35 = load i32, i32* %4
%36 = call i32 @close(i32 %35)
store i32 -1, i32* %3
br label %39
37: ; preds = %17
%38 = load i32, i32* %6
store i32 %38, i32* %3
br label %39
39: ; preds = %37, %34
%40 = load i32, i32* %3
ret i32 %40
}

LLVM IR (-O2):
%struct.group = type { i32 }
@.str = [4 x i8] c"tty\00"
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IWGRP = global i32 0
@O_RDWR = global i32 0
define i32 @ptys_open(i32 %0, i8* nocapture %1) {
%3 = tail call %struct.group* @getgrnam(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq %struct.group* %3, null
br i1 %4, label %8, label %5
5: ; preds = %2
%6 = getelementptr inbounds %struct.group, %struct.group* %3, i64 0, i32 0
%7 = load i32, i32* %6
br label %8
8: ; preds = %2, %5
%9 = phi i32 [ %7, %5 ], [ -1, %2 ]
%10 = tail call i32 (...) @getuid()
%11 = tail call i32 @chown(i8* %1, i32 %10, i32 %9)
%12 = load i32, i32* @S_IRUSR
%13 = load i32, i32* @S_IWUSR
%14 = or i32 %13, %12
%15 = load i32, i32* @S_IWGRP
%16 = or i32 %14, %15
%17 = tail call i32 @chmod(i8* %1, i32 %16)
%18 = load i32, i32* @O_RDWR
%19 = tail call i32 @open(i8* %1, i32 %18)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %8
%22 = tail call i32 @close(i32 %0)
br label %23
23: ; preds = %8, %21
%24 = phi i32 [ -1, %21 ], [ %19, %8 ]
ret i32 %24
}