#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 /*<<< orphan*/ uint16_t;

/* Variables and functions */
scalar_t__ bind_socket(int /*<<< orphan*/, int *);
int /*<<< orphan*/ close(int);
scalar_t__ create_and_send_session_key(char *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
scalar_t__ get_client(int, int *);
int /*<<< orphan*/ printf(char *);
scalar_t__ receive_and_validate_session_key(char *, int);
int /*<<< orphan*/ stdout;
int accept_authentic_connection_from_client(uint16_t port) {
  int listen_socket = -1;
  int client_socket = -1;
  if (bind_socket(port, &listen_socket) != 0) {
    goto fail1;
  }
  char secret_key[(32) + 1];
  if (create_and_send_session_key(secret_key) != 0) {
    goto fail1;
  }
  printf("z1\n");
  fflush(stdout);
  if (get_client(listen_socket, &client_socket) != 0) {
    goto fail1;
  }
  close(listen_socket);
  listen_socket = -1;
  if (receive_and_validate_session_key(secret_key, client_socket) != 0) {
    goto fail1;
  }
  return client_socket;
fail1:
  if (client_socket >= 0) {
    close(client_socket);
  }
  if (listen_socket >= 0) {
    close(listen_socket);
  }
  return -1;
}


x86 assembly (clang, -O0):
<accept_authentic_connection_from_client>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
movl   $0xffffffff,-0xc(%rbp)
movl   $0xffffffff,-0x10(%rbp)
mov    -0x8(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1030 <bind_socket@plt>
cmp    $0x0,%rax
je     11a4 <accept_authentic_connection_from_client+0x34>
jmp    1226 <accept_authentic_connection_from_client+0xb6>
lea    -0x40(%rbp),%rdi
call   1090 <create_and_send_session_key@plt>
cmp    $0x0,%rax
je     11bc <accept_authentic_connection_from_client+0x4c>
jmp    1226 <accept_authentic_connection_from_client+0xb6>
lea    0xe3d(%rip),%rdi
call   1040 <printf@plt>
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
call   1080 <fflush@plt>
mov    -0xc(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1050 <get_client@plt>
cmp    $0x0,%rax
je     11f1 <accept_authentic_connection_from_client+0x81>
jmp    1226 <accept_authentic_connection_from_client+0xb6>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0xc(%rbp)
lea    -0x40(%rbp),%rdi
mov    -0x10(%rbp),%esi
call   1070 <receive_and_validate_session_key@plt>
cmp    $0x0,%rax
je     121b <accept_authentic_connection_from_client+0xab>
jmp    1226 <accept_authentic_connection_from_client+0xb6>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1251 <accept_authentic_connection_from_client+0xe1>
cmpl   $0x0,-0x10(%rbp)
jl     1238 <accept_authentic_connection_from_client+0xc8>
mov    -0x10(%rbp),%edi
call   1060 <close@plt>
cmpl   $0x0,-0xc(%rbp)
jl     124a <accept_authentic_connection_from_client+0xda>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<accept_authentic_connection_from_client>:
push   %rbx
sub    $0x40,%rsp
movl   $0xffffffff,0x8(%rsp)
ff
movl   $0xffffffff,0xc(%rsp)
ff
lea    0x8(%rsp),%rsi
call   1030 <bind_socket@plt>
test   %rax,%rax
jne    11f4 <accept_authentic_connection_from_client+0x84>
lea    0x10(%rsp),%rdi
call   1090 <create_and_send_session_key@plt>
test   %rax,%rax
jne    11f4 <accept_authentic_connection_from_client+0x84>
lea    0xe56(%rip),%rdi
call   1040 <puts@plt>
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
call   1080 <fflush@plt>
mov    0x8(%rsp),%edi
lea    0xc(%rsp),%rsi
call   1050 <get_client@plt>
test   %rax,%rax
jne    11f4 <accept_authentic_connection_from_client+0x84>
mov    0x8(%rsp),%edi
call   1060 <close@plt>
movl   $0xffffffff,0x8(%rsp)
ff
mov    0xc(%rsp),%esi
lea    0x10(%rsp),%rdi
call   1070 <receive_and_validate_session_key@plt>
test   %rax,%rax
je     121b <accept_authentic_connection_from_client+0xab>
mov    0xc(%rsp),%edi
test   %edi,%edi
js     1201 <accept_authentic_connection_from_client+0x91>
call   1060 <close@plt>
mov    0x8(%rsp),%edi
mov    $0xffffffff,%ebx
test   %edi,%edi
js     1213 <accept_authentic_connection_from_client+0xa3>
call   1060 <close@plt>
mov    %ebx,%eax
add    $0x40,%rsp
pop    %rbx
ret
mov    0xc(%rsp),%ebx
jmp    1213 <accept_authentic_connection_from_client+0xa3>

LLVM IR (-O0):
@.str = [4 x i8] c"z1\0A\00"
@stdout = global i32 0
define i32 @accept_authentic_connection_from_client(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [33 x i8]
store i32 %0, i32* %3
store i32 -1, i32* %4
store i32 -1, i32* %5
%7 = load i32, i32* %3
%8 = call i64 @bind_socket(i32 %7, i32* %4)
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %1
br label %34
11: ; preds = %1
%12 = getelementptr inbounds [33 x i8], [33 x i8]* %6, i64 0, i64 0
%13 = call i64 @create_and_send_session_key(i8* %12)
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %11
br label %34
16: ; preds = %11
%17 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%18 = load i32, i32* @stdout
%19 = call i32 @fflush(i32 %18)
%20 = load i32, i32* %4
%21 = call i64 @get_client(i32 %20, i32* %5)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %16
br label %34
24: ; preds = %16
%25 = load i32, i32* %4
%26 = call i32 @close(i32 %25)
store i32 -1, i32* %4
%27 = getelementptr inbounds [33 x i8], [33 x i8]* %6, i64 0, i64 0
%28 = load i32, i32* %5
%29 = call i64 @receive_and_validate_session_key(i8* %27, i32 %28)
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %24
br label %34
32: ; preds = %24
%33 = load i32, i32* %5
store i32 %33, i32* %2
br label %47
34: ; preds = %31, %23, %15, %10
%35 = load i32, i32* %5
%36 = icmp sge i32 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %34
%38 = load i32, i32* %5
%39 = call i32 @close(i32 %38)
br label %40
40: ; preds = %37, %34
%41 = load i32, i32* %4
%42 = icmp sge i32 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %40
%44 = load i32, i32* %4
%45 = call i32 @close(i32 %44)
br label %46
46: ; preds = %43, %40
store i32 -1, i32* %2
br label %47
47: ; preds = %46, %32
%48 = load i32, i32* %2
ret i32 %48
}

LLVM IR (-O2):
@stdout = global i32 0
@str = [3 x i8] c"z1\00"
define i32 @accept_authentic_connection_from_client(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [33 x i8]
%5 = bitcast i32* %2 to i8*
store i32 -1, i32* %2
%6 = bitcast i32* %3 to i8*
store i32 -1, i32* %3
%7 = call i64 @bind_socket(i32 %0, i32* %2)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %28
9: ; preds = %1
%10 = getelementptr inbounds [33 x i8], [33 x i8]* %4, i64 0, i64 0
%11 = call i64 @create_and_send_session_key(i8* %10)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %28
13: ; preds = %9
%14 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @str, i64 0, i64 0))
%15 = load i32, i32* @stdout
%16 = call i32 @fflush(i32 %15)
%17 = load i32, i32* %2
%18 = call i64 @get_client(i32 %17, i32* %3)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %28
20: ; preds = %13
%21 = load i32, i32* %2
%22 = call i32 @close(i32 %21)
store i32 -1, i32* %2
%23 = load i32, i32* %3
%24 = call i64 @receive_and_validate_session_key(i8* %10, i32 %23)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %20
%27 = load i32, i32* %3
br label %38
28: ; preds = %20, %13, %9, %1
%29 = load i32, i32* %3
%30 = icmp sgt i32 %29, -1
br i1 %30, label %31, label %33
31: ; preds = %28
%32 = call i32 @close(i32 %29)
br label %33
33: ; preds = %31, %28
%34 = load i32, i32* %2
%35 = icmp sgt i32 %34, -1
br i1 %35, label %36, label %38
36: ; preds = %33
%37 = call i32 @close(i32 %34)
br label %38
38: ; preds = %33, %36, %26
%39 = phi i32 [ %27, %26 ], [ -1, %36 ], [ -1, %33 ]
ret i32 %39
}