#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 stat {
  scalar_t__ st_dev;
  scalar_t__ st_ino;
};
struct rlimit {
  int rlim_cur;
};
typedef int ssize_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ RLIMIT_NOFILE;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fstat(int, struct stat *);
int getline(char **, size_t *, int /*<<< orphan*/ *);
int /*<<< orphan*/ getrlimit(int /*<<< orphan*/, struct rlimit *);
char *readLine_(const char *aArgFile) {
  int rc = -1;
  char *linebuf = 0;
  size_t bufsize = 0;
  FILE *fp = fopen(aArgFile, "r");
  if (!fp)
    goto out;
  ssize_t linelen = getline(&linebuf, &bufsize, fp);
  if (-1 == linelen)
    goto out;
  for (char *cp = linebuf; *cp; ++cp) {
    if ('\n' == *cp) {
      *cp = 0;
      break;
    }
  }
  struct rlimit rlim;
  struct stat fpstat;
  if (!getrlimit(RLIMIT_NOFILE, &rlim) && !fstat(fileno(fp), &fpstat)) {
    for (int fd = 0; fd < rlim.rlim_cur; ++fd) {
      struct stat fdstat;
      if (!fstat(fd, &fdstat) && fpstat.st_dev == fdstat.st_dev &&
          fpstat.st_ino == fdstat.st_ino) {
        if (fd != fileno(fp)) {
          close(fd);
          break;
        }
      }
    }
  }
  rc = 0;
out:
  if (rc)
    free(linebuf);
  if (fp)
    fclose(fp);
  return rc ? 0 : linebuf;
}


x86 assembly (clang, -O0):
<readLine_>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0xffffffff,-0xc(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe4f(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11ca <readLine_+0x4a>
jmp    1305 <readLine_+0x185>
mov    -0x28(%rbp),%rdx
lea    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1080 <getline@plt>
mov    %eax,-0x2c(%rbp)
mov    $0xffffffff,%eax
cmp    -0x2c(%rbp),%eax
jne    11f1 <readLine_+0x71>
jmp    1305 <readLine_+0x185>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmpb   $0x0,(%rax)
je     123c <readLine_+0xbc>
mov    -0x38(%rbp),%rax
movsbl (%rax),%ecx
mov    $0xa,%eax
cmp    %ecx,%eax
jne    1226 <readLine_+0xa6>
mov    -0x38(%rbp),%rax
movb   $0x0,(%rax)
jmp    123c <readLine_+0xbc>
jmp    122b <readLine_+0xab>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    11f9 <readLine_+0x79>
mov    0x2da5(%rip),%rax
mov    (%rax),%edi
lea    -0x40(%rbp),%rsi
call   1090 <getrlimit@plt>
cmp    $0x0,%eax
jne    12fe <readLine_+0x17e>
mov    -0x28(%rbp),%rdi
call   1060 <fileno@plt>
mov    %eax,%edi
lea    -0x50(%rbp),%rsi
call   10a0 <fstat@plt>
cmp    $0x0,%eax
jne    12fe <readLine_+0x17e>
movl   $0x0,-0x54(%rbp)
mov    -0x54(%rbp),%eax
cmp    -0x40(%rbp),%eax
jge    12f9 <readLine_+0x179>
mov    -0x54(%rbp),%edi
lea    -0x68(%rbp),%rsi
call   10a0 <fstat@plt>
cmp    $0x0,%eax
jne    12e6 <readLine_+0x166>
mov    -0x50(%rbp),%rax
cmp    -0x68(%rbp),%rax
jne    12e6 <readLine_+0x166>
mov    -0x48(%rbp),%rax
cmp    -0x60(%rbp),%rax
jne    12e6 <readLine_+0x166>
mov    -0x54(%rbp),%eax
mov    %eax,-0x6c(%rbp)
mov    -0x28(%rbp),%rdi
call   1060 <fileno@plt>
mov    %eax,%ecx
mov    -0x6c(%rbp),%eax
cmp    %ecx,%eax
je     12e1 <readLine_+0x161>
mov    -0x54(%rbp),%edi
call   1050 <close@plt>
jmp    12f9 <readLine_+0x179>
jmp    12e6 <readLine_+0x166>
jmp    12eb <readLine_+0x16b>
mov    -0x54(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x54(%rbp)
jmp    127b <readLine_+0xfb>
jmp    12fe <readLine_+0x17e>
movl   $0x0,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     1318 <readLine_+0x198>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
cmpq   $0x0,-0x28(%rbp)
je     132c <readLine_+0x1ac>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
cmpl   $0x0,-0xc(%rbp)
je     1341 <readLine_+0x1c1>
xor    %eax,%eax
mov    %rax,-0x78(%rbp)
jmp    1349 <readLine_+0x1c9>
mov    -0x18(%rbp),%rax
mov    %rax,-0x78(%rbp)
mov    -0x78(%rbp),%rax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readLine_>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
movq   $0x0,0x8(%rsp)
movq   $0x0,0x10(%rsp)
lea    0xe59(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11e1 <readLine_+0x61>
lea    0x8(%rsp),%rdi
lea    0x10(%rsp),%rsi
mov    %rbx,%rdx
call   1080 <getline@plt>
mov    0x8(%rsp),%rdi
cmp    $0xffffffff,%eax
je     11e3 <readLine_+0x63>
movzbl (%rdi),%eax
test   %al,%al
je     11f0 <readLine_+0x70>
cmp    $0xa,%al
je     11ed <readLine_+0x6d>
add    $0x1,%rdi
jmp    11d0 <readLine_+0x50>
xor    %edi,%edi
call   1030 <free@plt>
xor    %r15d,%r15d
jmp    1220 <readLine_+0xa0>
movb   $0x0,(%rdi)
mov    0x2df1(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rsi
call   1090 <getrlimit@plt>
mov    $0x1,%r15b
test   %eax,%eax
jne    1220 <readLine_+0xa0>
mov    %rbx,%rdi
call   1060 <fileno@plt>
lea    0x28(%rsp),%rsi
mov    %eax,%edi
call   10a0 <fstat@plt>
test   %eax,%eax
je     124a <readLine_+0xca>
test   %rbx,%rbx
je     122d <readLine_+0xad>
mov    %rbx,%rdi
call   1040 <fclose@plt>
test   %r15b,%r15b
je     1239 <readLine_+0xb9>
mov    0x8(%rsp),%rax
jmp    123b <readLine_+0xbb>
xor    %eax,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
cmpl   $0x0,(%rsp)
jle    1220 <readLine_+0xa0>
mov    0x28(%rsp),%r12
mov    0x30(%rsp),%r13
xor    %ebp,%ebp
lea    0x18(%rsp),%r14
jmp    1278 <readLine_+0xf8>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    $0x1,%ebp
cmp    (%rsp),%ebp
jge    1220 <readLine_+0xa0>
mov    %ebp,%edi
mov    %r14,%rsi
call   10a0 <fstat@plt>
test   %eax,%eax
jne    1270 <readLine_+0xf0>
cmp    0x18(%rsp),%r12
jne    1270 <readLine_+0xf0>
cmp    0x20(%rsp),%r13
jne    1270 <readLine_+0xf0>
mov    %rbx,%rdi
call   1060 <fileno@plt>
cmp    %eax,%ebp
je     1270 <readLine_+0xf0>
mov    %ebp,%edi
call   1050 <close@plt>
jmp    1220 <readLine_+0xa0>

LLVM IR (-O0):
%struct.rlimit = type { i32 }
%struct.stat = type { i64, i64 }
@.str = [2 x i8] c"r\00"
@RLIMIT_NOFILE = global i32 0
define i8* @readLine_(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca %struct.rlimit
%10 = alloca %struct.stat
%11 = alloca i32
%12 = alloca %struct.stat
store i8* %0, i8** %2
store i32 -1, i32* %3
store i8* null, i8** %4
store i64 0, i64* %5
%13 = load i8*, i8** %2
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %14, i32** %6
%15 = load i32*, i32** %6
%16 = icmp ne i32* %15, null
br i1 %16, label %18, label %17
17: ; preds = %1
br label %87
18: ; preds = %1
%19 = load i32*, i32** %6
%20 = call i32 @getline(i8** %4, i64* %5, i32* %19)
store i32 %20, i32* %7
%21 = load i32, i32* %7
%22 = icmp eq i32 -1, %21
br i1 %22, label %23, label %24
23: ; preds = %18
br label %87
24: ; preds = %18
%25 = load i8*, i8** %4
store i8* %25, i8** %8
br label %26
26: ; preds = %38, %24
%27 = load i8*, i8** %8
%28 = load i8, i8* %27
%29 = icmp ne i8 %28, 0
br i1 %29, label %30, label %41
30: ; preds = %26
%31 = load i8*, i8** %8
%32 = load i8, i8* %31
%33 = sext i8 %32 to i32
%34 = icmp eq i32 10, %33
br i1 %34, label %35, label %37
35: ; preds = %30
%36 = load i8*, i8** %8
store i8 0, i8* %36
br label %41
37: ; preds = %30
br label %38
38: ; preds = %37
%39 = load i8*, i8** %8
%40 = getelementptr inbounds i8, i8* %39, i32 1
store i8* %40, i8** %8
br label %26
41: ; preds = %35, %26
%42 = load i32, i32* @RLIMIT_NOFILE
%43 = call i32 @getrlimit(i32 %42, %struct.rlimit* %9)
%44 = icmp ne i32 %43, 0
br i1 %44, label %86, label %45
45: ; preds = %41
%46 = load i32*, i32** %6
%47 = call i32 @fileno(i32* %46)
%48 = call i32 @fstat(i32 %47, %struct.stat* %10)
%49 = icmp ne i32 %48, 0
br i1 %49, label %86, label %50
50: ; preds = %45
store i32 0, i32* %11
br label %51
51: ; preds = %82, %50
%52 = load i32, i32* %11
%53 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %9, i32 0, i32 0
%54 = load i32, i32* %53
%55 = icmp slt i32 %52, %54
br i1 %55, label %56, label %85
56: ; preds = %51
%57 = load i32, i32* %11
%58 = call i32 @fstat(i32 %57, %struct.stat* %12)
%59 = icmp ne i32 %58, 0
br i1 %59, label %81, label %60
60: ; preds = %56
%61 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%62 = load i64, i64* %61
%63 = getelementptr inbounds %struct.stat, %struct.stat* %12, i32 0, i32 0
%64 = load i64, i64* %63
%65 = icmp eq i64 %62, %64
br i1 %65, label %66, label %81
66: ; preds = %60
%67 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 1
%68 = load i64, i64* %67
%69 = getelementptr inbounds %struct.stat, %struct.stat* %12, i32 0, i32 1
%70 = load i64, i64* %69
%71 = icmp eq i64 %68, %70
br i1 %71, label %72, label %81
72: ; preds = %66
%73 = load i32, i32* %11
%74 = load i32*, i32** %6
%75 = call i32 @fileno(i32* %74)
%76 = icmp ne i32 %73, %75
br i1 %76, label %77, label %80
77: ; preds = %72
%78 = load i32, i32* %11
%79 = call i32 @close(i32 %78)
br label %85
80: ; preds = %72
br label %81
81: ; preds = %80, %66, %60, %56
br label %82
82: ; preds = %81
%83 = load i32, i32* %11
%84 = add nsw i32 %83, 1
store i32 %84, i32* %11
br label %51
85: ; preds = %77, %51
br label %86
86: ; preds = %85, %45, %41
store i32 0, i32* %3
br label %87
87: ; preds = %86, %23, %17
%88 = load i32, i32* %3
%89 = icmp ne i32 %88, 0
br i1 %89, label %90, label %93
90: ; preds = %87
%91 = load i8*, i8** %4
%92 = call i32 @free(i8* %91)
br label %93
93: ; preds = %90, %87
%94 = load i32*, i32** %6
%95 = icmp ne i32* %94, null
br i1 %95, label %96, label %99
96: ; preds = %93
%97 = load i32*, i32** %6
%98 = call i32 @fclose(i32* %97)
br label %99
99: ; preds = %96, %93
%100 = load i32, i32* %3
%101 = icmp ne i32 %100, 0
br i1 %101, label %102, label %103
102: ; preds = %99
br label %105
103: ; preds = %99
%104 = load i8*, i8** %4
br label %105
105: ; preds = %103, %102
%106 = phi i8* [ null, %102 ], [ %104, %103 ]
ret i8* %106
}

LLVM IR (-O2):
%struct.rlimit = type { i32 }
%struct.stat = type { i64, i64 }
@.str = [2 x i8] c"r\00"
@RLIMIT_NOFILE = global i32 0
define i8* @readLine_(i8* nocapture %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca %struct.rlimit
%5 = alloca %struct.stat
%6 = alloca %struct.stat
%7 = bitcast i8** %2 to i8*
store i8* null, i8** %2
%8 = bitcast i64* %3 to i8*
store i64 0, i64* %3
%9 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %60, label %11
11: ; preds = %1
%12 = call i32 @getline(i8** %2, i64* %3, i32* %9)
%13 = icmp eq i32 %12, -1
%14 = load i8*, i8** %2
br i1 %13, label %60, label %15
15: ; preds = %11, %19
%16 = phi i8* [ %20, %19 ], [ %14, %11 ]
%17 = load i8, i8* %16
switch i8 %17, label %19 [
i8 0, label %21
i8 10, label %18
]
18: ; preds = %15
store i8 0, i8* %16
br label %21
19: ; preds = %15
%20 = getelementptr inbounds i8, i8* %16, i64 1
br label %15
21: ; preds = %15, %18
%22 = load i32, i32* @RLIMIT_NOFILE
%23 = call i32 @getrlimit(i32 %22, %struct.rlimit* %4)
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %63
25: ; preds = %21
%26 = call i32 @fileno(i32* %9)
%27 = call i32 @fstat(i32 %26, %struct.stat* %5)
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %63
29: ; preds = %25
%30 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %4, i64 0, i32 0
%31 = load i32, i32* %30
%32 = icmp sgt i32 %31, 0
br i1 %32, label %33, label %63
33: ; preds = %29
%34 = bitcast %struct.stat* %6 to i8*
%35 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 0
%36 = load i64, i64* %35
%37 = getelementptr inbounds %struct.stat, %struct.stat* %6, i64 0, i32 0
%38 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 1
%39 = load i64, i64* %38
%40 = getelementptr inbounds %struct.stat, %struct.stat* %6, i64 0, i32 1
br label %41
41: ; preds = %33, %56
%42 = phi i32 [ 0, %33 ], [ %57, %56 ]
%43 = call i32 @fstat(i32 %42, %struct.stat* %6)
%44 = icmp eq i32 %43, 0
%45 = load i64, i64* %37
%46 = icmp eq i64 %36, %45
%47 = select i1 %44, i1 %46, i1 false
%48 = load i64, i64* %40
%49 = icmp eq i64 %39, %48
%50 = select i1 %47, i1 %49, i1 false
br i1 %50, label %51, label %56
51: ; preds = %41
%52 = call i32 @fileno(i32* %9)
%53 = icmp eq i32 %42, %52
br i1 %53, label %56, label %54
54: ; preds = %51
%55 = call i32 @close(i32 %42)
br label %63
56: ; preds = %41, %51
%57 = add nuw nsw i32 %42, 1
%58 = load i32, i32* %30
%59 = icmp slt i32 %57, %58
br i1 %59, label %41, label %63
60: ; preds = %11, %1
%61 = phi i8* [ null, %1 ], [ %14, %11 ]
%62 = call i32 @free(i8* %61)
br label %63
63: ; preds = %56, %29, %21, %25, %54, %60
%64 = phi i1 [ false, %60 ], [ true, %54 ], [ true, %25 ], [ true, %21 ], [ true, %29 ], [ true, %56 ]
br i1 %10, label %67, label %65
65: ; preds = %63
%66 = call i32 @fclose(i32* %9)
br label %67
67: ; preds = %65, %63
%68 = load i8*, i8** %2
%69 = select i1 %64, i8* %68, i8* null
ret i8* %69
}