#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 tv_sec;
  scalar_t__ tv_usec;
};
typedef int /*<<< orphan*/ fd_set;
typedef int /*<<< orphan*/ buffer;

/* Variables and functions */
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SETSIZE;
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ select(int /*<<< orphan*/, int /*<<< orphan*/ *,
                          int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                          struct timeval *);
scalar_t__ strcmp(char *, char *);
int waitforack(int sock) {
  char buffer[6];
  buffer[3] = '\0';
  fd_set set;
  struct timeval timeout;
  FD_ZERO(&set);
  FD_SET(sock, &set);
  timeout.tv_sec = 10;
  ;
  timeout.tv_usec = 0;
  if (!select(FD_SETSIZE, &set, NULL, NULL, &timeout))
    return (1);
  read(sock, buffer, sizeof(buffer));
  if (strcmp("ACK", buffer))
    return (-1);
  else {
    return (0);
  }
}


x86 assembly (clang, -O0):
<waitforack>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
movb   $0x0,-0xb(%rbp)
lea    -0x14(%rbp),%rdi
call   1050 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x14(%rbp),%rsi
call   1070 <FD_SET@plt>
movl   $0xa,-0x28(%rbp)
movq   $0x0,-0x20(%rbp)
mov    0x2e56(%rip),%rax
mov    (%rax),%edi
lea    -0x14(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x28(%rbp),%r8
mov    %rcx,%rdx
call   1060 <select@plt>
cmp    $0x0,%eax
jne    11b5 <waitforack+0x65>
movl   $0x1,-0x4(%rbp)
jmp    11f3 <waitforack+0xa3>
mov    -0x8(%rbp),%edi
lea    -0xe(%rbp),%rsi
mov    $0x6,%edx
call   1030 <read@plt>
lea    -0xe(%rbp),%rsi
lea    0xe2f(%rip),%rdi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
je     11ec <waitforack+0x9c>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f3 <waitforack+0xa3>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<waitforack>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    %edi,%ebp
movb   $0x0,0x11(%rsp)
lea    0x14(%rsp),%rbx
mov    %rbx,%rdi
call   1050 <FD_ZERO@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1070 <FD_SET@plt>
movl   $0xa,0x18(%rsp)
movq   $0x0,0x20(%rsp)
mov    0x2e54(%rip),%rax
mov    (%rax),%edi
lea    0x18(%rsp),%r8
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1060 <select@plt>
test   %eax,%eax
je     11cd <waitforack+0x7d>
lea    0xe(%rsp),%rbx
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x6,%edx
call   1030 <read@plt>
lea    0xe42(%rip),%rdi
mov    %rbx,%rsi
call   1040 <strcmp@plt>
neg    %rax
sbb    %eax,%eax
jmp    11d2 <waitforack+0x82>
mov    $0x1,%eax
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@FD_SETSIZE = global i32 0
@.str = [4 x i8] c"ACK\00"
define i32 @waitforack(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [6 x i8]
%5 = alloca i32
%6 = alloca %struct.timeval
store i32 %0, i32* %3
%7 = getelementptr inbounds [6 x i8], [6 x i8]* %4, i64 0, i64 3
store i8 0, i8* %7
%8 = call i32 @FD_ZERO(i32* %5)
%9 = load i32, i32* %3
%10 = call i32 @FD_SET(i32 %9, i32* %5)
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 0
store i32 10, i32* %11
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 1
store i64 0, i64* %12
%13 = load i32, i32* @FD_SETSIZE
%14 = call i32 @select(i32 %13, i32* %5, i32* null, i32* null, %struct.timeval* %6)
%15 = icmp ne i32 %14, 0
br i1 %15, label %17, label %16
16: ; preds = %1
store i32 1, i32* %2
br label %26
17: ; preds = %1
%18 = load i32, i32* %3
%19 = getelementptr inbounds [6 x i8], [6 x i8]* %4, i64 0, i64 0
%20 = call i32 @read(i32 %18, i8* %19, i32 6)
%21 = getelementptr inbounds [6 x i8], [6 x i8]* %4, i64 0, i64 0
%22 = call i64 @strcmp(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %21)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %17
store i32 -1, i32* %2
br label %26
25: ; preds = %17
store i32 0, i32* %2
br label %26
26: ; preds = %25, %24, %16
%27 = load i32, i32* %2
ret i32 %27
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@FD_SETSIZE = global i32 0
@.str = [4 x i8] c"ACK\00"
define i32 @waitforack(i32 %0) {
%2 = alloca [6 x i8]
%3 = alloca i32
%4 = alloca %struct.timeval
%5 = getelementptr inbounds [6 x i8], [6 x i8]* %2, i64 0, i64 0
%6 = getelementptr inbounds [6 x i8], [6 x i8]* %2, i64 0, i64 3
store i8 0, i8* %6
%7 = bitcast i32* %3 to i8*
%8 = bitcast %struct.timeval* %4 to i8*
%9 = call i32 @FD_ZERO(i32* %3)
%10 = call i32 @FD_SET(i32 %0, i32* %3)
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
store i32 10, i32* %11
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
store i64 0, i64* %12
%13 = load i32, i32* @FD_SETSIZE
%14 = call i32 @select(i32 %13, i32* %3, i32* null, i32* null, %struct.timeval* %4)
%15 = icmp eq i32 %14, 0
br i1 %15, label %21, label %16
16: ; preds = %1
%17 = call i32 @read(i32 %0, i8* %5, i32 6)
%18 = call i64 @strcmp(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %5)
%19 = icmp ne i64 %18, 0
%20 = sext i1 %19 to i32
br label %21
21: ; preds = %16, %1
%22 = phi i32 [ 1, %1 ], [ %20, %16 ]
ret i32 %22
}