#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 EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ IPPROTO_ICMP;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_RAW;
int /*<<< orphan*/ execve(char *, char **, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
char *malloc(scalar_t__);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ setresgid(int, int, int);
int /*<<< orphan*/ setresuid(int, int, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ strcpy(char *, char *);
scalar_t__ strlen(char *);
int main(int argc, char *argv[]) {
  int rfd;
  char *argv0, *argv1;
  if (argc < 2) {
    printf("usage: rawwrapper <program>\n");
    printf("argv[1] will be the raw socket\n");
    exit(EXIT_FAILURE);
  }
  if ((rfd = socket(PF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0) {
    perror("socket");
    return EXIT_FAILURE;
  }
  setresgid(1009, 1009, 1009);
  setresuid(1009, 1009, 1009);
  argv0 = malloc(strlen(argv[0]) + 1);
  strcpy(argv0, argv[0]);
  argv1 = malloc(strlen(argv[1]) + 1);
  strcpy(argv1, argv[1]);
  argv[0] = argv1;
  sprintf(argv[1], "%d", rfd);
  execve(argv[0], argv, NULL);
  return EXIT_SUCCESS;
}


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)
cmpl   $0x2,-0x8(%rbp)
jge    11f6 <main+0x46>
lea    0xe29(%rip),%rdi
call   1070 <printf@plt>
lea    0xe3a(%rip),%rdi
call   1070 <printf@plt>
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
mov    0x2de2(%rip),%rax
mov    (%rax),%esi
mov    0x2dd1(%rip),%rax
mov    (%rax),%edx
call   10d0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    123f <main+0x8f>
lea    0xe14(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d93(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12f7 <main+0x147>
mov    $0x3f1,%edx
mov    %edx,%edi
mov    %edx,%esi
call   1060 <setresgid@plt>
mov    $0x3f1,%edx
mov    %edx,%edi
mov    %edx,%esi
call   1040 <setresuid@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1050 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1090 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
call   1030 <strcpy@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1050 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1090 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
call   1030 <strcpy@plt>
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x14(%rbp),%edx
lea    0xd72(%rip),%rsi
call   10b0 <sprintf@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1080 <execve@plt>
mov    0x2cce(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
cmp    $0x1,%edi
jle    12a4 <main+0xf4>
mov    %rsi,%r15
mov    0x2e06(%rip),%rax
mov    (%rax),%edi
mov    0x2e15(%rip),%rax
mov    (%rax),%esi
mov    0x2e04(%rip),%rax
mov    (%rax),%edx
call   10d0 <socket@plt>
test   %eax,%eax
js     128f <main+0xdf>
mov    %eax,%r14d
mov    $0x3f1,%edi
mov    $0x3f1,%esi
mov    $0x3f1,%edx
call   1070 <setresgid@plt>
mov    $0x3f1,%edi
mov    $0x3f1,%esi
mov    $0x3f1,%edx
call   1050 <setresuid@plt>
mov    (%r15),%rbp
mov    %rbp,%rdi
call   1060 <strlen@plt>
lea    0x1(%rax),%rdi
call   1090 <malloc@plt>
mov    %rax,%rdi
mov    %rbp,%rsi
call   1030 <strcpy@plt>
mov    0x8(%r15),%rbp
mov    %rbp,%rdi
call   1060 <strlen@plt>
lea    0x1(%rax),%rdi
call   1090 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %rbp,%rsi
call   1030 <strcpy@plt>
mov    %rbx,(%r15)
mov    0x8(%r15),%rdi
lea    0xd9a(%rip),%rsi
mov    %r14d,%edx
call   10b0 <sprintf@plt>
mov    (%r15),%rdi
mov    %r15,%rsi
xor    %edx,%edx
call   1080 <execve@plt>
mov    0x2d3e(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xda1(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d26(%rip),%rax
jmp    1282 <main+0xd2>
lea    0xd58(%rip),%rdi
call   1040 <puts@plt>
lea    0xd68(%rip),%rdi
call   1040 <puts@plt>
mov    0x2d05(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [29 x i8] c"usage: rawwrapper <program>\0A\00"
@.str.1 = [32 x i8] c"argv[1] will be the raw socket\0A\00"
@EXIT_FAILURE = global i32 0
@PF_INET = global i32 0
@SOCK_RAW = global i32 0
@IPPROTO_ICMP = global i32 0
@.str.2 = [7 x i8] c"socket\00"
@.str.3 = [3 x i8] c"%d\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 2
br i1 %10, label %11, label %16
11: ; preds = %2
%12 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
%14 = load i32, i32* @EXIT_FAILURE
%15 = call i32 @exit(i32 %14)
unreachable
16: ; preds = %2
%17 = load i32, i32* @PF_INET
%18 = load i32, i32* @SOCK_RAW
%19 = load i32, i32* @IPPROTO_ICMP
%20 = call i32 @socket(i32 %17, i32 %18, i32 %19)
store i32 %20, i32* %6
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %16
%23 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%24 = load i32, i32* @EXIT_FAILURE
store i32 %24, i32* %3
br label %64
25: ; preds = %16
%26 = call i32 @setresgid(i32 1009, i32 1009, i32 1009)
%27 = call i32 @setresuid(i32 1009, i32 1009, i32 1009)
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 0
%30 = load i8*, i8** %29
%31 = call i64 @strlen(i8* %30)
%32 = add nsw i64 %31, 1
%33 = call i8* @malloc(i64 %32)
store i8* %33, i8** %7
%34 = load i8*, i8** %7
%35 = load i8**, i8*** %5
%36 = getelementptr inbounds i8*, i8** %35, i64 0
%37 = load i8*, i8** %36
%38 = call i32 @strcpy(i8* %34, i8* %37)
%39 = load i8**, i8*** %5
%40 = getelementptr inbounds i8*, i8** %39, i64 1
%41 = load i8*, i8** %40
%42 = call i64 @strlen(i8* %41)
%43 = add nsw i64 %42, 1
%44 = call i8* @malloc(i64 %43)
store i8* %44, i8** %8
%45 = load i8*, i8** %8
%46 = load i8**, i8*** %5
%47 = getelementptr inbounds i8*, i8** %46, i64 1
%48 = load i8*, i8** %47
%49 = call i32 @strcpy(i8* %45, i8* %48)
%50 = load i8*, i8** %8
%51 = load i8**, i8*** %5
%52 = getelementptr inbounds i8*, i8** %51, i64 0
store i8* %50, i8** %52
%53 = load i8**, i8*** %5
%54 = getelementptr inbounds i8*, i8** %53, i64 1
%55 = load i8*, i8** %54
%56 = load i32, i32* %6
%57 = call i32 @sprintf(i8* %55, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %56)
%58 = load i8**, i8*** %5
%59 = getelementptr inbounds i8*, i8** %58, i64 0
%60 = load i8*, i8** %59
%61 = load i8**, i8*** %5
%62 = call i32 @execve(i8* %60, i8** %61, i32* null)
%63 = load i32, i32* @EXIT_SUCCESS
store i32 %63, i32* %3
br label %64
64: ; preds = %25, %22
%65 = load i32, i32* %3
ret i32 %65
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@PF_INET = global i32 0
@SOCK_RAW = global i32 0
@IPPROTO_ICMP = global i32 0
@.str.2 = [7 x i8] c"socket\00"
@.str.3 = [3 x i8] c"%d\00"
@EXIT_SUCCESS = global i32 0
@str = [28 x i8] c"usage: rawwrapper <program>\00"
@str.4 = [31 x i8] c"argv[1] will be the raw socket\00"
define i32 @main(i32 %0, i8** %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %9
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str, i64 0, i64 0))
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.4, i64 0, i64 0))
%7 = load i32, i32* @EXIT_FAILURE
%8 = tail call i32 @exit(i32 %7)
unreachable
9: ; preds = %2
%10 = load i32, i32* @PF_INET
%11 = load i32, i32* @SOCK_RAW
%12 = load i32, i32* @IPPROTO_ICMP
%13 = tail call i32 @socket(i32 %10, i32 %11, i32 %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %9
%16 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %35
17: ; preds = %9
%18 = tail call i32 @setresgid(i32 1009, i32 1009, i32 1009)
%19 = tail call i32 @setresuid(i32 1009, i32 1009, i32 1009)
%20 = load i8*, i8** %1
%21 = tail call i64 @strlen(i8* dereferenceable(1) %20)
%22 = add nsw i64 %21, 1
%23 = tail call i8* @malloc(i64 %22)
%24 = tail call i32 @strcpy(i8* %23, i8* %20)
%25 = getelementptr inbounds i8*, i8** %1, i64 1
%26 = load i8*, i8** %25
%27 = tail call i64 @strlen(i8* dereferenceable(1) %26)
%28 = add nsw i64 %27, 1
%29 = tail call i8* @malloc(i64 %28)
%30 = tail call i32 @strcpy(i8* %29, i8* %26)
store i8* %29, i8** %1
%31 = load i8*, i8** %25
%32 = tail call i32 @sprintf(i8* dereferenceable(1) %31, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %13)
%33 = load i8*, i8** %1
%34 = tail call i32 @execve(i8* %33, i8** %1, i32* null)
br label %35
35: ; preds = %17, %15
%36 = phi i32* [ @EXIT_FAILURE, %15 ], [ @EXIT_SUCCESS, %17 ]
%37 = load i32, i32* %36
ret i32 %37
}