#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 */

/* Variables and functions */
int ENOENT;
int ERANGE;
int errno;
char *getlogin();
int /*<<< orphan*/ memcpy(char *, char *, size_t);
size_t strlen(char *);
int getlogin_r(char *name, size_t size) {
  char *n;
  size_t nlen;
  errno = 0;
  n = getlogin();
  if (!errno)
    errno = ENOENT;
  if (!n)
    return errno;
  nlen = strlen(n);
  if (size <= nlen)
    return ERANGE;
  memcpy(name, n, nlen + 1);
  return 0;
}


x86 assembly (clang, -O0):
<getlogin_r>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e91(%rip),%rax
movl   $0x0,(%rax)
mov    $0x0,%al
call   1050 <getlogin@plt>
mov    %rax,-0x20(%rbp)
mov    0x2e79(%rip),%rax
cmpl   $0x0,(%rax)
jne    117a <getlogin_r+0x4a>
mov    0x2e59(%rip),%rax
mov    (%rax),%ecx
mov    0x2e60(%rip),%rax
mov    %ecx,(%rax)
cmpq   $0x0,-0x20(%rbp)
jne    1196 <getlogin_r+0x66>
mov    0x2e4c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11de <getlogin_r+0xae>
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x28(%rbp),%rax
ja     11c2 <getlogin_r+0x92>
mov    0x2e30(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11de <getlogin_r+0xae>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
add    $0x1,%rdx
call   1040 <memcpy@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getlogin_r>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r14
mov    0x2e93(%rip),%r12
movl   $0x0,(%r12)
xor    %eax,%eax
call   1050 <getlogin@plt>
mov    %rax,%rbx
mov    (%r12),%eax
test   %eax,%eax
jne    116c <getlogin_r+0x3c>
mov    0x2e62(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%r12)
test   %rbx,%rbx
je     119d <getlogin_r+0x6d>
mov    %rbx,%rdi
call   1030 <strlen@plt>
cmp    %r15,%rax
jae    1194 <getlogin_r+0x64>
add    $0x1,%rax
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1040 <memcpy@plt>
xor    %eax,%eax
jmp    119d <getlogin_r+0x6d>
mov    0x2e4d(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@errno = global i32 0
@ENOENT = global i32 0
@ERANGE = global i32 0
define i32 @getlogin_r(i8* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i64
store i8* %0, i8** %4
store i64 %1, i64* %5
store i32 0, i32* @errno
%8 = call i8* (...) @getlogin()
store i8* %8, i8** %6
%9 = load i32, i32* @errno
%10 = icmp ne i32 %9, 0
br i1 %10, label %13, label %11
11: ; preds = %2
%12 = load i32, i32* @ENOENT
store i32 %12, i32* @errno
br label %13
13: ; preds = %11, %2
%14 = load i8*, i8** %6
%15 = icmp ne i8* %14, null
br i1 %15, label %18, label %16
16: ; preds = %13
%17 = load i32, i32* @errno
store i32 %17, i32* %3
br label %32
18: ; preds = %13
%19 = load i8*, i8** %6
%20 = call i64 @strlen(i8* %19)
store i64 %20, i64* %7
%21 = load i64, i64* %5
%22 = load i64, i64* %7
%23 = icmp ule i64 %21, %22
br i1 %23, label %24, label %26
24: ; preds = %18
%25 = load i32, i32* @ERANGE
store i32 %25, i32* %3
br label %32
26: ; preds = %18
%27 = load i8*, i8** %4
%28 = load i8*, i8** %6
%29 = load i64, i64* %7
%30 = add i64 %29, 1
%31 = call i32 @memcpy(i8* %27, i8* %28, i64 %30)
store i32 0, i32* %3
br label %32
32: ; preds = %26, %24, %16
%33 = load i32, i32* %3
ret i32 %33
}

LLVM IR (-O2):
@errno = global i32 0
@ENOENT = global i32 0
@ERANGE = global i32 0
define i32 @getlogin_r(i8* %0, i64 %1) {
store i32 0, i32* @errno
%3 = tail call i8* (...) @getlogin()
%4 = load i32, i32* @errno
%5 = icmp eq i32 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = load i32, i32* @ENOENT
store i32 %7, i32* @errno
br label %8
8: ; preds = %6, %2
%9 = phi i32 [ %7, %6 ], [ %4, %2 ]
%10 = icmp eq i8* %3, null
br i1 %10, label %19, label %11
11: ; preds = %8
%12 = tail call i64 @strlen(i8* %3)
%13 = icmp ult i64 %12, %1
br i1 %13, label %16, label %14
14: ; preds = %11
%15 = load i32, i32* @ERANGE
br label %19
16: ; preds = %11
%17 = add nuw i64 %12, 1
%18 = tail call i32 @memcpy(i8* %0, i8* %3, i64 %17)
br label %19
19: ; preds = %8, %16, %14
%20 = phi i32 [ %15, %14 ], [ 0, %16 ], [ %9, %8 ]
ret i32 %20
}