#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_passwd;
  char *pw_shell;
  int /*<<< orphan*/ pw_uid;
  int /*<<< orphan*/ pw_gid;
};

/* Variables and functions */
char *_do_ask_password();
char *_do_ask_username();
int _login_error(char *, int);
scalar_t__ _login_nologin();
int /*<<< orphan*/ execl(char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
struct passwd *getpwnam(char const *);
scalar_t__ getuid();
scalar_t__ setgid(int /*<<< orphan*/);
scalar_t__ setuid(int /*<<< orphan*/);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char const *, char *);
int _login_do(char const *user) {
  struct passwd *pw;
  char const *password = NULL;
  if (user == NULL && (user = _do_ask_username()) == NULL)
    return 1;
  pw = getpwnam(user);
  if (getuid() != 0 && (password = _do_ask_password()) == NULL)
    return 1;
  if (pw == NULL ||
      (password != NULL && (pw->pw_passwd == NULL || pw->pw_passwd[0] == '*' ||
                            strcmp(password, pw->pw_passwd) != 0))) {
    fputs("Login failed\n", stderr);
    sleep(3);
    return 0;
  }
  if (setgid(pw->pw_gid) != 0)
    return -_login_error("setgid", 1);
  if (setuid(pw->pw_uid) != 0)
    return -_login_error("setuid", 1);
  if (_login_nologin() != 0)
    return -1;
  execl(pw->pw_shell, pw->pw_shell, NULL);
  _login_error(pw->pw_shell, 1);
  exit(2);
  return 0;
}


x86 assembly (clang, -O0):
<_login_do>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1210 <_login_do+0x40>
mov    $0x0,%al
call   1080 <_do_ask_username@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    1210 <_login_do+0x40>
movl   $0x1,-0x4(%rbp)
jmp    1387 <_login_do+0x1b7>
mov    -0x10(%rbp),%rdi
call   1070 <getpwnam@plt>
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   1030 <getuid@plt>
cmp    $0x0,%rax
je     124f <_login_do+0x7f>
mov    $0x0,%al
call   1050 <_do_ask_password@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    124f <_login_do+0x7f>
movl   $0x1,-0x4(%rbp)
jmp    1387 <_login_do+0x1b7>
cmpq   $0x0,-0x18(%rbp)
je     12a0 <_login_do+0xd0>
cmpq   $0x0,-0x20(%rbp)
je     12cb <_login_do+0xfb>
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
je     12a0 <_login_do+0xd0>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2a,%eax
je     12a0 <_login_do+0xd0>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
je     12cb <_login_do+0xfb>
mov    0x2d39(%rip),%rax
mov    (%rax),%esi
lea    0xd50(%rip),%rdi
call   1040 <fputs@plt>
mov    $0x3,%edi
call   10f0 <sleep@plt>
movl   $0x0,-0x4(%rbp)
jmp    1387 <_login_do+0x1b7>
mov    -0x18(%rbp),%rax
mov    0x14(%rax),%edi
call   10a0 <setgid@plt>
cmp    $0x0,%rax
je     1300 <_login_do+0x130>
lea    0xd26(%rip),%rdi
mov    $0x1,%esi
call   10c0 <_login_error@plt>
mov    %eax,%ecx
xor    %eax,%eax
sub    %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    1387 <_login_do+0x1b7>
mov    -0x18(%rbp),%rax
mov    0x10(%rax),%edi
call   10d0 <setuid@plt>
cmp    $0x0,%rax
je     1335 <_login_do+0x165>
lea    0xcf8(%rip),%rdi
mov    $0x1,%esi
call   10c0 <_login_error@plt>
mov    %eax,%ecx
xor    %eax,%eax
sub    %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    1387 <_login_do+0x1b7>
mov    $0x0,%al
call   1090 <_login_nologin@plt>
cmp    $0x0,%rax
je     1352 <_login_do+0x182>
movl   $0xffffffff,-0x4(%rbp)
jmp    1387 <_login_do+0x1b7>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   10e0 <execl@plt>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rdi
mov    $0x1,%esi
call   10c0 <_login_error@plt>
mov    $0x2,%edi
call   10b0 <exit@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_login_do>:
push   %rbx
test   %rdi,%rdi
jne    11e5 <_login_do+0x15>
xor    %eax,%eax
call   1080 <_do_ask_username@plt>
mov    %rax,%rdi
test   %rax,%rax
je     1247 <_login_do+0x77>
call   1070 <getpwnam@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1030 <getuid@plt>
test   %rax,%rax
je     124e <_login_do+0x7e>
xor    %eax,%eax
call   1050 <_do_ask_password@plt>
test   %rax,%rax
je     1247 <_login_do+0x77>
test   %rbx,%rbx
je     1224 <_login_do+0x54>
mov    (%rbx),%rsi
test   %rsi,%rsi
je     1224 <_login_do+0x54>
cmpb   $0x2a,(%rsi)
je     1224 <_login_do+0x54>
mov    %rax,%rdi
call   1060 <strcmp@plt>
test   %rax,%rax
je     1253 <_login_do+0x83>
mov    0x2db5(%rip),%rax
mov    (%rax),%esi
lea    0xdcc(%rip),%rdi
call   1040 <fputs@plt>
mov    $0x3,%edi
call   10f0 <sleep@plt>
xor    %eax,%eax
pop    %rbx
ret
mov    $0x1,%eax
pop    %rbx
ret
test   %rbx,%rbx
je     1224 <_login_do+0x54>
mov    0x14(%rbx),%edi
call   10a0 <setgid@plt>
test   %rax,%rax
je     1269 <_login_do+0x99>
lea    0xda7(%rip),%rdi
jmp    127d <_login_do+0xad>
mov    0x10(%rbx),%edi
call   10d0 <setuid@plt>
test   %rax,%rax
je     128b <_login_do+0xbb>
lea    0xd98(%rip),%rdi
mov    $0x1,%esi
call   10c0 <_login_error@plt>
neg    %eax
pop    %rbx
ret
xor    %eax,%eax
call   1090 <_login_nologin@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
test   %rcx,%rcx
je     12a1 <_login_do+0xd1>
pop    %rbx
ret
mov    0x8(%rbx),%rdi
mov    %rdi,%rsi
xor    %edx,%edx
call   10e0 <execl@plt>
mov    0x8(%rbx),%rdi
mov    $0x1,%esi
call   10c0 <_login_error@plt>
mov    $0x2,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.passwd = type { i8*, i8*, i32, i32 }
@.str = [14 x i8] c"Login failed\0A\00"
@stderr = global i32 0
@.str.1 = [7 x i8] c"setgid\00"
@.str.2 = [7 x i8] c"setuid\00"
define i32 @_login_do(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.passwd*
%5 = alloca i8*
store i8* %0, i8** %3
store i8* null, i8** %5
%6 = load i8*, i8** %3
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %12
8: ; preds = %1
%9 = call i8* (...) @_do_ask_username()
store i8* %9, i8** %3
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %12
11: ; preds = %8
store i32 1, i32* %2
br label %86
12: ; preds = %8, %1
%13 = load i8*, i8** %3
%14 = call %struct.passwd* @getpwnam(i8* %13)
store %struct.passwd* %14, %struct.passwd** %4
%15 = call i64 (...) @getuid()
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %12
%18 = call i8* (...) @_do_ask_password()
store i8* %18, i8** %5
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %21
20: ; preds = %17
store i32 1, i32* %2
br label %86
21: ; preds = %17, %12
%22 = load %struct.passwd*, %struct.passwd** %4
%23 = icmp eq %struct.passwd* %22, null
br i1 %23, label %47, label %24
24: ; preds = %21
%25 = load i8*, i8** %5
%26 = icmp ne i8* %25, null
br i1 %26, label %27, label %51
27: ; preds = %24
%28 = load %struct.passwd*, %struct.passwd** %4
%29 = getelementptr inbounds %struct.passwd, %struct.passwd* %28, i32 0, i32 0
%30 = load i8*, i8** %29
%31 = icmp eq i8* %30, null
br i1 %31, label %47, label %32
32: ; preds = %27
%33 = load %struct.passwd*, %struct.passwd** %4
%34 = getelementptr inbounds %struct.passwd, %struct.passwd* %33, i32 0, i32 0
%35 = load i8*, i8** %34
%36 = getelementptr inbounds i8, i8* %35, i64 0
%37 = load i8, i8* %36
%38 = sext i8 %37 to i32
%39 = icmp eq i32 %38, 42
br i1 %39, label %47, label %40
40: ; preds = %32
%41 = load i8*, i8** %5
%42 = load %struct.passwd*, %struct.passwd** %4
%43 = getelementptr inbounds %struct.passwd, %struct.passwd* %42, i32 0, i32 0
%44 = load i8*, i8** %43
%45 = call i64 @strcmp(i8* %41, i8* %44)
%46 = icmp ne i64 %45, 0
br i1 %46, label %47, label %51
47: ; preds = %40, %32, %27, %21
%48 = load i32, i32* @stderr
%49 = call i32 @fputs(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %48)
%50 = call i32 @sleep(i32 3)
store i32 0, i32* %2
br label %86
51: ; preds = %40, %24
%52 = load %struct.passwd*, %struct.passwd** %4
%53 = getelementptr inbounds %struct.passwd, %struct.passwd* %52, i32 0, i32 3
%54 = load i32, i32* %53
%55 = call i64 @setgid(i32 %54)
%56 = icmp ne i64 %55, 0
br i1 %56, label %57, label %60
57: ; preds = %51
%58 = call i32 @_login_error(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 1)
%59 = sub nsw i32 0, %58
store i32 %59, i32* %2
br label %86
60: ; preds = %51
%61 = load %struct.passwd*, %struct.passwd** %4
%62 = getelementptr inbounds %struct.passwd, %struct.passwd* %61, i32 0, i32 2
%63 = load i32, i32* %62
%64 = call i64 @setuid(i32 %63)
%65 = icmp ne i64 %64, 0
br i1 %65, label %66, label %69
66: ; preds = %60
%67 = call i32 @_login_error(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 1)
%68 = sub nsw i32 0, %67
store i32 %68, i32* %2
br label %86
69: ; preds = %60
%70 = call i64 (...) @_login_nologin()
%71 = icmp ne i64 %70, 0
br i1 %71, label %72, label %73
72: ; preds = %69
store i32 -1, i32* %2
br label %86
73: ; preds = %69
%74 = load %struct.passwd*, %struct.passwd** %4
%75 = getelementptr inbounds %struct.passwd, %struct.passwd* %74, i32 0, i32 1
%76 = load i8*, i8** %75
%77 = load %struct.passwd*, %struct.passwd** %4
%78 = getelementptr inbounds %struct.passwd, %struct.passwd* %77, i32 0, i32 1
%79 = load i8*, i8** %78
%80 = call i32 @execl(i8* %76, i8* %79, i32* null)
%81 = load %struct.passwd*, %struct.passwd** %4
%82 = getelementptr inbounds %struct.passwd, %struct.passwd* %81, i32 0, i32 1
%83 = load i8*, i8** %82
%84 = call i32 @_login_error(i8* %83, i32 1)
%85 = call i32 @exit(i32 2)
unreachable
86: ; preds = %72, %66, %57, %47, %20, %11
%87 = load i32, i32* %2
ret i32 %87
}

LLVM IR (-O2):
%struct.passwd = type { i8*, i8*, i32, i32 }
@.str = [14 x i8] c"Login failed\0A\00"
@stderr = global i32 0
@.str.1 = [7 x i8] c"setgid\00"
@.str.2 = [7 x i8] c"setuid\00"
define i32 @_login_do(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %3, label %6
3: ; preds = %1
%4 = tail call i8* (...) @_do_ask_username()
%5 = icmp eq i8* %4, null
br i1 %5, label %58, label %6
6: ; preds = %3, %1
%7 = phi i8* [ %4, %3 ], [ %0, %1 ]
%8 = tail call %struct.passwd* @getpwnam(i8* %7)
%9 = tail call i64 (...) @getuid()
%10 = icmp eq i64 %9, 0
br i1 %10, label %16, label %11
11: ; preds = %6
%12 = tail call i8* (...) @_do_ask_password()
%13 = icmp eq i8* %12, null
br i1 %13, label %58, label %14
14: ; preds = %11
%15 = icmp eq %struct.passwd* %8, null
br i1 %15, label %28, label %18
16: ; preds = %6
%17 = icmp eq %struct.passwd* %8, null
br i1 %17, label %28, label %32
18: ; preds = %14
%19 = getelementptr inbounds %struct.passwd, %struct.passwd* %8, i64 0, i32 0
%20 = load i8*, i8** %19
%21 = icmp eq i8* %20, null
br i1 %21, label %28, label %22
22: ; preds = %18
%23 = load i8, i8* %20
%24 = icmp eq i8 %23, 42
br i1 %24, label %28, label %25
25: ; preds = %22
%26 = tail call i64 @strcmp(i8* %12, i8* %20)
%27 = icmp eq i64 %26, 0
br i1 %27, label %32, label %28
28: ; preds = %16, %25, %22, %18, %14
%29 = load i32, i32* @stderr
%30 = tail call i32 @fputs(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %29)
%31 = tail call i32 @sleep(i32 3)
br label %58
32: ; preds = %16, %25
%33 = getelementptr inbounds %struct.passwd, %struct.passwd* %8, i64 0, i32 3
%34 = load i32, i32* %33
%35 = tail call i64 @setgid(i32 %34)
%36 = icmp eq i64 %35, 0
br i1 %36, label %40, label %37
37: ; preds = %32
%38 = tail call i32 @_login_error(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 1)
%39 = sub nsw i32 0, %38
br label %58
40: ; preds = %32
%41 = getelementptr inbounds %struct.passwd, %struct.passwd* %8, i64 0, i32 2
%42 = load i32, i32* %41
%43 = tail call i64 @setuid(i32 %42)
%44 = icmp eq i64 %43, 0
br i1 %44, label %48, label %45
45: ; preds = %40
%46 = tail call i32 @_login_error(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 1)
%47 = sub nsw i32 0, %46
br label %58
48: ; preds = %40
%49 = tail call i64 (...) @_login_nologin()
%50 = icmp eq i64 %49, 0
br i1 %50, label %51, label %58
51: ; preds = %48
%52 = getelementptr inbounds %struct.passwd, %struct.passwd* %8, i64 0, i32 1
%53 = load i8*, i8** %52
%54 = tail call i32 @execl(i8* %53, i8* %53, i32* null)
%55 = load i8*, i8** %52
%56 = tail call i32 @_login_error(i8* %55, i32 1)
%57 = tail call i32 @exit(i32 2)
unreachable
58: ; preds = %48, %11, %3, %45, %37, %28
%59 = phi i32 [ 0, %28 ], [ %39, %37 ], [ %47, %45 ], [ 1, %3 ], [ 1, %11 ], [ -1, %48 ]
ret i32 %59
}