#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 ENXIO;
int ERANGE;
char *getlogin();
int /*<<< orphan*/ strcpy(char *, char *);
size_t strlen(char *);
int getlogin_r(char *name, size_t size) {
  char *logname = getlogin();
  if (!logname)
    return ENXIO;
  if (strlen(logname) >= size)
    return ERANGE;
  strcpy(name, logname);
  return 0;
}


x86 assembly (clang, -O0):
<getlogin_r>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    $0x0,%al
call   1050 <getlogin@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1167 <getlogin_r+0x37>
mov    0x2e7b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    119f <getlogin_r+0x6f>
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
cmp    -0x18(%rbp),%rax
jb     118b <getlogin_r+0x5b>
mov    0x2e67(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    119f <getlogin_r+0x6f>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1030 <strcpy@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getlogin_r>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r15
mov    %rdi,%r14
xor    %eax,%eax
call   1050 <getlogin@plt>
test   %rax,%rax
je     1166 <getlogin_r+0x36>
mov    %rax,%rbx
mov    %rax,%rdi
call   1040 <strlen@plt>
cmp    %r15,%rax
jae    116f <getlogin_r+0x3f>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
xor    %eax,%eax
jmp    1178 <getlogin_r+0x48>
mov    0x2e6b(%rip),%rax
jmp    1176 <getlogin_r+0x46>
mov    0x2e72(%rip),%rax
mov    (%rax),%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@ENXIO = 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*
store i8* %0, i8** %4
store i64 %1, i64* %5
%7 = call i8* (...) @getlogin()
store i8* %7, i8** %6
%8 = load i8*, i8** %6
%9 = icmp ne i8* %8, null
br i1 %9, label %12, label %10
10: ; preds = %2
%11 = load i32, i32* @ENXIO
store i32 %11, i32* %3
br label %23
12: ; preds = %2
%13 = load i8*, i8** %6
%14 = call i64 @strlen(i8* %13)
%15 = load i64, i64* %5
%16 = icmp uge i64 %14, %15
br i1 %16, label %17, label %19
17: ; preds = %12
%18 = load i32, i32* @ERANGE
store i32 %18, i32* %3
br label %23
19: ; preds = %12
%20 = load i8*, i8** %4
%21 = load i8*, i8** %6
%22 = call i32 @strcpy(i8* %20, i8* %21)
store i32 0, i32* %3
br label %23
23: ; preds = %19, %17, %10
%24 = load i32, i32* %3
ret i32 %24
}

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