#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 /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ O_RDWR;
char *ctermid(char *);
int errno;
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
char *ttyname(int);
scalar_t__ ttyname_r(int, char *, int);
int main() {
  char buffer[256];
  int d = open("/device", O_RDWR);
  int f = open("/", O_RDONLY);
  char *result;
  result = ctermid(buffer);
  if (result) {
    printf("ctermid: %s\n", result);
  } else {
    printf("ctermid errno: %d\n", errno);
    errno = 0;
  }
  if (ttyname_r(d, buffer, 256) == 0) {
    printf("ttyname_r(d, ..., 256): %s\n", buffer);
  } else {
    printf("ttyname_r(d, ..., 256) errno: %d\n", errno);
    errno = 0;
  }
  if (ttyname_r(d, buffer, 2) == 0) {
    printf("ttyname_r(d, ..., 2): %s\n", buffer);
  } else {
    printf("ttyname_r(d, ..., 2) errno: %d\n", errno);
    errno = 0;
  }
  result = ttyname(d);
  if (result) {
    printf("ttyname(d): %s\n", result);
  } else {
    printf("ttyname(d) errno: %d\n", errno);
    errno = 0;
  }
  result = ttyname(f);
  if (result) {
    printf("ttyname(f): %s\n", result);
  } else {
    printf("ttyname(f) errno: %d\n", errno);
    errno = 0;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e7f(%rip),%rax
mov    (%rax),%esi
lea    0xe8e(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x114(%rbp)
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
lea    0xe7b(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x118(%rbp)
lea    -0x110(%rbp),%rdi
call   1070 <ctermid@plt>
mov    %rax,-0x120(%rbp)
cmpq   $0x0,-0x120(%rbp)
je     11d3 <main+0x83>
mov    -0x120(%rbp),%rsi
lea    0xe43(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11f7 <main+0xa7>
mov    0x2df6(%rip),%rax
mov    (%rax),%esi
lea    0xe34(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2ddf(%rip),%rax
movl   $0x0,(%rax)
mov    -0x114(%rbp),%edi
lea    -0x110(%rbp),%rsi
mov    $0x100,%edx
call   1040 <ttyname_r@plt>
cmp    $0x0,%rax
jne    1232 <main+0xe2>
lea    -0x110(%rbp),%rsi
lea    0xe04(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1256 <main+0x106>
mov    0x2d97(%rip),%rax
mov    (%rax),%esi
lea    0xe04(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2d80(%rip),%rax
movl   $0x0,(%rax)
mov    -0x114(%rbp),%edi
lea    -0x110(%rbp),%rsi
mov    $0x2,%edx
call   1040 <ttyname_r@plt>
cmp    $0x0,%rax
jne    1291 <main+0x141>
lea    -0x110(%rbp),%rsi
lea    0xde3(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12b5 <main+0x165>
mov    0x2d38(%rip),%rax
mov    (%rax),%esi
lea    0xde1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2d21(%rip),%rax
movl   $0x0,(%rax)
mov    -0x114(%rbp),%edi
call   1050 <ttyname@plt>
mov    %rax,-0x120(%rbp)
cmpq   $0x0,-0x120(%rbp)
je     12ef <main+0x19f>
mov    -0x120(%rbp),%rsi
lea    0xdbf(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1313 <main+0x1c3>
mov    0x2cda(%rip),%rax
mov    (%rax),%esi
lea    0xdb3(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2cc3(%rip),%rax
movl   $0x0,(%rax)
mov    -0x118(%rbp),%edi
call   1050 <ttyname@plt>
mov    %rax,-0x120(%rbp)
cmpq   $0x0,-0x120(%rbp)
je     134d <main+0x1fd>
mov    -0x120(%rbp),%rsi
lea    0xd87(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1371 <main+0x221>
mov    0x2c7c(%rip),%rax
mov    (%rax),%esi
lea    0xd7b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2c65(%rip),%rax
movl   $0x0,(%rax)
xor    %eax,%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x100,%rsp
mov    0x2e86(%rip),%rax
mov    (%rax),%esi
lea    0xe95(%rip),%rdi
call   1060 <open@plt>
mov    %eax,%ebp
mov    0x2e67(%rip),%rax
mov    (%rax),%esi
lea    0xe86(%rip),%rdi
call   1060 <open@plt>
mov    %eax,%r14d
mov    %rsp,%rdi
call   1070 <ctermid@plt>
test   %rax,%rax
je     11aa <main+0x5a>
lea    0xe6c(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1030 <printf@plt>
jmp    11c7 <main+0x77>
mov    0x2e1f(%rip),%rbx
mov    (%rbx),%esi
lea    0xe5d(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
movl   $0x0,(%rbx)
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x100,%edx
call   1040 <ttyname_r@plt>
test   %rax,%rax
je     11fa <main+0xaa>
mov    0x2dee(%rip),%rbx
mov    (%rbx),%esi
lea    0xe5b(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
movl   $0x0,(%rbx)
jmp    120b <main+0xbb>
lea    0xe29(%rip),%rdi
mov    %rsp,%rsi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x2,%edx
call   1040 <ttyname_r@plt>
test   %rax,%rax
je     123e <main+0xee>
mov    0x2daa(%rip),%rbx
mov    (%rbx),%esi
lea    0xe53(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
movl   $0x0,(%rbx)
jmp    124f <main+0xff>
lea    0xe23(%rip),%rdi
mov    %rsp,%rsi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %ebp,%edi
call   1050 <ttyname@plt>
test   %rax,%rax
je     126e <main+0x11e>
lea    0xe40(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1030 <printf@plt>
jmp    128b <main+0x13b>
mov    0x2d5b(%rip),%rbx
mov    (%rbx),%esi
lea    0xe34(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
movl   $0x0,(%rbx)
mov    %r14d,%edi
call   1050 <ttyname@plt>
test   %rax,%rax
je     12ab <main+0x15b>
lea    0xe29(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1030 <printf@plt>
jmp    12c8 <main+0x178>
mov    0x2d1e(%rip),%rbx
mov    (%rbx),%esi
lea    0xe1d(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
movl   $0x0,(%rbx)
xor    %eax,%eax
add    $0x100,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [8 x i8] c"/device\00"
@O_RDWR = global i32 0
@.str.1 = [2 x i8] c"/\00"
@O_RDONLY = global i32 0
@.str.2 = [13 x i8] c"ctermid: %s\0A\00"
@.str.3 = [19 x i8] c"ctermid errno: %d\0A\00"
@errno = global i32 0
@.str.4 = [28 x i8] c"ttyname_r(d, ..., 256): %s\0A\00"
@.str.5 = [34 x i8] c"ttyname_r(d, ..., 256) errno: %d\0A\00"
@.str.6 = [26 x i8] c"ttyname_r(d, ..., 2): %s\0A\00"
@.str.7 = [32 x i8] c"ttyname_r(d, ..., 2) errno: %d\0A\00"
@.str.8 = [16 x i8] c"ttyname(d): %s\0A\00"
@.str.9 = [22 x i8] c"ttyname(d) errno: %d\0A\00"
@.str.10 = [16 x i8] c"ttyname(f): %s\0A\00"
@.str.11 = [22 x i8] c"ttyname(f) errno: %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [256 x i8]
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
store i32 0, i32* %1
%6 = load i32, i32* @O_RDWR
%7 = call i32 @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %6)
store i32 %7, i32* %3
%8 = load i32, i32* @O_RDONLY
%9 = call i32 @open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 %8)
store i32 %9, i32* %4
%10 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%11 = call i8* @ctermid(i8* %10)
store i8* %11, i8** %5
%12 = load i8*, i8** %5
%13 = icmp ne i8* %12, null
br i1 %13, label %14, label %17
14: ; preds = %0
%15 = load i8*, i8** %5
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* %15)
br label %20
17: ; preds = %0
%18 = load i32, i32* @errno
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i32 %18)
store i32 0, i32* @errno
br label %20
20: ; preds = %17, %14
%21 = load i32, i32* %3
%22 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%23 = call i64 @ttyname_r(i32 %21, i8* %22, i32 256)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %20
%26 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i8* %26)
br label %31
28: ; preds = %20
%29 = load i32, i32* @errno
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0), i32 %29)
store i32 0, i32* @errno
br label %31
31: ; preds = %28, %25
%32 = load i32, i32* %3
%33 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%34 = call i64 @ttyname_r(i32 %32, i8* %33, i32 2)
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %31
%37 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0), i8* %37)
br label %42
39: ; preds = %31
%40 = load i32, i32* @errno
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.7, i64 0, i64 0), i32 %40)
store i32 0, i32* @errno
br label %42
42: ; preds = %39, %36
%43 = load i32, i32* %3
%44 = call i8* @ttyname(i32 %43)
store i8* %44, i8** %5
%45 = load i8*, i8** %5
%46 = icmp ne i8* %45, null
br i1 %46, label %47, label %50
47: ; preds = %42
%48 = load i8*, i8** %5
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.8, i64 0, i64 0), i8* %48)
br label %53
50: ; preds = %42
%51 = load i32, i32* @errno
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.9, i64 0, i64 0), i32 %51)
store i32 0, i32* @errno
br label %53
53: ; preds = %50, %47
%54 = load i32, i32* %4
%55 = call i8* @ttyname(i32 %54)
store i8* %55, i8** %5
%56 = load i8*, i8** %5
%57 = icmp ne i8* %56, null
br i1 %57, label %58, label %61
58: ; preds = %53
%59 = load i8*, i8** %5
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.10, i64 0, i64 0), i8* %59)
br label %64
61: ; preds = %53
%62 = load i32, i32* @errno
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.11, i64 0, i64 0), i32 %62)
store i32 0, i32* @errno
br label %64
64: ; preds = %61, %58
ret i32 0
}

LLVM IR (-O2):
@.str = [8 x i8] c"/device\00"
@O_RDWR = global i32 0
@.str.1 = [2 x i8] c"/\00"
@O_RDONLY = global i32 0
@.str.2 = [13 x i8] c"ctermid: %s\0A\00"
@.str.3 = [19 x i8] c"ctermid errno: %d\0A\00"
@errno = global i32 0
@.str.4 = [28 x i8] c"ttyname_r(d, ..., 256): %s\0A\00"
@.str.5 = [34 x i8] c"ttyname_r(d, ..., 256) errno: %d\0A\00"
@.str.6 = [26 x i8] c"ttyname_r(d, ..., 2): %s\0A\00"
@.str.7 = [32 x i8] c"ttyname_r(d, ..., 2) errno: %d\0A\00"
@.str.8 = [16 x i8] c"ttyname(d): %s\0A\00"
@.str.9 = [22 x i8] c"ttyname(d) errno: %d\0A\00"
@.str.10 = [16 x i8] c"ttyname(f): %s\0A\00"
@.str.11 = [22 x i8] c"ttyname(f) errno: %d\0A\00"
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_RDWR
%4 = tail call i32 @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 %5)
%7 = call i8* @ctermid(i8* %2)
%8 = icmp eq i8* %7, null
br i1 %8, label %11, label %9
9: ; preds = %0
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* %7)
br label %14
11: ; preds = %0
%12 = load i32, i32* @errno
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i32 %12)
store i32 0, i32* @errno
br label %14
14: ; preds = %11, %9
%15 = call i64 @ttyname_r(i32 %4, i8* %2, i32 256)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i8* %2)
br label %22
19: ; preds = %14
%20 = load i32, i32* @errno
%21 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0), i32 %20)
store i32 0, i32* @errno
br label %22
22: ; preds = %19, %17
%23 = call i64 @ttyname_r(i32 %4, i8* %2, i32 2)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0), i8* %2)
br label %30
27: ; preds = %22
%28 = load i32, i32* @errno
%29 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.7, i64 0, i64 0), i32 %28)
store i32 0, i32* @errno
br label %30
30: ; preds = %27, %25
%31 = call i8* @ttyname(i32 %4)
%32 = icmp eq i8* %31, null
br i1 %32, label %35, label %33
33: ; preds = %30
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.8, i64 0, i64 0), i8* %31)
br label %38
35: ; preds = %30
%36 = load i32, i32* @errno
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.9, i64 0, i64 0), i32 %36)
store i32 0, i32* @errno
br label %38
38: ; preds = %35, %33
%39 = call i8* @ttyname(i32 %6)
%40 = icmp eq i8* %39, null
br i1 %40, label %43, label %41
41: ; preds = %38
%42 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.10, i64 0, i64 0), i8* %39)
br label %46
43: ; preds = %38
%44 = load i32, i32* @errno
%45 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.11, i64 0, i64 0), i32 %44)
store i32 0, i32* @errno
br label %46
46: ; preds = %43, %41
ret i32 0
}