#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];
  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)
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    0x2e5a(%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    11b1 <waitforack+0x61>
movl   $0x1,-0x4(%rbp)
jmp    11ef <waitforack+0x9f>
mov    -0x8(%rbp),%edi
lea    -0xe(%rbp),%rsi
mov    $0x6,%edx
call   1030 <read@plt>
lea    -0xe(%rbp),%rsi
lea    0xe33(%rip),%rdi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
je     11e8 <waitforack+0x98>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ef <waitforack+0x9f>
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
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1050 <FD_ZERO@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1070 <FD_SET@plt>
movl   $0xa,0x10(%rsp)
movq   $0x0,0x18(%rsp)
mov    0x2e59(%rip),%rax
mov    (%rax),%edi
lea    0x10(%rsp),%r8
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1060 <select@plt>
test   %eax,%eax
je     11c8 <waitforack+0x78>
lea    0x22(%rsp),%rbx
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x6,%edx
call   1030 <read@plt>
lea    0xe47(%rip),%rdi
mov    %rbx,%rsi
call   1040 <strcmp@plt>
neg    %rax
sbb    %eax,%eax
jmp    11cd <waitforack+0x7d>
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 = call i32 @FD_ZERO(i32* %5)
%8 = load i32, i32* %3
%9 = call i32 @FD_SET(i32 %8, i32* %5)
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 0
store i32 10, i32* %10
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 1
store i64 0, i64* %11
%12 = load i32, i32* @FD_SETSIZE
%13 = call i32 @select(i32 %12, i32* %5, i32* null, i32* null, %struct.timeval* %6)
%14 = icmp ne i32 %13, 0
br i1 %14, label %16, label %15
15: ; preds = %1
store i32 1, i32* %2
br label %25
16: ; preds = %1
%17 = load i32, i32* %3
%18 = getelementptr inbounds [6 x i8], [6 x i8]* %4, i64 0, i64 0
%19 = call i32 @read(i32 %17, i8* %18, i32 6)
%20 = getelementptr inbounds [6 x i8], [6 x i8]* %4, i64 0, i64 0
%21 = call i64 @strcmp(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %20)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %16
store i32 -1, i32* %2
br label %25
24: ; preds = %16
store i32 0, i32* %2
br label %25
25: ; preds = %24, %23, %15
%26 = load i32, i32* %2
ret i32 %26
}

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 = bitcast i32* %3 to i8*
%7 = bitcast %struct.timeval* %4 to i8*
%8 = call i32 @FD_ZERO(i32* %3)
%9 = call i32 @FD_SET(i32 %0, i32* %3)
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
store i32 10, i32* %10
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
store i64 0, i64* %11
%12 = load i32, i32* @FD_SETSIZE
%13 = call i32 @select(i32 %12, i32* %3, i32* null, i32* null, %struct.timeval* %4)
%14 = icmp eq i32 %13, 0
br i1 %14, label %20, label %15
15: ; preds = %1
%16 = call i32 @read(i32 %0, i8* %5, i32 6)
%17 = call i64 @strcmp(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %5)
%18 = icmp ne i64 %17, 0
%19 = sext i1 %18 to i32
br label %20
20: ; preds = %15, %1
%21 = phi i32 [ 1, %1 ], [ %19, %15 ]
ret i32 %21
}