#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 /*<<< orphan*/ close(int);
struct group *getgrnam(char *);
int open(char *, int /*<<< orphan*/);
int tty_open(int fdm, char *tty_name) {
  struct group *grptr;
  int gid, fds;
  if ((grptr = getgrnam("tty")) != NULL)
    gid = grptr->gr_gid;
  else
    gid = -1;
  if ((fds = open(tty_name, O_RDWR)) < 0) {
    close(fdm);
    return (-1);
  }
  return (fds);
}


x86 assembly (clang, -O0):
<tty_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xeba(%rip),%rdi
call   1040 <getgrnam@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     1167 <tty_open+0x37>
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
jmp    116e <tty_open+0x3e>
movl   $0xffffffff,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e6f(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jge    11a0 <tty_open+0x70>
mov    -0x8(%rbp),%edi
call   1030 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a6 <tty_open+0x76>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tty_open>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0xec1(%rip),%rdi
call   1040 <getgrnam@plt>
mov    0x2e9d(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1050 <open@plt>
test   %eax,%eax
jns    1165 <tty_open+0x35>
mov    %ebp,%edi
call   1030 <close@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.group = type { i32 }
@.str = [4 x i8] c"tty\00"
@O_RDWR = global i32 0
define i32 @tty_open(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca %struct.group*
%7 = alloca i32
%8 = alloca i32
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** %6
%10 = icmp ne %struct.group* %9, null
br i1 %10, label %11, label %15
11: ; preds = %2
%12 = load %struct.group*, %struct.group** %6
%13 = getelementptr inbounds %struct.group, %struct.group* %12, i32 0, i32 0
%14 = load i32, i32* %13
store i32 %14, i32* %7
br label %16
15: ; preds = %2
store i32 -1, i32* %7
br label %16
16: ; preds = %15, %11
%17 = load i8*, i8** %5
%18 = load i32, i32* @O_RDWR
%19 = call i32 @open(i8* %17, i32 %18)
store i32 %19, i32* %8
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %16
%22 = load i32, i32* %4
%23 = call i32 @close(i32 %22)
store i32 -1, i32* %3
br label %26
24: ; preds = %16
%25 = load i32, i32* %8
store i32 %25, i32* %3
br label %26
26: ; preds = %24, %21
%27 = load i32, i32* %3
ret i32 %27
}

LLVM IR (-O2):
%struct.group = type { i32 }
@.str = [4 x i8] c"tty\00"
@O_RDWR = global i32 0
define i32 @tty_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 = load i32, i32* @O_RDWR
%5 = tail call i32 @open(i8* %1, i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 @close(i32 %0)
br label %9
9: ; preds = %2, %7
%10 = phi i32 [ -1, %7 ], [ %5, %2 ]
ret i32 %10
}