#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 */

/* Variables and functions */
scalar_t__ qSocketWaitReadable(int, int);
int read(int, char *, int);
int qSocketRead(char *binary, int size, int sockfd, int timeoutsec) {
  char *ptr;
  int readcnt;
  if (qSocketWaitReadable(sockfd, timeoutsec) == 0)
    return -1;
  for (ptr = binary, readcnt = 0; size > 0; size--, ptr++, readcnt++) {
    if (read(sockfd, ptr, 1) != 1)
      break;
  }
  return readcnt;
}


x86 assembly (clang, -O0):
<qSocketRead>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x18(%rbp),%edi
mov    -0x1c(%rbp),%esi
call   1030 <qSocketWaitReadable@plt>
cmp    $0x0,%rax
jne    1156 <qSocketRead+0x36>
movl   $0xffffffff,-0x4(%rbp)
jmp    11bc <qSocketRead+0x9c>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    11b6 <qSocketRead+0x96>
mov    -0x18(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0x1,%eax
je     118e <qSocketRead+0x6e>
jmp    11b6 <qSocketRead+0x96>
jmp    1193 <qSocketRead+0x73>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1165 <qSocketRead+0x45>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<qSocketRead>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %esi,%ebx
mov    %rdi,%r14
mov    %edx,%edi
mov    %ecx,%esi
call   1030 <qSocketWaitReadable@plt>
test   %rax,%rax
je     1170 <qSocketRead+0x50>
test   %ebx,%ebx
jle    1177 <qSocketRead+0x57>
mov    %ebx,%ebp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
lea    (%r14,%rbx,1),%rsi
mov    %r15d,%edi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0x1,%eax
jne    117b <qSocketRead+0x5b>
add    $0x1,%rbx
cmp    %ebx,%ebp
jne    1150 <qSocketRead+0x30>
jmp    117d <qSocketRead+0x5d>
mov    $0xffffffff,%ebp
jmp    117d <qSocketRead+0x5d>
xor    %ebp,%ebp
jmp    117d <qSocketRead+0x5d>
mov    %ebx,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @qSocketRead(i8* %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
store i8* %0, i8** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%12 = load i32, i32* %8
%13 = load i32, i32* %9
%14 = call i64 @qSocketWaitReadable(i32 %12, i32 %13)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %4
store i32 -1, i32* %5
br label %38
17: ; preds = %4
%18 = load i8*, i8** %6
store i8* %18, i8** %10
store i32 0, i32* %11
br label %19
19: ; preds = %29, %17
%20 = load i32, i32* %7
%21 = icmp sgt i32 %20, 0
br i1 %21, label %22, label %36
22: ; preds = %19
%23 = load i32, i32* %8
%24 = load i8*, i8** %10
%25 = call i32 @read(i32 %23, i8* %24, i32 1)
%26 = icmp ne i32 %25, 1
br i1 %26, label %27, label %28
27: ; preds = %22
br label %36
28: ; preds = %22
br label %29
29: ; preds = %28
%30 = load i32, i32* %7
%31 = add nsw i32 %30, -1
store i32 %31, i32* %7
%32 = load i8*, i8** %10
%33 = getelementptr inbounds i8, i8* %32, i32 1
store i8* %33, i8** %10
%34 = load i32, i32* %11
%35 = add nsw i32 %34, 1
store i32 %35, i32* %11
br label %19
36: ; preds = %27, %19
%37 = load i32, i32* %11
store i32 %37, i32* %5
br label %38
38: ; preds = %36, %16
%39 = load i32, i32* %5
ret i32 %39
}

LLVM IR (-O2):
define i32 @qSocketRead(i8* nocapture %0, i32 %1, i32 %2, i32 %3) {
%5 = tail call i64 @qSocketWaitReadable(i32 %2, i32 %3)
%6 = icmp eq i64 %5, 0
br i1 %6, label %18, label %7
7: ; preds = %4
%8 = icmp sgt i32 %1, 0
br i1 %8, label %9, label %18
9: ; preds = %7, %14
%10 = phi i32 [ %16, %14 ], [ 0, %7 ]
%11 = phi i8* [ %15, %14 ], [ %0, %7 ]
%12 = tail call i32 @read(i32 %2, i8* %11, i32 1)
%13 = icmp eq i32 %12, 1
br i1 %13, label %14, label %18
14: ; preds = %9
%15 = getelementptr inbounds i8, i8* %11, i64 1
%16 = add nuw nsw i32 %10, 1
%17 = icmp eq i32 %16, %1
br i1 %17, label %18, label %9
18: ; preds = %9, %14, %7, %4
%19 = phi i32 [ -1, %4 ], [ 0, %7 ], [ %10, %9 ], [ %1, %14 ]
ret i32 %19
}