#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 */

/* Variables and functions */
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ chroot(char *);
int /*<<< orphan*/ execlp(char *, char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, int *, int *, char *, char *, char *);
int /*<<< orphan*/ setgid(int);
int /*<<< orphan*/ setuid(int);
int /*<<< orphan*/ stdout;
int main() {
  int uid, gid;
  char jailDir[100], prog[100];
  char argv[100];
  int fd;
  printf("uid, gid, jailDir, prog, arg\n");
  scanf("%d%d%s%s%s", &uid, &gid, jailDir, prog, argv);
  fd = open("logfile", O_WRONLY);
  if (fd < 0) {
    printf("open ERROR");
    exit(1);
  }
  if (uid == 0 || gid == 0)
    exit(1);
  chroot(jailDir);
  setuid(uid);
  setgid(gid);
  printf("execvp of %s as uid=%d gid=%d\n", prog, uid, gid);
  fflush(stdout);
  execlp(prog, prog, argv, NULL);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x160,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe57(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x70(%rbp),%rcx
lea    -0xe0(%rbp),%r8
lea    -0x150(%rbp),%r9
lea    0xe55(%rip),%rdi
lea    -0x8(%rbp),%rsi
lea    -0xc(%rbp),%rdx
call   1060 <scanf@plt>
mov    0x2df3(%rip),%rax
mov    (%rax),%esi
lea    0xe43(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x154(%rbp)
cmpl   $0x0,-0x154(%rbp)
jge    1216 <main+0x86>
lea    0xe2c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
cmpl   $0x0,-0x8(%rbp)
je     122a <main+0x9a>
cmpl   $0x0,-0xc(%rbp)
jne    1234 <main+0xa4>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    -0x70(%rbp),%rdi
call   1030 <chroot@plt>
mov    -0x8(%rbp),%edi
call   10a0 <setuid@plt>
mov    -0xc(%rbp),%edi
call   1070 <setgid@plt>
lea    -0xe0(%rbp),%rsi
mov    -0x8(%rbp),%edx
mov    -0xc(%rbp),%ecx
lea    0xddb(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2d71(%rip),%rax
mov    (%rax),%edi
call   1050 <fflush@plt>
lea    -0xe0(%rbp),%rdi
lea    -0xe0(%rbp),%rsi
lea    -0x150(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   10b0 <execlp@plt>
xor    %eax,%eax
add    $0x160,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x160,%rsp
lea    0xe8e(%rip),%rdi
call   1040 <puts@plt>
lea    0xe45(%rip),%rdi
lea    0xc(%rsp),%rsi
lea    0x8(%rsp),%rdx
lea    0xf0(%rsp),%rcx
lea    0x80(%rsp),%r8
lea    0x10(%rsp),%r9
call   1070 <scanf@plt>
mov    0x2dea(%rip),%rax
mov    (%rax),%esi
lea    0xe1c(%rip),%rdi
call   1090 <open@plt>
test   %eax,%eax
js     1275 <main+0xd5>
cmpl   $0x0,0xc(%rsp)
je     1283 <main+0xe3>
cmpl   $0x0,0x8(%rsp)
je     1283 <main+0xe3>
lea    0xf0(%rsp),%rdi
call   1030 <chroot@plt>
mov    0xc(%rsp),%edi
call   10b0 <setuid@plt>
mov    0x8(%rsp),%edi
call   1080 <setgid@plt>
mov    0xc(%rsp),%edx
mov    0x8(%rsp),%ecx
lea    0xde6(%rip),%rdi
lea    0x80(%rsp),%rbx
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2d8f(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
lea    0x10(%rsp),%rdx
mov    %rbx,%rdi
mov    %rbx,%rsi
xor    %ecx,%ecx
call   10c0 <execlp@plt>
xor    %eax,%eax
add    $0x160,%rsp
pop    %rbx
ret
lea    0xd97(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [30 x i8] c"uid, gid, jailDir, prog, arg\0A\00"
@.str.1 = [11 x i8] c"%d%d%s%s%s\00"
@.str.2 = [8 x i8] c"logfile\00"
@O_WRONLY = global i32 0
@.str.3 = [11 x i8] c"open ERROR\00"
@.str.4 = [31 x i8] c"execvp of %s as uid=%d gid=%d\0A\00"
@stdout = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [100 x i8]
%5 = alloca [100 x i8]
%6 = alloca [100 x i8]
%7 = alloca i32
store i32 0, i32* %1
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%9 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%10 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%11 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%12 = call i32 @scanf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32* %2, i32* %3, i8* %9, i8* %10, i8* %11)
%13 = load i32, i32* @O_WRONLY
%14 = call i32 @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32 %13)
store i32 %14, i32* %7
%15 = load i32, i32* %7
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %0
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %0
%21 = load i32, i32* %2
%22 = icmp eq i32 %21, 0
br i1 %22, label %26, label %23
23: ; preds = %20
%24 = load i32, i32* %3
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %23, %20
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %23
%29 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%30 = call i32 @chroot(i8* %29)
%31 = load i32, i32* %2
%32 = call i32 @setuid(i32 %31)
%33 = load i32, i32* %3
%34 = call i32 @setgid(i32 %33)
%35 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%36 = load i32, i32* %2
%37 = load i32, i32* %3
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i8* %35, i32 %36, i32 %37)
%39 = load i32, i32* @stdout
%40 = call i32 @fflush(i32 %39)
%41 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%42 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%43 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%44 = call i32 @execlp(i8* %41, i8* %42, i8* %43, i32* null)
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [11 x i8] c"%d%d%s%s%s\00"
@.str.2 = [8 x i8] c"logfile\00"
@O_WRONLY = global i32 0
@.str.3 = [11 x i8] c"open ERROR\00"
@.str.4 = [31 x i8] c"execvp of %s as uid=%d gid=%d\0A\00"
@stdout = global i32 0
@str = [29 x i8] c"uid, gid, jailDir, prog, arg\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [100 x i8]
%4 = alloca [100 x i8]
%5 = alloca [100 x i8]
%6 = bitcast i32* %1 to i8*
%7 = bitcast i32* %2 to i8*
%8 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%9 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%10 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str, i64 0, i64 0))
%12 = call i32 @scanf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32* %1, i32* %2, i8* %8, i8* %9, i8* %10)
%13 = load i32, i32* @O_WRONLY
%14 = call i32 @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32 %13)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %0
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %0
%20 = load i32, i32* %1
%21 = icmp eq i32 %20, 0
%22 = load i32, i32* %2
%23 = icmp eq i32 %22, 0
%24 = select i1 %21, i1 true, i1 %23
br i1 %24, label %25, label %27
25: ; preds = %19
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %19
%28 = call i32 @chroot(i8* %8)
%29 = load i32, i32* %1
%30 = call i32 @setuid(i32 %29)
%31 = load i32, i32* %2
%32 = call i32 @setgid(i32 %31)
%33 = load i32, i32* %1
%34 = load i32, i32* %2
%35 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i8* %9, i32 %33, i32 %34)
%36 = load i32, i32* @stdout
%37 = call i32 @fflush(i32 %36)
%38 = call i32 @execlp(i8* %9, i8* %9, i8* %10, i32* null)
ret i32 0
}