#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 */

/* Variables and functions */
int ERANGE;
int EXIT_SUCCESS;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ assert(int);
char *ctermid(char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ strcmp(char *, char *);
int strstr(char *, char *);
char *ttyname(int);
int ttyname_r(int, char *, int);
int main() {
  int err;
  int stdin, null, dev;
  char buffer[256];
  char *result;
  stdin = open("/dev/stdin", O_RDONLY);
  null = open("/dev/null", O_RDONLY);
  dev = open("/dev", O_RDONLY);
  result = ctermid(buffer);
  assert(!strcmp(result, "/dev/tty"));
  err = ttyname_r(stdin, buffer, 256);
  assert(!err);
  assert(strstr(buffer, "/dev/tty"));
  err = ttyname_r(stdin, buffer, 2);
  assert(err == ERANGE);
  result = ttyname(stdin);
  assert(strstr(result, "/dev/tty"));
  result = ttyname(null);
  assert(!result);
  result = ttyname(dev);
  assert(!result);
  puts("success");
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e4f(%rip),%rax
mov    (%rax),%esi
lea    0xe5e(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0xc(%rbp)
mov    0x2e37(%rip),%rax
mov    (%rax),%esi
lea    0xe51(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x10(%rbp)
mov    0x2e1f(%rip),%rax
mov    (%rax),%esi
lea    0xe43(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
lea    -0x120(%rbp),%rdi
call   1090 <ctermid@plt>
mov    %rax,-0x128(%rbp)
mov    -0x128(%rbp),%rdi
lea    0xe1f(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0xc(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    $0x100,%edx
call   1060 <ttyname_r@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
lea    -0x120(%rbp),%rdi
lea    0xdd0(%rip),%rsi
call   10a0 <strstr@plt>
mov    %eax,%edi
call   1050 <assert@plt>
mov    -0xc(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    $0x2,%edx
call   1060 <ttyname_r@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    0x2d69(%rip),%rcx
cmp    (%rcx),%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0xc(%rbp),%edi
call   1070 <ttyname@plt>
mov    %rax,-0x128(%rbp)
mov    -0x128(%rbp),%rdi
lea    0xd77(%rip),%rsi
call   10a0 <strstr@plt>
mov    %eax,%edi
call   1050 <assert@plt>
mov    -0x10(%rbp),%edi
call   1070 <ttyname@plt>
mov    %rax,-0x128(%rbp)
cmpq   $0x0,-0x128(%rbp)
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x14(%rbp),%edi
call   1070 <ttyname@plt>
mov    %rax,-0x128(%rbp)
cmpq   $0x0,-0x128(%rbp)
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
lea    0xd21(%rip),%rdi
call   1030 <puts@plt>
mov    0x2cc2(%rip),%rax
mov    (%rax),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x100,%rsp
mov    0x2e52(%rip),%rbp
mov    0x0(%rbp),%esi
lea    0xe60(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%ebx
mov    0x0(%rbp),%esi
lea    0xe5a(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%r15d
mov    0x0(%rbp),%esi
lea    0xe52(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%r14d
mov    %rsp,%rbp
mov    %rbp,%rdi
call   1090 <ctermid@plt>
lea    0xe3d(%rip),%r12
mov    %rax,%rdi
mov    %r12,%rsi
call   1040 <strcmp@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1050 <assert@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x100,%edx
call   1060 <ttyname_r@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1050 <assert@plt>
mov    %rbp,%rdi
mov    %r12,%rsi
call   10a0 <strstr@plt>
mov    %eax,%edi
call   1050 <assert@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x2,%edx
call   1060 <ttyname_r@plt>
mov    0x2da7(%rip),%rcx
xor    %edi,%edi
cmp    (%rcx),%eax
sete   %dil
call   1050 <assert@plt>
mov    %ebx,%edi
call   1070 <ttyname@plt>
mov    %rax,%rdi
mov    %r12,%rsi
call   10a0 <strstr@plt>
mov    %eax,%edi
call   1050 <assert@plt>
mov    %r15d,%edi
call   1070 <ttyname@plt>
xor    %edi,%edi
test   %rax,%rax
sete   %dil
call   1050 <assert@plt>
mov    %r14d,%edi
call   1070 <ttyname@plt>
xor    %edi,%edi
test   %rax,%rax
sete   %dil
call   1050 <assert@plt>
lea    0xd91(%rip),%rdi
call   1030 <puts@plt>
mov    0x2d32(%rip),%rax
mov    (%rax),%eax
add    $0x100,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [11 x i8] c"/dev/stdin\00"
@O_RDONLY = global i32 0
@.str.1 = [10 x i8] c"/dev/null\00"
@.str.2 = [5 x i8] c"/dev\00"
@.str.3 = [9 x i8] c"/dev/tty\00"
@ERANGE = global i32 0
@.str.4 = [8 x i8] c"success\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [256 x i8]
%7 = alloca i8*
store i32 0, i32* %1
%8 = load i32, i32* @O_RDONLY
%9 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %8)
store i32 %9, i32* %3
%10 = load i32, i32* @O_RDONLY
%11 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %10)
store i32 %11, i32* %4
%12 = load i32, i32* @O_RDONLY
%13 = call i32 @open(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 %12)
store i32 %13, i32* %5
%14 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%15 = call i8* @ctermid(i8* %14)
store i8* %15, i8** %7
%16 = load i8*, i8** %7
%17 = call i32 @strcmp(i8* %16, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%18 = icmp ne i32 %17, 0
%19 = xor i1 %18, true
%20 = zext i1 %19 to i32
%21 = call i32 @assert(i32 %20)
%22 = load i32, i32* %3
%23 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%24 = call i32 @ttyname_r(i32 %22, i8* %23, i32 256)
store i32 %24, i32* %2
%25 = load i32, i32* %2
%26 = icmp ne i32 %25, 0
%27 = xor i1 %26, true
%28 = zext i1 %27 to i32
%29 = call i32 @assert(i32 %28)
%30 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%31 = call i32 @strstr(i8* %30, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%32 = call i32 @assert(i32 %31)
%33 = load i32, i32* %3
%34 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%35 = call i32 @ttyname_r(i32 %33, i8* %34, i32 2)
store i32 %35, i32* %2
%36 = load i32, i32* %2
%37 = load i32, i32* @ERANGE
%38 = icmp eq i32 %36, %37
%39 = zext i1 %38 to i32
%40 = call i32 @assert(i32 %39)
%41 = load i32, i32* %3
%42 = call i8* @ttyname(i32 %41)
store i8* %42, i8** %7
%43 = load i8*, i8** %7
%44 = call i32 @strstr(i8* %43, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%45 = call i32 @assert(i32 %44)
%46 = load i32, i32* %4
%47 = call i8* @ttyname(i32 %46)
store i8* %47, i8** %7
%48 = load i8*, i8** %7
%49 = icmp ne i8* %48, null
%50 = xor i1 %49, true
%51 = zext i1 %50 to i32
%52 = call i32 @assert(i32 %51)
%53 = load i32, i32* %5
%54 = call i8* @ttyname(i32 %53)
store i8* %54, i8** %7
%55 = load i8*, i8** %7
%56 = icmp ne i8* %55, null
%57 = xor i1 %56, true
%58 = zext i1 %57 to i32
%59 = call i32 @assert(i32 %58)
%60 = call i32 @puts(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
%61 = load i32, i32* @EXIT_SUCCESS
ret i32 %61
}

LLVM IR (-O2):
@.str = [11 x i8] c"/dev/stdin\00"
@O_RDONLY = global i32 0
@.str.1 = [10 x i8] c"/dev/null\00"
@.str.2 = [5 x i8] c"/dev\00"
@.str.3 = [9 x i8] c"/dev/tty\00"
@ERANGE = global i32 0
@.str.4 = [8 x i8] c"success\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca [256 x i8]
%2 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %5)
%7 = load i32, i32* @O_RDONLY
%8 = tail call i32 @open(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 %7)
%9 = call i8* @ctermid(i8* %2)
%10 = tail call i32 @strcmp(i8* dereferenceable(1) %9, i8* dereferenceable(9) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%11 = icmp eq i32 %10, 0
%12 = zext i1 %11 to i32
%13 = tail call i32 @assert(i32 %12)
%14 = call i32 @ttyname_r(i32 %4, i8* %2, i32 256)
%15 = icmp eq i32 %14, 0
%16 = zext i1 %15 to i32
%17 = call i32 @assert(i32 %16)
%18 = call i32 @strstr(i8* %2, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%19 = call i32 @assert(i32 %18)
%20 = call i32 @ttyname_r(i32 %4, i8* %2, i32 2)
%21 = load i32, i32* @ERANGE
%22 = icmp eq i32 %20, %21
%23 = zext i1 %22 to i32
%24 = call i32 @assert(i32 %23)
%25 = call i8* @ttyname(i32 %4)
%26 = call i32 @strstr(i8* %25, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%27 = call i32 @assert(i32 %26)
%28 = call i8* @ttyname(i32 %6)
%29 = icmp eq i8* %28, null
%30 = zext i1 %29 to i32
%31 = call i32 @assert(i32 %30)
%32 = call i8* @ttyname(i32 %8)
%33 = icmp eq i8* %32, null
%34 = zext i1 %33 to i32
%35 = call i32 @assert(i32 %34)
%36 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
%37 = load i32, i32* @EXIT_SUCCESS
ret i32 %37
}