#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 utmpx {
  char *ut_line;
  char *ut_user;
};

/* Variables and functions */
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ _PATH_UTMP;
int __UT_LINESIZE;
int /*<<< orphan*/ endutxent();
scalar_t__ errno;
struct utmpx *getutxline(struct utmpx *);
int /*<<< orphan*/ setutxent();
char *strchr(char *, char);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *ttyname(int /*<<< orphan*/);
int /*<<< orphan*/ utmpname(int /*<<< orphan*/);
char *_getlogin() {
  char *tty;
  if ((tty = ttyname(STDIN_FILENO)) == NULL)
    return NULL;
  static char login[(256)];
  utmpname((_PATH_UTMP));
  errno = 0;
  setutxent();
  if (errno != 0)
    return NULL;
  struct utmpx *entry, criteria;
  char *line;
  if (tty[0] == '/')
    ++tty;
  if ((line = strchr(tty, '/')) == NULL)
    line = tty;
  else
    ++line;
  strncpy(criteria.ut_line, line, __UT_LINESIZE);
  if ((entry = getutxline(&criteria)) == NULL)
    return NULL;
  strncpy(login, entry->ut_user, (256));
  errno = 0;
  endutxent();
  if (errno != 0)
    return NULL;
  return login;
}


x86 assembly (clang, -O0):
<_getlogin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
call   1090 <ttyname@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    11a1 <_getlogin+0x31>
movq   $0x0,-0x8(%rbp)
jmp    12c4 <_getlogin+0x154>
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
call   1060 <utmpname@plt>
mov    0x2e1a(%rip),%rax
movq   $0x0,(%rax)
mov    $0x0,%al
call   1080 <setutxent@plt>
mov    0x2e05(%rip),%rax
cmpq   $0x0,(%rax)
je     11e2 <_getlogin+0x72>
movq   $0x0,-0x8(%rbp)
jmp    12c4 <_getlogin+0x154>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    11fe <_getlogin+0x8e>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1070 <strchr@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    1227 <_getlogin+0xb7>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    1233 <_getlogin+0xc3>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    0x2da6(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
lea    -0x28(%rbp),%rdi
call   1040 <getutxline@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    126d <_getlogin+0xfd>
movq   $0x0,-0x8(%rbp)
jmp    12c4 <_getlogin+0x154>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0x2e04(%rip),%rdi
mov    $0x100,%edx
call   1030 <strncpy@plt>
mov    0x2d43(%rip),%rax
movq   $0x0,(%rax)
mov    $0x0,%al
call   1050 <endutxent@plt>
mov    0x2d2e(%rip),%rax
cmpq   $0x0,(%rax)
je     12b9 <_getlogin+0x149>
movq   $0x0,-0x8(%rbp)
jmp    12c4 <_getlogin+0x154>
lea    0x2dc0(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_getlogin>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e48(%rip),%rax
mov    (%rax),%edi
call   1090 <ttyname@plt>
test   %rax,%rax
je     1232 <_getlogin+0xc2>
mov    %rax,%rbx
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
call   1060 <utmpname@plt>
mov    0x2e28(%rip),%r15
movq   $0x0,(%r15)
xor    %r14d,%r14d
xor    %eax,%eax
call   1080 <setutxent@plt>
cmpq   $0x0,(%r15)
jne    1235 <_getlogin+0xc5>
xor    %r14d,%r14d
cmpb   $0x2f,(%rbx)
sete   %r14b
add    %rbx,%r14
mov    %r14,%rdi
mov    $0x2f,%esi
call   1070 <strchr@plt>
lea    0x1(%rax),%rsi
test   %rax,%rax
cmove  %r14,%rsi
mov    0x2dfd(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    %rsp,%rdi
call   1040 <getutxline@plt>
test   %rax,%rax
je     1232 <_getlogin+0xc2>
mov    0x8(%rax),%rsi
lea    0x2e76(%rip),%rbx
mov    %rbx,%rdi
mov    $0x100,%edx
call   1030 <strncpy@plt>
movq   $0x0,(%r15)
xor    %r14d,%r14d
xor    %eax,%eax
call   1050 <endutxent@plt>
cmpq   $0x0,(%r15)
cmove  %rbx,%r14
jmp    1235 <_getlogin+0xc5>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.utmpx = type { i8*, i8* }
@STDIN_FILENO = global i32 0
@_getlogin.login = internal global [256 x i8] zeroinitializer
@_PATH_UTMP = global i32 0
@errno = global i64 0
@__UT_LINESIZE = global i32 0
define i8* @_getlogin() {
%1 = alloca i8*
%2 = alloca i8*
%3 = alloca %struct.utmpx*
%4 = alloca %struct.utmpx
%5 = alloca i8*
%6 = load i32, i32* @STDIN_FILENO
%7 = call i8* @ttyname(i32 %6)
store i8* %7, i8** %2
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %10
9: ; preds = %0
store i8* null, i8** %1
br label %54
10: ; preds = %0
%11 = load i32, i32* @_PATH_UTMP
%12 = call i32 @utmpname(i32 %11)
store i64 0, i64* @errno
%13 = call i32 (...) @setutxent()
%14 = load i64, i64* @errno
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %10
store i8* null, i8** %1
br label %54
17: ; preds = %10
%18 = load i8*, i8** %2
%19 = getelementptr inbounds i8, i8* %18, i64 0
%20 = load i8, i8* %19
%21 = sext i8 %20 to i32
%22 = icmp eq i32 %21, 47
br i1 %22, label %23, label %26
23: ; preds = %17
%24 = load i8*, i8** %2
%25 = getelementptr inbounds i8, i8* %24, i32 1
store i8* %25, i8** %2
br label %26
26: ; preds = %23, %17
%27 = load i8*, i8** %2
%28 = call i8* @strchr(i8* %27, i8 signext 47)
store i8* %28, i8** %5
%29 = icmp eq i8* %28, null
br i1 %29, label %30, label %32
30: ; preds = %26
%31 = load i8*, i8** %2
store i8* %31, i8** %5
br label %35
32: ; preds = %26
%33 = load i8*, i8** %5
%34 = getelementptr inbounds i8, i8* %33, i32 1
store i8* %34, i8** %5
br label %35
35: ; preds = %32, %30
%36 = getelementptr inbounds %struct.utmpx, %struct.utmpx* %4, i32 0, i32 0
%37 = load i8*, i8** %36
%38 = load i8*, i8** %5
%39 = load i32, i32* @__UT_LINESIZE
%40 = call i32 @strncpy(i8* %37, i8* %38, i32 %39)
%41 = call %struct.utmpx* @getutxline(%struct.utmpx* %4)
store %struct.utmpx* %41, %struct.utmpx** %3
%42 = icmp eq %struct.utmpx* %41, null
br i1 %42, label %43, label %44
43: ; preds = %35
store i8* null, i8** %1
br label %54
44: ; preds = %35
%45 = load %struct.utmpx*, %struct.utmpx** %3
%46 = getelementptr inbounds %struct.utmpx, %struct.utmpx* %45, i32 0, i32 1
%47 = load i8*, i8** %46
%48 = call i32 @strncpy(i8* getelementptr inbounds ([256 x i8], [256 x i8]* @_getlogin.login, i64 0, i64 0), i8* %47, i32 256)
store i64 0, i64* @errno
%49 = call i32 (...) @endutxent()
%50 = load i64, i64* @errno
%51 = icmp ne i64 %50, 0
br i1 %51, label %52, label %53
52: ; preds = %44
store i8* null, i8** %1
br label %54
53: ; preds = %44
store i8* getelementptr inbounds ([256 x i8], [256 x i8]* @_getlogin.login, i64 0, i64 0), i8** %1
br label %54
54: ; preds = %53, %52, %43, %16, %9
%55 = load i8*, i8** %1
ret i8* %55
}

LLVM IR (-O2):
%struct.utmpx = type { i8*, i8* }
@STDIN_FILENO = global i32 0
@_getlogin.login = internal global [256 x i8] zeroinitializer
@_PATH_UTMP = global i32 0
@errno = global i64 0
@__UT_LINESIZE = global i32 0
define i8* @_getlogin() {
%1 = alloca %struct.utmpx
%2 = load i32, i32* @STDIN_FILENO
%3 = tail call i8* @ttyname(i32 %2)
%4 = icmp eq i8* %3, null
br i1 %4, label %35, label %5
5: ; preds = %0
%6 = load i32, i32* @_PATH_UTMP
%7 = tail call i32 @utmpname(i32 %6)
store i64 0, i64* @errno
%8 = tail call i32 (...) @setutxent()
%9 = load i64, i64* @errno
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %35
11: ; preds = %5
%12 = bitcast %struct.utmpx* %1 to i8*
%13 = load i8, i8* %3
%14 = icmp eq i8 %13, 47
%15 = zext i1 %14 to i64
%16 = getelementptr i8, i8* %3, i64 %15
%17 = tail call i8* @strchr(i8* %16, i8 signext 47)
%18 = icmp eq i8* %17, null
%19 = getelementptr inbounds i8, i8* %17, i64 1
%20 = select i1 %18, i8* %16, i8* %19
%21 = load i32, i32* @__UT_LINESIZE
%22 = tail call i32 @strncpy(i8* undef, i8* %20, i32 %21)
%23 = call %struct.utmpx* @getutxline(%struct.utmpx* %1)
%24 = icmp eq %struct.utmpx* %23, null
br i1 %24, label %33, label %25
25: ; preds = %11
%26 = getelementptr inbounds %struct.utmpx, %struct.utmpx* %23, i64 0, i32 1
%27 = load i8*, i8** %26
%28 = call i32 @strncpy(i8* getelementptr inbounds ([256 x i8], [256 x i8]* @_getlogin.login, i64 0, i64 0), i8* %27, i32 256)
store i64 0, i64* @errno
%29 = call i32 (...) @endutxent()
%30 = load i64, i64* @errno
%31 = icmp eq i64 %30, 0
%32 = select i1 %31, i8* getelementptr inbounds ([256 x i8], [256 x i8]* @_getlogin.login, i64 0, i64 0), i8* null
br label %33
33: ; preds = %25, %11
%34 = phi i8* [ null, %11 ], [ %32, %25 ]
br label %35
35: ; preds = %5, %0, %33
%36 = phi i8* [ %34, %33 ], [ null, %0 ], [ null, %5 ]
ret i8* %36
}