#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 */
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
char *malloc(int);
int read_n(int, ...);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int read_string(int fd, char **bufp) {
  int len = 0;
  int rc;
  *bufp = NULL;
  if ((rc = read_n(fd, &len, sizeof(int))) < 0) {
    fprintf(stderr, "systemsafe: read_string: %s\n", strerror(errno));
    return (-1);
  }
  if (rc == 0)
    return (0);
  if ((*bufp = malloc(len + 1)) == NULL) {
    fprintf(stderr, "systemsafe: read_string: malloc (%d): %s\n", len,
            strerror(errno));
    return (-1);
  }
  if ((rc = read_n(fd, *bufp, len)) < 0) {
    fprintf(stderr, "systemsafe: read_string: %s\n", strerror(errno));
    return (-1);
  }
  if (rc == 0)
    return (0);
  (*bufp)[len] = '\0';
  return (len);
}


x86 assembly (clang, -O0):
<read_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x8(%rbp),%edi
lea    -0x14(%rbp),%rsi
mov    $0x4,%edx
mov    $0x0,%al
call   1030 <read_n@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    11ba <read_string+0x7a>
mov    0x2e59(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x1c(%rbp),%edi
mov    %rax,%rdx
lea    0xe59(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b4 <read_string+0x174>
cmpl   $0x0,-0x18(%rbp)
jne    11d0 <read_string+0x90>
movl   $0x0,-0x4(%rbp)
jmp    12b4 <read_string+0x174>
mov    -0x14(%rbp),%edi
add    $0x1,%edi
call   1050 <malloc@plt>
mov    -0x10(%rbp),%rcx
mov    %rax,(%rcx)
cmp    $0x0,%rax
jne    122f <read_string+0xef>
mov    0x2ded(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%edx
mov    %rax,%rcx
lea    0xe01(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b4 <read_string+0x174>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
mov    -0x14(%rbp),%edx
mov    $0x0,%al
call   1030 <read_n@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    1289 <read_string+0x149>
mov    0x2d8a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2d76(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x28(%rbp),%edi
mov    %rax,%rdx
lea    0xd8a(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b4 <read_string+0x174>
cmpl   $0x0,-0x18(%rbp)
jne    129f <read_string+0x15f>
movl   $0x0,-0x4(%rbp)
jmp    12b4 <read_string+0x174>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x14(%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   %rbx
push   %rax
mov    %rsi,%r14
mov    %edi,%ebx
movl   $0x0,0x4(%rsp)
movq   $0x0,(%rsi)
xor    %ebp,%ebp
lea    0x4(%rsp),%rsi
mov    $0x4,%edx
xor    %eax,%eax
call   1030 <read_n@plt>
test   %eax,%eax
js     11b7 <read_string+0x77>
je     1215 <read_string+0xd5>
mov    0x4(%rsp),%r15d
lea    0x1(%r15),%edi
call   1050 <malloc@plt>
mov    %rax,(%r14)
test   %rax,%rax
je     11e3 <read_string+0xa3>
xor    %ebp,%ebp
mov    %ebx,%edi
mov    %rax,%rsi
mov    %r15d,%edx
xor    %eax,%eax
call   1030 <read_n@plt>
test   %eax,%eax
js     11b7 <read_string+0x77>
je     1215 <read_string+0xd5>
mov    (%r14),%rax
movslq 0x4(%rsp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    0x4(%rsp),%ebp
jmp    1215 <read_string+0xd5>
mov    0x2e22(%rip),%rax
mov    (%rax),%ebx
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe2b(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
jmp    1210 <read_string+0xd0>
mov    0x2df6(%rip),%rax
mov    (%rax),%ebx
mov    0x2de5(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe1c(%rip),%rsi
mov    %ebx,%edi
mov    %r15d,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [29 x i8] c"systemsafe: read_string: %s\0A\00"
@errno = global i32 0
@.str.1 = [42 x i8] c"systemsafe: read_string: malloc (%d): %s\0A\00"
define i32 @read_string(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
%8 = load i8**, i8*** %5
store i8* null, i8** %8
%9 = load i32, i32* %4
%10 = call i32 (i32, ...) @read_n(i32 %9, i32* %6, i64 4)
store i32 %10, i32* %7
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %17
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = load i32, i32* @errno
%15 = call i8* @strerror(i32 %14)
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %15)
store i32 -1, i32* %3
br label %56
17: ; preds = %2
%18 = load i32, i32* %7
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %17
store i32 0, i32* %3
br label %56
21: ; preds = %17
%22 = load i32, i32* %6
%23 = add nsw i32 %22, 1
%24 = call i8* @malloc(i32 %23)
%25 = load i8**, i8*** %5
store i8* %24, i8** %25
%26 = icmp eq i8* %24, null
br i1 %26, label %27, label %33
27: ; preds = %21
%28 = load i32, i32* @stderr
%29 = load i32, i32* %6
%30 = load i32, i32* @errno
%31 = call i8* @strerror(i32 %30)
%32 = call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i32 %29, i8* %31)
store i32 -1, i32* %3
br label %56
33: ; preds = %21
%34 = load i32, i32* %4
%35 = load i8**, i8*** %5
%36 = load i8*, i8** %35
%37 = load i32, i32* %6
%38 = call i32 (i32, ...) @read_n(i32 %34, i8* %36, i32 %37)
store i32 %38, i32* %7
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %45
40: ; preds = %33
%41 = load i32, i32* @stderr
%42 = load i32, i32* @errno
%43 = call i8* @strerror(i32 %42)
%44 = call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %43)
store i32 -1, i32* %3
br label %56
45: ; preds = %33
%46 = load i32, i32* %7
%47 = icmp eq i32 %46, 0
br i1 %47, label %48, label %49
48: ; preds = %45
store i32 0, i32* %3
br label %56
49: ; preds = %45
%50 = load i8**, i8*** %5
%51 = load i8*, i8** %50
%52 = load i32, i32* %6
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i8, i8* %51, i64 %53
store i8 0, i8* %54
%55 = load i32, i32* %6
store i32 %55, i32* %3
br label %56
56: ; preds = %49, %48, %40, %27, %20, %12
%57 = load i32, i32* %3
ret i32 %57
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [29 x i8] c"systemsafe: read_string: %s\0A\00"
@errno = global i32 0
@.str.1 = [42 x i8] c"systemsafe: read_string: malloc (%d): %s\0A\00"
define i32 @read_string(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
store i32 0, i32* %3
store i8* null, i8** %1
%5 = call i32 (i32, ...) @read_n(i32 %0, i32* %3, i64 4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %12
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = load i32, i32* @errno
%10 = call i8* @strerror(i32 %9)
%11 = call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %10)
br label %40
12: ; preds = %2
%13 = icmp eq i32 %5, 0
br i1 %13, label %40, label %14
14: ; preds = %12
%15 = load i32, i32* %3
%16 = add nsw i32 %15, 1
%17 = call i8* @malloc(i32 %16)
store i8* %17, i8** %1
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %24
19: ; preds = %14
%20 = load i32, i32* @stderr
%21 = load i32, i32* @errno
%22 = call i8* @strerror(i32 %21)
%23 = call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i32 %15, i8* %22)
br label %40
24: ; preds = %14
%25 = call i32 (i32, ...) @read_n(i32 %0, i8* %17, i32 %15)
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %32
27: ; preds = %24
%28 = load i32, i32* @stderr
%29 = load i32, i32* @errno
%30 = call i8* @strerror(i32 %29)
%31 = call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %30)
br label %40
32: ; preds = %24
%33 = icmp eq i32 %25, 0
br i1 %33, label %40, label %34
34: ; preds = %32
%35 = load i8*, i8** %1
%36 = load i32, i32* %3
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %35, i64 %37
store i8 0, i8* %38
%39 = load i32, i32* %3
br label %40
40: ; preds = %32, %12, %34, %27, %19, %7
%41 = phi i32 [ -1, %7 ], [ -1, %19 ], [ -1, %27 ], [ %39, %34 ], [ 0, %12 ], [ 0, %32 ]
ret i32 %41
}