#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 */
typedef char u_char;

/* Variables and functions */
int atomicio(int /*<<< orphan*/, int, char *, int);
int /*<<< orphan*/ read;
int _getstr(int sock, char *buf, int len) {
  u_char byte;
  int pos;
  for (pos = 0; pos < len; pos++) {
    if (atomicio(read, sock, &byte, 1) != 1)
      return -1;
    buf[pos] = byte;
    if (byte == 0)
      return pos;
  }
  return -1;
}


x86 assembly (clang, -O0):
<_getstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    119d <_getstr+0x8d>
mov    0x2eac(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%esi
lea    -0x15(%rbp),%rdx
mov    $0x1,%ecx
call   1030 <atomicio@plt>
cmp    $0x1,%eax
je     1164 <_getstr+0x54>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a4 <_getstr+0x94>
mov    -0x15(%rbp),%dl
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
movsbl -0x15(%rbp),%eax
cmp    $0x0,%eax
jne    118a <_getstr+0x7a>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11a4 <_getstr+0x94>
jmp    118f <_getstr+0x7f>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1129 <_getstr+0x19>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_getstr>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0xffffffff,%eax
test   %edx,%edx
jle    1176 <_getstr+0x66>
mov    %rsi,%r12
mov    %edi,%ebp
mov    %edx,%r13d
xor    %ebx,%ebx
mov    0x2eb3(%rip),%r14
lea    0x7(%rsp),%r15
nopw   0x0(%rax,%rax,1)
mov    (%r14),%edi
mov    %ebp,%esi
mov    %r15,%rdx
mov    $0x1,%ecx
call   1030 <atomicio@plt>
cmp    $0x1,%eax
jne    116d <_getstr+0x5d>
movzbl 0x7(%rsp),%eax
mov    %al,(%r12,%rbx,1)
test   %al,%al
je     1174 <_getstr+0x64>
add    $0x1,%rbx
cmp    %rbx,%r13
jne    1140 <_getstr+0x30>
mov    $0xffffffff,%eax
jmp    1176 <_getstr+0x66>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@read = global i32 0
define i32 @_getstr(i32 %0, i8* nocapture writeonly %1, i32 %2) {
%4 = alloca i8
%5 = icmp sgt i32 %2, 0
br i1 %5, label %6, label %22
6: ; preds = %3
%7 = zext i32 %2 to i64
br label %8
8: ; preds = %6, %17
%9 = phi i64 [ 0, %6 ], [ %18, %17 ]
%10 = load i32, i32* @read
%11 = call i32 @atomicio(i32 %10, i32 %0, i8* %4, i32 1)
%12 = icmp eq i32 %11, 1
br i1 %12, label %13, label %22
13: ; preds = %8
%14 = load i8, i8* %4
%15 = getelementptr inbounds i8, i8* %1, i64 %9
store i8 %14, i8* %15
%16 = icmp eq i8 %14, 0
br i1 %16, label %20, label %17
17: ; preds = %13
%18 = add nuw nsw i64 %9, 1
%19 = icmp eq i64 %18, %7
br i1 %19, label %22, label %8
20: ; preds = %13
%21 = trunc i64 %9 to i32
br label %22
22: ; preds = %17, %8, %20, %3
%23 = phi i32 [ -1, %3 ], [ %21, %20 ], [ -1, %8 ], [ -1, %17 ]
ret i32 %23
}