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

/* Variables and functions */
int /*<<< orphan*/ getuid();
int v_chmod(char *, int);
int v_chown(char *, int /*<<< orphan*/, int /*<<< orphan*/);
struct group *v_getgrnam(char *);
int my_grantpt(char *pts_name) {
  struct group *grp;
  uid_t uid;
  uid = getuid();
  if (!(grp = v_getgrnam("tty")))
    return (-1);
  if ((v_chmod(pts_name, 00620)) == -1)
    return (-1);
  if ((v_chown(pts_name, uid, grp->gr_gid)) == -1)
    return (-1);
  return (0);
}


x86 assembly (clang, -O0):
<my_grantpt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x0,%al
call   1040 <getuid@plt>
mov    %eax,-0x1c(%rbp)
lea    0xea3(%rip),%rdi
call   1050 <v_getgrnam@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    117c <my_grantpt+0x3c>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cd <my_grantpt+0x8d>
mov    -0x10(%rbp),%rdi
mov    $0x190,%esi
call   1060 <v_chmod@plt>
cmp    $0xffffffff,%eax
jne    119f <my_grantpt+0x5f>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cd <my_grantpt+0x8d>
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x18(%rbp),%rax
mov    (%rax),%edx
call   1030 <v_chown@plt>
cmp    $0xffffffff,%eax
jne    11c6 <my_grantpt+0x86>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cd <my_grantpt+0x8d>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_grantpt>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r15
xor    %eax,%eax
call   1040 <getuid@plt>
mov    %eax,%r14d
lea    0xea5(%rip),%rdi
call   1050 <v_getgrnam@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
je     1197 <my_grantpt+0x57>
mov    %rax,%rbx
mov    %r15,%rdi
mov    $0x190,%esi
call   1060 <v_chmod@plt>
cmp    $0xffffffff,%eax
je     1197 <my_grantpt+0x57>
mov    (%rbx),%edx
mov    %r15,%rdi
mov    %r14d,%esi
call   1030 <v_chown@plt>
xor    %ebp,%ebp
cmp    $0xffffffff,%eax
sete   %bpl
neg    %ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.group = type { i32 }
@.str = [4 x i8] c"tty\00"
define i32 @my_grantpt(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.group*
%5 = alloca i32
store i8* %0, i8** %3
%6 = call i32 (...) @getuid()
store i32 %6, i32* %5
%7 = call %struct.group* @v_getgrnam(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store %struct.group* %7, %struct.group** %4
%8 = icmp ne %struct.group* %7, null
br i1 %8, label %10, label %9
9: ; preds = %1
store i32 -1, i32* %2
br label %25
10: ; preds = %1
%11 = load i8*, i8** %3
%12 = call i32 @v_chmod(i8* %11, i32 400)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %15
14: ; preds = %10
store i32 -1, i32* %2
br label %25
15: ; preds = %10
%16 = load i8*, i8** %3
%17 = load i32, i32* %5
%18 = load %struct.group*, %struct.group** %4
%19 = getelementptr inbounds %struct.group, %struct.group* %18, i32 0, i32 0
%20 = load i32, i32* %19
%21 = call i32 @v_chown(i8* %16, i32 %17, i32 %20)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %24
23: ; preds = %15
store i32 -1, i32* %2
br label %25
24: ; preds = %15
store i32 0, i32* %2
br label %25
25: ; preds = %24, %23, %14, %9
%26 = load i32, i32* %2
ret i32 %26
}

LLVM IR (-O2):
%struct.group = type { i32 }
@.str = [4 x i8] c"tty\00"
define i32 @my_grantpt(i8* %0) {
%2 = tail call i32 (...) @getuid()
%3 = tail call %struct.group* @v_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 %14, label %5
5: ; preds = %1
%6 = tail call i32 @v_chmod(i8* %0, i32 400)
%7 = icmp eq i32 %6, -1
br i1 %7, label %14, label %8
8: ; preds = %5
%9 = getelementptr inbounds %struct.group, %struct.group* %3, i64 0, i32 0
%10 = load i32, i32* %9
%11 = tail call i32 @v_chown(i8* %0, i32 %2, i32 %10)
%12 = icmp eq i32 %11, -1
%13 = sext i1 %12 to i32
br label %14
14: ; preds = %8, %5, %1
%15 = phi i32 [ -1, %1 ], [ -1, %5 ], [ %13, %8 ]
ret i32 %15
}