#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 passwd {
  char *pw_name;
};

/* Variables and functions */
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
char *getenv(char *);
char *getlogin();
struct passwd *getpwuid(int /*<<< orphan*/);
int /*<<< orphan*/ getuid();
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
char *getname() {
  char *logname;
  if ((logname = getenv("LOGNAME")) == (char *)NULL)
    if ((logname = getlogin()) == (char *)NULL) {
      struct passwd *pw;
      if ((pw = getpwuid(getuid())) == 0) {
        fprintf(stderr, "ttc: getname(): %s\n", strerror(errno));
        exit(1);
      }
      logname = pw->pw_name;
    }
  return logname;
}


x86 assembly (clang, -O0):
<getname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xe81(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x8(%rbp)
cmp    $0x0,%rax
jne    1209 <getname+0x99>
mov    $0x0,%al
call   1070 <getlogin@plt>
mov    %rax,-0x8(%rbp)
cmp    $0x0,%rax
jne    1204 <getname+0x94>
mov    $0x0,%al
call   1050 <getuid@plt>
mov    %eax,%edi
call   1040 <getpwuid@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    11f9 <getname+0x89>
mov    0x2e16(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x14(%rbp),%edi
mov    %rax,%rdx
lea    0xe1e(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    1209 <getname+0x99>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getname>:
push   %rbx
lea    0xe88(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1184 <getname+0x14>
pop    %rbx
ret
xor    %eax,%eax
call   1070 <getlogin@plt>
test   %rax,%rax
jne    1182 <getname+0x12>
xor    %eax,%eax
call   1050 <getuid@plt>
mov    %eax,%edi
call   1040 <getpwuid@plt>
test   %rax,%rax
je     11a8 <getname+0x38>
mov    (%rax),%rax
pop    %rbx
ret
mov    0x2e31(%rip),%rax
mov    (%rax),%ebx
mov    0x2e20(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe42(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.passwd = type { i8* }
@.str = [8 x i8] c"LOGNAME\00"
@stderr = global i32 0
@.str.1 = [20 x i8] c"ttc: getname(): %s\0A\00"
@errno = global i32 0
define i8* @getname() {
%1 = alloca i8*
%2 = alloca %struct.passwd*
%3 = call i8* @getenv(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
store i8* %3, i8** %1
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %23
5: ; preds = %0
%6 = call i8* (...) @getlogin()
store i8* %6, i8** %1
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %22
8: ; preds = %5
%9 = call i32 (...) @getuid()
%10 = call %struct.passwd* @getpwuid(i32 %9)
store %struct.passwd* %10, %struct.passwd** %2
%11 = icmp eq %struct.passwd* %10, null
br i1 %11, label %12, label %18
12: ; preds = %8
%13 = load i32, i32* @stderr
%14 = load i32, i32* @errno
%15 = call i8* @strerror(i32 %14)
%16 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %15)
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %8
%19 = load %struct.passwd*, %struct.passwd** %2
%20 = getelementptr inbounds %struct.passwd, %struct.passwd* %19, i32 0, i32 0
%21 = load i8*, i8** %20
store i8* %21, i8** %1
br label %22
22: ; preds = %18, %5
br label %23
23: ; preds = %22, %0
%24 = load i8*, i8** %1
ret i8* %24
}

LLVM IR (-O2):
%struct.passwd = type { i8* }
@.str = [8 x i8] c"LOGNAME\00"
@stderr = global i32 0
@.str.1 = [20 x i8] c"ttc: getname(): %s\0A\00"
@errno = global i32 0
define i8* @getname() {
%1 = tail call i8* @getenv(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%2 = icmp eq i8* %1, null
br i1 %2, label %3, label %19
3: ; preds = %0
%4 = tail call i8* (...) @getlogin()
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %19
6: ; preds = %3
%7 = tail call i32 (...) @getuid()
%8 = tail call %struct.passwd* @getpwuid(i32 %7)
%9 = icmp eq %struct.passwd* %8, null
br i1 %9, label %10, label %16
10: ; preds = %6
%11 = load i32, i32* @stderr
%12 = load i32, i32* @errno
%13 = tail call i8* @strerror(i32 %12)
%14 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %13)
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %6
%17 = getelementptr inbounds %struct.passwd, %struct.passwd* %8, i64 0, i32 0
%18 = load i8*, i8** %17
br label %19
19: ; preds = %3, %16, %0
%20 = phi i8* [ %18, %16 ], [ %4, %3 ], [ %1, %0 ]
ret i8* %20
}