#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 int ssize_t;
typedef int /*<<< orphan*/ count;

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(unsigned short);
unsigned short ntohs(unsigned short);
int read(int, ...);
ssize_t read_string(int fd, char **ps) {
  unsigned short count;
  char *s;
  ssize_t n;
  while ((n = read(fd, &count, sizeof(count))) == -1 && errno == EINTR)
    ;
  if (n < (ssize_t)sizeof(count))
    return -1;
  count = ntohs(count);
  s = (char *)malloc(count + 1);
  if (s == NULL)
    return -1;
  while ((n = read(fd, s, count)) == -1 && errno == EINTR)
    ;
  if (n < (ssize_t)count) {
    free(s);
    return -1;
  }
  s[count] = '\0';
  *ps = s;
  return n;
}


x86 assembly (clang, -O0):
<read_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x12(%rbp),%rsi
mov    $0x2,%edx
mov    $0x0,%al
call   1040 <read@plt>
mov    %eax,%ecx
mov    %ecx,-0x24(%rbp)
xor    %eax,%eax
cmp    $0xffffffff,%ecx
mov    %al,-0x25(%rbp)
jne    118f <read_string+0x4f>
mov    0x2e64(%rip),%rax
mov    (%rax),%rax
mov    0x2e52(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x25(%rbp)
mov    -0x25(%rbp),%al
test   $0x1,%al
jne    119f <read_string+0x5f>
jmp    11a4 <read_string+0x64>
jmp    114f <read_string+0xf>
cmpl   $0x2,-0x24(%rbp)
jge    11ba <read_string+0x7a>
movl   $0xffffffff,-0x4(%rbp)
jmp    128b <read_string+0x14b>
movzwl -0x12(%rbp),%edi
call   1060 <ntohs@plt>
mov    %ax,-0x12(%rbp)
movzwl -0x12(%rbp),%eax
add    $0x1,%eax
movzwl %ax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11f1 <read_string+0xb1>
movl   $0xffffffff,-0x4(%rbp)
jmp    128b <read_string+0x14b>
jmp    11f6 <read_string+0xb6>
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%rsi
movzwl -0x12(%rbp),%edx
mov    $0x0,%al
call   1040 <read@plt>
mov    %eax,%ecx
mov    %ecx,-0x24(%rbp)
xor    %eax,%eax
cmp    $0xffffffff,%ecx
mov    %al,-0x26(%rbp)
jne    1235 <read_string+0xf5>
mov    0x2dbe(%rip),%rax
mov    (%rax),%rax
mov    0x2dac(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x26(%rbp)
mov    -0x26(%rbp),%al
test   $0x1,%al
jne    1245 <read_string+0x105>
jmp    124a <read_string+0x10a>
jmp    11f6 <read_string+0xb6>
mov    -0x24(%rbp),%eax
movzwl -0x12(%rbp),%ecx
cmp    %ecx,%eax
jge    126e <read_string+0x12e>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    128b <read_string+0x14b>
mov    -0x20(%rbp),%rax
movzwl -0x12(%rbp),%ecx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_string>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %edi,%ebp
lea    0x6(%rsp),%rbx
mov    0x2e84(%rip),%r12
mov    0x2e75(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    $0x2,%edx
mov    %ebp,%edi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <read@plt>
cmp    $0xffffffff,%eax
jne    1190 <read_string+0x50>
mov    0x0(%r13),%rcx
cmp    %rcx,(%r12)
je     1170 <read_string+0x30>
mov    $0xffffffff,%r15d
cmp    $0x2,%eax
jl     11fd <read_string+0xbd>
movzwl 0x6(%rsp),%edi
call   1060 <ntohs@plt>
mov    %ax,0x6(%rsp)
add    $0x1,%eax
movzwl %ax,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     11fd <read_string+0xbd>
mov    %rax,%rbx
nopl   (%rax)
movzwl 0x6(%rsp),%edx
mov    %ebp,%edi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <read@plt>
cmp    $0xffffffff,%eax
jne    11e0 <read_string+0xa0>
mov    0x0(%r13),%rcx
cmp    %rcx,(%r12)
je     11c0 <read_string+0x80>
movzwl 0x6(%rsp),%ecx
cmp    %ecx,%eax
jge    11f3 <read_string+0xb3>
mov    %rbx,%rdi
call   1030 <free@plt>
jmp    11fd <read_string+0xbd>
movb   $0x0,(%rbx,%rcx,1)
mov    %rbx,(%r14)
mov    %eax,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
define i32 @read_string(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i16
%7 = alloca i8*
%8 = alloca i32
store i32 %0, i32* %4
store i8** %1, i8*** %5
br label %9
9: ; preds = %19, %2
%10 = load i32, i32* %4
%11 = call i32 (i32, ...) @read(i32 %10, i16* %6, i64 2)
store i32 %11, i32* %8
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %17
13: ; preds = %9
%14 = load i64, i64* @errno
%15 = load i64, i64* @EINTR
%16 = icmp eq i64 %14, %15
br label %17
17: ; preds = %13, %9
%18 = phi i1 [ false, %9 ], [ %16, %13 ]
br i1 %18, label %19, label %20
19: ; preds = %17
br label %9
20: ; preds = %17
%21 = load i32, i32* %8
%22 = icmp slt i32 %21, 2
br i1 %22, label %23, label %24
23: ; preds = %20
store i32 -1, i32* %3
br label %67
24: ; preds = %20
%25 = load i16, i16* %6
%26 = call zeroext i16 @ntohs(i16 zeroext %25)
store i16 %26, i16* %6
%27 = load i16, i16* %6
%28 = zext i16 %27 to i32
%29 = add nsw i32 %28, 1
%30 = trunc i32 %29 to i16
%31 = call i64 @malloc(i16 zeroext %30)
%32 = inttoptr i64 %31 to i8*
store i8* %32, i8** %7
%33 = load i8*, i8** %7
%34 = icmp eq i8* %33, null
br i1 %34, label %35, label %36
35: ; preds = %24
store i32 -1, i32* %3
br label %67
36: ; preds = %24
br label %37
37: ; preds = %50, %36
%38 = load i32, i32* %4
%39 = load i8*, i8** %7
%40 = load i16, i16* %6
%41 = zext i16 %40 to i32
%42 = call i32 (i32, ...) @read(i32 %38, i8* %39, i32 %41)
store i32 %42, i32* %8
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %48
44: ; preds = %37
%45 = load i64, i64* @errno
%46 = load i64, i64* @EINTR
%47 = icmp eq i64 %45, %46
br label %48
48: ; preds = %44, %37
%49 = phi i1 [ false, %37 ], [ %47, %44 ]
br i1 %49, label %50, label %51
50: ; preds = %48
br label %37
51: ; preds = %48
%52 = load i32, i32* %8
%53 = load i16, i16* %6
%54 = zext i16 %53 to i32
%55 = icmp slt i32 %52, %54
br i1 %55, label %56, label %59
56: ; preds = %51
%57 = load i8*, i8** %7
%58 = call i32 @free(i8* %57)
store i32 -1, i32* %3
br label %67
59: ; preds = %51
%60 = load i8*, i8** %7
%61 = load i16, i16* %6
%62 = zext i16 %61 to i64
%63 = getelementptr inbounds i8, i8* %60, i64 %62
store i8 0, i8* %63
%64 = load i8*, i8** %7
%65 = load i8**, i8*** %5
store i8* %64, i8** %65
%66 = load i32, i32* %8
store i32 %66, i32* %3
br label %67
67: ; preds = %59, %56, %35, %23
%68 = load i32, i32* %3
ret i32 %68
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
define i32 @read_string(i32 %0, i8** nocapture writeonly %1) {
%3 = alloca i16
%4 = bitcast i16* %3 to i8*
br label %5
5: ; preds = %5, %2
%6 = call i32 (i32, ...) @read(i32 %0, i16* %3, i64 2)
%7 = icmp eq i32 %6, -1
%8 = load i64, i64* @errno
%9 = load i64, i64* @EINTR
%10 = icmp eq i64 %8, %9
%11 = select i1 %7, i1 %10, i1 false
br i1 %11, label %5, label %12
12: ; preds = %5
%13 = icmp slt i32 %6, 2
br i1 %13, label %39, label %14
14: ; preds = %12
%15 = load i16, i16* %3
%16 = call zeroext i16 @ntohs(i16 zeroext %15)
store i16 %16, i16* %3
%17 = add i16 %16, 1
%18 = call i64 @malloc(i16 zeroext %17)
%19 = inttoptr i64 %18 to i8*
%20 = icmp eq i64 %18, 0
br i1 %20, label %39, label %21
21: ; preds = %14, %21
%22 = load i16, i16* %3
%23 = zext i16 %22 to i32
%24 = call i32 (i32, ...) @read(i32 %0, i8* %19, i32 %23)
%25 = icmp eq i32 %24, -1
%26 = load i64, i64* @errno
%27 = load i64, i64* @EINTR
%28 = icmp eq i64 %26, %27
%29 = select i1 %25, i1 %28, i1 false
br i1 %29, label %21, label %30
30: ; preds = %21
%31 = load i16, i16* %3
%32 = zext i16 %31 to i32
%33 = icmp slt i32 %24, %32
br i1 %33, label %34, label %36
34: ; preds = %30
%35 = call i32 @free(i8* %19)
br label %39
36: ; preds = %30
%37 = zext i16 %31 to i64
%38 = getelementptr inbounds i8, i8* %19, i64 %37
store i8 0, i8* %38
store i8* %19, i8** %1
br label %39
39: ; preds = %14, %12, %36, %34
%40 = phi i32 [ -1, %34 ], [ %24, %36 ], [ -1, %12 ], [ -1, %14 ]
ret i32 %40
}