#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 {
  scalar_t__ pw_gid;
  scalar_t__ pw_uid;
  int /*<<< orphan*/ pw_shell;
  int /*<<< orphan*/ pw_dir;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ chdir(int /*<<< orphan*/);
int /*<<< orphan*/ execl(int /*<<< orphan*/, int /*<<< orphan*/, char *,
                         char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
scalar_t__ getgid();
scalar_t__ getuid();
scalar_t__ malloc(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ puts(char *);
scalar_t__ setgid(scalar_t__);
scalar_t__ setuid(scalar_t__);
int /*<<< orphan*/ stdout;
void run_shell(struct passwd *pwd, bool print_motd) {
  if (pwd->pw_gid != getgid()) {
    if (setgid(pwd->pw_gid) < 0) {
      perror("Could not change group");
      return;
    }
  }
  if (pwd->pw_uid != getuid()) {
    if (setuid(pwd->pw_uid) < 0) {
      perror("Could not change user");
      return;
    }
  }
  chdir(pwd->pw_dir);
  if (print_motd) {
    printf("\033[H\033[J");
    FILE *motd_fp = fopen("/etc/motd", "r");
    if (motd_fp) {
      char *motd = (char *)malloc(1024);
      fread(motd, 1024, 1, motd_fp);
      puts(motd);
      free(motd);
      fclose(motd_fp);
    }
    fflush(stdout);
  }
  execl(pwd->pw_shell, pwd->pw_shell, "-l", (char *)NULL);
  perror("Could not launch shell");
  return;
}


x86 assembly (clang, -O0):
<run_shell>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   10a0 <getgid@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
cmp    %rcx,%rax
je     124d <run_shell+0x5d>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   10d0 <setgid@plt>
cmp    $0x0,%rax
jge    1248 <run_shell+0x58>
lea    0xdc2(%rip),%rdi
call   10f0 <perror@plt>
jmp    1359 <run_shell+0x169>
jmp    124d <run_shell+0x5d>
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x30(%rbp)
mov    $0x0,%al
call   1080 <getuid@plt>
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
cmp    %rcx,%rax
je     129d <run_shell+0xad>
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%rdi
call   1100 <setuid@plt>
cmp    $0x0,%rax
jge    1298 <run_shell+0xa8>
lea    0xd89(%rip),%rdi
call   10f0 <perror@plt>
jmp    1359 <run_shell+0x169>
jmp    129d <run_shell+0xad>
mov    -0x8(%rbp),%rax
mov    0x14(%rax),%edi
call   1070 <chdir@plt>
cmpl   $0x0,-0xc(%rbp)
je     132f <run_shell+0x13f>
lea    0xd73(%rip),%rdi
call   1090 <printf@plt>
lea    0xd6e(%rip),%rdi
lea    0xd5e(%rip),%rsi
call   10e0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1321 <run_shell+0x131>
mov    $0x400,%edi
call   10b0 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x400,%esi
mov    $0x1,%edx
call   1050 <fread@plt>
mov    -0x20(%rbp),%rdi
call   1040 <puts@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1060 <fclose@plt>
mov    0x2cb8(%rip),%rax
mov    (%rax),%edi
call   10c0 <fflush@plt>
mov    -0x8(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x8(%rbp),%rax
mov    0x10(%rax),%esi
lea    0xcfa(%rip),%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   1110 <execl@plt>
lea    0xced(%rip),%rdi
call   10f0 <perror@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<run_shell>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%r14d
mov    %rdi,%rbx
mov    (%rdi),%rbp
xor    %eax,%eax
call   10a0 <getgid@plt>
cmp    %rax,%rbp
je     1222 <run_shell+0x32>
mov    (%rbx),%rdi
call   10d0 <setgid@plt>
test   %rax,%rax
jns    1222 <run_shell+0x32>
lea    0xde3(%rip),%rdi
jmp    12e2 <run_shell+0xf2>
mov    0x8(%rbx),%rbp
xor    %eax,%eax
call   1080 <getuid@plt>
cmp    %rax,%rbp
je     124c <run_shell+0x5c>
mov    0x8(%rbx),%rdi
call   1100 <setuid@plt>
test   %rax,%rax
jns    124c <run_shell+0x5c>
lea    0xdd0(%rip),%rdi
jmp    12e2 <run_shell+0xf2>
mov    0x14(%rbx),%edi
call   1070 <chdir@plt>
test   %r14d,%r14d
je     12c8 <run_shell+0xd8>
lea    0xdcd(%rip),%rdi
call   1090 <printf@plt>
lea    0xdc8(%rip),%rdi
lea    0xdb8(%rip),%rsi
call   10e0 <fopen@plt>
test   %rax,%rax
je     12ba <run_shell+0xca>
mov    %rax,%rbp
mov    $0x400,%edi
call   10b0 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    $0x400,%esi
mov    $0x1,%edx
mov    %rbp,%rcx
call   1050 <fread@plt>
mov    %r14,%rdi
call   1040 <puts@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbp,%rdi
call   1060 <fclose@plt>
mov    0x2d1f(%rip),%rax
mov    (%rax),%edi
call   10c0 <fflush@plt>
mov    0x10(%rbx),%edi
lea    0xd6c(%rip),%rdx
mov    %edi,%esi
xor    %ecx,%ecx
call   1110 <execl@plt>
lea    0xd5f(%rip),%rdi
pop    %rbx
pop    %r14
pop    %rbp
jmp    10f0 <perror@plt>

LLVM IR (-O0):
%struct.passwd = type { i64, i64, i32, i32 }
@.str = [23 x i8] c"Could not change group\00"
@.str.1 = [22 x i8] c"Could not change user\00"
@.str.2 = [7 x i8] c"\1B[H\1B[J\00"
@.str.3 = [10 x i8] c"/etc/motd\00"
@.str.4 = [2 x i8] c"r\00"
@stdout = global i32 0
@.str.5 = [3 x i8] c"-l\00"
@.str.6 = [23 x i8] c"Could not launch shell\00"
define void @run_shell(%struct.passwd* %0, i32 %1) {
%3 = alloca %struct.passwd*
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i8*
store %struct.passwd* %0, %struct.passwd** %3
store i32 %1, i32* %4
%7 = load %struct.passwd*, %struct.passwd** %3
%8 = getelementptr inbounds %struct.passwd, %struct.passwd* %7, i32 0, i32 0
%9 = load i64, i64* %8
%10 = call i64 (...) @getgid()
%11 = icmp ne i64 %9, %10
br i1 %11, label %12, label %21
12: ; preds = %2
%13 = load %struct.passwd*, %struct.passwd** %3
%14 = getelementptr inbounds %struct.passwd, %struct.passwd* %13, i32 0, i32 0
%15 = load i64, i64* %14
%16 = call i64 @setgid(i64 %15)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %12
%19 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %72
20: ; preds = %12
br label %21
21: ; preds = %20, %2
%22 = load %struct.passwd*, %struct.passwd** %3
%23 = getelementptr inbounds %struct.passwd, %struct.passwd* %22, i32 0, i32 1
%24 = load i64, i64* %23
%25 = call i64 (...) @getuid()
%26 = icmp ne i64 %24, %25
br i1 %26, label %27, label %36
27: ; preds = %21
%28 = load %struct.passwd*, %struct.passwd** %3
%29 = getelementptr inbounds %struct.passwd, %struct.passwd* %28, i32 0, i32 1
%30 = load i64, i64* %29
%31 = call i64 @setuid(i64 %30)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %27
%34 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %72
35: ; preds = %27
br label %36
36: ; preds = %35, %21
%37 = load %struct.passwd*, %struct.passwd** %3
%38 = getelementptr inbounds %struct.passwd, %struct.passwd* %37, i32 0, i32 3
%39 = load i32, i32* %38
%40 = call i32 @chdir(i32 %39)
%41 = load i32, i32* %4
%42 = icmp ne i32 %41, 0
br i1 %42, label %43, label %63
43: ; preds = %36
%44 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%45 = call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %45, i32** %5
%46 = load i32*, i32** %5
%47 = icmp ne i32* %46, null
br i1 %47, label %48, label %60
48: ; preds = %43
%49 = call i64 @malloc(i32 1024)
%50 = inttoptr i64 %49 to i8*
store i8* %50, i8** %6
%51 = load i8*, i8** %6
%52 = load i32*, i32** %5
%53 = call i32 @fread(i8* %51, i32 1024, i32 1, i32* %52)
%54 = load i8*, i8** %6
%55 = call i32 @puts(i8* %54)
%56 = load i8*, i8** %6
%57 = call i32 @free(i8* %56)
%58 = load i32*, i32** %5
%59 = call i32 @fclose(i32* %58)
br label %60
60: ; preds = %48, %43
%61 = load i32, i32* @stdout
%62 = call i32 @fflush(i32 %61)
br label %63
63: ; preds = %60, %36
%64 = load %struct.passwd*, %struct.passwd** %3
%65 = getelementptr inbounds %struct.passwd, %struct.passwd* %64, i32 0, i32 2
%66 = load i32, i32* %65
%67 = load %struct.passwd*, %struct.passwd** %3
%68 = getelementptr inbounds %struct.passwd, %struct.passwd* %67, i32 0, i32 2
%69 = load i32, i32* %68
%70 = call i32 @execl(i32 %66, i32 %69, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* null)
%71 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0))
br label %72
72: ; preds = %63, %33, %18
ret void
}

LLVM IR (-O2):
%struct.passwd = type { i64, i64, i32, i32 }
@.str = [23 x i8] c"Could not change group\00"
@.str.1 = [22 x i8] c"Could not change user\00"
@.str.2 = [7 x i8] c"\1B[H\1B[J\00"
@.str.3 = [10 x i8] c"/etc/motd\00"
@.str.4 = [2 x i8] c"r\00"
@stdout = global i32 0
@.str.5 = [3 x i8] c"-l\00"
@.str.6 = [23 x i8] c"Could not launch shell\00"
define void @run_shell(%struct.passwd* nocapture %0, i32 %1) {
%3 = getelementptr inbounds %struct.passwd, %struct.passwd* %0, i64 0, i32 0
%4 = load i64, i64* %3
%5 = tail call i64 (...) @getgid()
%6 = icmp eq i64 %4, %5
br i1 %6, label %11, label %7
7: ; preds = %2
%8 = load i64, i64* %3
%9 = tail call i64 @setgid(i64 %8)
%10 = icmp slt i64 %9, 0
br i1 %10, label %43, label %11
11: ; preds = %7, %2
%12 = getelementptr inbounds %struct.passwd, %struct.passwd* %0, i64 0, i32 1
%13 = load i64, i64* %12
%14 = tail call i64 (...) @getuid()
%15 = icmp eq i64 %13, %14
br i1 %15, label %20, label %16
16: ; preds = %11
%17 = load i64, i64* %12
%18 = tail call i64 @setuid(i64 %17)
%19 = icmp slt i64 %18, 0
br i1 %19, label %43, label %20
20: ; preds = %16, %11
%21 = getelementptr inbounds %struct.passwd, %struct.passwd* %0, i64 0, i32 3
%22 = load i32, i32* %21
%23 = tail call i32 @chdir(i32 %22)
%24 = icmp eq i32 %1, 0
br i1 %24, label %39, label %25
25: ; preds = %20
%26 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%27 = tail call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%28 = icmp eq i32* %27, null
br i1 %28, label %36, label %29
29: ; preds = %25
%30 = tail call i64 @malloc(i32 1024)
%31 = inttoptr i64 %30 to i8*
%32 = tail call i32 @fread(i8* %31, i32 1024, i32 1, i32* %27)
%33 = tail call i32 @puts(i8* dereferenceable(1) %31)
%34 = tail call i32 @free(i8* %31)
%35 = tail call i32 @fclose(i32* %27)
br label %36
36: ; preds = %29, %25
%37 = load i32, i32* @stdout
%38 = tail call i32 @fflush(i32 %37)
br label %39
39: ; preds = %36, %20
%40 = getelementptr inbounds %struct.passwd, %struct.passwd* %0, i64 0, i32 2
%41 = load i32, i32* %40
%42 = tail call i32 @execl(i32 %41, i32 %41, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* null)
br label %43
43: ; preds = %16, %7, %39
%44 = phi i8* [ getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0), %39 ], [ getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), %7 ], [ getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), %16 ]
%45 = tail call i32 @perror(i8* %44)
ret void
}