#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 *);
struct passwd *getpwnam(char *);
int /*<<< orphan*/ *strchr(char *, char);
size_t strcspn(char *, char *);
scalar_t__ strlen(char *);
char *getusername() {
  struct passwd *pwd;
  char *loginname = getenv("USER");
  char *retval = NULL;
  if (loginname != NULL) {
    pwd = getpwnam(loginname);
    if (pwd != NULL) {
      retval = (strlen(pwd->pw_gecos) > 0) ? pwd->pw_gecos : loginname;
      if (strchr(retval, ',') != NULL) {
        retval[strcspn(retval, ",")] = '\0';
      }
    } else {
      retval = loginname;
    }
  }
  return retval;
}


x86 assembly (clang, -O0):
<getusername>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    0xea1(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1216 <getusername+0xc6>
mov    -0x10(%rbp),%rdi
call   1070 <getpwnam@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     1209 <getusername+0xb9>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1040 <strlen@plt>
cmp    $0x0,%rax
jle    11b9 <getusername+0x69>
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
jmp    11c1 <getusername+0x71>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x2c,%esi
call   1050 <strchr@plt>
cmp    $0x0,%rax
je     1204 <getusername+0xb4>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe11(%rip),%rsi
call   1060 <strcspn@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax,%rcx,1)
jmp    1211 <getusername+0xc1>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
jmp    1216 <getusername+0xc6>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getusername>:
push   %r14
push   %rbx
push   %rax
lea    0xea5(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11ae <getusername+0x5e>
mov    %rax,%rbx
mov    %rax,%rdi
call   1070 <getpwnam@plt>
test   %rax,%rax
je     11b0 <getusername+0x60>
mov    (%rax),%r14
mov    %r14,%rdi
call   1040 <strlen@plt>
test   %rax,%rax
cmovg  %r14,%rbx
mov    %rbx,%rdi
mov    $0x2c,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     11b0 <getusername+0x60>
lea    0xe65(%rip),%rsi
mov    %rbx,%rdi
call   1060 <strcspn@plt>
movb   $0x0,(%rbx,%rax,1)
jmp    11b0 <getusername+0x60>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

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