#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;
};
typedef int gid_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ chmod(char *, int);
int /*<<< orphan*/ chown(char *, int /*<<< orphan*/, int);
struct group *getgrnam(char *);
int /*<<< orphan*/ getuid();
int open(char *, int /*<<< orphan*/);
scalar_t__ strcmp(char *, char *);
int get_slave_pty(char *name) {
  struct group *gptr;
  gid_t gid;
  int slave = -1;
  if (strcmp(name, "dev/pts/")) {
    if ((gptr = getgrnam("tty")) != 0) {
      gid = gptr->gr_gid;
    } else {
      gid = -1;
    }
    chown(name, getuid(), gid);
    chmod(name, S_IRUSR | S_IWUSR);
  }
  slave = open(name, O_RDWR);
  return (slave);
}


x86 assembly (clang, -O0):
<get_slave_pty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0xffffffff,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe82(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
je     11f4 <get_slave_pty+0x94>
lea    0xe75(%rip),%rdi
call   1050 <getgrnam@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     11b5 <get_slave_pty+0x55>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
jmp    11bc <get_slave_pty+0x5c>
movl   $0xffffffff,-0x14(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1030 <getuid@plt>
mov    -0x20(%rbp),%rdi
mov    %eax,%esi
mov    -0x14(%rbp),%edx
call   1060 <chown@plt>
mov    -0x8(%rbp),%rdi
mov    0x2dec(%rip),%rax
mov    (%rax),%esi
mov    0x2df3(%rip),%rax
or     (%rax),%esi
call   1070 <chmod@plt>
mov    -0x8(%rbp),%rdi
mov    0x2de9(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_slave_pty>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
lea    0xe93(%rip),%rsi
call   1040 <strcmp@plt>
test   %rax,%rax
je     11be <get_slave_pty+0x5e>
lea    0xe8b(%rip),%rdi
call   1050 <getgrnam@plt>
test   %rax,%rax
je     118c <get_slave_pty+0x2c>
mov    (%rax),%ebp
jmp    1191 <get_slave_pty+0x31>
mov    $0xffffffff,%ebp
xor    %eax,%eax
call   1030 <getuid@plt>
mov    %rbx,%rdi
mov    %eax,%esi
mov    %ebp,%edx
call   1060 <chown@plt>
mov    0x2e25(%rip),%rax
mov    0x2e2e(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
call   1070 <chmod@plt>
mov    0x2e23(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1080 <open@plt>

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

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