#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 network_status {
  int dummy;
};
struct keypair {
  int dummy;
};

/* Variables and functions */
int *get_polarssl_principals();
int /*<<< orphan*/ increment_and_send_nonce(struct network_status *);
struct network_status *network_bind_and_accept(int);
struct network_status *network_connect(char *, int);
int /*<<< orphan*/ network_disconnect(struct network_status *);
int /*<<< orphan*/ polarssl_attacker(int *);
int random_int();
int /*<<< orphan*/ send_asymmetric_decrypted(struct network_status *,
                                             struct keypair *);
int /*<<< orphan*/ send_asymmetric_encrypted(struct network_status *,
                                             struct keypair *);
int /*<<< orphan*/ send_asymmetric_signature(struct network_status *,
                                             struct keypair *);
int /*<<< orphan*/ send_data(struct network_status *);
int /*<<< orphan*/ send_hmac(struct network_status *, struct keypair *);
int /*<<< orphan*/ send_keys(struct network_status *, struct keypair *);
int /*<<< orphan*/ send_nonce(struct network_status *);
int /*<<< orphan*/ send_pair_composed(struct network_status *);
int /*<<< orphan*/ send_pair_decomposed(struct network_status *);
int /*<<< orphan*/ send_symmetric_decrypted(struct network_status *,
                                            struct keypair *);
int /*<<< orphan*/ send_symmetric_encrypted(struct network_status *,
                                            struct keypair *);
void attacker(int attacker_id, struct keypair *keypair) {
  for (;;) {
    struct network_status *net_stat = 0;
    int net_choise = random_int();
    int port = random_int();
    if (net_choise % 2 == 0)
      net_stat = network_bind_and_accept(port % 65536);
    else
      net_stat = network_connect("localhost", port % 65536);
    {
      int action = random_int();
      int *c;
      switch (action % 13) {
      case 0:
        c = get_polarssl_principals();
        polarssl_attacker(c);
        break;
      case 1:
        send_data(net_stat);
        break;
      case 2:
        send_pair_composed(net_stat);
        break;
      case 3:
        send_pair_decomposed(net_stat);
        break;
      case 4:
        send_nonce(net_stat);
        break;
      case 5:
        increment_and_send_nonce(net_stat);
        break;
      case 6:
        send_keys(net_stat, keypair);
        break;
      case 7:
        send_hmac(net_stat, keypair);
        break;
      case 8:
        send_symmetric_encrypted(net_stat, keypair);
        break;
      case 9:
        send_symmetric_decrypted(net_stat, keypair);
        break;
      case 10:
        send_asymmetric_encrypted(net_stat, keypair);
        break;
      case 11:
        send_asymmetric_decrypted(net_stat, keypair);
        break;
      case 12:
        send_asymmetric_signature(net_stat, keypair);
      }
    }
    network_disconnect(net_stat);
  }
}


x86 assembly (clang, -O0):
<attacker>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    $0x0,%al
call   10f0 <random_int@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   10f0 <random_int@plt>
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    127a <attacker+0x5a>
mov    -0x20(%rbp),%eax
mov    $0x10000,%ecx
cltd
idiv   %ecx
mov    %edx,%edi
call   10b0 <network_bind_and_accept@plt>
mov    %rax,-0x18(%rbp)
jmp    1297 <attacker+0x77>
mov    -0x20(%rbp),%eax
mov    $0x10000,%ecx
cltd
idiv   %ecx
mov    %edx,%esi
lea    0xda6(%rip),%rdi
call   1110 <network_connect@plt>
mov    %rax,-0x18(%rbp)
xor    %eax,%eax
call   10f0 <random_int@plt>
mov    %eax,-0x24(%rbp)
movslq -0x24(%rbp),%rcx
mov    %ecx,%eax
imul   $0x4ec4ec4f,%rcx,%rdx
mov    %rdx,%rcx
shr    $0x3f,%rcx
sar    $0x22,%rdx
add    %ecx,%edx
mov    %edx,%ecx
lea    (%rcx,%rcx,2),%esi
mov    %esi,%edx
lea    (%rcx,%rdx,4),%ecx
sub    %ecx,%eax
mov    %eax,%ecx
mov    %rcx,-0x38(%rbp)
sub    $0xc,%eax
ja     13c2 <attacker+0x1a2>
mov    -0x38(%rbp),%rax
lea    0xd1f(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    $0x0,%al
call   1120 <get_polarssl_principals@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   10c0 <polarssl_attacker@plt>
jmp    13c2 <attacker+0x1a2>
mov    -0x18(%rbp),%rdi
call   10a0 <send_data@plt>
jmp    13c2 <attacker+0x1a2>
mov    -0x18(%rbp),%rdi
call   1130 <send_pair_composed@plt>
jmp    13c2 <attacker+0x1a2>
mov    -0x18(%rbp),%rdi
call   10e0 <send_pair_decomposed@plt>
jmp    13c2 <attacker+0x1a2>
mov    -0x18(%rbp),%rdi
call   1080 <send_nonce@plt>
jmp    13c2 <attacker+0x1a2>
mov    -0x18(%rbp),%rdi
call   1060 <increment_and_send_nonce@plt>
jmp    13c2 <attacker+0x1a2>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <send_keys@plt>
jmp    13c2 <attacker+0x1a2>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <send_hmac@plt>
jmp    13c2 <attacker+0x1a2>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <send_symmetric_encrypted@plt>
jmp    13c2 <attacker+0x1a2>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1090 <send_symmetric_decrypted@plt>
jmp    13c2 <attacker+0x1a2>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1100 <send_asymmetric_encrypted@plt>
jmp    13c2 <attacker+0x1a2>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <send_asymmetric_decrypted@plt>
jmp    13c2 <attacker+0x1a2>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1140 <send_asymmetric_signature@plt>
mov    -0x18(%rbp),%rdi
call   10d0 <network_disconnect@plt>
jmp    122f <attacker+0xf>

x86 assembly (clang, -O2):
<attacker>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
lea    0xdce(%rip),%r12
lea    0xdfb(%rip),%r14
jmp    1258 <attacker+0x38>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1140 <send_asymmetric_signature@plt>
cs nopw 0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   10d0 <network_disconnect@plt>
xor    %eax,%eax
call   10f0 <random_int@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   10f0 <random_int@plt>
lea    0xffff(%rax),%ecx
test   %eax,%eax
cmovns %eax,%ecx
and    $0xffff0000,%ecx
sub    %ecx,%eax
test   $0x1,%bl
jne    1290 <attacker+0x70>
mov    %eax,%edi
call   10b0 <network_bind_and_accept@plt>
jmp    129a <attacker+0x7a>
nopl   0x0(%rax)
mov    %r14,%rdi
mov    %eax,%esi
call   1110 <network_connect@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   10f0 <random_int@plt>
cltq
imul   $0x4ec4ec4f,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x22,%rcx
add    %edx,%ecx
lea    (%rcx,%rcx,2),%edx
lea    (%rcx,%rdx,4),%ecx
sub    %ecx,%eax
cmp    $0xc,%eax
ja     1250 <attacker+0x30>
movslq (%r12,%rax,4),%rax
add    %r12,%rax
jmp    *%rax
xor    %eax,%eax
call   1120 <get_polarssl_principals@plt>
mov    %rax,%rdi
call   10c0 <polarssl_attacker@plt>
jmp    1250 <attacker+0x30>
mov    %rbx,%rdi
call   10a0 <send_data@plt>
jmp    1250 <attacker+0x30>
mov    %rbx,%rdi
call   1130 <send_pair_composed@plt>
jmp    1250 <attacker+0x30>
mov    %rbx,%rdi
call   10e0 <send_pair_decomposed@plt>
jmp    1250 <attacker+0x30>
mov    %rbx,%rdi
call   1080 <send_nonce@plt>
jmp    1250 <attacker+0x30>
mov    %rbx,%rdi
call   1060 <increment_and_send_nonce@plt>
jmp    1250 <attacker+0x30>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1030 <send_keys@plt>
jmp    1250 <attacker+0x30>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1050 <send_hmac@plt>
jmp    1250 <attacker+0x30>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1070 <send_symmetric_encrypted@plt>
jmp    1250 <attacker+0x30>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1090 <send_symmetric_decrypted@plt>
jmp    1250 <attacker+0x30>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1100 <send_asymmetric_encrypted@plt>
jmp    1250 <attacker+0x30>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1040 <send_asymmetric_decrypted@plt>
jmp    1250 <attacker+0x30>

LLVM IR (-O0):
%struct.keypair = type { i32 }
%struct.network_status = type { i32 }
@.str = [10 x i8] c"localhost\00"
define void @attacker(i32 %0, %struct.keypair* %1) {
%3 = alloca i32
%4 = alloca %struct.keypair*
%5 = alloca %struct.network_status*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
store i32 %0, i32* %3
store %struct.keypair* %1, %struct.keypair** %4
br label %10
10: ; preds = %75, %2
store %struct.network_status* null, %struct.network_status** %5
%11 = call i32 (...) @random_int()
store i32 %11, i32* %6
%12 = call i32 (...) @random_int()
store i32 %12, i32* %7
%13 = load i32, i32* %6
%14 = srem i32 %13, 2
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %10
%17 = load i32, i32* %7
%18 = srem i32 %17, 65536
%19 = call %struct.network_status* @network_bind_and_accept(i32 %18)
store %struct.network_status* %19, %struct.network_status** %5
br label %24
20: ; preds = %10
%21 = load i32, i32* %7
%22 = srem i32 %21, 65536
%23 = call %struct.network_status* @network_connect(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %22)
store %struct.network_status* %23, %struct.network_status** %5
br label %24
24: ; preds = %20, %16
%25 = call i32 (...) @random_int()
store i32 %25, i32* %8
%26 = load i32, i32* %8
%27 = srem i32 %26, 13
switch i32 %27, label %75 [
i32 0, label %28
i32 1, label %32
i32 2, label %35
i32 3, label %38
i32 4, label %41
i32 5, label %44
i32 6, label %47
i32 7, label %51
i32 8, label %55
i32 9, label %59
i32 10, label %63
i32 11, label %67
i32 12, label %71
]
28: ; preds = %24
%29 = call i32* (...) @get_polarssl_principals()
store i32* %29, i32** %9
%30 = load i32*, i32** %9
%31 = call i32 @polarssl_attacker(i32* %30)
br label %75
32: ; preds = %24
%33 = load %struct.network_status*, %struct.network_status** %5
%34 = call i32 @send_data(%struct.network_status* %33)
br label %75
35: ; preds = %24
%36 = load %struct.network_status*, %struct.network_status** %5
%37 = call i32 @send_pair_composed(%struct.network_status* %36)
br label %75
38: ; preds = %24
%39 = load %struct.network_status*, %struct.network_status** %5
%40 = call i32 @send_pair_decomposed(%struct.network_status* %39)
br label %75
41: ; preds = %24
%42 = load %struct.network_status*, %struct.network_status** %5
%43 = call i32 @send_nonce(%struct.network_status* %42)
br label %75
44: ; preds = %24
%45 = load %struct.network_status*, %struct.network_status** %5
%46 = call i32 @increment_and_send_nonce(%struct.network_status* %45)
br label %75
47: ; preds = %24
%48 = load %struct.network_status*, %struct.network_status** %5
%49 = load %struct.keypair*, %struct.keypair** %4
%50 = call i32 @send_keys(%struct.network_status* %48, %struct.keypair* %49)
br label %75
51: ; preds = %24
%52 = load %struct.network_status*, %struct.network_status** %5
%53 = load %struct.keypair*, %struct.keypair** %4
%54 = call i32 @send_hmac(%struct.network_status* %52, %struct.keypair* %53)
br label %75
55: ; preds = %24
%56 = load %struct.network_status*, %struct.network_status** %5
%57 = load %struct.keypair*, %struct.keypair** %4
%58 = call i32 @send_symmetric_encrypted(%struct.network_status* %56, %struct.keypair* %57)
br label %75
59: ; preds = %24
%60 = load %struct.network_status*, %struct.network_status** %5
%61 = load %struct.keypair*, %struct.keypair** %4
%62 = call i32 @send_symmetric_decrypted(%struct.network_status* %60, %struct.keypair* %61)
br label %75
63: ; preds = %24
%64 = load %struct.network_status*, %struct.network_status** %5
%65 = load %struct.keypair*, %struct.keypair** %4
%66 = call i32 @send_asymmetric_encrypted(%struct.network_status* %64, %struct.keypair* %65)
br label %75
67: ; preds = %24
%68 = load %struct.network_status*, %struct.network_status** %5
%69 = load %struct.keypair*, %struct.keypair** %4
%70 = call i32 @send_asymmetric_decrypted(%struct.network_status* %68, %struct.keypair* %69)
br label %75
71: ; preds = %24
%72 = load %struct.network_status*, %struct.network_status** %5
%73 = load %struct.keypair*, %struct.keypair** %4
%74 = call i32 @send_asymmetric_signature(%struct.network_status* %72, %struct.keypair* %73)
br label %75
75: ; preds = %71, %24, %67, %63, %59, %55, %51, %47, %44, %41, %38, %35, %32, %28
%76 = load %struct.network_status*, %struct.network_status** %5
%77 = call i32 @network_disconnect(%struct.network_status* %76)
br label %10
}

LLVM IR (-O2):
%struct.keypair = type { i32 }
%struct.network_status = type { i32 }
@.str = [10 x i8] c"localhost\00"
define void @attacker(i32 %0, %struct.keypair* %1) {
br label %3
3: ; preds = %44, %2
%4 = tail call i32 (...) @random_int()
%5 = tail call i32 (...) @random_int()
%6 = and i32 %4, 1
%7 = icmp eq i32 %6, 0
%8 = srem i32 %5, 65536
br i1 %7, label %9, label %11
9: ; preds = %3
%10 = tail call %struct.network_status* @network_bind_and_accept(i32 %8)
br label %13
11: ; preds = %3
%12 = tail call %struct.network_status* @network_connect(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %8)
br label %13
13: ; preds = %11, %9
%14 = phi %struct.network_status* [ %10, %9 ], [ %12, %11 ]
%15 = tail call i32 (...) @random_int()
%16 = srem i32 %15, 13
switch i32 %16, label %44 [
i32 0, label %17
i32 1, label %20
i32 2, label %22
i32 3, label %24
i32 4, label %26
i32 5, label %28
i32 6, label %30
i32 7, label %32
i32 8, label %34
i32 9, label %36
i32 10, label %38
i32 11, label %40
i32 12, label %42
]
17: ; preds = %13
%18 = tail call i32* (...) @get_polarssl_principals()
%19 = tail call i32 @polarssl_attacker(i32* %18)
br label %44
20: ; preds = %13
%21 = tail call i32 @send_data(%struct.network_status* %14)
br label %44
22: ; preds = %13
%23 = tail call i32 @send_pair_composed(%struct.network_status* %14)
br label %44
24: ; preds = %13
%25 = tail call i32 @send_pair_decomposed(%struct.network_status* %14)
br label %44
26: ; preds = %13
%27 = tail call i32 @send_nonce(%struct.network_status* %14)
br label %44
28: ; preds = %13
%29 = tail call i32 @increment_and_send_nonce(%struct.network_status* %14)
br label %44
30: ; preds = %13
%31 = tail call i32 @send_keys(%struct.network_status* %14, %struct.keypair* %1)
br label %44
32: ; preds = %13
%33 = tail call i32 @send_hmac(%struct.network_status* %14, %struct.keypair* %1)
br label %44
34: ; preds = %13
%35 = tail call i32 @send_symmetric_encrypted(%struct.network_status* %14, %struct.keypair* %1)
br label %44
36: ; preds = %13
%37 = tail call i32 @send_symmetric_decrypted(%struct.network_status* %14, %struct.keypair* %1)
br label %44
38: ; preds = %13
%39 = tail call i32 @send_asymmetric_encrypted(%struct.network_status* %14, %struct.keypair* %1)
br label %44
40: ; preds = %13
%41 = tail call i32 @send_asymmetric_decrypted(%struct.network_status* %14, %struct.keypair* %1)
br label %44
42: ; preds = %13
%43 = tail call i32 @send_asymmetric_signature(%struct.network_status* %14, %struct.keypair* %1)
br label %44
44: ; preds = %42, %13, %40, %38, %36, %34, %32, %30, %28, %26, %24, %22, %20, %17
%45 = tail call i32 @network_disconnect(%struct.network_status* %14)
br label %3
}