#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*/);
int get_slave_pty(char *name) {
  struct group *gptr;
  gid_t gid;
  int slave = -1;
  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)
lea    0xe96(%rip),%rdi
call   1040 <getgrnam@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     118b <get_slave_pty+0x3b>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
jmp    1192 <get_slave_pty+0x42>
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   1050 <chown@plt>
mov    -0x8(%rbp),%rdi
mov    0x2e16(%rip),%rax
mov    (%rax),%esi
mov    0x2e1d(%rip),%rax
or     (%rax),%esi
call   1060 <chmod@plt>
mov    -0x8(%rbp),%rdi
mov    0x2e13(%rip),%rax
mov    (%rax),%esi
call   1070 <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    0xea3(%rip),%rdi
call   1040 <getgrnam@plt>
test   %rax,%rax
je     116b <get_slave_pty+0x1b>
mov    (%rax),%ebp
jmp    1170 <get_slave_pty+0x20>
mov    $0xffffffff,%ebp
xor    %eax,%eax
call   1030 <getuid@plt>
mov    %rbx,%rdi
mov    %eax,%esi
mov    %ebp,%edx
call   1050 <chown@plt>
mov    0x2e46(%rip),%rax
mov    0x2e4f(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
call   1060 <chmod@plt>
mov    0x2e44(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1070 <open@plt>

LLVM IR (-O0):
%struct.group = type { i32 }
@.str = [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 = call %struct.group* @getgrnam(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store %struct.group* %6, %struct.group** %3
%7 = icmp ne %struct.group* %6, null
br i1 %7, label %8, label %12
8: ; preds = %1
%9 = load %struct.group*, %struct.group** %3
%10 = getelementptr inbounds %struct.group, %struct.group* %9, i32 0, i32 0
%11 = load i32, i32* %10
store i32 %11, i32* %4
br label %13
12: ; preds = %1
store i32 -1, i32* %4
br label %13
13: ; preds = %12, %8
%14 = load i8*, i8** %2
%15 = call i32 (...) @getuid()
%16 = load i32, i32* %4
%17 = call i32 @chown(i8* %14, i32 %15, i32 %16)
%18 = load i8*, i8** %2
%19 = load i32, i32* @S_IRUSR
%20 = load i32, i32* @S_IWUSR
%21 = or i32 %19, %20
%22 = call i32 @chmod(i8* %18, i32 %21)
%23 = load i8*, i8** %2
%24 = load i32, i32* @O_RDWR
%25 = call i32 @open(i8* %23, i32 %24)
store i32 %25, i32* %5
%26 = load i32, i32* %5
ret i32 %26
}

LLVM IR (-O2):
%struct.group = type { i32 }
@.str = [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* nocapture %0) {
%2 = tail call %struct.group* @getgrnam(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq %struct.group* %2, null
br i1 %3, label %7, label %4
4: ; preds = %1
%5 = getelementptr inbounds %struct.group, %struct.group* %2, i64 0, i32 0
%6 = load i32, i32* %5
br label %7
7: ; preds = %1, %4
%8 = phi i32 [ %6, %4 ], [ -1, %1 ]
%9 = tail call i32 (...) @getuid()
%10 = tail call i32 @chown(i8* %0, i32 %9, i32 %8)
%11 = load i32, i32* @S_IRUSR
%12 = load i32, i32* @S_IWUSR
%13 = or i32 %12, %11
%14 = tail call i32 @chmod(i8* %0, i32 %13)
%15 = load i32, i32* @O_RDWR
%16 = tail call i32 @open(i8* %0, i32 %15)
ret i32 %16
}