#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_gecos;
};

/* Variables and functions */
char *getenv(char *);
char *getlogin();
struct passwd *getpwnam(char *);
char *strchr(char *, char);
void get_user(char **logname) {
  char *user_name, *separator;
  struct passwd *user_info;
  *logname = getlogin();
  if (!*logname)
    *logname = getenv("LOGNAME");
  if (!*logname)
    *logname = getenv("USER");
  if (*logname) {
    user_info = getpwnam(*logname);
    user_name = user_info->pw_gecos;
    if ((separator = strchr(user_name, ',')) != 0)
      *separator = '\0';
    if (user_name && *user_name != '\0')
      *logname = user_name;
  }
}


x86 assembly (clang, -O0):
<get_user>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x0,%al
call   1060 <getlogin@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1181 <get_user+0x41>
lea    0xe8e(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11a5 <get_user+0x65>
lea    0xe72(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
cmpq   $0x0,(%rax)
je     121c <get_user+0xdc>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1050 <getpwnam@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x2c,%esi
call   1040 <strchr@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     11f1 <get_user+0xb1>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
cmpq   $0x0,-0x10(%rbp)
je     1217 <get_user+0xd7>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1217 <get_user+0xd7>
mov    -0x10(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
jmp    121c <get_user+0xdc>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_user>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
xor    %eax,%eax
call   1060 <getlogin@plt>
mov    %rax,(%r14)
test   %rax,%rax
jne    117e <get_user+0x3e>
lea    0xea3(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,(%r14)
test   %rax,%rax
jne    117e <get_user+0x3e>
lea    0xe97(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,(%r14)
test   %rax,%rax
je     11ab <get_user+0x6b>
mov    %rax,%rdi
call   1050 <getpwnam@plt>
mov    (%rax),%rbx
mov    %rbx,%rdi
mov    $0x2c,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     119e <get_user+0x5e>
movb   $0x0,(%rax)
test   %rbx,%rbx
je     11ab <get_user+0x6b>
cmpb   $0x0,(%rbx)
je     11ab <get_user+0x6b>
mov    %rbx,(%r14)
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.passwd = type { i8* }
@.str = [8 x i8] c"LOGNAME\00"
@.str.1 = [5 x i8] c"USER\00"
define void @get_user(i8** %0) {
%2 = alloca i8**
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca %struct.passwd*
store i8** %0, i8*** %2
%6 = call i8* (...) @getlogin()
%7 = load i8**, i8*** %2
store i8* %6, i8** %7
%8 = load i8**, i8*** %2
%9 = load i8*, i8** %8
%10 = icmp ne i8* %9, null
br i1 %10, label %14, label %11
11: ; preds = %1
%12 = call i8* @getenv(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%13 = load i8**, i8*** %2
store i8* %12, i8** %13
br label %14
14: ; preds = %11, %1
%15 = load i8**, i8*** %2
%16 = load i8*, i8** %15
%17 = icmp ne i8* %16, null
br i1 %17, label %21, label %18
18: ; preds = %14
%19 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%20 = load i8**, i8*** %2
store i8* %19, i8** %20
br label %21
21: ; preds = %18, %14
%22 = load i8**, i8*** %2
%23 = load i8*, i8** %22
%24 = icmp ne i8* %23, null
br i1 %24, label %25, label %49
25: ; preds = %21
%26 = load i8**, i8*** %2
%27 = load i8*, i8** %26
%28 = call %struct.passwd* @getpwnam(i8* %27)
store %struct.passwd* %28, %struct.passwd** %5
%29 = load %struct.passwd*, %struct.passwd** %5
%30 = getelementptr inbounds %struct.passwd, %struct.passwd* %29, i32 0, i32 0
%31 = load i8*, i8** %30
store i8* %31, i8** %3
%32 = load i8*, i8** %3
%33 = call i8* @strchr(i8* %32, i8 signext 44)
store i8* %33, i8** %4
%34 = icmp ne i8* %33, null
br i1 %34, label %35, label %37
35: ; preds = %25
%36 = load i8*, i8** %4
store i8 0, i8* %36
br label %37
37: ; preds = %35, %25
%38 = load i8*, i8** %3
%39 = icmp ne i8* %38, null
br i1 %39, label %40, label %48
40: ; preds = %37
%41 = load i8*, i8** %3
%42 = load i8, i8* %41
%43 = sext i8 %42 to i32
%44 = icmp ne i32 %43, 0
br i1 %44, label %45, label %48
45: ; preds = %40
%46 = load i8*, i8** %3
%47 = load i8**, i8*** %2
store i8* %46, i8** %47
br label %48
48: ; preds = %45, %40, %37
br label %49
49: ; preds = %48, %21
ret void
}

LLVM IR (-O2):
%struct.passwd = type { i8* }
@.str = [8 x i8] c"LOGNAME\00"
@.str.1 = [5 x i8] c"USER\00"
define void @get_user(i8** nocapture %0) {
%2 = tail call i8* (...) @getlogin()
store i8* %2, i8** %0
%3 = icmp eq i8* %2, null
br i1 %3, label %4, label %10
4: ; preds = %1
%5 = tail call i8* @getenv(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
store i8* %5, i8** %0
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %10
7: ; preds = %4
%8 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i8* %8, i8** %0
%9 = icmp eq i8* %8, null
br i1 %9, label %24, label %10
10: ; preds = %1, %4, %7
%11 = phi i8* [ %8, %7 ], [ %5, %4 ], [ %2, %1 ]
%12 = tail call %struct.passwd* @getpwnam(i8* %11)
%13 = getelementptr inbounds %struct.passwd, %struct.passwd* %12, i64 0, i32 0
%14 = load i8*, i8** %13
%15 = tail call i8* @strchr(i8* dereferenceable(1) %14, i8 signext 44)
%16 = icmp eq i8* %15, null
br i1 %16, label %18, label %17
17: ; preds = %10
store i8 0, i8* %15
br label %18
18: ; preds = %17, %10
%19 = icmp eq i8* %14, null
br i1 %19, label %24, label %20
20: ; preds = %18
%21 = load i8, i8* %14
%22 = icmp eq i8 %21, 0
br i1 %22, label %24, label %23
23: ; preds = %20
store i8* %14, i8** %0
br label %24
24: ; preds = %18, %20, %23, %7
ret void
}