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

/* Variables and functions */
int /*<<< orphan*/ assert(int /*<<< orphan*/);
int errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
char *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
char *strdup(char *);
size_t strlen(char *);
int read_file_string(const char *filename, char **value) {
  FILE *file;
  char line[1024], *p;
  int save_errno;
  size_t len;
  assert(value != NULL);
  assert(*value == NULL);
  file = fopen(filename, "r");
  if (file == NULL)
    return -1;
  p = fgets(line, sizeof(line), file);
  save_errno = errno;
  fclose(file);
  if (p == NULL) {
    errno = save_errno;
    return -1;
  }
  while (*p > 0 && *p <= 0x20)
    p++;
  len = strlen(p);
  while (p[len - 1] > 0 && p[len - 1] <= 0x20)
    len--;
  p[len] = 0;
  *value = strdup(p);
  if (*value == NULL)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<read_file_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x10(%rbp),%rdi
lea    0xe5b(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11c5 <read_file_string+0x65>
movl   $0xffffffff,-0x4(%rbp)
jmp    1351 <read_file_string+0x1f1>
lea    -0x420(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    $0x400,%esi
call   1050 <fgets@plt>
mov    %rax,-0x428(%rbp)
mov    0x2df8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x42c(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
cmpq   $0x0,-0x428(%rbp)
jne    1222 <read_file_string+0xc2>
mov    -0x42c(%rbp),%ecx
mov    0x2dcc(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1351 <read_file_string+0x1f1>
jmp    1227 <read_file_string+0xc7>
mov    -0x428(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x439(%rbp)
jle    1258 <read_file_string+0xf8>
mov    -0x428(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x20,%eax
setle  %al
mov    %al,-0x439(%rbp)
mov    -0x439(%rbp),%al
test   $0x1,%al
jne    126b <read_file_string+0x10b>
jmp    1282 <read_file_string+0x122>
mov    -0x428(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x428(%rbp)
jmp    1227 <read_file_string+0xc7>
mov    -0x428(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x438(%rbp)
mov    -0x428(%rbp),%rax
mov    -0x438(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x43a(%rbp)
jle    12de <read_file_string+0x17e>
mov    -0x428(%rbp),%rax
mov    -0x438(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x20,%eax
setle  %al
mov    %al,-0x43a(%rbp)
mov    -0x43a(%rbp),%al
test   $0x1,%al
jne    12f1 <read_file_string+0x191>
jmp    1308 <read_file_string+0x1a8>
mov    -0x438(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x438(%rbp)
jmp    1295 <read_file_string+0x135>
mov    -0x428(%rbp),%rax
mov    -0x438(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x428(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
jne    134a <read_file_string+0x1ea>
movl   $0xffffffff,-0x4(%rbp)
jmp    1351 <read_file_string+0x1f1>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file_string>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
xor    %edi,%edi
test   %rsi,%rsi
setne  %dil
call   1060 <assert@plt>
xor    %edi,%edi
cmpq   $0x0,(%r14)
sete   %dil
call   1060 <assert@plt>
lea    0xe65(%rip),%rsi
mov    %rbx,%rdi
call   1070 <fopen@plt>
mov    $0xffffffff,%r15d
test   %rax,%rax
je     122b <read_file_string+0xcb>
mov    %rax,%rbp
mov    %rsp,%rdi
mov    $0x400,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
mov    %rax,%rbx
mov    0x2e15(%rip),%r12
mov    (%r12),%r13d
mov    %rbp,%rdi
call   1030 <fclose@plt>
test   %rbx,%rbx
je     1227 <read_file_string+0xc7>
nopl   0x0(%rax)
movzbl (%rbx),%eax
add    $0xff,%al
add    $0x1,%rbx
cmp    $0x20,%al
jb     11e0 <read_file_string+0x80>
lea    -0x1(%rbx),%rbp
mov    %rbp,%rdi
call   1040 <strlen@plt>
add    %rbx,%rax
nopl   0x0(%rax)
movzbl -0x2(%rax),%ecx
add    $0xff,%cl
add    $0xffffffffffffffff,%rax
cmp    $0x20,%cl
jb     1200 <read_file_string+0xa0>
movb   $0x0,(%rax)
mov    %rbp,%rdi
call   1080 <strdup@plt>
mov    %rax,(%r14)
cmp    $0x1,%rax
sbb    %r15d,%r15d
jmp    122b <read_file_string+0xcb>
mov    %r13d,(%r12)
mov    %r15d,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@errno = global i32 0
define i32 @read_file_string(i8* %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca [1024 x i8]
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i64
store i8* %0, i8** %4
store i8** %1, i8*** %5
%11 = load i8**, i8*** %5
%12 = icmp ne i8** %11, null
%13 = zext i1 %12 to i32
%14 = call i32 @assert(i32 %13)
%15 = load i8**, i8*** %5
%16 = load i8*, i8** %15
%17 = icmp eq i8* %16, null
%18 = zext i1 %17 to i32
%19 = call i32 @assert(i32 %18)
%20 = load i8*, i8** %4
%21 = call i32* @fopen(i8* %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %21, i32** %6
%22 = load i32*, i32** %6
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %25
24: ; preds = %2
store i32 -1, i32* %3
br label %88
25: ; preds = %2
%26 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%27 = load i32*, i32** %6
%28 = call i8* @fgets(i8* %26, i32 1024, i32* %27)
store i8* %28, i8** %8
%29 = load i32, i32* @errno
store i32 %29, i32* %9
%30 = load i32*, i32** %6
%31 = call i32 @fclose(i32* %30)
%32 = load i8*, i8** %8
%33 = icmp eq i8* %32, null
br i1 %33, label %34, label %36
34: ; preds = %25
%35 = load i32, i32* %9
store i32 %35, i32* @errno
store i32 -1, i32* %3
br label %88
36: ; preds = %25
br label %37
37: ; preds = %49, %36
%38 = load i8*, i8** %8
%39 = load i8, i8* %38
%40 = sext i8 %39 to i32
%41 = icmp sgt i32 %40, 0
br i1 %41, label %42, label %47
42: ; preds = %37
%43 = load i8*, i8** %8
%44 = load i8, i8* %43
%45 = sext i8 %44 to i32
%46 = icmp sle i32 %45, 32
br label %47
47: ; preds = %42, %37
%48 = phi i1 [ false, %37 ], [ %46, %42 ]
br i1 %48, label %49, label %52
49: ; preds = %47
%50 = load i8*, i8** %8
%51 = getelementptr inbounds i8, i8* %50, i32 1
store i8* %51, i8** %8
br label %37
52: ; preds = %47
%53 = load i8*, i8** %8
%54 = call i64 @strlen(i8* %53)
store i64 %54, i64* %10
br label %55
55: ; preds = %73, %52
%56 = load i8*, i8** %8
%57 = load i64, i64* %10
%58 = sub i64 %57, 1
%59 = getelementptr inbounds i8, i8* %56, i64 %58
%60 = load i8, i8* %59
%61 = sext i8 %60 to i32
%62 = icmp sgt i32 %61, 0
br i1 %62, label %63, label %71
63: ; preds = %55
%64 = load i8*, i8** %8
%65 = load i64, i64* %10
%66 = sub i64 %65, 1
%67 = getelementptr inbounds i8, i8* %64, i64 %66
%68 = load i8, i8* %67
%69 = sext i8 %68 to i32
%70 = icmp sle i32 %69, 32
br label %71
71: ; preds = %63, %55
%72 = phi i1 [ false, %55 ], [ %70, %63 ]
br i1 %72, label %73, label %76
73: ; preds = %71
%74 = load i64, i64* %10
%75 = add i64 %74, -1
store i64 %75, i64* %10
br label %55
76: ; preds = %71
%77 = load i8*, i8** %8
%78 = load i64, i64* %10
%79 = getelementptr inbounds i8, i8* %77, i64 %78
store i8 0, i8* %79
%80 = load i8*, i8** %8
%81 = call i8* @strdup(i8* %80)
%82 = load i8**, i8*** %5
store i8* %81, i8** %82
%83 = load i8**, i8*** %5
%84 = load i8*, i8** %83
%85 = icmp eq i8* %84, null
br i1 %85, label %86, label %87
86: ; preds = %76
store i32 -1, i32* %3
br label %88
87: ; preds = %76
store i32 0, i32* %3
br label %88
88: ; preds = %87, %86, %34, %24
%89 = load i32, i32* %3
ret i32 %89
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@errno = global i32 0
define i32 @read_file_string(i8* nocapture %0, i8** %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = icmp ne i8** %1, null
%6 = zext i1 %5 to i32
%7 = tail call i32 @assert(i32 %6)
%8 = load i8*, i8** %1
%9 = icmp eq i8* %8, null
%10 = zext i1 %9 to i32
%11 = tail call i32 @assert(i32 %10)
%12 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %40, label %14
14: ; preds = %2
%15 = call i8* @fgets(i8* %4, i32 1024, i32* %12)
%16 = load i32, i32* @errno
%17 = call i32 @fclose(i32* %12)
%18 = icmp eq i8* %15, null
br i1 %18, label %19, label %20
19: ; preds = %14
store i32 %16, i32* @errno
br label %40
20: ; preds = %14, %20
%21 = phi i8* [ %25, %20 ], [ %15, %14 ]
%22 = load i8, i8* %21
%23 = add i8 %22, -1
%24 = icmp ult i8 %23, 32
%25 = getelementptr inbounds i8, i8* %21, i64 1
br i1 %24, label %20, label %26
26: ; preds = %20
%27 = call i64 @strlen(i8* %21)
br label %28
28: ; preds = %28, %26
%29 = phi i64 [ %27, %26 ], [ %30, %28 ]
%30 = add i64 %29, -1
%31 = getelementptr inbounds i8, i8* %21, i64 %30
%32 = load i8, i8* %31
%33 = add i8 %32, -1
%34 = icmp ult i8 %33, 32
br i1 %34, label %28, label %35
35: ; preds = %28
%36 = getelementptr inbounds i8, i8* %21, i64 %29
store i8 0, i8* %36
%37 = call i8* @strdup(i8* %21)
store i8* %37, i8** %1
%38 = icmp eq i8* %37, null
%39 = sext i1 %38 to i32
br label %40
40: ; preds = %35, %2, %19
%41 = phi i32 [ -1, %19 ], [ -1, %2 ], [ %39, %35 ]
ret i32 %41
}