#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 */
char EOF;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
char fgetc(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ open_cfgfile(int /*<<< orphan*/ **, int const);
int sanity_check(int const, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ write_line(int /*<<< orphan*/ **, char *);
void stdin_to_cfgfile(const int cfgfile) {
  char line[500];
  int linepos = 0;
  line[0] = '\0';
  FILE *cfg_fh;
  open_cfgfile(&cfg_fh, cfgfile);
  while (true) {
    char buffer = fgetc(stdin);
    if (buffer == '\r')
      continue;
    if (buffer == '\n' || buffer == EOF) {
      line[linepos] = '\0';
      int checkret;
      if ((checkret = sanity_check(cfgfile, line)) >= 0) {
        write_line(&cfg_fh, line);
      } else {
        fprintf(stderr, "Err %d, '%s' is not a valid config line. Ignoring.\n",
                checkret, line);
      }
      line[0] = '\0';
      if (buffer == EOF)
        break;
      linepos = 0;
    } else {
      line[linepos] = buffer;
      if (linepos >= 500) {
        fprintf(stderr, "Line '%s'.. too long, giving up.\n", line);
        exit(EXIT_FAILURE);
      }
      linepos++;
    }
  }
  fclose(cfg_fh);
}


x86 assembly (clang, -O0):
<stdin_to_cfgfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x204(%rbp)
movb   $0x0,-0x200(%rbp)
mov    -0x4(%rbp),%esi
lea    -0x210(%rbp),%rdi
call   1050 <open_cfgfile@plt>
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
call   1040 <fgetc@plt>
mov    %al,-0x211(%rbp)
movsbl -0x211(%rbp),%eax
cmp    $0xd,%eax
jne    11c7 <stdin_to_cfgfile+0x57>
jmp    119e <stdin_to_cfgfile+0x2e>
movsbl -0x211(%rbp),%eax
cmp    $0xa,%eax
je     11f0 <stdin_to_cfgfile+0x80>
movsbl -0x211(%rbp),%eax
mov    0x2dfb(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
jne    128d <stdin_to_cfgfile+0x11d>
movslq -0x204(%rbp),%rax
movb   $0x0,-0x200(%rbp,%rax,1)
mov    -0x4(%rbp),%edi
lea    -0x200(%rbp),%rsi
call   1070 <sanity_check@plt>
mov    %eax,-0x218(%rbp)
cmp    $0x0,%eax
jl     1235 <stdin_to_cfgfile+0xc5>
lea    -0x200(%rbp),%rsi
lea    -0x210(%rbp),%rdi
call   1090 <write_line@plt>
jmp    1259 <stdin_to_cfgfile+0xe9>
mov    0x2d94(%rip),%rax
mov    (%rax),%edi
mov    -0x218(%rbp),%edx
lea    -0x200(%rbp),%rcx
lea    0xdae(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movb   $0x0,-0x200(%rbp)
movsbl -0x211(%rbp),%eax
mov    0x2d72(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
jne    127e <stdin_to_cfgfile+0x10e>
jmp    12f1 <stdin_to_cfgfile+0x181>
movl   $0x0,-0x204(%rbp)
jmp    12ec <stdin_to_cfgfile+0x17c>
mov    -0x211(%rbp),%cl
movslq -0x204(%rbp),%rax
mov    %cl,-0x200(%rbp,%rax,1)
cmpl   $0x1f4,-0x204(%rbp)
jl     12dd <stdin_to_cfgfile+0x16d>
mov    0x2d18(%rip),%rax
mov    (%rax),%edi
lea    -0x200(%rbp),%rdx
lea    0xd6c(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2cf2(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x204(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x204(%rbp)
jmp    119e <stdin_to_cfgfile+0x2e>
mov    -0x210(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<stdin_to_cfgfile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x208,%rsp
mov    %edi,%r12d
movb   $0x0,0x10(%rsp)
lea    0x8(%rsp),%rdi
mov    %r12d,%esi
call   1050 <open_cfgfile@plt>
mov    0x2e4b(%rip),%rbp
lea    0x10(%rsp),%r13
mov    0x2e37(%rip),%r15
xor    %r14d,%r14d
jmp    11e0 <stdin_to_cfgfile+0x70>
mov    0x2e1b(%rip),%rcx
mov    (%rcx),%edi
lea    0xe42(%rip),%rsi
mov    %eax,%edx
mov    %r13,%rcx
xor    %eax,%eax
call   1060 <fprintf@plt>
movb   $0x0,0x10(%rsp)
mov    $0x0,%r14d
cmp    (%r15),%bl
je     122f <stdin_to_cfgfile+0xbf>
nopw   0x0(%rax,%rax,1)
mov    0x0(%rbp),%edi
call   1040 <fgetc@plt>
cmp    $0xd,%al
je     11e0 <stdin_to_cfgfile+0x70>
mov    %eax,%ebx
movslq %r14d,%rax
cmp    $0xa,%bl
je     11fb <stdin_to_cfgfile+0x8b>
cmp    (%r15),%bl
jne    121e <stdin_to_cfgfile+0xae>
movb   $0x0,0x10(%rsp,%rax,1)
mov    %r12d,%edi
mov    %r13,%rsi
call   1070 <sanity_check@plt>
test   %eax,%eax
js     11ae <stdin_to_cfgfile+0x3e>
lea    0x8(%rsp),%rdi
mov    %r13,%rsi
call   1090 <write_line@plt>
jmp    11ca <stdin_to_cfgfile+0x5a>
mov    %bl,0x10(%rsp,%rax,1)
cmp    $0x1f4,%eax
jge    124b <stdin_to_cfgfile+0xdb>
add    $0x1,%r14d
jmp    11e0 <stdin_to_cfgfile+0x70>
mov    0x8(%rsp),%rdi
call   1030 <fclose@plt>
add    $0x208,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d7e(%rip),%rax
mov    (%rax),%edi
lea    0xdd9(%rip),%rsi
lea    0x10(%rsp),%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    0x2d5a(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@stdin = global i32 0
@EOF = global i8 0
@stderr = global i32 0
@.str = [52 x i8] c"Err %d, '%s' is not a valid config line. Ignoring.\0A\00"
@.str.1 = [34 x i8] c"Line '%s'.. too long, giving up.\0A\00"
@EXIT_FAILURE = global i32 0
define void @stdin_to_cfgfile(i32 %0) {
%2 = alloca i32
%3 = alloca [500 x i8]
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i8
%7 = alloca i32
store i32 %0, i32* %2
store i32 0, i32* %4
%8 = getelementptr inbounds [500 x i8], [500 x i8]* %3, i64 0, i64 0
store i8 0, i8* %8
%9 = load i32, i32* %2
%10 = call i32 @open_cfgfile(i32** %5, i32 %9)
br label %11
11: ; preds = %1, %17, %69
%12 = load i32, i32* @stdin
%13 = call signext i8 @fgetc(i32 %12)
store i8 %13, i8* %6
%14 = load i8, i8* %6
%15 = sext i8 %14 to i32
%16 = icmp eq i32 %15, 13
br i1 %16, label %17, label %18
17: ; preds = %11
br label %11
18: ; preds = %11
%19 = load i8, i8* %6
%20 = sext i8 %19 to i32
%21 = icmp eq i32 %20, 10
br i1 %21, label %28, label %22
22: ; preds = %18
%23 = load i8, i8* %6
%24 = sext i8 %23 to i32
%25 = load i8, i8* @EOF
%26 = sext i8 %25 to i32
%27 = icmp eq i32 %24, %26
br i1 %27, label %28, label %53
28: ; preds = %22, %18
%29 = load i32, i32* %4
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds [500 x i8], [500 x i8]* %3, i64 0, i64 %30
store i8 0, i8* %31
%32 = load i32, i32* %2
%33 = getelementptr inbounds [500 x i8], [500 x i8]* %3, i64 0, i64 0
%34 = call i32 @sanity_check(i32 %32, i8* %33)
store i32 %34, i32* %7
%35 = icmp sge i32 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %28
%37 = getelementptr inbounds [500 x i8], [500 x i8]* %3, i64 0, i64 0
%38 = call i32 @write_line(i32** %5, i8* %37)
br label %44
39: ; preds = %28
%40 = load i32, i32* @stderr
%41 = load i32, i32* %7
%42 = getelementptr inbounds [500 x i8], [500 x i8]* %3, i64 0, i64 0
%43 = call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str, i64 0, i64 0), i32 %41, i8* %42)
br label %44
44: ; preds = %39, %36
%45 = getelementptr inbounds [500 x i8], [500 x i8]* %3, i64 0, i64 0
store i8 0, i8* %45
%46 = load i8, i8* %6
%47 = sext i8 %46 to i32
%48 = load i8, i8* @EOF
%49 = sext i8 %48 to i32
%50 = icmp eq i32 %47, %49
br i1 %50, label %51, label %52
51: ; preds = %44
br label %70
52: ; preds = %44
store i32 0, i32* %4
br label %69
53: ; preds = %22
%54 = load i8, i8* %6
%55 = load i32, i32* %4
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds [500 x i8], [500 x i8]* %3, i64 0, i64 %56
store i8 %54, i8* %57
%58 = load i32, i32* %4
%59 = icmp sge i32 %58, 500
br i1 %59, label %60, label %66
60: ; preds = %53
%61 = load i32, i32* @stderr
%62 = getelementptr inbounds [500 x i8], [500 x i8]* %3, i64 0, i64 0
%63 = call i32 (i32, i8*, ...) @fprintf(i32 %61, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* %62)
%64 = load i32, i32* @EXIT_FAILURE
%65 = call i32 @exit(i32 %64)
unreachable
66: ; preds = %53
%67 = load i32, i32* %4
%68 = add nsw i32 %67, 1
store i32 %68, i32* %4
br label %69
69: ; preds = %66, %52
br label %11
70: ; preds = %51
%71 = load i32*, i32** %5
%72 = call i32 @fclose(i32* %71)
ret void
}

LLVM IR (-O2):
@stdin = global i32 0
@EOF = global i8 0
@stderr = global i32 0
@.str = [52 x i8] c"Err %d, '%s' is not a valid config line. Ignoring.\0A\00"
@.str.1 = [34 x i8] c"Line '%s'.. too long, giving up.\0A\00"
@EXIT_FAILURE = global i32 0
define void @stdin_to_cfgfile(i32 %0) {
%2 = alloca [500 x i8]
%3 = alloca i32*
%4 = getelementptr inbounds [500 x i8], [500 x i8]* %2, i64 0, i64 0
store i8 0, i8* %4
%5 = bitcast i32** %3 to i8*
%6 = call i32 @open_cfgfile(i32** %3, i32 %0)
br label %7
7: ; preds = %28, %1
%8 = phi i32 [ 0, %1 ], [ %29, %28 ]
br label %9
9: ; preds = %7, %9
%10 = load i32, i32* @stdin
%11 = call signext i8 @fgetc(i32 %10)
switch i8 %11, label %12 [
i8 13, label %9
i8 10, label %15
]
12: ; preds = %9
%13 = load i8, i8* @EOF
%14 = icmp eq i8 %11, %13
br i1 %14, label %15, label %30
15: ; preds = %9, %12
%16 = sext i32 %8 to i64
%17 = getelementptr inbounds [500 x i8], [500 x i8]* %2, i64 0, i64 %16
store i8 0, i8* %17
%18 = call i32 @sanity_check(i32 %0, i8* %4)
%19 = icmp sgt i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %15
%21 = call i32 @write_line(i32** %3, i8* %4)
br label %25
22: ; preds = %15
%23 = load i32, i32* @stderr
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str, i64 0, i64 0), i32 %18, i8* %4)
br label %25
25: ; preds = %22, %20
store i8 0, i8* %4
%26 = load i8, i8* @EOF
%27 = icmp eq i8 %11, %26
br i1 %27, label %41, label %28
28: ; preds = %25, %39
%29 = phi i32 [ %40, %39 ], [ 0, %25 ]
br label %7
30: ; preds = %12
%31 = sext i32 %8 to i64
%32 = getelementptr inbounds [500 x i8], [500 x i8]* %2, i64 0, i64 %31
store i8 %11, i8* %32
%33 = icmp sgt i32 %8, 499
br i1 %33, label %34, label %39
34: ; preds = %30
%35 = load i32, i32* @stderr
%36 = call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* %4)
%37 = load i32, i32* @EXIT_FAILURE
%38 = call i32 @exit(i32 %37)
unreachable
39: ; preds = %30
%40 = add nsw i32 %8, 1
br label %28
41: ; preds = %25
%42 = load i32*, i32** %3
%43 = call i32 @fclose(i32* %42)
ret void
}