#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*/ L_ctermid;
int O_NOCTTY;
int O_WRONLY;
int /*<<< orphan*/ close(int);
char *malloc(int /*<<< orphan*/);
int open(char *, int);
char *strcpy(char *, char *);
scalar_t__ ttyname_r(int, char *, int /*<<< orphan*/);
char *ctermid(char *s) {
  static char *s2;
  int fd;
  if (!s) {
    if (!s2)
      s2 = malloc(L_ctermid);
    s = s2;
  }
  fd = open("/dev/tty", O_WRONLY | O_NOCTTY);
  if (fd < 0)
    return strcpy(s, "");
  if (ttyname_r(fd, s, L_ctermid))
    strcpy(s, "");
  close(fd);
  return s;
}


x86 assembly (clang, -O0):
<ctermid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1195 <ctermid+0x45>
cmpq   $0x0,0x2ee1(%rip)
jne    118a <ctermid+0x3a>
mov    0x2e5c(%rip),%rax
mov    (%rax),%edi
call   1050 <malloc@plt>
mov    %rax,0x2ec6(%rip)
mov    0x2ebf(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
mov    0x2e3b(%rip),%rax
or     (%rax),%esi
lea    0xe52(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11d9 <ctermid+0x89>
mov    -0x10(%rbp),%rdi
lea    0xe3d(%rip),%rsi
call   1030 <strcpy@plt>
mov    %rax,-0x8(%rbp)
jmp    1218 <ctermid+0xc8>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2df1(%rip),%rax
mov    (%rax),%edx
call   1060 <ttyname_r@plt>
cmp    $0x0,%rax
je     1208 <ctermid+0xb8>
mov    -0x10(%rbp),%rdi
lea    0xe05(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ctermid>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
test   %rdi,%rdi
jne    116f <ctermid+0x2f>
mov    0x2ef6(%rip),%rbx
test   %rbx,%rbx
jne    116f <ctermid+0x2f>
mov    0x2e7a(%rip),%rax
mov    (%rax),%edi
call   1040 <malloc@plt>
mov    %rax,%rbx
mov    %rax,0x2ed9(%rip)
mov    0x2e52(%rip),%rax
mov    0x2e63(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe78(%rip),%rdi
call   1060 <open@plt>
test   %eax,%eax
js     11b7 <ctermid+0x77>
mov    %eax,%ebp
mov    0x2e3e(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
mov    %rbx,%rsi
call   1050 <ttyname_r@plt>
test   %rax,%rax
je     11ae <ctermid+0x6e>
movb   $0x0,(%rbx)
mov    %ebp,%edi
call   1030 <close@plt>
jmp    11ba <ctermid+0x7a>
movb   $0x0,(%rbx)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@ctermid.s2 = internal global i8* null
@L_ctermid = global i32 0
@.str = [9 x i8] c"/dev/tty\00"
@O_WRONLY = global i32 0
@O_NOCTTY = global i32 0
@.str.1 = [1 x i8] zeroinitializer
define i8* @ctermid(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = icmp ne i8* %5, null
br i1 %6, label %15, label %7
7: ; preds = %1
%8 = load i8*, i8** @ctermid.s2
%9 = icmp ne i8* %8, null
br i1 %9, label %13, label %10
10: ; preds = %7
%11 = load i32, i32* @L_ctermid
%12 = call i8* @malloc(i32 %11)
store i8* %12, i8** @ctermid.s2
br label %13
13: ; preds = %10, %7
%14 = load i8*, i8** @ctermid.s2
store i8* %14, i8** %3
br label %15
15: ; preds = %13, %1
%16 = load i32, i32* @O_WRONLY
%17 = load i32, i32* @O_NOCTTY
%18 = or i32 %16, %17
%19 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %18)
store i32 %19, i32* %4
%20 = load i32, i32* %4
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %15
%23 = load i8*, i8** %3
%24 = call i8* @strcpy(i8* %23, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
store i8* %24, i8** %2
br label %38
25: ; preds = %15
%26 = load i32, i32* %4
%27 = load i8*, i8** %3
%28 = load i32, i32* @L_ctermid
%29 = call i64 @ttyname_r(i32 %26, i8* %27, i32 %28)
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %25
%32 = load i8*, i8** %3
%33 = call i8* @strcpy(i8* %32, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
br label %34
34: ; preds = %31, %25
%35 = load i32, i32* %4
%36 = call i32 @close(i32 %35)
%37 = load i8*, i8** %3
store i8* %37, i8** %2
br label %38
38: ; preds = %34, %22
%39 = load i8*, i8** %2
ret i8* %39
}

LLVM IR (-O2):
@ctermid.s2 = internal unnamed_addr global i8* null
@L_ctermid = global i32 0
@.str = [9 x i8] c"/dev/tty\00"
@O_WRONLY = global i32 0
@O_NOCTTY = global i32 0
define i8* @ctermid(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %3, label %9
3: ; preds = %1
%4 = load i8*, i8** @ctermid.s2
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %9
6: ; preds = %3
%7 = load i32, i32* @L_ctermid
%8 = tail call i8* @malloc(i32 %7)
store i8* %8, i8** @ctermid.s2
br label %9
9: ; preds = %3, %6, %1
%10 = phi i8* [ %0, %1 ], [ %8, %6 ], [ %4, %3 ]
%11 = load i32, i32* @O_WRONLY
%12 = load i32, i32* @O_NOCTTY
%13 = or i32 %12, %11
%14 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %9
store i8 0, i8* %10
br label %24
17: ; preds = %9
%18 = load i32, i32* @L_ctermid
%19 = tail call i64 @ttyname_r(i32 %14, i8* %10, i32 %18)
%20 = icmp eq i64 %19, 0
br i1 %20, label %22, label %21
21: ; preds = %17
store i8 0, i8* %10
br label %22
22: ; preds = %21, %17
%23 = tail call i32 @close(i32 %14)
br label %24
24: ; preds = %22, %16
ret i8* %10
}