#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 /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int S_IWGRP;
int /*<<< orphan*/ chmod(char *, int);
int /*<<< orphan*/ err(int, char *, ...);
int /*<<< orphan*/ exit(int);
int getopt(int, char **, char *);
scalar_t__ optind;
int /*<<< orphan*/ puts(char *);
scalar_t__ stat(char *, struct stat *);
char *ttyname(int /*<<< orphan*/);
int /*<<< orphan*/ usage();
int main(int argc, char *argv[]) {
  struct stat sb;
  char *tty;
  int ch;
  while ((ch = getopt(argc, argv, "")) != -1)
    switch (ch) {
    case '?':
    default:
      usage();
    }
  argc -= optind;
  argv += optind;
  if ((tty = ttyname(STDIN_FILENO)) == NULL &&
      (tty = ttyname(STDOUT_FILENO)) == NULL &&
      (tty = ttyname(STDERR_FILENO)) == NULL)
    err(2, "ttyname");
  if (stat(tty, &sb) < 0)
    err(2, "%s", tty);
  if (*argv == NULL) {
    if (sb.st_mode & S_IWGRP) {
      (void)puts("is y");
      exit(0);
    }
    (void)puts("is n");
    exit(1);
  }
  switch (*argv[0]) {
  case 'y':
    if (chmod(tty, sb.st_mode | S_IWGRP) < 0)
      err(2, "%s", tty);
    exit(0);
  case 'n':
    if (chmod(tty, sb.st_mode & ~S_IWGRP) < 0)
      err(2, "%s", tty);
    exit(1);
  }
  usage();
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xe63(%rip),%rdx
call   1090 <getopt@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
je     11d7 <main+0x57>
mov    -0x24(%rbp),%eax
sub    $0x3f,%eax
jne    11cb <main+0x4b>
jmp    11c6 <main+0x46>
jmp    11cb <main+0x4b>
mov    $0x0,%al
call   1070 <usage@plt>
jmp    1196 <main+0x16>
mov    0x2dfa(%rip),%rax
mov    (%rax),%rcx
movslq -0x8(%rbp),%rax
sub    %rcx,%rax
mov    %eax,-0x8(%rbp)
mov    0x2de6(%rip),%rax
mov    (%rax),%rax
shl    $0x3,%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    0x2db8(%rip),%rax
mov    (%rax),%edi
call   1060 <ttyname@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1268 <main+0xe8>
mov    0x2dcc(%rip),%rax
mov    (%rax),%edi
call   1060 <ttyname@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1268 <main+0xe8>
mov    0x2d88(%rip),%rax
mov    (%rax),%edi
call   1060 <ttyname@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1268 <main+0xe8>
mov    $0x2,%edi
lea    0xd9f(%rip),%rsi
mov    $0x0,%al
call   1050 <err@plt>
mov    -0x20(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0x0,%rax
jge    1296 <main+0x116>
mov    -0x20(%rbp),%rdx
mov    $0x2,%edi
lea    0xd79(%rip),%rsi
mov    $0x0,%al
call   1050 <err@plt>
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    12e2 <main+0x162>
mov    -0x18(%rbp),%eax
mov    0x2d32(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     12cc <main+0x14c>
lea    0xd4b(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
lea    0xd3d(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
mov    %eax,-0x28(%rbp)
sub    $0x6e,%eax
je     134a <main+0x1ca>
jmp    12fd <main+0x17d>
mov    -0x28(%rbp),%eax
sub    $0x79,%eax
jne    138e <main+0x20e>
jmp    130e <main+0x18e>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%esi
mov    0x2cc4(%rip),%rax
or     (%rax),%esi
call   1080 <chmod@plt>
cmp    $0x0,%eax
jge    1343 <main+0x1c3>
mov    -0x20(%rbp),%rdx
mov    $0x2,%edi
lea    0xccc(%rip),%rsi
mov    $0x0,%al
call   1050 <err@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%esi
mov    0x2c88(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    %eax,%esi
call   1080 <chmod@plt>
cmp    $0x0,%eax
jge    1384 <main+0x204>
mov    -0x20(%rbp),%rdx
mov    $0x2,%edi
lea    0xc8b(%rip),%rsi
mov    $0x0,%al
call   1050 <err@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    $0x0,%al
call   1070 <usage@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %edi,%ebp
lea    0xe74(%rip),%rdx
call   1090 <getopt@plt>
cmp    $0xffffffff,%eax
je     11c9 <main+0x49>
lea    0xe63(%rip),%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
xor    %eax,%eax
call   1070 <usage@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1090 <getopt@plt>
cmp    $0xffffffff,%eax
jne    11b0 <main+0x30>
mov    0x2e08(%rip),%rax
mov    (%rax),%rbx
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
call   1060 <ttyname@plt>
mov    %rax,%r14
test   %rax,%rax
jne    122b <main+0xab>
mov    0x2e00(%rip),%rax
mov    (%rax),%edi
call   1060 <ttyname@plt>
mov    %rax,%r14
test   %rax,%rax
jne    122b <main+0xab>
mov    0x2dc2(%rip),%rax
mov    (%rax),%edi
call   1060 <ttyname@plt>
mov    %rax,%r14
test   %rax,%rax
jne    122b <main+0xab>
lea    0xde4(%rip),%rsi
xor    %r14d,%r14d
mov    $0x2,%edi
xor    %eax,%eax
call   1050 <err@plt>
mov    %rsp,%rsi
mov    %r14,%rdi
call   1040 <stat@plt>
test   %rax,%rax
jns    1251 <main+0xd1>
lea    0xdc6(%rip),%rsi
mov    $0x2,%edi
mov    %r14,%rdx
xor    %eax,%eax
call   1050 <err@plt>
mov    (%r15,%rbx,8),%rax
test   %rax,%rax
je     127b <main+0xfb>
movsbl (%rax),%eax
cmp    $0x6e,%eax
je     12a3 <main+0x123>
cmp    $0x79,%eax
je     12dd <main+0x15d>
xor    %eax,%eax
call   1070 <usage@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d5e(%rip),%rax
mov    (%rax),%eax
test   %eax,(%rsp)
jne    1312 <main+0x192>
lea    0xd7c(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    0x2d36(%rip),%rax
mov    (%rax),%esi
not    %esi
and    (%rsp),%esi
mov    %r14,%rdi
call   1080 <chmod@plt>
test   %eax,%eax
jns    12d3 <main+0x153>
lea    0xd44(%rip),%rsi
mov    $0x2,%edi
mov    %r14,%rdx
xor    %eax,%eax
call   1050 <err@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    0x2cfc(%rip),%rax
mov    (%rax),%esi
or     (%rsp),%esi
mov    %r14,%rdi
call   1080 <chmod@plt>
test   %eax,%eax
jns    130b <main+0x18b>
lea    0xd0c(%rip),%rsi
mov    $0x2,%edi
mov    %r14,%rdx
xor    %eax,%eax
call   1050 <err@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
lea    0xcf2(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [1 x i8] zeroinitializer
@optind = global i64 0
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str.1 = [8 x i8] c"ttyname\00"
@.str.2 = [3 x i8] c"%s\00"
@S_IWGRP = global i32 0
@.str.3 = [5 x i8] c"is y\00"
@.str.4 = [5 x i8] c"is n\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.stat
%7 = alloca i8*
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
br label %9
9: ; preds = %19, %2
%10 = load i32, i32* %4
%11 = load i8**, i8*** %5
%12 = call i32 @getopt(i32 %10, i8** %11, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
store i32 %12, i32* %8
%13 = icmp ne i32 %12, -1
br i1 %13, label %14, label %20
14: ; preds = %9
%15 = load i32, i32* %8
switch i32 %15, label %17 [
i32 63, label %16
]
16: ; preds = %14
br label %17
17: ; preds = %14, %16
%18 = call i32 (...) @usage()
br label %19
19: ; preds = %17
br label %9
20: ; preds = %9
%21 = load i64, i64* @optind
%22 = load i32, i32* %4
%23 = sext i32 %22 to i64
%24 = sub nsw i64 %23, %21
%25 = trunc i64 %24 to i32
store i32 %25, i32* %4
%26 = load i64, i64* @optind
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 %26
store i8** %28, i8*** %5
%29 = load i32, i32* @STDIN_FILENO
%30 = call i8* @ttyname(i32 %29)
store i8* %30, i8** %7
%31 = icmp eq i8* %30, null
br i1 %31, label %32, label %42
32: ; preds = %20
%33 = load i32, i32* @STDOUT_FILENO
%34 = call i8* @ttyname(i32 %33)
store i8* %34, i8** %7
%35 = icmp eq i8* %34, null
br i1 %35, label %36, label %42
36: ; preds = %32
%37 = load i32, i32* @STDERR_FILENO
%38 = call i8* @ttyname(i32 %37)
store i8* %38, i8** %7
%39 = icmp eq i8* %38, null
br i1 %39, label %40, label %42
40: ; preds = %36
%41 = call i32 (i32, i8*, ...) @err(i32 2, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %42
42: ; preds = %40, %36, %32, %20
%43 = load i8*, i8** %7
%44 = call i64 @stat(i8* %43, %struct.stat* %6)
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %42
%47 = load i8*, i8** %7
%48 = call i32 (i32, i8*, ...) @err(i32 2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %47)
br label %49
49: ; preds = %46, %42
%50 = load i8**, i8*** %5
%51 = load i8*, i8** %50
%52 = icmp eq i8* %51, null
br i1 %52, label %53, label %65
53: ; preds = %49
%54 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%55 = load i32, i32* %54
%56 = load i32, i32* @S_IWGRP
%57 = and i32 %55, %56
%58 = icmp ne i32 %57, 0
br i1 %58, label %59, label %62
59: ; preds = %53
%60 = call i32 @puts(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%61 = call i32 @exit(i32 0)
unreachable
62: ; preds = %53
%63 = call i32 @puts(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%64 = call i32 @exit(i32 1)
unreachable
65: ; preds = %49
%66 = load i8**, i8*** %5
%67 = getelementptr inbounds i8*, i8** %66, i64 0
%68 = load i8*, i8** %67
%69 = load i8, i8* %68
%70 = sext i8 %69 to i32
switch i32 %70, label %98 [
i32 121, label %71
i32 110, label %84
]
71: ; preds = %65
%72 = load i8*, i8** %7
%73 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%74 = load i32, i32* %73
%75 = load i32, i32* @S_IWGRP
%76 = or i32 %74, %75
%77 = call i32 @chmod(i8* %72, i32 %76)
%78 = icmp slt i32 %77, 0
br i1 %78, label %79, label %82
79: ; preds = %71
%80 = load i8*, i8** %7
%81 = call i32 (i32, i8*, ...) @err(i32 2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %80)
br label %82
82: ; preds = %79, %71
%83 = call i32 @exit(i32 0)
unreachable
84: ; preds = %65
%85 = load i8*, i8** %7
%86 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%87 = load i32, i32* %86
%88 = load i32, i32* @S_IWGRP
%89 = xor i32 %88, -1
%90 = and i32 %87, %89
%91 = call i32 @chmod(i8* %85, i32 %90)
%92 = icmp slt i32 %91, 0
br i1 %92, label %93, label %96
93: ; preds = %84
%94 = load i8*, i8** %7
%95 = call i32 (i32, i8*, ...) @err(i32 2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %94)
br label %96
96: ; preds = %93, %84
%97 = call i32 @exit(i32 1)
unreachable
98: ; preds = %65
%99 = call i32 (...) @usage()
ret i32 0
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [1 x i8] zeroinitializer
@optind = global i64 0
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str.1 = [8 x i8] c"ttyname\00"
@.str.2 = [3 x i8] c"%s\00"
@S_IWGRP = global i32 0
@.str.3 = [5 x i8] c"is y\00"
@.str.4 = [5 x i8] c"is n\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32 %5, -1
br i1 %6, label %11, label %7
7: ; preds = %2, %7
%8 = tail call i32 (...) @usage()
%9 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %7
11: ; preds = %7, %2
%12 = load i64, i64* @optind
%13 = getelementptr inbounds i8*, i8** %1, i64 %12
%14 = load i32, i32* @STDIN_FILENO
%15 = tail call i8* @ttyname(i32 %14)
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %27
17: ; preds = %11
%18 = load i32, i32* @STDOUT_FILENO
%19 = tail call i8* @ttyname(i32 %18)
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %27
21: ; preds = %17
%22 = load i32, i32* @STDERR_FILENO
%23 = tail call i8* @ttyname(i32 %22)
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = tail call i32 (i32, i8*, ...) @err(i32 2, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %27
27: ; preds = %25, %21, %17, %11
%28 = phi i8* [ null, %25 ], [ %23, %21 ], [ %19, %17 ], [ %15, %11 ]
%29 = call i64 @stat(i8* %28, %struct.stat* %3)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %27
%32 = call i32 (i32, i8*, ...) @err(i32 2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %28)
br label %33
33: ; preds = %31, %27
%34 = load i8*, i8** %13
%35 = icmp eq i8* %34, null
br i1 %35, label %36, label %48
36: ; preds = %33
%37 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%38 = load i32, i32* %37
%39 = load i32, i32* @S_IWGRP
%40 = and i32 %39, %38
%41 = icmp eq i32 %40, 0
br i1 %41, label %45, label %42
42: ; preds = %36
%43 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%44 = call i32 @exit(i32 0)
unreachable
45: ; preds = %36
%46 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%47 = call i32 @exit(i32 1)
unreachable
48: ; preds = %33
%49 = load i8, i8* %34
%50 = sext i8 %49 to i32
switch i32 %50, label %74 [
i32 121, label %51
i32 110, label %62
]
51: ; preds = %48
%52 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%53 = load i32, i32* %52
%54 = load i32, i32* @S_IWGRP
%55 = or i32 %54, %53
%56 = call i32 @chmod(i8* %28, i32 %55)
%57 = icmp slt i32 %56, 0
br i1 %57, label %58, label %60
58: ; preds = %51
%59 = call i32 (i32, i8*, ...) @err(i32 2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %28)
br label %60
60: ; preds = %58, %51
%61 = call i32 @exit(i32 0)
unreachable
62: ; preds = %48
%63 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%64 = load i32, i32* %63
%65 = load i32, i32* @S_IWGRP
%66 = xor i32 %65, -1
%67 = and i32 %64, %66
%68 = call i32 @chmod(i8* %28, i32 %67)
%69 = icmp slt i32 %68, 0
br i1 %69, label %70, label %72
70: ; preds = %62
%71 = call i32 (i32, i8*, ...) @err(i32 2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %28)
br label %72
72: ; preds = %70, %62
%73 = call i32 @exit(i32 1)
unreachable
74: ; preds = %48
%75 = call i32 (...) @usage()
ret i32 0
}