#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 {
  int st_mode;
  scalar_t__ st_uid;
  int st_size;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ S_ISREG(int);
int S_IWGRP;
int S_IWOTH;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(char *);
scalar_t__ fstat(int, struct stat *);
scalar_t__ malloc(int);
int open(char const *, int /*<<< orphan*/);
scalar_t__ read(int, char *, int);
int _read_conf_file(char **buffer, const char *conffile) {
  char *buf;
  int rc = 0, fd = 0;
  struct stat fstatbuf;
  buf = NULL;
  if ((fd = open(conffile, O_RDONLY)) == -1 || fstat(fd, &fstatbuf))
    return -1;
  if (!S_ISREG(fstatbuf.st_mode) || fstatbuf.st_uid != 0 ||
      (fstatbuf.st_mode & S_IWGRP) || (fstatbuf.st_mode & S_IWOTH)) {
    rc = -2;
    goto conf_failed;
  }
  if ((buf = (char *)malloc(1 + fstatbuf.st_size)) == NULL) {
    rc = -3;
    goto conf_failed;
  }
  if (fstatbuf.st_size > 0) {
    if (read(fd, buf, fstatbuf.st_size) <= 0) {
      rc = -1;
      goto conf_failed;
    }
  }
  buf[fstatbuf.st_size] = '\0';
  rc = 0;
  close(fd);
  *buffer = buf;
  return rc;
conf_failed:
  if (buf)
    free(buf);
  if (fd > 0)
    close(fd);
  return rc;
}


x86 assembly (clang, -O0):
<_read_conf_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2e47(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x28(%rbp)
cmp    $0xffffffff,%eax
je     11ca <_read_conf_file+0x5a>
mov    -0x28(%rbp),%edi
lea    -0x40(%rbp),%rsi
call   1090 <fstat@plt>
cmp    $0x0,%rax
je     11d6 <_read_conf_file+0x66>
movl   $0xffffffff,-0x4(%rbp)
jmp    12de <_read_conf_file+0x16e>
mov    -0x40(%rbp),%edi
call   1060 <S_ISREG@plt>
cmp    $0x0,%eax
je     121c <_read_conf_file+0xac>
cmpq   $0x0,-0x38(%rbp)
jne    121c <_read_conf_file+0xac>
mov    -0x40(%rbp),%eax
mov    0x2de4(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    121c <_read_conf_file+0xac>
mov    -0x40(%rbp),%eax
mov    0x2db7(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1228 <_read_conf_file+0xb8>
movl   $0xfffffffe,-0x24(%rbp)
jmp    12b2 <_read_conf_file+0x142>
mov    -0x30(%rbp),%edi
add    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    124d <_read_conf_file+0xdd>
movl   $0xfffffffd,-0x24(%rbp)
jmp    12b2 <_read_conf_file+0x142>
cmpl   $0x0,-0x30(%rbp)
jle    1281 <_read_conf_file+0x111>
mov    -0x28(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x30(%rbp),%edx
call   1050 <read@plt>
cmp    $0x0,%rax
jg     127c <_read_conf_file+0x10c>
movl   $0xffffffff,-0x24(%rbp)
jmp    12b2 <_read_conf_file+0x142>
jmp    1281 <_read_conf_file+0x111>
mov    -0x20(%rbp),%rax
movslq -0x30(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x24(%rbp)
mov    -0x28(%rbp),%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12de <_read_conf_file+0x16e>
cmpq   $0x0,-0x20(%rbp)
je     12c6 <_read_conf_file+0x156>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
cmpl   $0x0,-0x28(%rbp)
jle    12d8 <_read_conf_file+0x168>
mov    -0x28(%rbp),%edi
call   1040 <close@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_read_conf_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rax
mov    %rdi,%r14
mov    0x2e61(%rip),%rcx
mov    (%rcx),%esi
mov    %rax,%rdi
call   1080 <open@plt>
mov    $0xffffffff,%ebp
cmp    $0xffffffff,%eax
je     11ec <_read_conf_file+0x7c>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
call   1090 <fstat@plt>
test   %rax,%rax
jne    11ec <_read_conf_file+0x7c>
mov    (%rsp),%r15d
mov    %r15d,%edi
call   1060 <S_ISREG@plt>
mov    $0xfffffffe,%ebp
test   %eax,%eax
je     11e1 <_read_conf_file+0x71>
cmpq   $0x0,0x8(%rsp)
jne    11e1 <_read_conf_file+0x71>
mov    0x2e10(%rip),%rax
test   %r15d,(%rax)
jne    11e1 <_read_conf_file+0x71>
mov    0x2dec(%rip),%rax
test   %r15d,(%rax)
je     11f9 <_read_conf_file+0x89>
test   %ebx,%ebx
jle    11ec <_read_conf_file+0x7c>
mov    %ebx,%edi
call   1040 <close@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
movslq 0x10(%rsp),%r15
lea    0x1(%r15),%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     123a <_read_conf_file+0xca>
mov    %rax,%rbp
test   %r15d,%r15d
jle    1226 <_read_conf_file+0xb6>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %r15d,%edx
call   1050 <read@plt>
test   %rax,%rax
jle    1241 <_read_conf_file+0xd1>
movb   $0x0,0x0(%rbp,%r15,1)
mov    %ebx,%edi
call   1040 <close@plt>
mov    %rbp,(%r14)
xor    %ebp,%ebp
jmp    11ec <_read_conf_file+0x7c>
mov    $0xfffffffd,%ebp
jmp    11e1 <_read_conf_file+0x71>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    $0xffffffff,%ebp
jmp    11e1 <_read_conf_file+0x71>

LLVM IR (-O0):
%struct.stat = type { i32, i64, i32 }
@O_RDONLY = global i32 0
@S_IWGRP = global i32 0
@S_IWOTH = global i32 0
define i32 @_read_conf_file(i8** %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.stat
store i8** %0, i8*** %4
store i8* %1, i8** %5
store i32 0, i32* %7
store i32 0, i32* %8
store i8* null, i8** %6
%10 = load i8*, i8** %5
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* %10, i32 %11)
store i32 %12, i32* %8
%13 = icmp eq i32 %12, -1
br i1 %13, label %18, label %14
14: ; preds = %2
%15 = load i32, i32* %8
%16 = call i64 @fstat(i32 %15, %struct.stat* %9)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %14, %2
store i32 -1, i32* %3
br label %87
19: ; preds = %14
%20 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%21 = load i32, i32* %20
%22 = call i32 @S_ISREG(i32 %21)
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %40
24: ; preds = %19
%25 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 1
%26 = load i64, i64* %25
%27 = icmp ne i64 %26, 0
br i1 %27, label %40, label %28
28: ; preds = %24
%29 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%30 = load i32, i32* %29
%31 = load i32, i32* @S_IWGRP
%32 = and i32 %30, %31
%33 = icmp ne i32 %32, 0
br i1 %33, label %40, label %34
34: ; preds = %28
%35 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%36 = load i32, i32* %35
%37 = load i32, i32* @S_IWOTH
%38 = and i32 %36, %37
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %41
40: ; preds = %34, %28, %24, %19
store i32 -2, i32* %7
br label %73
41: ; preds = %34
%42 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 2
%43 = load i32, i32* %42
%44 = add nsw i32 1, %43
%45 = call i64 @malloc(i32 %44)
%46 = inttoptr i64 %45 to i8*
store i8* %46, i8** %6
%47 = icmp eq i8* %46, null
br i1 %47, label %48, label %49
48: ; preds = %41
store i32 -3, i32* %7
br label %73
49: ; preds = %41
%50 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 2
%51 = load i32, i32* %50
%52 = icmp sgt i32 %51, 0
br i1 %52, label %53, label %62
53: ; preds = %49
%54 = load i32, i32* %8
%55 = load i8*, i8** %6
%56 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 2
%57 = load i32, i32* %56
%58 = call i64 @read(i32 %54, i8* %55, i32 %57)
%59 = icmp sle i64 %58, 0
br i1 %59, label %60, label %61
60: ; preds = %53
store i32 -1, i32* %7
br label %73
61: ; preds = %53
br label %62
62: ; preds = %61, %49
%63 = load i8*, i8** %6
%64 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 2
%65 = load i32, i32* %64
%66 = sext i32 %65 to i64
%67 = getelementptr inbounds i8, i8* %63, i64 %66
store i8 0, i8* %67
store i32 0, i32* %7
%68 = load i32, i32* %8
%69 = call i32 @close(i32 %68)
%70 = load i8*, i8** %6
%71 = load i8**, i8*** %4
store i8* %70, i8** %71
%72 = load i32, i32* %7
store i32 %72, i32* %3
br label %87
73: ; preds = %60, %48, %40
%74 = load i8*, i8** %6
%75 = icmp ne i8* %74, null
br i1 %75, label %76, label %79
76: ; preds = %73
%77 = load i8*, i8** %6
%78 = call i32 @free(i8* %77)
br label %79
79: ; preds = %76, %73
%80 = load i32, i32* %8
%81 = icmp sgt i32 %80, 0
br i1 %81, label %82, label %85
82: ; preds = %79
%83 = load i32, i32* %8
%84 = call i32 @close(i32 %83)
br label %85
85: ; preds = %82, %79
%86 = load i32, i32* %7
store i32 %86, i32* %3
br label %87
87: ; preds = %85, %62, %18
%88 = load i32, i32* %3
ret i32 %88
}

LLVM IR (-O2):
%struct.stat = type { i32, i64, i32 }
@O_RDONLY = global i32 0
@S_IWGRP = global i32 0
@S_IWOTH = global i32 0
define i32 @_read_conf_file(i8** nocapture writeonly %0, i8* nocapture %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i8* %1, i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %51, label %8
8: ; preds = %2
%9 = call i64 @fstat(i32 %6, %struct.stat* %3)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %51
11: ; preds = %8
%12 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%13 = load i32, i32* %12
%14 = tail call i32 @S_ISREG(i32 %13)
%15 = icmp eq i32 %14, 0
%16 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 1
%17 = load i64, i64* %16
%18 = icmp ne i64 %17, 0
%19 = select i1 %15, i1 true, i1 %18
br i1 %19, label %46, label %20
20: ; preds = %11
%21 = load i32, i32* @S_IWGRP
%22 = and i32 %21, %13
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %46
24: ; preds = %20
%25 = load i32, i32* @S_IWOTH
%26 = and i32 %25, %13
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %46
28: ; preds = %24
%29 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 2
%30 = load i32, i32* %29
%31 = add nsw i32 %30, 1
%32 = tail call i64 @malloc(i32 %31)
%33 = inttoptr i64 %32 to i8*
%34 = icmp eq i64 %32, 0
br i1 %34, label %46, label %35
35: ; preds = %28
%36 = icmp sgt i32 %30, 0
br i1 %36, label %37, label %40
37: ; preds = %35
%38 = tail call i64 @read(i32 %6, i8* %33, i32 %30)
%39 = icmp slt i64 %38, 1
br i1 %39, label %44, label %40
40: ; preds = %37, %35
%41 = sext i32 %30 to i64
%42 = getelementptr inbounds i8, i8* %33, i64 %41
store i8 0, i8* %42
%43 = tail call i32 @close(i32 %6)
store i8* %33, i8** %0
br label %51
44: ; preds = %37
%45 = tail call i32 @free(i8* %33)
br label %46
46: ; preds = %28, %11, %20, %24, %44
%47 = phi i32 [ -1, %44 ], [ -3, %28 ], [ -2, %11 ], [ -2, %20 ], [ -2, %24 ]
%48 = icmp sgt i32 %6, 0
br i1 %48, label %49, label %51
49: ; preds = %46
%50 = tail call i32 @close(i32 %6)
br label %51
51: ; preds = %46, %49, %2, %8, %40
%52 = phi i32 [ 0, %40 ], [ -1, %8 ], [ -1, %2 ], [ %47, %49 ], [ %47, %46 ]
ret i32 %52
}