#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ geteuid();
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ *stdin;
int /*<<< orphan*/ stdout;
int strlen(char *);
int getCommand(FILE *source, char *command) {
  char *tmpuser, *prompt;
  if (source == stdin) {
    if (!geteuid()) {
      prompt = "# ";
    } else {
      prompt = "$ ";
    }
    fflush(stdout);
  }
  printf(prompt);
  fflush(stdout);
  if (!fgets(command, 250, source)) {
    if (source == stdin)
      printf("\n");
    return 1;
  }
  command[strlen(command) - 1] = '\0';
  return 0;
}


x86 assembly (clang, -O0):
<getCommand>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2e7d(%rip),%rcx
cmp    (%rcx),%rax
jne    11ad <getCommand+0x5d>
mov    $0x0,%al
call   1050 <geteuid@plt>
cmp    $0x0,%eax
jne    1194 <getCommand+0x44>
lea    0xe75(%rip),%rax
mov    %rax,-0x28(%rbp)
jmp    119f <getCommand+0x4f>
lea    0xe68(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
mov    -0x28(%rbp),%rdi
call   1040 <printf@plt>
mov    0x2e1b(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0xfa,%esi
call   1060 <fgets@plt>
cmp    $0x0,%eax
jne    120b <getCommand+0xbb>
mov    -0x10(%rbp),%rax
mov    0x2dfe(%rip),%rcx
cmp    (%rcx),%rax
jne    11ff <getCommand+0xaf>
lea    0xe0c(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1233 <getCommand+0xe3>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getCommand>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%rbx
mov    %rdi,%r15
xor    %r14d,%r14d
xor    %eax,%eax
call   1060 <geteuid@plt>
test   %eax,%eax
lea    0xe7f(%rip),%rax
lea    0xe7b(%rip),%rbp
cmove  %rax,%rbp
mov    0x2e45(%rip),%r12
mov    (%r12),%edi
call   1080 <fflush@plt>
mov    %rbp,%rdi
call   1050 <printf@plt>
mov    (%r12),%edi
call   1080 <fflush@plt>
mov    %rbx,%rdi
mov    $0xfa,%esi
mov    %r15,%rdx
call   1070 <fgets@plt>
test   %eax,%eax
je     11d2 <getCommand+0x72>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
jmp    11ee <getCommand+0x8e>
mov    0x2e0f(%rip),%rax
mov    $0x1,%r14d
cmp    %r15,(%rax)
jne    11ee <getCommand+0x8e>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stdin = global i32* null
@.str = [3 x i8] c"# \00"
@.str.1 = [3 x i8] c"$ \00"
@stdout = global i32 0
@.str.2 = [2 x i8] c"\0A\00"
define i32 @getCommand(i32* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
store i32* %0, i32** %4
store i8* %1, i8** %5
%8 = load i32*, i32** %4
%9 = load i32*, i32** @stdin
%10 = icmp eq i32* %8, %9
br i1 %10, label %11, label %19
11: ; preds = %2
%12 = call i32 (...) @geteuid()
%13 = icmp ne i32 %12, 0
br i1 %13, label %15, label %14
14: ; preds = %11
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8** %7
br label %16
15: ; preds = %11
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8** %7
br label %16
16: ; preds = %15, %14
%17 = load i32, i32* @stdout
%18 = call i32 @fflush(i32 %17)
br label %19
19: ; preds = %16, %2
%20 = load i8*, i8** %7
%21 = call i32 @printf(i8* %20)
%22 = load i32, i32* @stdout
%23 = call i32 @fflush(i32 %22)
%24 = load i8*, i8** %5
%25 = load i32*, i32** %4
%26 = call i32 @fgets(i8* %24, i32 250, i32* %25)
%27 = icmp ne i32 %26, 0
br i1 %27, label %35, label %28
28: ; preds = %19
%29 = load i32*, i32** %4
%30 = load i32*, i32** @stdin
%31 = icmp eq i32* %29, %30
br i1 %31, label %32, label %34
32: ; preds = %28
%33 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %34
34: ; preds = %32, %28
store i32 1, i32* %3
br label %42
35: ; preds = %19
%36 = load i8*, i8** %5
%37 = load i8*, i8** %5
%38 = call i32 @strlen(i8* %37)
%39 = sub nsw i32 %38, 1
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %36, i64 %40
store i8 0, i8* %41
store i32 0, i32* %3
br label %42
42: ; preds = %35, %34
%43 = load i32, i32* %3
ret i32 %43
}

LLVM IR (-O2):
@stdin = global i32* null
@.str = [3 x i8] c"# \00"
@.str.1 = [3 x i8] c"$ \00"
@stdout = global i32 0
define i32 @getCommand(i32* nocapture %0, i8* %1) {
%3 = load i32*, i32** @stdin
%4 = icmp eq i32* %3, %0
tail call void @llvm.assume(i1 %4)
%5 = tail call i32 (...) @geteuid()
%6 = icmp eq i32 %5, 0
%7 = select i1 %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0)
%8 = load i32, i32* @stdout
%9 = tail call i32 @fflush(i32 %8)
%10 = tail call i32 @printf(i8* dereferenceable(1) %7)
%11 = load i32, i32* @stdout
%12 = tail call i32 @fflush(i32 %11)
%13 = tail call i32 @fgets(i8* %1, i32 250, i32* %0)
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %2
%16 = load i32*, i32** @stdin
%17 = icmp eq i32* %16, %0
br i1 %17, label %18, label %25
18: ; preds = %15
%19 = tail call i32 @putchar(i32 10)
br label %25
20: ; preds = %2
%21 = tail call i32 @strlen(i8* %1)
%22 = add nsw i32 %21, -1
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %1, i64 %23
store i8 0, i8* %24
br label %25
25: ; preds = %15, %18, %20
%26 = phi i32 [ 0, %20 ], [ 1, %18 ], [ 1, %15 ]
ret i32 %26
}