#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 */
struct controlparse {
  int dummy;
};
typedef int ssize_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ close(int);
scalar_t__ controlparse_eof(struct controlparse *);
scalar_t__ controlparse_line(struct controlparse *, char *, int);
int dup(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fdopen(int, char *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, unsigned int);
int /*<<< orphan*/ free(char *);
int getline(char **, size_t *, int /*<<< orphan*/ *);
char *malloc(size_t);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int controlparse_parsefd(struct controlparse *cp, int fd) {
  FILE *f;
  int dfd;
  unsigned line = 0;
  char *linebuf;
  size_t lb_size;
  ssize_t bs;
  if ((dfd = dup(fd)) == -1) {
    perror("controlparse_parsefd: dup failed");
    return -1;
  }
  if ((f = fdopen(dfd, "r")) == NULL) {
    perror("control_parsefd: fopen failed");
    close(dfd);
    return -1;
  }
  lb_size = 128;
  if ((linebuf = malloc(lb_size)) == NULL) {
    perror("control_parsefd: malloc failed");
    goto error_malloc;
  }
  while ((bs = getline(&linebuf, &lb_size, f)) != -1) {
    line++;
    if (controlparse_line(cp, linebuf, bs) != 0)
      goto error_parse;
  }
  if (ferror(f)) {
    perror("controlparse_parsefd: reading line failed");
    goto error_parse;
  }
  if (controlparse_eof(cp) != 0)
    goto error_free;
  free(linebuf);
  fclose(f);
  return 0;
error_parse:
  fprintf(stderr, "Parsing Line %u failed\n", line);
error_free:
  free(linebuf);
error_malloc:
  fclose(f);
  return -1;
}


x86 assembly (clang, -O0):
<controlparse_parsefd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x14(%rbp),%edi
call   1080 <dup@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
jne    1202 <controlparse_parsefd+0x42>
lea    0xe0f(%rip),%rdi
call   10d0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1349 <controlparse_parsefd+0x189>
mov    -0x24(%rbp),%edi
lea    0xe15(%rip),%rsi
call   10c0 <fdopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    123f <controlparse_parsefd+0x7f>
lea    0xdfd(%rip),%rdi
call   10d0 <perror@plt>
mov    -0x24(%rbp),%edi
call   1090 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1349 <controlparse_parsefd+0x189>
movq   $0x80,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
call   10b0 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    126f <controlparse_parsefd+0xaf>
lea    0xddc(%rip),%rdi
call   10d0 <perror@plt>
jmp    1339 <controlparse_parsefd+0x179>
jmp    1274 <controlparse_parsefd+0xb4>
mov    -0x20(%rbp),%rdx
lea    -0x30(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   10e0 <getline@plt>
mov    %eax,-0x3c(%rbp)
cmp    $0xffffffff,%eax
je     12be <controlparse_parsefd+0xfe>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x3c(%rbp),%edx
call   1030 <controlparse_line@plt>
cmp    $0x0,%rax
je     12b9 <controlparse_parsefd+0xf9>
jmp    1318 <controlparse_parsefd+0x158>
jmp    1274 <controlparse_parsefd+0xb4>
mov    -0x20(%rbp),%rdi
call   1050 <ferror@plt>
cmp    $0x0,%rax
je     12e2 <controlparse_parsefd+0x122>
lea    0xd88(%rip),%rdi
call   10d0 <perror@plt>
jmp    1318 <controlparse_parsefd+0x158>
mov    -0x10(%rbp),%rdi
call   1070 <controlparse_eof@plt>
cmp    $0x0,%rax
je     12fa <controlparse_parsefd+0x13a>
jmp    1330 <controlparse_parsefd+0x170>
mov    -0x30(%rbp),%rdi
call   1040 <free@plt>
mov    -0x20(%rbp),%rdi
call   1060 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    1349 <controlparse_parsefd+0x189>
mov    0x2cc1(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%edx
lea    0xd5f(%rip),%rsi
call   10a0 <fprintf@plt>
mov    -0x30(%rbp),%rdi
call   1040 <free@plt>
mov    -0x20(%rbp),%rdi
call   1060 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<controlparse_parsefd>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
mov    %esi,%edi
call   1080 <dup@plt>
cmp    $0xffffffff,%eax
je     12c4 <controlparse_parsefd+0x104>
mov    %eax,%ebp
lea    0xe39(%rip),%rsi
mov    %eax,%edi
call   10c0 <fdopen@plt>
test   %rax,%rax
je     12d2 <controlparse_parsefd+0x112>
mov    %rax,%r14
movq   $0x80,0x8(%rsp)
mov    $0x80,%edi
call   10b0 <malloc@plt>
mov    %rax,(%rsp)
test   %rax,%rax
je     12e7 <controlparse_parsefd+0x127>
xor    %ebp,%ebp
mov    %rsp,%r15
lea    0x8(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r15,%rdi
mov    %r12,%rsi
mov    %r14,%rdx
call   10e0 <getline@plt>
cmp    $0xffffffff,%eax
je     1272 <controlparse_parsefd+0xb2>
add    $0x1,%ebp
mov    (%rsp),%rsi
mov    %rbx,%rdi
mov    %eax,%edx
call   1030 <controlparse_line@plt>
test   %rax,%rax
je     1230 <controlparse_parsefd+0x70>
mov    0x2d80(%rip),%rax
mov    (%rax),%edi
lea    0xe21(%rip),%rsi
mov    %ebp,%edx
call   10a0 <fprintf@plt>
jmp    128c <controlparse_parsefd+0xcc>
mov    %r14,%rdi
call   1050 <ferror@plt>
test   %rax,%rax
jne    12f5 <controlparse_parsefd+0x135>
mov    %rbx,%rdi
call   1070 <controlparse_eof@plt>
test   %rax,%rax
je     12af <controlparse_parsefd+0xef>
mov    (%rsp),%rdi
call   1040 <free@plt>
mov    %r14,%rdi
call   1060 <fclose@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%rsp),%rdi
call   1040 <free@plt>
mov    %r14,%rdi
call   1060 <fclose@plt>
xor    %eax,%eax
jmp    12a2 <controlparse_parsefd+0xe2>
lea    0xd35(%rip),%rdi
call   10d0 <perror@plt>
jmp    129d <controlparse_parsefd+0xdd>
lea    0xd4a(%rip),%rdi
call   10d0 <perror@plt>
mov    %ebp,%edi
call   1090 <close@plt>
jmp    129d <controlparse_parsefd+0xdd>
lea    0xd53(%rip),%rdi
call   10d0 <perror@plt>
jmp    1295 <controlparse_parsefd+0xd5>
lea    0xd64(%rip),%rdi
call   10d0 <perror@plt>
jmp    1259 <controlparse_parsefd+0x99>

LLVM IR (-O0):
%struct.controlparse = type { i32 }
@.str = [33 x i8] c"controlparse_parsefd: dup failed\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [30 x i8] c"control_parsefd: fopen failed\00"
@.str.3 = [31 x i8] c"control_parsefd: malloc failed\00"
@.str.4 = [42 x i8] c"controlparse_parsefd: reading line failed\00"
@stderr = global i32 0
@.str.5 = [24 x i8] c"Parsing Line %u failed\0A\00"
define i32 @controlparse_parsefd(%struct.controlparse* %0, i32 %1) {
%3 = alloca i32
%4 = alloca %struct.controlparse*
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i32
store %struct.controlparse* %0, %struct.controlparse** %4
store i32 %1, i32* %5
store i32 0, i32* %8
%12 = load i32, i32* %5
%13 = call i32 @dup(i32 %12)
store i32 %13, i32* %7
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %72
17: ; preds = %2
%18 = load i32, i32* %7
%19 = call i32* @fdopen(i32 %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %19, i32** %6
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %25
21: ; preds = %17
%22 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
%23 = load i32, i32* %7
%24 = call i32 @close(i32 %23)
store i32 -1, i32* %3
br label %72
25: ; preds = %17
store i64 128, i64* %10
%26 = load i64, i64* %10
%27 = call i8* @malloc(i64 %26)
store i8* %27, i8** %9
%28 = icmp eq i8* %27, null
br i1 %28, label %29, label %31
29: ; preds = %25
%30 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0))
br label %69
31: ; preds = %25
br label %32
32: ; preds = %45, %31
%33 = load i32*, i32** %6
%34 = call i32 @getline(i8** %9, i64* %10, i32* %33)
store i32 %34, i32* %11
%35 = icmp ne i32 %34, -1
br i1 %35, label %36, label %46
36: ; preds = %32
%37 = load i32, i32* %8
%38 = add i32 %37, 1
store i32 %38, i32* %8
%39 = load %struct.controlparse*, %struct.controlparse** %4
%40 = load i8*, i8** %9
%41 = load i32, i32* %11
%42 = call i64 @controlparse_line(%struct.controlparse* %39, i8* %40, i32 %41)
%43 = icmp ne i64 %42, 0
br i1 %43, label %44, label %45
44: ; preds = %36
br label %62
45: ; preds = %36
br label %32
46: ; preds = %32
%47 = load i32*, i32** %6
%48 = call i64 @ferror(i32* %47)
%49 = icmp ne i64 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %46
%51 = call i32 @perror(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.4, i64 0, i64 0))
br label %62
52: ; preds = %46
%53 = load %struct.controlparse*, %struct.controlparse** %4
%54 = call i64 @controlparse_eof(%struct.controlparse* %53)
%55 = icmp ne i64 %54, 0
br i1 %55, label %56, label %57
56: ; preds = %52
br label %66
57: ; preds = %52
%58 = load i8*, i8** %9
%59 = call i32 @free(i8* %58)
%60 = load i32*, i32** %6
%61 = call i32 @fclose(i32* %60)
store i32 0, i32* %3
br label %72
62: ; preds = %50, %44
%63 = load i32, i32* @stderr
%64 = load i32, i32* %8
%65 = call i32 @fprintf(i32 %63, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i32 %64)
br label %66
66: ; preds = %62, %56
%67 = load i8*, i8** %9
%68 = call i32 @free(i8* %67)
br label %69
69: ; preds = %66, %29
%70 = load i32*, i32** %6
%71 = call i32 @fclose(i32* %70)
store i32 -1, i32* %3
br label %72
72: ; preds = %69, %57, %21, %15
%73 = load i32, i32* %3
ret i32 %73
}

LLVM IR (-O2):
%struct.controlparse = type { i32 }
@.str = [33 x i8] c"controlparse_parsefd: dup failed\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [30 x i8] c"control_parsefd: fopen failed\00"
@.str.3 = [31 x i8] c"control_parsefd: malloc failed\00"
@.str.4 = [42 x i8] c"controlparse_parsefd: reading line failed\00"
@stderr = global i32 0
@.str.5 = [24 x i8] c"Parsing Line %u failed\0A\00"
define i32 @controlparse_parsefd(%struct.controlparse* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = bitcast i8** %3 to i8*
%6 = bitcast i64* %4 to i8*
%7 = tail call i32 @dup(i32 %1)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = tail call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
br label %52
11: ; preds = %2
%12 = tail call i32* @fdopen(i32 %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = tail call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
%16 = tail call i32 @close(i32 %7)
br label %52
17: ; preds = %11
store i64 128, i64* %4
%18 = tail call dereferenceable_or_null(128) i8* @malloc(i64 128)
store i8* %18, i8** %3
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = tail call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0))
br label %50
22: ; preds = %17, %26
%23 = phi i32 [ %27, %26 ], [ 0, %17 ]
%24 = call i32 @getline(i8** %3, i64* %4, i32* %12)
%25 = icmp eq i32 %24, -1
br i1 %25, label %31, label %26
26: ; preds = %22
%27 = add i32 %23, 1
%28 = load i8*, i8** %3
%29 = call i64 @controlparse_line(%struct.controlparse* %0, i8* %28, i32 %24)
%30 = icmp eq i64 %29, 0
br i1 %30, label %22, label %43
31: ; preds = %22
%32 = call i64 @ferror(i32* %12)
%33 = icmp eq i64 %32, 0
br i1 %33, label %36, label %34
34: ; preds = %31
%35 = call i32 @perror(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.4, i64 0, i64 0))
br label %43
36: ; preds = %31
%37 = call i64 @controlparse_eof(%struct.controlparse* %0)
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %47
39: ; preds = %36
%40 = load i8*, i8** %3
%41 = call i32 @free(i8* %40)
%42 = call i32 @fclose(i32* %12)
br label %52
43: ; preds = %26, %34
%44 = phi i32 [ %23, %34 ], [ %27, %26 ]
%45 = load i32, i32* @stderr
%46 = call i32 @fprintf(i32 %45, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i32 %44)
br label %47
47: ; preds = %36, %43
%48 = load i8*, i8** %3
%49 = call i32 @free(i8* %48)
br label %50
50: ; preds = %47, %20
%51 = call i32 @fclose(i32* %12)
br label %52
52: ; preds = %50, %39, %14, %9
%53 = phi i32 [ -1, %9 ], [ -1, %14 ], [ -1, %50 ], [ 0, %39 ]
ret i32 %53
}