#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*/ exit(int);
scalar_t__ feof(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ malloc(int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(char *, char *);
char *bde_fgets(FILE *f) {
  char *str;
  int ch;
  char buf[1000];
  int r_len;
  ch = fgetc(f);
  if (0 != ch) {
    fprintf(stderr, "Error in bde_fgets(): leading %i expected, %i found\n", 0,
            ch);
    exit(1);
  }
  r_len = 0;
  do {
    ch = fgetc(f);
    buf[r_len] = (char)ch;
    r_len++;
    if (1000 == r_len) {
      fprintf(stderr,
              "Error in bde_fgets(): buffer overflow in string parser\n");
      exit(1);
    }
    if (feof(f)) {
      fprintf(
          stderr,
          "Error in bde_fgets(): unexpected end of file in string parser\n");
      exit(1);
    }
  } while ((0 != ch));
  str = (char *)malloc(r_len);
  if (!str) {
    fprintf(stderr,
            "Error in bde_fgets(): failed to allocate %i bytes of memory for "
            "return string\n",
            r_len);
    exit(1);
  }
  (strcpy(str, buf));
  return str;
}


x86 assembly (clang, -O0):
<bde_fgets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x410,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %eax,-0x14(%rbp)
xor    %eax,%eax
cmp    -0x14(%rbp),%eax
je     11ac <bde_fgets+0x4c>
mov    0x2e53(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%ecx
lea    0xe67(%rip),%rsi
xor    %edx,%edx
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
movl   $0x0,-0x404(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %al,%cl
movslq -0x404(%rbp),%rax
mov    %cl,-0x400(%rbp,%rax,1)
mov    -0x404(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x404(%rbp)
mov    $0x3e8,%eax
cmp    -0x404(%rbp),%eax
jne    1216 <bde_fgets+0xb6>
mov    0x2de4(%rip),%rax
mov    (%rax),%edi
lea    0xe30(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x8(%rbp),%rdi
call   1060 <feof@plt>
cmp    $0x0,%rax
je     124a <bde_fgets+0xea>
mov    0x2db0(%rip),%rax
mov    (%rax),%edi
lea    0xe34(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    124f <bde_fgets+0xef>
xor    %eax,%eax
cmp    -0x14(%rbp),%eax
jne    11b6 <bde_fgets+0x56>
mov    -0x404(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    129b <bde_fgets+0x13b>
mov    0x2d65(%rip),%rax
mov    (%rax),%edi
mov    -0x404(%rbp),%edx
lea    0xe22(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rdi
lea    -0x400(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x10(%rbp),%rax
add    $0x410,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bde_fgets>:
push   %rbp
push   %r14
push   %rbx
sub    $0x3f0,%rsp
mov    %rdi,%r14
call   1040 <fgetc@plt>
test   %eax,%eax
jne    1208 <bde_fgets+0xa8>
xor    %ebx,%ebx
nopl   (%rax)
mov    %r14,%rdi
call   1040 <fgetc@plt>
mov    %eax,%ebp
mov    %bpl,(%rsp,%rbx,1)
cmp    $0x3e7,%rbx
je     11d5 <bde_fgets+0x75>
mov    %r14,%rdi
call   1060 <feof@plt>
test   %rax,%rax
jne    11e7 <bde_fgets+0x87>
add    $0x1,%rbx
test   %ebp,%ebp
jne    1180 <bde_fgets+0x20>
mov    %ebx,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     122d <bde_fgets+0xcd>
mov    %rax,%rbp
mov    %rsp,%rsi
mov    %rax,%rdi
call   1030 <strcpy@plt>
mov    %rbp,%rax
add    $0x3f0,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
lea    0xe50(%rip),%rsi
jmp    11f7 <bde_fgets+0x97>
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
lea    0xe76(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2dd1(%rip),%rcx
mov    (%rcx),%edi
lea    0xde8(%rip),%rsi
xor    %edx,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2dac(%rip),%rax
mov    (%rax),%edi
lea    0xe6f(%rip),%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [53 x i8] c"Error in bde_fgets(): leading %i expected, %i found\0A\00"
@.str.1 = [56 x i8] c"Error in bde_fgets(): buffer overflow in string parser\0A\00"
@.str.2 = [63 x i8] c"Error in bde_fgets(): unexpected end of file in string parser\0A\00"
@.str.3 = [79 x i8] c"Error in bde_fgets(): failed to allocate %i bytes of memory for return string\0A\00"
define i8* @bde_fgets(i32* %0) {
%2 = alloca i32*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca [1000 x i8]
%6 = alloca i32
store i32* %0, i32** %2
%7 = load i32*, i32** %2
%8 = call i32 @fgetc(i32* %7)
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = icmp ne i32 0, %9
br i1 %10, label %11, label %16
11: ; preds = %1
%12 = load i32, i32* @stderr
%13 = load i32, i32* %4
%14 = call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str, i64 0, i64 0), i32 0, i32 %13)
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %1
store i32 0, i32* %6
br label %17
17: ; preds = %42, %16
%18 = load i32*, i32** %2
%19 = call i32 @fgetc(i32* %18)
store i32 %19, i32* %4
%20 = load i32, i32* %4
%21 = trunc i32 %20 to i8
%22 = load i32, i32* %6
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds [1000 x i8], [1000 x i8]* %5, i64 0, i64 %23
store i8 %21, i8* %24
%25 = load i32, i32* %6
%26 = add nsw i32 %25, 1
store i32 %26, i32* %6
%27 = load i32, i32* %6
%28 = icmp eq i32 1000, %27
br i1 %28, label %29, label %33
29: ; preds = %17
%30 = load i32, i32* @stderr
%31 = call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.1, i64 0, i64 0))
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %17
%34 = load i32*, i32** %2
%35 = call i64 @feof(i32* %34)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %41
37: ; preds = %33
%38 = load i32, i32* @stderr
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.2, i64 0, i64 0))
%40 = call i32 @exit(i32 1)
unreachable
41: ; preds = %33
br label %42
42: ; preds = %41
%43 = load i32, i32* %4
%44 = icmp ne i32 0, %43
br i1 %44, label %17, label %45
45: ; preds = %42
%46 = load i32, i32* %6
%47 = call i64 @malloc(i32 %46)
%48 = inttoptr i64 %47 to i8*
store i8* %48, i8** %3
%49 = load i8*, i8** %3
%50 = icmp ne i8* %49, null
br i1 %50, label %56, label %51
51: ; preds = %45
%52 = load i32, i32* @stderr
%53 = load i32, i32* %6
%54 = call i32 (i32, i8*, ...) @fprintf(i32 %52, i8* getelementptr inbounds ([79 x i8], [79 x i8]* @.str.3, i64 0, i64 0), i32 %53)
%55 = call i32 @exit(i32 1)
unreachable
56: ; preds = %45
%57 = load i8*, i8** %3
%58 = getelementptr inbounds [1000 x i8], [1000 x i8]* %5, i64 0, i64 0
%59 = call i32 @strcpy(i8* %57, i8* %58)
%60 = load i8*, i8** %3
ret i8* %60
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [53 x i8] c"Error in bde_fgets(): leading %i expected, %i found\0A\00"
@.str.1 = [56 x i8] c"Error in bde_fgets(): buffer overflow in string parser\0A\00"
@.str.2 = [63 x i8] c"Error in bde_fgets(): unexpected end of file in string parser\0A\00"
@.str.3 = [79 x i8] c"Error in bde_fgets(): failed to allocate %i bytes of memory for return string\0A\00"
define i8* @bde_fgets(i32* nocapture %0) {
%2 = alloca [1000 x i8]
%3 = getelementptr inbounds [1000 x i8], [1000 x i8]* %2, i64 0, i64 0
%4 = tail call i32 @fgetc(i32* %0)
%5 = icmp eq i32 %4, 0
br i1 %5, label %10, label %6
6: ; preds = %1
%7 = load i32, i32* @stderr
%8 = tail call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str, i64 0, i64 0), i32 0, i32 %4)
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %1, %28
%11 = phi i64 [ %15, %28 ], [ 0, %1 ]
%12 = tail call i32 @fgetc(i32* %0)
%13 = trunc i32 %12 to i8
%14 = getelementptr inbounds [1000 x i8], [1000 x i8]* %2, i64 0, i64 %11
store i8 %13, i8* %14
%15 = add nuw nsw i64 %11, 1
%16 = icmp eq i64 %15, 1000
br i1 %16, label %17, label %21
17: ; preds = %10
%18 = load i32, i32* @stderr
%19 = tail call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.1, i64 0, i64 0))
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %10
%22 = tail call i64 @feof(i32* %0)
%23 = icmp eq i64 %22, 0
br i1 %23, label %28, label %24
24: ; preds = %21
%25 = load i32, i32* @stderr
%26 = tail call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.2, i64 0, i64 0))
%27 = tail call i32 @exit(i32 1)
unreachable
28: ; preds = %21
%29 = icmp eq i32 %12, 0
br i1 %29, label %30, label %10
30: ; preds = %28
%31 = trunc i64 %15 to i32
%32 = tail call i64 @malloc(i32 %31)
%33 = inttoptr i64 %32 to i8*
%34 = icmp eq i64 %32, 0
br i1 %34, label %35, label %39
35: ; preds = %30
%36 = load i32, i32* @stderr
%37 = tail call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([79 x i8], [79 x i8]* @.str.3, i64 0, i64 0), i32 %31)
%38 = tail call i32 @exit(i32 1)
unreachable
39: ; preds = %30
%40 = call i32 @strcpy(i8* %33, i8* %3)
ret i8* %33
}