#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*/ execv(char *, char **);
int main(int argc, char **argv) {
  char *interpreter = "/usr/bin/python";
  char *source = "suid-test.py";
  char *args[argc + 2];
  args[0] = argv[0];
  args[1] = source;
  int i, j;
  for (i = 2, j = 1; i < argc; ++i, ++j) {
    args[i] = argv[j];
  }
  args[i] = NULL;
  execv(interpreter, args);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xed3(%rip),%rax
mov    %rax,-0x18(%rbp)
lea    0xed8(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%eax
add    $0x2,%eax
mov    %eax,%ecx
mov    %rsp,%rax
mov    %rax,-0x28(%rbp)
lea    0xf(,%rcx,8),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rax
sub    %rdx,%rax
mov    %rax,-0x40(%rbp)
mov    %rax,%rsp
mov    %rcx,-0x30(%rbp)
mov    -0x10(%rbp),%rcx
mov    (%rcx),%rcx
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rcx
mov    %rcx,0x8(%rax)
movl   $0x2,-0x34(%rbp)
movl   $0x1,-0x38(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    11c3 <main+0xb3>
mov    -0x40(%rbp),%rax
mov    -0x10(%rbp),%rcx
movslq -0x38(%rbp),%rdx
mov    (%rcx,%rdx,8),%rdx
movslq -0x34(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    1188 <main+0x78>
mov    -0x40(%rbp),%rsi
movslq -0x34(%rbp),%rax
movq   $0x0,(%rsi,%rax,8)
mov    -0x18(%rbp),%rdi
call   1030 <execv@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r14
push   %rbx
mov    %edi,%r14d
lea    0x2(%r14),%eax
mov    %rsp,%rcx
lea    0xf(,%rax,8),%rax
and    $0xfffffffffffffff0,%rax
mov    %rcx,%rbx
sub    %rax,%rbx
mov    %rbx,%rsp
neg    %rax
mov    (%rsi),%rdx
mov    %rdx,(%rcx,%rax,1)
lea    0xeb9(%rip),%rax
mov    %rax,0x8(%rbx)
mov    $0x2,%eax
cmp    $0x3,%edi
jl     117d <main+0x5d>
lea    0x10(%rbx),%rdi
add    $0x8,%rsi
lea    -0x2(%r14),%edx
shl    $0x3,%rdx
call   1030 <memcpy@plt>
mov    %r14d,%eax
movq   $0x0,(%rbx,%rax,8)
lea    0xe74(%rip),%rdi
mov    %rbx,%rsi
call   1040 <execv@plt>
xor    %eax,%eax
lea    -0x10(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [16 x i8] c"/usr/bin/python\00"
@.str.1 = [13 x i8] c"suid-test.py\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8** %6
store i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8** %7
%12 = load i32, i32* %4
%13 = add nsw i32 %12, 2
%14 = zext i32 %13 to i64
%15 = call i8* @llvm.stacksave()
store i8* %15, i8** %8
%16 = alloca i8*, i64 %14
store i64 %14, i64* %9
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 0
%19 = load i8*, i8** %18
%20 = getelementptr inbounds i8*, i8** %16, i64 0
store i8* %19, i8** %20
%21 = load i8*, i8** %7
%22 = getelementptr inbounds i8*, i8** %16, i64 1
store i8* %21, i8** %22
store i32 2, i32* %10
store i32 1, i32* %11
br label %23
23: ; preds = %36, %2
%24 = load i32, i32* %10
%25 = load i32, i32* %4
%26 = icmp slt i32 %24, %25
br i1 %26, label %27, label %41
27: ; preds = %23
%28 = load i8**, i8*** %5
%29 = load i32, i32* %11
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8*, i8** %28, i64 %30
%32 = load i8*, i8** %31
%33 = load i32, i32* %10
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8*, i8** %16, i64 %34
store i8* %32, i8** %35
br label %36
36: ; preds = %27
%37 = load i32, i32* %10
%38 = add nsw i32 %37, 1
store i32 %38, i32* %10
%39 = load i32, i32* %11
%40 = add nsw i32 %39, 1
store i32 %40, i32* %11
br label %23
41: ; preds = %23
%42 = load i32, i32* %10
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i8*, i8** %16, i64 %43
store i8* null, i8** %44
%45 = load i8*, i8** %6
%46 = call i32 @execv(i8* %45, i8** %16)
store i32 0, i32* %3
%47 = load i8*, i8** %8
%48 = load i32, i32* %3
ret i32 %48
}

LLVM IR (-O2):
@.str = [16 x i8] c"/usr/bin/python\00"
@.str.1 = [13 x i8] c"suid-test.py\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = add nsw i32 %0, 2
%4 = zext i32 %3 to i64
%5 = alloca i8*, i64 %4
%6 = load i8*, i8** %1
store i8* %6, i8** %5
%7 = getelementptr inbounds i8*, i8** %5, i64 1
store i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8** %7
%8 = icmp sgt i32 %0, 2
br i1 %8, label %9, label %18
9: ; preds = %2
%10 = getelementptr i8*, i8** %5, i64 2
%11 = bitcast i8** %10 to i8*
%12 = getelementptr i8*, i8** %1, i64 1
%13 = bitcast i8** %12 to i8*
%14 = add nsw i32 %0, -2
%15 = zext i32 %14 to i64
%16 = shl nuw nsw i64 %15, 3
%17 = zext i32 %0 to i64
br label %18
18: ; preds = %9, %2
%19 = phi i64 [ 2, %2 ], [ %17, %9 ]
%20 = getelementptr inbounds i8*, i8** %5, i64 %19
store i8* null, i8** %20
%21 = call i32 @execv(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8** %5)
ret i32 0
}