#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*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ isatty(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ puts(char *);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
scalar_t__ strncmp(char *, char *, int);
char *ttyname(int /*<<< orphan*/);
int /*<<< orphan*/ usage();
int main(int argc, char **argv) {
  char *tty;
  struct stat stbuf;
  if (argc != 1 && argc != 2)
    usage();
  if (argc == 2 && (strncmp(argv[1], "y", 1) && strncmp(argv[1], "n", 1)))
    usage();
  if (!isatty(STDIN_FILENO)) {
    fprintf(stderr, "stdin: is not a tty\n");
    exit(1);
  }
  if ((tty = ttyname(STDIN_FILENO)) == NULL &&
      (tty = ttyname(STDOUT_FILENO)) == NULL &&
      (tty = ttyname(STDERR_FILENO)) == NULL) {
    perror(tty);
    exit(1);
  }
  if (stat(tty, &stbuf) < 0) {
    perror(tty);
    exit(1);
  }
  if (argc == 1) {
    if (stbuf.st_mode & S_IWGRP)
      puts("is y");
    else
      puts("is n");
    return 0;
  }
  switch (argv[1][0]) {
  case 'y':
    if (chmod(tty, stbuf.st_mode | S_IWGRP) < 0) {
      perror(tty);
      exit(1);
    }
    break;
  case 'n':
    if (chmod(tty, stbuf.st_mode & ~S_IWGRP) < 0) {
      perror(tty);
      exit(1);
    }
    break;
  default:
    usage();
    break;
  }
  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)
cmpl   $0x1,-0x8(%rbp)
je     11d1 <main+0x31>
cmpl   $0x2,-0x8(%rbp)
je     11d1 <main+0x31>
mov    $0x0,%al
call   1090 <usage@plt>
cmpl   $0x2,-0x8(%rbp)
jne    1228 <main+0x88>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe2e(%rip),%rsi
mov    $0x1,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
je     1228 <main+0x88>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe10(%rip),%rsi
mov    $0x1,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
je     1228 <main+0x88>
mov    $0x0,%al
call   1090 <usage@plt>
mov    0x2d91(%rip),%rax
mov    (%rax),%edi
call   1050 <isatty@plt>
cmp    $0x0,%eax
jne    125e <main+0xbe>
mov    0x2d8a(%rip),%rax
mov    (%rax),%edi
lea    0xdb1(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    0x2d5b(%rip),%rax
mov    (%rax),%edi
call   1080 <ttyname@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    12c5 <main+0x125>
mov    0x2d6f(%rip),%rax
mov    (%rax),%edi
call   1080 <ttyname@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    12c5 <main+0x125>
mov    0x2d2b(%rip),%rax
mov    (%rax),%edi
call   1080 <ttyname@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    12c5 <main+0x125>
mov    -0x18(%rbp),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
jge    12ef <main+0x14f>
mov    -0x18(%rbp),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
cmpl   $0x1,-0x8(%rbp)
jne    1337 <main+0x197>
mov    -0x20(%rbp),%eax
mov    0x2cdd(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     131f <main+0x17f>
lea    0xd00(%rip),%rdi
call   1040 <puts@plt>
jmp    132b <main+0x18b>
lea    0xcf4(%rip),%rdi
call   1040 <puts@plt>
movl   $0x0,-0x4(%rbp)
jmp    13e3 <main+0x243>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
movsbl (%rax),%eax
mov    %eax,-0x24(%rbp)
sub    $0x6e,%eax
je     139a <main+0x1fa>
jmp    1353 <main+0x1b3>
mov    -0x24(%rbp),%eax
sub    $0x79,%eax
jne    13d5 <main+0x235>
jmp    1364 <main+0x1c4>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%esi
mov    0x2c6e(%rip),%rax
or     (%rax),%esi
call   10a0 <chmod@plt>
cmp    $0x0,%eax
jge    1395 <main+0x1f5>
mov    -0x18(%rbp),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
jmp    13dc <main+0x23c>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%esi
mov    0x2c38(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    %eax,%esi
call   10a0 <chmod@plt>
cmp    $0x0,%eax
jge    13d0 <main+0x230>
mov    -0x18(%rbp),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
jmp    13dc <main+0x23c>
mov    $0x0,%al
call   1090 <usage@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %edi,%r15d
lea    -0x3(%r15),%eax
cmp    $0xfffffffe,%eax
jae    11c1 <main+0x21>
xor    %eax,%eax
call   1090 <usage@plt>
jmp    11fb <main+0x5b>
cmp    $0x2,%r15d
jne    11fb <main+0x5b>
mov    0x8(%r14),%rdi
lea    0xe46(%rip),%rsi
mov    $0x1,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     11fb <main+0x5b>
mov    0x8(%r14),%rdi
lea    0xe31(%rip),%rsi
mov    $0x1,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
jne    11b8 <main+0x18>
mov    0x2dbe(%rip),%rbx
mov    (%rbx),%edi
call   1050 <isatty@plt>
test   %eax,%eax
je     12f3 <main+0x153>
mov    (%rbx),%edi
call   1080 <ttyname@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1250 <main+0xb0>
mov    0x2dc9(%rip),%rax
mov    (%rax),%edi
call   1080 <ttyname@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1250 <main+0xb0>
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
call   1080 <ttyname@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1312 <main+0x172>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1070 <stat@plt>
test   %rax,%rax
js     12ce <main+0x12e>
cmp    $0x1,%r15d
jne    1285 <main+0xe5>
mov    0x2d71(%rip),%rax
mov    (%rax),%eax
test   %eax,0x8(%rsp)
je     12a5 <main+0x105>
lea    0xd97(%rip),%rdi
call   1040 <puts@plt>
jmp    12e7 <main+0x147>
mov    0x8(%r14),%rax
movsbl (%rax),%eax
cmp    $0x6e,%eax
je     12b3 <main+0x113>
cmp    $0x79,%eax
jne    12e0 <main+0x140>
mov    0x2d43(%rip),%rax
mov    (%rax),%esi
or     0x8(%rsp),%esi
jmp    12c2 <main+0x122>
lea    0xd6e(%rip),%rdi
call   1040 <puts@plt>
jmp    12e7 <main+0x147>
mov    0x2d26(%rip),%rax
mov    (%rax),%esi
not    %esi
and    0x8(%rsp),%esi
mov    %rbx,%rdi
call   10a0 <chmod@plt>
test   %eax,%eax
jns    12e7 <main+0x147>
mov    %rbx,%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
xor    %eax,%eax
call   1090 <usage@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2cd6(%rip),%rax
mov    (%rax),%edi
lea    0xcfd(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
xor    %edi,%edi
jmp    12d1 <main+0x131>

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

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [2 x i8] c"y\00"
@.str.1 = [2 x i8] c"n\00"
@STDIN_FILENO = global i32 0
@stderr = global i32 0
@.str.2 = [21 x i8] c"stdin: is not a tty\0A\00"
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@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** nocapture %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = add i32 %0, -3
%6 = icmp ult i32 %5, -2
br i1 %6, label %18, label %7
7: ; preds = %2
%8 = icmp eq i32 %0, 2
br i1 %8, label %9, label %20
9: ; preds = %7
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = tail call i64 @strncmp(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 1)
%13 = icmp eq i64 %12, 0
br i1 %13, label %20, label %14
14: ; preds = %9
%15 = load i8*, i8** %10
%16 = tail call i64 @strncmp(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1)
%17 = icmp eq i64 %16, 0
br i1 %17, label %20, label %18
18: ; preds = %14, %2
%19 = tail call i32 (...) @usage()
br label %20
20: ; preds = %18, %14, %9, %7
%21 = load i32, i32* @STDIN_FILENO
%22 = tail call i32 @isatty(i32 %21)
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %20
%25 = load i32, i32* @stderr
%26 = tail call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%27 = tail call i32 @exit(i32 1)
unreachable
28: ; preds = %20
%29 = load i32, i32* @STDIN_FILENO
%30 = tail call i8* @ttyname(i32 %29)
%31 = icmp eq i8* %30, null
br i1 %31, label %32, label %43
32: ; preds = %28
%33 = load i32, i32* @STDOUT_FILENO
%34 = tail call i8* @ttyname(i32 %33)
%35 = icmp eq i8* %34, null
br i1 %35, label %36, label %43
36: ; preds = %32
%37 = load i32, i32* @STDERR_FILENO
%38 = tail call i8* @ttyname(i32 %37)
%39 = icmp eq i8* %38, null
br i1 %39, label %40, label %43
40: ; preds = %36
%41 = tail call i32 @perror(i8* null)
%42 = tail call i32 @exit(i32 1)
unreachable
43: ; preds = %36, %32, %28
%44 = phi i8* [ %38, %36 ], [ %34, %32 ], [ %30, %28 ]
%45 = call i64 @stat(i8* %44, %struct.stat* %3)
%46 = icmp slt i64 %45, 0
br i1 %46, label %47, label %50
47: ; preds = %43
%48 = call i32 @perror(i8* %44)
%49 = call i32 @exit(i32 1)
unreachable
50: ; preds = %43
%51 = icmp eq i32 %0, 1
br i1 %51, label %52, label %62
52: ; preds = %50
%53 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%54 = load i32, i32* %53
%55 = load i32, i32* @S_IWGRP
%56 = and i32 %55, %54
%57 = icmp eq i32 %56, 0
br i1 %57, label %60, label %58
58: ; preds = %52
%59 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %90
60: ; preds = %52
%61 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
br label %90
62: ; preds = %50
%63 = getelementptr inbounds i8*, i8** %1, i64 1
%64 = load i8*, i8** %63
%65 = load i8, i8* %64
%66 = sext i8 %65 to i32
switch i32 %66, label %88 [
i32 121, label %67
i32 110, label %77
]
67: ; preds = %62
%68 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%69 = load i32, i32* %68
%70 = load i32, i32* @S_IWGRP
%71 = or i32 %70, %69
%72 = call i32 @chmod(i8* %44, i32 %71)
%73 = icmp slt i32 %72, 0
br i1 %73, label %74, label %90
74: ; preds = %67
%75 = call i32 @perror(i8* %44)
%76 = call i32 @exit(i32 1)
unreachable
77: ; preds = %62
%78 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%79 = load i32, i32* %78
%80 = load i32, i32* @S_IWGRP
%81 = xor i32 %80, -1
%82 = and i32 %79, %81
%83 = call i32 @chmod(i8* %44, i32 %82)
%84 = icmp slt i32 %83, 0
br i1 %84, label %85, label %90
85: ; preds = %77
%86 = call i32 @perror(i8* %44)
%87 = call i32 @exit(i32 1)
unreachable
88: ; preds = %62
%89 = call i32 (...) @usage()
br label %90
90: ; preds = %88, %67, %77, %58, %60
ret i32 0
}