int do_handshake(int dev_fd, char *handshake_string) {
  int ret;
  char ack;
  char host_handshake[(13) + 1] = {'\0'};
  strncpy(host_handshake, handshake_string, (13));
  ret = write(dev_fd, host_handshake, (13));
  if (ret != (13)) {
    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("%s: Target Handshake successful\n", __func__);
  return 0;
}


x86 assembly (clang, -O0):
<do_handshake>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x23(%rbp),%rdi
xor    %esi,%esi
mov    $0xe,%edx
call   1060 <memset@plt>
lea    -0x23(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0xd,%edx
call   1030 <strncpy@plt>
mov    -0x8(%rbp),%edi
lea    -0x23(%rbp),%rsi
mov    $0xd,%edx
mov    $0x0,%al
call   1040 <write@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xd,-0x14(%rbp)
je     11be <do_handshake+0x6e>
mov    -0x14(%rbp),%esi
lea    0xe55(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1242 <do_handshake+0xf2>
mov    -0x8(%rbp),%edi
lea    -0x15(%rbp),%rsi
mov    $0x1,%edx
mov    $0x0,%al
call   1070 <read@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x1,-0x14(%rbp)
je     11fb <do_handshake+0xab>
mov    -0x14(%rbp),%esi
lea    0xe3c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1242 <do_handshake+0xf2>
movsbl -0x15(%rbp),%eax
cmp    $0x41,%eax
je     1226 <do_handshake+0xd6>
movsbl -0x15(%rbp),%esi
lea    0xe37(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1242 <do_handshake+0xf2>
lea    0xe3f(%rip),%rdi
lea    0xe59(%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   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebx
movb   $0x0,0x17(%rsp)
lea    0xa(%rsp),%r14
mov    $0xd,%edx
mov    %r14,%rdi
call   1030 <strncpy@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xd,%edx
xor    %eax,%eax
call   1040 <write@plt>
cmp    $0xd,%eax
jne    11b1 <do_handshake+0x71>
lea    0x9(%rsp),%rsi
mov    $0x1,%edx
mov    %ebx,%edi
xor    %eax,%eax
call   1060 <read@plt>
cmp    $0x1,%eax
jne    11ba <do_handshake+0x7a>
movsbl 0x9(%rsp),%esi
cmp    $0x41,%esi
jne    11d9 <do_handshake+0x99>
lea    0xecd(%rip),%rdi
lea    0xee7(%rip),%rsi
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11cf <do_handshake+0x8f>
lea    0xe48(%rip),%rdi
jmp    11c1 <do_handshake+0x81>
lea    0xe63(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe6a(%rip),%rdi
jmp    11c3 <do_handshake+0x83>

LLVM IR (-O0):
@.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 = [33 x i8] c"%s: Target Handshake successful\0A\00"
@__func__.do_handshake = [13 x i8] c"do_handshake\00"
define i32 @do_handshake(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8
%8 = alloca [14 x i8]
store i32 %0, i32* %4
store i8* %1, i8** %5
%9 = bitcast [14 x i8]* %8 to i8*
%10 = getelementptr inbounds [14 x i8], [14 x i8]* %8, i64 0, i64 0
%11 = load i8*, i8** %5
%12 = call i8* @strncpy(i8* %10, i8* %11, i64 13)
%13 = load i32, i32* %4
%14 = getelementptr inbounds [14 x i8], [14 x i8]* %8, i64 0, i64 0
%15 = call i32 (i32, i8*, i32, ...) bitcast (i32 (...)* @write to i32 (i32, i8*, i32, ...)*)(i32 %13, i8* %14, i32 13)
store i32 %15, i32* %6
%16 = load i32, i32* %6
%17 = icmp ne i32 %16, 13
br i1 %17, label %18, label %21
18: ; preds = %2
%19 = load i32, i32* %6
%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* %3
br label %39
21: ; preds = %2
%22 = load i32, i32* %4
%23 = call i32 (i32, i8*, i64, ...) bitcast (i32 (...)* @read to i32 (i32, i8*, i64, ...)*)(i32 %22, i8* %7, i64 1)
store i32 %23, i32* %6
%24 = load i32, i32* %6
%25 = icmp ne i32 %24, 1
br i1 %25, label %26, label %29
26: ; preds = %21
%27 = load i32, i32* %6
%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* %3
br label %39
29: ; preds = %21
%30 = load i8, i8* %7
%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* %7
%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* %3
br label %39
37: ; preds = %29
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 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* %3
br label %39
39: ; preds = %37, %33, %26, %18
%40 = load i32, i32* %3
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 = [33 x i8] c"%s: Target Handshake successful\0A\00"
@__func__.do_handshake = [13 x i8] c"do_handshake\00"
define i32 @do_handshake(i32 %0, i8* nocapture %1) {
%3 = alloca i8
%4 = alloca [14 x i8]
%5 = getelementptr inbounds [14 x i8], [14 x i8]* %4, i64 0, i64 0
%6 = getelementptr inbounds [14 x i8], [14 x i8]* %4, i64 0, i64 13
store i8 0, i8* %6
%7 = call i8* @strncpy(i8* %5, i8* dereferenceable(1) %1, i64 13)
%8 = call i32 (i32, i8*, i32, ...) bitcast (i32 (...)* @write to i32 (i32, i8*, i32, ...)*)(i32 %0, i8* %5, i32 13)
%9 = icmp eq i32 %8, 13
br i1 %9, label %12, label %10
10: ; preds = %2
%11 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 %8)
br label %25
12: ; preds = %2
%13 = call i32 (i32, i8*, i64, ...) bitcast (i32 (...)* @read to i32 (i32, i8*, i64, ...)*)(i32 %0, i8* %3, 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* %3
%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 ([33 x i8], [33 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
}