#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 utsname {
  char *nodename;
};

/* Variables and functions */
int execute(char **);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
char *fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ free(char **);
char *getenv(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
char **tokenise(char *);
int /*<<< orphan*/ uname(struct utsname *);
void shell_loop() {
  int status;
  char line[4096];
  char *cmd;
  char **tokens;
  struct utsname unameData;
  uname(&unameData);
  while (status = 1) {
    printf("[%s@%s]$ ", getenv("USER"), unameData.nodename);
    fflush(stdout);
    cmd = fgets(line, 4096, stdin);
    if (cmd) {
      tokens = tokenise(cmd);
      status = execute(tokens);
      free(tokens);
    } else {
      printf("\n");
      break;
    }
  }
}


x86 assembly (clang, -O0):
<shell_loop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
lea    -0x1028(%rbp),%rdi
call   1060 <uname@plt>
movl   $0x1,-0x4(%rbp)
lea    0xe65(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rsi
mov    -0x1028(%rbp),%rdx
lea    0xe45(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2e0f(%rip),%rax
mov    (%rax),%edi
call   1090 <fflush@plt>
lea    -0x1010(%rbp),%rdi
mov    0x2e0a(%rip),%rax
mov    (%rax),%edx
mov    $0x1000,%esi
call   1080 <fgets@plt>
mov    %rax,-0x1018(%rbp)
cmpq   $0x0,-0x1018(%rbp)
je     1232 <shell_loop+0xb2>
mov    -0x1018(%rbp),%rdi
call   10a0 <tokenise@plt>
mov    %rax,-0x1020(%rbp)
mov    -0x1020(%rbp),%rdi
call   1070 <execute@plt>
mov    %eax,-0x4(%rbp)
mov    -0x1020(%rbp),%rdi
call   1040 <free@plt>
jmp    1245 <shell_loop+0xc5>
lea    0xdd6(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    124a <shell_loop+0xca>
jmp    1197 <shell_loop+0x17>
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<shell_loop>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1018,%rsp
lea    0x8(%rsp),%rdi
call   1070 <uname@plt>
lea    0xe58(%rip),%rdi
call   1030 <getenv@plt>
mov    0x8(%rsp),%r13
lea    0xe3d(%rip),%rdi
mov    %rax,%rsi
mov    %r13,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2e01(%rip),%rbx
mov    (%rbx),%edi
call   10a0 <fflush@plt>
mov    0x2e03(%rip),%r14
mov    (%r14),%edx
lea    0x10(%rsp),%rdi
mov    $0x1000,%esi
call   1090 <fgets@plt>
test   %rax,%rax
je     126a <shell_loop+0xda>
lea    0xdfd(%rip),%r15
lea    0x10(%rsp),%r12
nopl   0x0(%rax,%rax,1)
mov    %rax,%rdi
call   10b0 <tokenise@plt>
mov    %rbx,%rbp
mov    %rax,%rbx
mov    %rax,%rdi
call   1080 <execute@plt>
mov    %rbx,%rdi
mov    %rbp,%rbx
call   1040 <free@plt>
lea    0xdd2(%rip),%rdi
call   1030 <getenv@plt>
mov    %r15,%rdi
mov    %rax,%rsi
mov    %r13,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x0(%rbp),%edi
call   10a0 <fflush@plt>
mov    (%r14),%edx
mov    %r12,%rdi
mov    $0x1000,%esi
call   1090 <fgets@plt>
test   %rax,%rax
jne    1210 <shell_loop+0x80>
mov    $0xa,%edi
call   1050 <putchar@plt>
add    $0x1018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.utsname = type { i8* }
@.str = [10 x i8] c"[%s@%s]$ \00"
@.str.1 = [5 x i8] c"USER\00"
@stdout = global i32 0
@stdin = global i32 0
@.str.2 = [2 x i8] c"\0A\00"
define void @shell_loop() {
%1 = alloca i32
%2 = alloca [4096 x i8]
%3 = alloca i8*
%4 = alloca i8**
%5 = alloca %struct.utsname
%6 = call i32 @uname(%struct.utsname* %5)
br label %7
7: ; preds = %29, %0
store i32 1, i32* %1
br label %8
8: ; preds = %7
%9 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%10 = getelementptr inbounds %struct.utsname, %struct.utsname* %5, i32 0, i32 0
%11 = load i8*, i8** %10
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %9, i8* %11)
%13 = load i32, i32* @stdout
%14 = call i32 @fflush(i32 %13)
%15 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%16 = load i32, i32* @stdin
%17 = call i8* @fgets(i8* %15, i32 4096, i32 %16)
store i8* %17, i8** %3
%18 = load i8*, i8** %3
%19 = icmp ne i8* %18, null
br i1 %19, label %20, label %27
20: ; preds = %8
%21 = load i8*, i8** %3
%22 = call i8** @tokenise(i8* %21)
store i8** %22, i8*** %4
%23 = load i8**, i8*** %4
%24 = call i32 @execute(i8** %23)
store i32 %24, i32* %1
%25 = load i8**, i8*** %4
%26 = call i32 @free(i8** %25)
br label %29
27: ; preds = %8
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %30
29: ; preds = %20
br label %7
30: ; preds = %27
ret void
}

LLVM IR (-O2):
%struct.utsname = type { i8* }
@.str = [10 x i8] c"[%s@%s]$ \00"
@.str.1 = [5 x i8] c"USER\00"
@stdout = global i32 0
@stdin = global i32 0
define void @shell_loop() {
%1 = alloca [4096 x i8]
%2 = alloca %struct.utsname
%3 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 0
%4 = bitcast %struct.utsname* %2 to i8*
%5 = call i32 @uname(%struct.utsname* %2)
%6 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%7 = getelementptr inbounds %struct.utsname, %struct.utsname* %2, i64 0, i32 0
%8 = load i8*, i8** %7
%9 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %6, i8* %8)
%10 = load i32, i32* @stdout
%11 = call i32 @fflush(i32 %10)
%12 = load i32, i32* @stdin
%13 = call i8* @fgets(i8* %3, i32 4096, i32 %12)
%14 = icmp eq i8* %13, null
br i1 %14, label %27, label %15
15: ; preds = %0, %15
%16 = phi i8* [ %25, %15 ], [ %13, %0 ]
%17 = call i8** @tokenise(i8* %16)
%18 = call i32 @execute(i8** %17)
%19 = call i32 @free(i8** %17)
%20 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%21 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %20, i8* %8)
%22 = load i32, i32* @stdout
%23 = call i32 @fflush(i32 %22)
%24 = load i32, i32* @stdin
%25 = call i8* @fgets(i8* %3, i32 4096, i32 %24)
%26 = icmp eq i8* %25, null
br i1 %26, label %27, label %15
27: ; preds = %15, %0
%28 = call i32 @putchar(i32 10)
ret void
}