#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 timeval {
  int member_1;
  int /*<<< orphan*/ member_0;
};

/* Variables and functions */
int /*<<< orphan*/ fatal(char *);
scalar_t__ wait_for_data_in_sock(int, struct timeval *);
int /*<<< orphan*/ xwrite(int, char *, int);
void establish_connection(int sock) {
  int i;
  for (i = 0; i < 4; i++) {
    struct timeval tv = {0, 125000};
    xwrite(sock, "\x11", 1);
    if (wait_for_data_in_sock(sock, &tv))
      return;
  }
  fatal("Unable to establish connection with device");
}


x86 assembly (clang, -O0):
<establish_connection>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
cmpl   $0x4,-0x8(%rbp)
jge    1199 <establish_connection+0x69>
mov    0xead(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x4(%rbp),%edi
lea    0xea7(%rip),%rsi
mov    $0x1,%edx
call   1030 <xwrite@plt>
mov    -0x4(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1040 <wait_for_data_in_sock@plt>
cmp    $0x0,%rax
je     1186 <establish_connection+0x56>
jmp    11a5 <establish_connection+0x75>
jmp    118b <establish_connection+0x5b>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1142 <establish_connection+0x12>
lea    0xe6a(%rip),%rdi
call   1050 <fatal@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<establish_connection>:
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebx
movabs $0x1e84800000000,%r14
mov    %r14,(%rsp)
lea    0xeb5(%rip),%rsi
mov    $0x1,%edx
call   1030 <xwrite@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
call   1040 <wait_for_data_in_sock@plt>
test   %rax,%rax
jne    11e2 <establish_connection+0xb2>
mov    %r14,(%rsp)
lea    0xe91(%rip),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1030 <xwrite@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
call   1040 <wait_for_data_in_sock@plt>
test   %rax,%rax
jne    11e2 <establish_connection+0xb2>
mov    %r14,(%rsp)
lea    0xe6b(%rip),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1030 <xwrite@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
call   1040 <wait_for_data_in_sock@plt>
test   %rax,%rax
jne    11e2 <establish_connection+0xb2>
mov    %r14,(%rsp)
lea    0xe45(%rip),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1030 <xwrite@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
call   1040 <wait_for_data_in_sock@plt>
test   %rax,%rax
jne    11e2 <establish_connection+0xb2>
lea    0xe25(%rip),%rdi
call   1050 <fatal@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
@__const.establish_connection.tv = %struct.timeval { i32 0, i32 125000 }
@.str = [2 x i8] c"\11\00"
@.str.1 = [43 x i8] c"Unable to establish connection with device\00"
define void @establish_connection(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.timeval
store i32 %0, i32* %2
store i32 0, i32* %3
br label %5
5: ; preds = %17, %1
%6 = load i32, i32* %3
%7 = icmp slt i32 %6, 4
br i1 %7, label %8, label %20
8: ; preds = %5
%9 = bitcast %struct.timeval* %4 to i8*
%10 = load i32, i32* %2
%11 = call i32 @xwrite(i32 %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 1)
%12 = load i32, i32* %2
%13 = call i64 @wait_for_data_in_sock(i32 %12, %struct.timeval* %4)
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %8
br label %22
16: ; preds = %8
br label %17
17: ; preds = %16
%18 = load i32, i32* %3
%19 = add nsw i32 %18, 1
store i32 %19, i32* %3
br label %5
20: ; preds = %5
%21 = call i32 @fatal(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0))
br label %22
22: ; preds = %20, %15
ret void
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@.str = [2 x i8] c"\11\00"
@.str.1 = [43 x i8] c"Unable to establish connection with device\00"
define void @establish_connection(i32 %0) {
%2 = alloca i64
%3 = bitcast i64* %2 to %struct.timeval*
%4 = bitcast i64* %2 to i8*
store i64 536870912000000, i64* %2
%5 = call i32 @xwrite(i32 %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 1)
%6 = call i64 @wait_for_data_in_sock(i32 %0, %struct.timeval* %3)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %22
8: ; preds = %1
store i64 536870912000000, i64* %2
%9 = call i32 @xwrite(i32 %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 1)
%10 = call i64 @wait_for_data_in_sock(i32 %0, %struct.timeval* %3)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %22
12: ; preds = %8
store i64 536870912000000, i64* %2
%13 = call i32 @xwrite(i32 %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 1)
%14 = call i64 @wait_for_data_in_sock(i32 %0, %struct.timeval* %3)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %22
16: ; preds = %12
store i64 536870912000000, i64* %2
%17 = call i32 @xwrite(i32 %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 1)
%18 = call i64 @wait_for_data_in_sock(i32 %0, %struct.timeval* %3)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = call i32 @fatal(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0))
br label %22
22: ; preds = %1, %8, %12, %16, %20
ret void
}