#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 stat {
  int /*<<< orphan*/ st_uid;
  int /*<<< orphan*/ st_gid;
};

/* Variables and functions */
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup(int /*<<< orphan*/);
int /*<<< orphan*/ execl(char *, char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ nice(int);
int /*<<< orphan*/ open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ setgid(int /*<<< orphan*/);
int /*<<< orphan*/ setuid(int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
int stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ system(char *);
int /*<<< orphan*/ unlink(char *);
int /*<<< orphan*/ wait(int *);
void run(char *file) {
  struct stat stbuf;
  int pid, i;
  char sbuf[64];
  if (fork() != 0)
    return;
  for (i = 0; i < 15; i++)
    close(i);
  dup(dup(open("/dev/null", 0)));
  sprintf(sbuf, "/bin/mv %.14s %s", file, "past");
  system(sbuf);
  chdir("past");
  if (stat(file, &stbuf) == -1)
    exit(1);
  setgid(stbuf.st_gid);
  setuid(stbuf.st_uid);
  if (pid = fork()) {
    if (pid == -1)
      exit(1);
    wait((int *)0);
    unlink(file);
    exit(0);
  }
  nice(3);
  execl("/bin/sh", "sh", file, 0);
  execl("/usr/bin/sh", "sh", file, 0);
  fprintf(stderr, "Can't execl shell\n");
  exit(1);
}


x86 assembly (clang, -O0):
<run>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x0,%al
call   1120 <fork@plt>
cmp    $0x0,%eax
je     1222 <run+0x22>
add    $0x60,%rsp
pop    %rbp
ret
movl   $0x0,-0x18(%rbp)
cmpl   $0xf,-0x18(%rbp)
jge    1249 <run+0x49>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1229 <run+0x29>
lea    0xdb0(%rip),%rdi
xor    %esi,%esi
call   10c0 <open@plt>
mov    %eax,%edi
call   1060 <dup@plt>
mov    %eax,%edi
call   1060 <dup@plt>
lea    -0x60(%rbp),%rdi
mov    -0x8(%rbp),%rdx
lea    0xd96(%rip),%rsi
lea    0xda0(%rip),%rcx
call   10d0 <sprintf@plt>
lea    -0x60(%rbp),%rdi
call   1050 <system@plt>
lea    0xd8b(%rip),%rdi
call   1040 <chdir@plt>
mov    -0x8(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   1090 <stat@plt>
cmp    $0xffffffff,%eax
jne    12b5 <run+0xb5>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    -0xc(%rbp),%edi
call   10b0 <setgid@plt>
mov    -0x10(%rbp),%edi
call   10f0 <setuid@plt>
mov    $0x0,%al
call   1120 <fork@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
je     1305 <run+0x105>
cmpl   $0xffffffff,-0x14(%rbp)
jne    12ec <run+0xec>
mov    $0x1,%edi
call   10e0 <exit@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1110 <wait@plt>
mov    -0x8(%rbp),%rdi
call   1030 <unlink@plt>
xor    %edi,%edi
call   10e0 <exit@plt>
mov    $0x3,%edi
call   10a0 <nice@plt>
mov    -0x8(%rbp),%rdx
lea    0xd0a(%rip),%rdi
lea    0xd08(%rip),%rsi
xor    %ecx,%ecx
call   1100 <execl@plt>
mov    -0x8(%rbp),%rdx
lea    0xced(%rip),%rdi
lea    0xcef(%rip),%rsi
xor    %ecx,%ecx
call   1100 <execl@plt>
mov    0x2c98(%rip),%rax
mov    (%rax),%edi
lea    0xcdb(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>

x86 assembly (clang, -O2):
<run>:
push   %r15
push   %r14
push   %rbx
sub    $0x50,%rsp
mov    %rdi,%rbx
xor    %eax,%eax
call   1120 <fork@plt>
test   %eax,%eax
je     1221 <run+0x21>
add    $0x50,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
xor    %edi,%edi
call   1070 <close@plt>
mov    $0x1,%edi
call   1070 <close@plt>
mov    $0x2,%edi
call   1070 <close@plt>
mov    $0x3,%edi
call   1070 <close@plt>
mov    $0x4,%edi
call   1070 <close@plt>
mov    $0x5,%edi
call   1070 <close@plt>
mov    $0x6,%edi
call   1070 <close@plt>
mov    $0x7,%edi
call   1070 <close@plt>
mov    $0x8,%edi
call   1070 <close@plt>
mov    $0x9,%edi
call   1070 <close@plt>
mov    $0xa,%edi
call   1070 <close@plt>
mov    $0xb,%edi
call   1070 <close@plt>
mov    $0xc,%edi
call   1070 <close@plt>
mov    $0xd,%edi
call   1070 <close@plt>
mov    $0xe,%edi
call   1070 <close@plt>
lea    0xd45(%rip),%rdi
xor    %esi,%esi
call   10c0 <open@plt>
mov    %eax,%edi
call   1060 <dup@plt>
mov    %eax,%edi
call   1060 <dup@plt>
lea    0xd33(%rip),%rsi
lea    0xd3d(%rip),%r14
lea    0x10(%rsp),%r15
mov    %r15,%rdi
mov    %rbx,%rdx
mov    %r14,%rcx
call   10d0 <sprintf@plt>
mov    %r15,%rdi
call   1050 <system@plt>
mov    %r14,%rdi
call   1040 <chdir@plt>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1090 <stat@plt>
cmp    $0xffffffff,%eax
je     1335 <run+0x135>
mov    0xc(%rsp),%edi
call   10b0 <setgid@plt>
mov    0x8(%rsp),%edi
call   10f0 <setuid@plt>
xor    %eax,%eax
call   1120 <fork@plt>
test   %eax,%eax
je     133f <run+0x13f>
cmp    $0xffffffff,%eax
jne    1397 <run+0x197>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    $0x3,%edi
call   10a0 <nice@plt>
lea    0xcd4(%rip),%rdi
lea    0xcd2(%rip),%r14
mov    %r14,%rsi
mov    %rbx,%rdx
xor    %ecx,%ecx
call   1100 <execl@plt>
lea    0xcb5(%rip),%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
xor    %ecx,%ecx
call   1100 <execl@plt>
mov    0x2c61(%rip),%rax
mov    (%rax),%edi
lea    0xca4(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
xor    %edi,%edi
call   1110 <wait@plt>
mov    %rbx,%rdi
call   1030 <unlink@plt>
xor    %edi,%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32, i32 }
@.str = [10 x i8] c"/dev/null\00"
@.str.1 = [17 x i8] c"/bin/mv %.14s %s\00"
@.str.2 = [5 x i8] c"past\00"
@.str.3 = [8 x i8] c"/bin/sh\00"
@.str.4 = [3 x i8] c"sh\00"
@.str.5 = [12 x i8] c"/usr/bin/sh\00"
@stderr = global i32 0
@.str.6 = [19 x i8] c"Can't execl shell\0A\00"
define void @run(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.stat
%4 = alloca i32
%5 = alloca i32
%6 = alloca [64 x i8]
store i8* %0, i8** %2
%7 = call i32 (...) @fork()
%8 = icmp ne i32 %7, 0
br i1 %8, label %9, label %10
9: ; preds = %1
ret void
10: ; preds = %1
store i32 0, i32* %5
br label %11
11: ; preds = %17, %10
%12 = load i32, i32* %5
%13 = icmp slt i32 %12, 15
br i1 %13, label %14, label %20
14: ; preds = %11
%15 = load i32, i32* %5
%16 = call i32 @close(i32 %15)
br label %17
17: ; preds = %14
%18 = load i32, i32* %5
%19 = add nsw i32 %18, 1
store i32 %19, i32* %5
br label %11
20: ; preds = %11
%21 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 0)
%22 = call i32 @dup(i32 %21)
%23 = call i32 @dup(i32 %22)
%24 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%25 = load i8*, i8** %2
%26 = call i32 @sprintf(i8* %24, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %25, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%27 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%28 = call i32 @system(i8* %27)
%29 = call i32 @chdir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%30 = load i8*, i8** %2
%31 = call i32 @stat(i8* %30, %struct.stat* %3)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %35
33: ; preds = %20
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %20
%36 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 1
%37 = load i32, i32* %36
%38 = call i32 @setgid(i32 %37)
%39 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%40 = load i32, i32* %39
%41 = call i32 @setuid(i32 %40)
%42 = call i32 (...) @fork()
store i32 %42, i32* %4
%43 = icmp ne i32 %42, 0
br i1 %43, label %44, label %54
44: ; preds = %35
%45 = load i32, i32* %4
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %49
47: ; preds = %44
%48 = call i32 @exit(i32 1)
unreachable
49: ; preds = %44
%50 = call i32 @wait(i32* null)
%51 = load i8*, i8** %2
%52 = call i32 @unlink(i8* %51)
%53 = call i32 @exit(i32 0)
unreachable
54: ; preds = %35
%55 = call i32 @nice(i32 3)
%56 = load i8*, i8** %2
%57 = call i32 @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %56, i32 0)
%58 = load i8*, i8** %2
%59 = call i32 @execl(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %58, i32 0)
%60 = load i32, i32* @stderr
%61 = call i32 @fprintf(i32 %60, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0))
%62 = call i32 @exit(i32 1)
unreachable
}

LLVM IR (-O2):
%struct.stat = type { i32, i32 }
@.str = [10 x i8] c"/dev/null\00"
@.str.1 = [17 x i8] c"/bin/mv %.14s %s\00"
@.str.2 = [5 x i8] c"past\00"
@.str.3 = [8 x i8] c"/bin/sh\00"
@.str.4 = [3 x i8] c"sh\00"
@.str.5 = [12 x i8] c"/usr/bin/sh\00"
@stderr = global i32 0
@.str.6 = [19 x i8] c"Can't execl shell\0A\00"
define void @run(i8* %0) {
%2 = alloca %struct.stat
%3 = alloca [64 x i8]
%4 = bitcast %struct.stat* %2 to i8*
%5 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%6 = tail call i32 (...) @fork()
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %32
8: ; preds = %1
%9 = tail call i32 @close(i32 0)
%10 = tail call i32 @close(i32 1)
%11 = tail call i32 @close(i32 2)
%12 = tail call i32 @close(i32 3)
%13 = tail call i32 @close(i32 4)
%14 = tail call i32 @close(i32 5)
%15 = tail call i32 @close(i32 6)
%16 = tail call i32 @close(i32 7)
%17 = tail call i32 @close(i32 8)
%18 = tail call i32 @close(i32 9)
%19 = tail call i32 @close(i32 10)
%20 = tail call i32 @close(i32 11)
%21 = tail call i32 @close(i32 12)
%22 = tail call i32 @close(i32 13)
%23 = tail call i32 @close(i32 14)
%24 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 0)
%25 = tail call i32 @dup(i32 %24)
%26 = tail call i32 @dup(i32 %25)
%27 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%28 = call i32 @system(i8* %5)
%29 = tail call i32 @chdir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%30 = call i32 @stat(i8* %0, %struct.stat* %2)
%31 = icmp eq i32 %30, -1
br i1 %31, label %33, label %35
32: ; preds = %1
ret void
33: ; preds = %8
%34 = tail call i32 @exit(i32 1)
unreachable
35: ; preds = %8
%36 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 1
%37 = load i32, i32* %36
%38 = tail call i32 @setgid(i32 %37)
%39 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%40 = load i32, i32* %39
%41 = tail call i32 @setuid(i32 %40)
%42 = tail call i32 (...) @fork()
switch i32 %42, label %45 [
i32 0, label %49
i32 -1, label %43
]
43: ; preds = %35
%44 = tail call i32 @exit(i32 1)
unreachable
45: ; preds = %35
%46 = tail call i32 @wait(i32* null)
%47 = tail call i32 @unlink(i8* %0)
%48 = tail call i32 @exit(i32 0)
unreachable
49: ; preds = %35
%50 = tail call i32 @nice(i32 3)
%51 = tail call i32 @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %0, i32 0)
%52 = tail call i32 @execl(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %0, i32 0)
%53 = load i32, i32* @stderr
%54 = tail call i32 @fprintf(i32 %53, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0))
%55 = tail call i32 @exit(i32 1)
unreachable
}