int do_handshake(int dev_fd) {
  int ret;
  char ack;
  char host_handshake[] = "RAMDUMP";
  ret = write(dev_fd, host_handshake, strlen(host_handshake));
  if (ret != strlen(host_handshake)) {
    printf("Sending handshake failed, ret = %d\n", ret);
    return -1;
  }
  ret = read(dev_fd, &ack, sizeof(ack));
  if (ret != 1) {
    printf("Receiving handshake failed, ret = %d\n", ret);
    return -1;
  }
  if (ack != 'A') {
    printf("Wrong Target Handshake, ack = %c\n", ack);
    return -1;
  }
  printf("Target Handshake successful %s\n", __func__);
  return 0;
}


x86 assembly (clang, -O0):
<do_handshake>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    0xeae(%rip),%rax
mov    %rax,-0x15(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x2c(%rbp)
lea    -0x15(%rbp),%rax
mov    %rax,-0x28(%rbp)
lea    -0x15(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    %rax,%rdx
mov    $0x0,%al
call   1030 <write@plt>
mov    %eax,-0xc(%rbp)
movslq -0xc(%rbp),%rax
mov    %rax,-0x20(%rbp)
lea    -0x15(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
cmp    %rcx,%rax
je     11bf <do_handshake+0x7f>
mov    -0xc(%rbp),%esi
lea    0xe5c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1243 <do_handshake+0x103>
mov    -0x8(%rbp),%edi
lea    -0xd(%rbp),%rsi
mov    $0x1,%edx
mov    $0x0,%al
call   1060 <read@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x1,-0xc(%rbp)
je     11fc <do_handshake+0xbc>
mov    -0xc(%rbp),%esi
lea    0xe43(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1243 <do_handshake+0x103>
movsbl -0xd(%rbp),%eax
cmp    $0x41,%eax
je     1227 <do_handshake+0xe7>
movsbl -0xd(%rbp),%esi
lea    0xe3e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1243 <do_handshake+0x103>
lea    0xe46(%rip),%rdi
lea    0xe5f(%rip),%rsi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_handshake>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edi,%r14d
movabs $0x504d55444d4152,%rax
mov    %rax,0x10(%rsp)
lea    0x10(%rsp),%r15
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %r14d,%edi
mov    %r15,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1030 <write@plt>
mov    %eax,%ebp
movslq %eax,%rbx
mov    %r15,%rdi
call   1040 <strlen@plt>
cmp    %rbx,%rax
jne    11c7 <do_handshake+0x87>
lea    0xf(%rsp),%rsi
mov    $0x1,%edx
mov    %r14d,%edi
xor    %eax,%eax
call   1060 <read@plt>
cmp    $0x1,%eax
jne    11d2 <do_handshake+0x92>
movsbl 0xf(%rsp),%esi
cmp    $0x41,%esi
jne    11dd <do_handshake+0x9d>
lea    0xeb7(%rip),%rdi
lea    0xed0(%rip),%rsi
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11f0 <do_handshake+0xb0>
lea    0xe32(%rip),%rdi
mov    %ebp,%esi
jmp    11e4 <do_handshake+0xa4>
lea    0xe4b(%rip),%rdi
mov    %eax,%esi
jmp    11e4 <do_handshake+0xa4>
lea    0xe66(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.do_handshake.host_handshake = [8 x i8] c"RAMDUMP\00"
@.str = [36 x i8] c"Sending handshake failed, ret = %d\0A\00"
@.str.1 = [38 x i8] c"Receiving handshake failed, ret = %d\0A\00"
@.str.2 = [34 x i8] c"Wrong Target Handshake, ack = %c\0A\00"
@.str.3 = [32 x i8] c"Target Handshake successful %s\0A\00"
@__func__.do_handshake = [13 x i8] c"do_handshake\00"
define i32 @do_handshake(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8
%6 = alloca [8 x i8]
store i32 %0, i32* %3
%7 = bitcast [8 x i8]* %6 to i8*
%8 = load i32, i32* %3
%9 = getelementptr inbounds [8 x i8], [8 x i8]* %6, i64 0, i64 0
%10 = getelementptr inbounds [8 x i8], [8 x i8]* %6, i64 0, i64 0
%11 = call i64 @strlen(i8* %10)
%12 = call i32 (i32, i8*, i64, ...) bitcast (i32 (...)* @write to i32 (i32, i8*, i64, ...)*)(i32 %8, i8* %9, i64 %11)
store i32 %12, i32* %4
%13 = load i32, i32* %4
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds [8 x i8], [8 x i8]* %6, i64 0, i64 0
%16 = call i64 @strlen(i8* %15)
%17 = icmp ne i64 %14, %16
br i1 %17, label %18, label %21
18: ; preds = %1
%19 = load i32, i32* %4
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 %19)
store i32 -1, i32* %2
br label %39
21: ; preds = %1
%22 = load i32, i32* %3
%23 = call i32 (i32, i8*, i64, ...) bitcast (i32 (...)* @read to i32 (i32, i8*, i64, ...)*)(i32 %22, i8* %5, i64 1)
store i32 %23, i32* %4
%24 = load i32, i32* %4
%25 = icmp ne i32 %24, 1
br i1 %25, label %26, label %29
26: ; preds = %21
%27 = load i32, i32* %4
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i32 %27)
store i32 -1, i32* %2
br label %39
29: ; preds = %21
%30 = load i8, i8* %5
%31 = sext i8 %30 to i32
%32 = icmp ne i32 %31, 65
br i1 %32, label %33, label %37
33: ; preds = %29
%34 = load i8, i8* %5
%35 = sext i8 %34 to i32
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i32 %35)
store i32 -1, i32* %2
br label %39
37: ; preds = %29
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @__func__.do_handshake, i64 0, i64 0))
store i32 0, i32* %2
br label %39
39: ; preds = %37, %33, %26, %18
%40 = load i32, i32* %2
ret i32 %40
}

LLVM IR (-O2):
@.str = [36 x i8] c"Sending handshake failed, ret = %d\0A\00"
@.str.1 = [38 x i8] c"Receiving handshake failed, ret = %d\0A\00"
@.str.2 = [34 x i8] c"Wrong Target Handshake, ack = %c\0A\00"
@.str.3 = [32 x i8] c"Target Handshake successful %s\0A\00"
@__func__.do_handshake = [13 x i8] c"do_handshake\00"
define i32 @do_handshake(i32 %0) {
%2 = alloca i8
%3 = alloca i64
%4 = bitcast i64* %3 to i8*
store i64 22603026750325074, i64* %3
%5 = call i64 @strlen(i8* %4)
%6 = call i32 (i32, i8*, i64, ...) bitcast (i32 (...)* @write to i32 (i32, i8*, i64, ...)*)(i32 %0, i8* %4, i64 %5)
%7 = sext i32 %6 to i64
%8 = call i64 @strlen(i8* %4)
%9 = icmp eq i64 %8, %7
br i1 %9, label %12, label %10
10: ; preds = %1
%11 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 %6)
br label %25
12: ; preds = %1
%13 = call i32 (i32, i8*, i64, ...) bitcast (i32 (...)* @read to i32 (i32, i8*, i64, ...)*)(i32 %0, i8* %2, i64 1)
%14 = icmp eq i32 %13, 1
br i1 %14, label %17, label %15
15: ; preds = %12
%16 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i32 %13)
br label %25
17: ; preds = %12
%18 = load i8, i8* %2
%19 = icmp eq i8 %18, 65
br i1 %19, label %23, label %20
20: ; preds = %17
%21 = sext i8 %18 to i32
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i32 %21)
br label %25
23: ; preds = %17
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @__func__.do_handshake, i64 0, i64 0))
br label %25
25: ; preds = %23, %20, %15, %10
%26 = phi i32 [ -1, %10 ], [ -1, %15 ], [ -1, %20 ], [ 0, %23 ]
ret i32 %26
}