#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 /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fatal(char *);
char fgetc(int /*<<< orphan*/ *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int fscanf(int /*<<< orphan*/ *, char *, int *);
int /*<<< orphan*/ pfatal(char *);
char *xmalloc(int);
char *ns_readstr(FILE *file) {
  int len;
  if (fscanf(file, "%d:", &len) != 1)
    pfatal("cannot read from file");
  char *buf = xmalloc(len + 1);
  if (fread(buf, len, 1, file) != 1)
    pfatal("cannot read from file");
  if (fgetc(file) != ',')
    fatal("invalid format");
  buf[len] = '\0';
  fgetc(file);
  return buf;
}


x86 assembly (clang, -O0):
<ns_readstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe89(%rip),%rsi
lea    -0xc(%rbp),%rdx
call   1050 <fscanf@plt>
cmp    $0x1,%eax
je     1195 <ns_readstr+0x35>
lea    0xe74(%rip),%rdi
call   1040 <pfatal@plt>
mov    -0xc(%rbp),%edi
add    $0x1,%edi
call   1060 <xmalloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x8(%rbp),%rcx
mov    $0x1,%edx
call   1030 <fread@plt>
cmp    $0x1,%eax
je     11ce <ns_readstr+0x6e>
lea    0xe3b(%rip),%rdi
call   1040 <pfatal@plt>
mov    -0x8(%rbp),%rdi
call   1070 <fgetc@plt>
movsbl %al,%eax
cmp    $0x2c,%eax
je     11ef <ns_readstr+0x8f>
lea    0xe30(%rip),%rdi
call   1080 <fatal@plt>
mov    -0x18(%rbp),%rax
movslq -0xc(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x8(%rbp),%rdi
call   1070 <fgetc@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ns_readstr>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
lea    0xe92(%rip),%rsi
lea    0x4(%rsp),%rdx
call   1050 <fscanf@plt>
cmp    $0x1,%eax
je     1189 <ns_readstr+0x29>
lea    0xe80(%rip),%rdi
call   1040 <pfatal@plt>
mov    0x4(%rsp),%edi
add    $0x1,%edi
call   1060 <xmalloc@plt>
mov    %rax,%rbx
mov    0x4(%rsp),%esi
mov    %rax,%rdi
mov    $0x1,%edx
mov    %r14,%rcx
call   1030 <fread@plt>
cmp    $0x1,%eax
je     11bd <ns_readstr+0x5d>
lea    0xe4c(%rip),%rdi
call   1040 <pfatal@plt>
mov    %r14,%rdi
call   1070 <fgetc@plt>
cmp    $0x2c,%al
je     11d5 <ns_readstr+0x75>
lea    0xe4a(%rip),%rdi
call   1080 <fatal@plt>
movslq 0x4(%rsp),%rax
movb   $0x0,(%rbx,%rax,1)
mov    %r14,%rdi
call   1070 <fgetc@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [4 x i8] c"%d:\00"
@.str.1 = [22 x i8] c"cannot read from file\00"
@.str.2 = [15 x i8] c"invalid format\00"
define i8* @ns_readstr(i32* %0) {
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i8*
store i32* %0, i32** %2
%5 = load i32*, i32** %2
%6 = call i32 @fscanf(i32* %5, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32* %3)
%7 = icmp ne i32 %6, 1
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = call i32 @pfatal(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %10
10: ; preds = %8, %1
%11 = load i32, i32* %3
%12 = add nsw i32 %11, 1
%13 = call i8* @xmalloc(i32 %12)
store i8* %13, i8** %4
%14 = load i8*, i8** %4
%15 = load i32, i32* %3
%16 = load i32*, i32** %2
%17 = call i32 @fread(i8* %14, i32 %15, i32 1, i32* %16)
%18 = icmp ne i32 %17, 1
br i1 %18, label %19, label %21
19: ; preds = %10
%20 = call i32 @pfatal(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %21
21: ; preds = %19, %10
%22 = load i32*, i32** %2
%23 = call signext i8 @fgetc(i32* %22)
%24 = sext i8 %23 to i32
%25 = icmp ne i32 %24, 44
br i1 %25, label %26, label %28
26: ; preds = %21
%27 = call i32 @fatal(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %28
28: ; preds = %26, %21
%29 = load i8*, i8** %4
%30 = load i32, i32* %3
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %29, i64 %31
store i8 0, i8* %32
%33 = load i32*, i32** %2
%34 = call signext i8 @fgetc(i32* %33)
%35 = load i8*, i8** %4
ret i8* %35
}

LLVM IR (-O2):
@.str = [4 x i8] c"%d:\00"
@.str.1 = [22 x i8] c"cannot read from file\00"
@.str.2 = [15 x i8] c"invalid format\00"
define i8* @ns_readstr(i32* nocapture %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = call i32 @fscanf(i32* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32* %2)
%5 = icmp eq i32 %4, 1
br i1 %5, label %8, label %6
6: ; preds = %1
%7 = call i32 @pfatal(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %8
8: ; preds = %6, %1
%9 = load i32, i32* %2
%10 = add nsw i32 %9, 1
%11 = call i8* @xmalloc(i32 %10)
%12 = load i32, i32* %2
%13 = call i32 @fread(i8* %11, i32 %12, i32 1, i32* %0)
%14 = icmp eq i32 %13, 1
br i1 %14, label %17, label %15
15: ; preds = %8
%16 = call i32 @pfatal(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %17
17: ; preds = %15, %8
%18 = call signext i8 @fgetc(i32* %0)
%19 = icmp eq i8 %18, 44
br i1 %19, label %22, label %20
20: ; preds = %17
%21 = call i32 @fatal(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %22
22: ; preds = %20, %17
%23 = load i32, i32* %2
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %11, i64 %24
store i8 0, i8* %25
%26 = call signext i8 @fgetc(i32* %0)
ret i8* %11
}