#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 */
typedef int /*<<< orphan*/ uidbuf;
typedef scalar_t__ uid_t;
struct passwd {
  int /*<<< orphan*/ pw_name;
};

/* Variables and functions */
struct passwd *getpwuid(scalar_t__);
scalar_t__ getuid();
int /*<<< orphan*/ sprintf(char *, char *, ...);
size_t strlen(char *);
int /*<<< orphan*/ strncat(char *, int /*<<< orphan*/, int);
scalar_t__ strncmp(char *, char *, size_t const);
char *ttyname(int /*<<< orphan*/);
int /*<<< orphan*/ xasprintf(char **, char *, char *);
void getuidtty(char **userp, char **ttyp) {
  struct passwd *pwd;
  uid_t uid;
  char *tty;
  static char uidbuf[32];
  char *ttynm = NULL;
  uid = getuid();
  if ((pwd = getpwuid(uid)) != NULL) {
    uidbuf[0] = 0;
    strncat(uidbuf, pwd->pw_name, sizeof(uidbuf) - 1);
  } else {
    if (uid)
      sprintf(uidbuf, "uid %d", (int)uid);
    else
      sprintf(uidbuf, "root");
  }
  if ((tty = ttyname(0)) != NULL) {
    const size_t plen = strlen("/dev/");
    if (strncmp(tty, "/dev/", plen) == 0) {
      tty += plen;
      if (tty[0] == '/')
        tty++;
    }
    xasprintf(&ttynm, "(%s) ", tty);
  }
  *userp = uidbuf;
  *ttyp = ttynm;
}


x86 assembly (clang, -O0):
<getuidtty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x30(%rbp)
mov    $0x0,%al
call   1060 <getuid@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <getpwuid@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     11dd <getuidtty+0x5d>
movb   $0x0,0x2eaf(%rip)
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
lea    0x2ea2(%rip),%rdi
mov    $0x1f,%edx
call   1070 <strncat@plt>
jmp    1222 <getuidtty+0xa2>
cmpq   $0x0,-0x20(%rbp)
je     1208 <getuidtty+0x88>
mov    -0x20(%rbp),%rax
mov    %eax,%edx
lea    0x2e7b(%rip),%rdi
lea    0xe04(%rip),%rsi
mov    $0x0,%al
call   10a0 <sprintf@plt>
jmp    121d <getuidtty+0x9d>
lea    0x2e61(%rip),%rdi
lea    0xdf1(%rip),%rsi
mov    $0x0,%al
call   10a0 <sprintf@plt>
jmp    1222 <getuidtty+0xa2>
xor    %edi,%edi
call   1090 <ttyname@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     12a6 <getuidtty+0x126>
lea    0xdce(%rip),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x38(%rbp),%rdx
lea    0xdb6(%rip),%rsi
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    1292 <getuidtty+0x112>
mov    -0x38(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    128d <getuidtty+0x10d>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    1292 <getuidtty+0x112>
mov    -0x28(%rbp),%rdx
lea    -0x30(%rbp),%rdi
lea    0xd71(%rip),%rsi
call   1080 <xasprintf@plt>
mov    -0x8(%rbp),%rax
lea    0x2dbf(%rip),%rcx
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getuidtty>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
movq   $0x0,(%rsp)
xor    %eax,%eax
call   1050 <getuid@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1040 <getpwuid@plt>
test   %rax,%rax
je     11b9 <getuidtty+0x49>
movb   $0x0,0x2ecc(%rip)
lea    0x2ec5(%rip),%rdi
mov    (%rax),%esi
mov    $0x1f,%edx
call   1060 <strncat@plt>
jmp    11e8 <getuidtty+0x78>
test   %rbx,%rbx
je     11d7 <getuidtty+0x67>
lea    0x2eab(%rip),%rdi
lea    0xe34(%rip),%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1090 <sprintf@plt>
jmp    11e8 <getuidtty+0x78>
movb   $0x0,0x2e96(%rip)
movl   $0x746f6f72,0x2e88(%rip)
6f 6f 74
xor    %r12d,%r12d
xor    %edi,%edi
call   1080 <ttyname@plt>
test   %rax,%rax
je     123f <getuidtty+0xcf>
mov    %rax,%rbx
lea    0xe0b(%rip),%rsi
mov    $0x5,%edx
mov    %rax,%rdi
call   1030 <strncmp@plt>
test   %rax,%rax
jne    1229 <getuidtty+0xb9>
lea    0x5(%rbx),%rax
mov    %rbx,%rcx
add    $0x6,%rcx
cmpb   $0x2f,0x5(%rbx)
cmovne %rax,%rcx
mov    %rcx,%rbx
lea    0xde2(%rip),%rsi
mov    %rsp,%rdi
mov    %rbx,%rdx
call   1070 <xasprintf@plt>
mov    (%rsp),%r12
lea    0x2e2a(%rip),%rax
mov    %rax,(%r15)
mov    %r12,(%r14)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.passwd = type { i32 }
@getuidtty.uidbuf = internal global [32 x i8] zeroinitializer
@.str = [7 x i8] c"uid %d\00"
@.str.1 = [5 x i8] c"root\00"
@.str.2 = [6 x i8] c"/dev/\00"
@.str.3 = [6 x i8] c"(%s) \00"
define void @getuidtty(i8** %0, i8** %1) {
%3 = alloca i8**
%4 = alloca i8**
%5 = alloca %struct.passwd*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64
store i8** %0, i8*** %3
store i8** %1, i8*** %4
store i8* null, i8** %8
%10 = call i64 (...) @getuid()
store i64 %10, i64* %6
%11 = load i64, i64* %6
%12 = call %struct.passwd* @getpwuid(i64 %11)
store %struct.passwd* %12, %struct.passwd** %5
%13 = icmp ne %struct.passwd* %12, null
br i1 %13, label %14, label %19
14: ; preds = %2
store i8 0, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @getuidtty.uidbuf, i64 0, i64 0)
%15 = load %struct.passwd*, %struct.passwd** %5
%16 = getelementptr inbounds %struct.passwd, %struct.passwd* %15, i32 0, i32 0
%17 = load i32, i32* %16
%18 = call i32 @strncat(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @getuidtty.uidbuf, i64 0, i64 0), i32 %17, i32 31)
br label %29
19: ; preds = %2
%20 = load i64, i64* %6
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %19
%23 = load i64, i64* %6
%24 = trunc i64 %23 to i32
%25 = call i32 (i8*, i8*, ...) @sprintf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @getuidtty.uidbuf, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %24)
br label %28
26: ; preds = %19
%27 = call i32 (i8*, i8*, ...) @sprintf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @getuidtty.uidbuf, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %28
28: ; preds = %26, %22
br label %29
29: ; preds = %28, %14
%30 = call i8* @ttyname(i32 0)
store i8* %30, i8** %7
%31 = icmp ne i8* %30, null
br i1 %31, label %32, label %54
32: ; preds = %29
%33 = call i64 @strlen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
store i64 %33, i64* %9
%34 = load i8*, i8** %7
%35 = load i64, i64* %9
%36 = call i64 @strncmp(i8* %34, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i64 %35)
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %51
38: ; preds = %32
%39 = load i64, i64* %9
%40 = load i8*, i8** %7
%41 = getelementptr inbounds i8, i8* %40, i64 %39
store i8* %41, i8** %7
%42 = load i8*, i8** %7
%43 = getelementptr inbounds i8, i8* %42, i64 0
%44 = load i8, i8* %43
%45 = sext i8 %44 to i32
%46 = icmp eq i32 %45, 47
br i1 %46, label %47, label %50
47: ; preds = %38
%48 = load i8*, i8** %7
%49 = getelementptr inbounds i8, i8* %48, i32 1
store i8* %49, i8** %7
br label %50
50: ; preds = %47, %38
br label %51
51: ; preds = %50, %32
%52 = load i8*, i8** %7
%53 = call i32 @xasprintf(i8** %8, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %52)
br label %54
54: ; preds = %51, %29
%55 = load i8**, i8*** %3
store i8* getelementptr inbounds ([32 x i8], [32 x i8]* @getuidtty.uidbuf, i64 0, i64 0), i8** %55
%56 = load i8*, i8** %8
%57 = load i8**, i8*** %4
store i8* %56, i8** %57
ret void
}

LLVM IR (-O2):
%struct.passwd = type { i32 }
@getuidtty.uidbuf = internal global [32 x i8] zeroinitializer
@.str = [7 x i8] c"uid %d\00"
@.str.1 = [5 x i8] c"root\00"
@.str.2 = [6 x i8] c"/dev/\00"
@.str.3 = [6 x i8] c"(%s) \00"
define void @getuidtty(i8** nocapture writeonly %0, i8** nocapture writeonly %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
store i8* null, i8** %3
%5 = tail call i64 (...) @getuid()
%6 = tail call %struct.passwd* @getpwuid(i64 %5)
%7 = icmp eq %struct.passwd* %6, null
br i1 %7, label %12, label %8
8: ; preds = %2
store i8 0, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @getuidtty.uidbuf, i64 0, i64 0)
%9 = getelementptr inbounds %struct.passwd, %struct.passwd* %6, i64 0, i32 0
%10 = load i32, i32* %9
%11 = tail call i32 @strncat(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @getuidtty.uidbuf, i64 0, i64 0), i32 %10, i32 31)
br label %18
12: ; preds = %2
%13 = icmp eq i64 %5, 0
br i1 %13, label %17, label %14
14: ; preds = %12
%15 = trunc i64 %5 to i32
%16 = tail call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @getuidtty.uidbuf, i64 0, i64 0), i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %15)
br label %18
17: ; preds = %12
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 16 dereferenceable(5) getelementptr inbounds ([32 x i8], [32 x i8]* @getuidtty.uidbuf, i64 0, i64 0), i8* align 1 dereferenceable(5) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i64 5, i1 false)
br label %18
18: ; preds = %14, %17, %8
%19 = tail call i8* @ttyname(i32 0)
%20 = icmp eq i8* %19, null
br i1 %20, label %34, label %21
21: ; preds = %18
%22 = tail call i64 @strncmp(i8* %19, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i64 5)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %30
24: ; preds = %21
%25 = getelementptr inbounds i8, i8* %19, i64 5
%26 = load i8, i8* %25
%27 = icmp eq i8 %26, 47
%28 = getelementptr inbounds i8, i8* %19, i64 6
%29 = select i1 %27, i8* %28, i8* %25
br label %30
30: ; preds = %24, %21
%31 = phi i8* [ %19, %21 ], [ %29, %24 ]
%32 = call i32 @xasprintf(i8** %3, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %31)
%33 = load i8*, i8** %3
br label %34
34: ; preds = %30, %18
%35 = phi i8* [ %33, %30 ], [ null, %18 ]
store i8* getelementptr inbounds ([32 x i8], [32 x i8]* @getuidtty.uidbuf, i64 0, i64 0), i8** %0
store i8* %35, i8** %1
ret void
}