#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 BUFSIZ;
int /*<<< orphan*/ execve(char *, char **, char **);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ readlink(char *, char *, int);
int /*<<< orphan*/ strcat(char *, char *);
int strlen(char *);
int main(int argc, char **argv, char **envp) {
  char buf[BUFSIZ];
  readlink("/proc/self/exe", buf, BUFSIZ);
  strcat(buf + strlen(buf), ".py");
  execve(buf, argv, envp);
  perror("exec failed:");
  return 1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2e6f(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rsi
sub    %rcx,%rsi
mov    %rsi,-0x30(%rbp)
mov    %rsi,%rsp
mov    %rax,-0x28(%rbp)
mov    0x2e43(%rip),%rax
mov    (%rax),%edx
lea    0xe5a(%rip),%rdi
call   1030 <readlink@plt>
mov    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x30(%rbp),%rdi
cltq
add    %rax,%rdi
lea    0xe4b(%rip),%rsi
call   1070 <strcat@plt>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1050 <execve@plt>
lea    0xe32(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x20(%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   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    0x2e79(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rbx
mov    %rdx,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe7c(%rip),%rdi
mov    %rbx,%rsi
call   1030 <readlink@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
movslq %eax,%rdi
add    %rbx,%rdi
lea    0xe6e(%rip),%rsi
call   1070 <strcat@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1050 <execve@plt>
lea    0xe58(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%eax
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@BUFSIZ = global i32 0
@.str = [15 x i8] c"/proc/self/exe\00"
@.str.1 = [4 x i8] c".py\00"
@.str.2 = [13 x i8] c"exec failed:\00"
define i32 @main(i32 %0, i8** %1, i8** %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i8**
%8 = alloca i8*
%9 = alloca i64
store i32 0, i32* %4
store i32 %0, i32* %5
store i8** %1, i8*** %6
store i8** %2, i8*** %7
%10 = load i32, i32* @BUFSIZ
%11 = zext i32 %10 to i64
%12 = call i8* @llvm.stacksave()
store i8* %12, i8** %8
%13 = alloca i8, i64 %11
store i64 %11, i64* %9
%14 = load i32, i32* @BUFSIZ
%15 = call i32 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %13, i32 %14)
%16 = call i32 @strlen(i8* %13)
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %13, i64 %17
%19 = call i32 @strcat(i8* %18, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%20 = load i8**, i8*** %6
%21 = load i8**, i8*** %7
%22 = call i32 @execve(i8* %13, i8** %20, i8** %21)
%23 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %4
%24 = load i8*, i8** %8
%25 = load i32, i32* %4
ret i32 %25
}

LLVM IR (-O2):
@BUFSIZ = global i32 0
@.str = [15 x i8] c"/proc/self/exe\00"
@.str.1 = [4 x i8] c".py\00"
@.str.2 = [13 x i8] c"exec failed:\00"
define i32 @main(i32 %0, i8** %1, i8** %2) {
%4 = load i32, i32* @BUFSIZ
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = call i32 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %6, i32 %4)
%8 = call i32 @strlen(i8* %6)
%9 = sext i32 %8 to i64
%10 = getelementptr inbounds i8, i8* %6, i64 %9
%11 = call i32 @strcat(i8* %10, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%12 = call i32 @execve(i8* %6, i8** %1, i8** %2)
%13 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
ret i32 1
}