#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 stat {
  int st_mode;
};

/* Variables and functions */
int /*<<< orphan*/ STDERR_FILENO;
int S_IWGRP;
int /*<<< orphan*/ err(int, char *, ...);
int /*<<< orphan*/ errx(int, char *);
scalar_t__ stat(char *, struct stat *);
char *ttyname(int /*<<< orphan*/);
void check_writeable(void) {
  char *tty;
  struct stat sb;
  if ((tty = ttyname(STDERR_FILENO)) == NULL)
    err(1, "ttyname");
  if (stat(tty, &sb) < 0)
    err(1, "%s", tty);
  if (!(sb.st_mode & S_IWGRP))
    errx(1, "The callee cannot write to this terminal, use \"mesg y\".");
}


x86 assembly (clang, -O0):
<check_writeable>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e89(%rip),%rax
mov    (%rax),%edi
call   1060 <ttyname@plt>
mov    %rax,-0x8(%rbp)
cmp    $0x0,%rax
jne    1177 <check_writeable+0x37>
mov    $0x1,%edi
lea    0xe90(%rip),%rsi
mov    $0x0,%al
call   1050 <err@plt>
mov    -0x8(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0x0,%rax
jge    11a5 <check_writeable+0x65>
mov    -0x8(%rbp),%rdx
mov    $0x1,%edi
lea    0xe6a(%rip),%rsi
mov    $0x0,%al
call   1050 <err@plt>
mov    -0x10(%rbp),%eax
mov    0x2e39(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    11cb <check_writeable+0x8b>
mov    $0x1,%edi
lea    0xe45(%rip),%rsi
call   1030 <errx@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_writeable>:
push   %rbx
sub    $0x10,%rsp
mov    0x2e8c(%rip),%rax
mov    (%rax),%edi
call   1060 <ttyname@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    116e <check_writeable+0x2e>
lea    0xe9e(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
call   1050 <err@plt>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1040 <stat@plt>
test   %rax,%rax
jns    1196 <check_writeable+0x56>
lea    0xe81(%rip),%rsi
mov    $0x1,%edi
mov    %rbx,%rdx
xor    %eax,%eax
call   1050 <err@plt>
mov    0x2e4b(%rip),%rax
mov    (%rax),%eax
test   %eax,0x8(%rsp)
jne    11b6 <check_writeable+0x76>
lea    0xe5f(%rip),%rsi
mov    $0x1,%edi
call   1030 <errx@plt>
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@STDERR_FILENO = global i32 0
@.str = [8 x i8] c"ttyname\00"
@.str.1 = [3 x i8] c"%s\00"
@S_IWGRP = global i32 0
@.str.2 = [56 x i8] c"The callee cannot write to this terminal, use \22mesg y\22.\00"
define void @check_writeable() {
%1 = alloca i8*
%2 = alloca %struct.stat
%3 = load i32, i32* @STDERR_FILENO
%4 = call i8* @ttyname(i32 %3)
store i8* %4, i8** %1
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %8
8: ; preds = %6, %0
%9 = load i8*, i8** %1
%10 = call i64 @stat(i8* %9, %struct.stat* %2)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %8
%13 = load i8*, i8** %1
%14 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %13)
br label %15
15: ; preds = %12, %8
%16 = getelementptr inbounds %struct.stat, %struct.stat* %2, i32 0, i32 0
%17 = load i32, i32* %16
%18 = load i32, i32* @S_IWGRP
%19 = and i32 %17, %18
%20 = icmp ne i32 %19, 0
br i1 %20, label %23, label %21
21: ; preds = %15
%22 = call i32 @errx(i32 1, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.2, i64 0, i64 0))
br label %23
23: ; preds = %21, %15
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@STDERR_FILENO = global i32 0
@.str = [8 x i8] c"ttyname\00"
@.str.1 = [3 x i8] c"%s\00"
@S_IWGRP = global i32 0
@.str.2 = [56 x i8] c"The callee cannot write to this terminal, use \22mesg y\22.\00"
define void @check_writeable() {
%1 = alloca %struct.stat
%2 = bitcast %struct.stat* %1 to i8*
%3 = load i32, i32* @STDERR_FILENO
%4 = tail call i8* @ttyname(i32 %3)
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = tail call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %8
8: ; preds = %6, %0
%9 = call i64 @stat(i8* %4, %struct.stat* %1)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %4)
br label %13
13: ; preds = %11, %8
%14 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
%15 = load i32, i32* %14
%16 = load i32, i32* @S_IWGRP
%17 = and i32 %16, %15
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %13
%20 = call i32 @errx(i32 1, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.2, i64 0, i64 0))
br label %21
21: ; preds = %19, %13
ret void
}