#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 {
  size_t st_size;
  int /*<<< orphan*/ st_mode;
};

/* Variables and functions */
int /*<<< orphan*/ LOG_DEBUG;
int /*<<< orphan*/ LOG_ERR;
int /*<<< orphan*/ O_RDONLY;
scalar_t__ S_ISREG(int /*<<< orphan*/);
int /*<<< orphan*/ asprintf(char **, char *, char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(char *);
scalar_t__ fstat(int, struct stat *);
char *malloc(int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
size_t read(int, char *, size_t);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, char *);
int read_mimefile(char *filename, char *mimetype) {
  char *mimefile;
  int fd;
  struct stat stat_buf;
  asprintf(&mimefile, "%s.mime", filename);
  fd = open(mimefile, O_RDONLY);
  if (fd == -1) {
    free(mimefile);
    return 0;
  } else {
    if (fstat(fd, &stat_buf) == 0) {
      if (S_ISREG(stat_buf.st_mode)) {
        mimetype = malloc(stat_buf.st_size + 1);
        if (read(fd, mimetype, stat_buf.st_size) != stat_buf.st_size) {
          syslog(LOG_ERR, "failed to read %s", mimefile);
          free(mimefile);
          free(mimetype);
          close(fd);
          return 0;
        }
        mimetype[stat_buf.st_size] = '\0';
        syslog(LOG_DEBUG, "mime type set from file: %s", mimetype);
      }
    }
    close(fd);
  }
  free(mimefile);
  return 1;
}


x86 assembly (clang, -O0):
<read_mimefile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdx
lea    -0x20(%rbp),%rdi
lea    0xe51(%rip),%rsi
call   1090 <asprintf@plt>
mov    -0x20(%rbp),%rdi
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11e8 <read_mimefile+0x58>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
jmp    12be <read_mimefile+0x12e>
mov    -0x24(%rbp),%edi
lea    -0x38(%rbp),%rsi
call   10b0 <fstat@plt>
cmp    $0x0,%rax
jne    12a6 <read_mimefile+0x116>
mov    -0x30(%rbp),%edi
call   1060 <S_ISREG@plt>
cmp    $0x0,%rax
je     12a1 <read_mimefile+0x111>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x24(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x38(%rbp),%rdx
call   1050 <read@plt>
cmp    -0x38(%rbp),%rax
je     127c <read_mimefile+0xec>
mov    0x2d84(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xdb7(%rip),%rsi
call   1080 <syslog@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    12be <read_mimefile+0x12e>
mov    -0x18(%rbp),%rax
mov    -0x38(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    0x2d49(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xd7e(%rip),%rsi
call   1080 <syslog@plt>
jmp    12a6 <read_mimefile+0x116>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_mimefile>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rdi,%rdx
lea    0xe5d(%rip),%rsi
lea    0x8(%rsp),%rdi
call   1090 <asprintf@plt>
mov    0x8(%rsp),%rdi
mov    0x2e2f(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     11f0 <read_mimefile+0x60>
mov    %eax,%ebx
lea    0x10(%rsp),%rsi
mov    %eax,%edi
call   10b0 <fstat@plt>
test   %rax,%rax
je     1206 <read_mimefile+0x76>
mov    %ebx,%edi
call   1040 <close@plt>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
mov    $0x1,%eax
jmp    11fc <read_mimefile+0x6c>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x18(%rsp),%edi
call   1060 <S_ISREG@plt>
test   %rax,%rax
je     11d8 <read_mimefile+0x48>
mov    0x10(%rsp),%r15
lea    0x1(%r15),%edi
call   1070 <malloc@plt>
mov    %rax,%r14
mov    %ebx,%edi
mov    %rax,%rsi
mov    %r15,%rdx
call   1050 <read@plt>
cmp    %r15,%rax
jne    1256 <read_mimefile+0xc6>
movb   $0x0,(%r14,%r15,1)
mov    0x2d95(%rip),%rax
mov    (%rax),%edi
lea    0xdce(%rip),%rsi
mov    %r14,%rdx
call   1080 <syslog@plt>
jmp    11d8 <read_mimefile+0x48>
mov    0x2d6b(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rsp),%rdx
lea    0xd9d(%rip),%rsi
call   1080 <syslog@plt>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %ebx,%edi
call   1040 <close@plt>
jmp    11fa <read_mimefile+0x6a>

LLVM IR (-O0):
%struct.stat = type { i64, i32 }
@.str = [8 x i8] c"%s.mime\00"
@O_RDONLY = global i32 0
@LOG_ERR = global i32 0
@.str.1 = [18 x i8] c"failed to read %s\00"
@LOG_DEBUG = global i32 0
@.str.2 = [28 x i8] c"mime type set from file: %s\00"
define i32 @read_mimefile(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca %struct.stat
store i8* %0, i8** %4
store i8* %1, i8** %5
%9 = load i8*, i8** %4
%10 = call i32 @asprintf(i8** %6, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %9)
%11 = load i8*, i8** %6
%12 = load i32, i32* @O_RDONLY
%13 = call i32 @open(i8* %11, i32 %12)
store i32 %13, i32* %7
%14 = load i32, i32* %7
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %19
16: ; preds = %2
%17 = load i8*, i8** %6
%18 = call i32 @free(i8* %17)
store i32 0, i32* %3
br label %67
19: ; preds = %2
%20 = load i32, i32* %7
%21 = call i64 @fstat(i32 %20, %struct.stat* %8)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %61
23: ; preds = %19
%24 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 1
%25 = load i32, i32* %24
%26 = call i64 @S_ISREG(i32 %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %60
28: ; preds = %23
%29 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%30 = load i64, i64* %29
%31 = add i64 %30, 1
%32 = trunc i64 %31 to i32
%33 = call i8* @malloc(i32 %32)
store i8* %33, i8** %5
%34 = load i32, i32* %7
%35 = load i8*, i8** %5
%36 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%37 = load i64, i64* %36
%38 = call i64 @read(i32 %34, i8* %35, i64 %37)
%39 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%40 = load i64, i64* %39
%41 = icmp ne i64 %38, %40
br i1 %41, label %42, label %52
42: ; preds = %28
%43 = load i32, i32* @LOG_ERR
%44 = load i8*, i8** %6
%45 = call i32 @syslog(i32 %43, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %44)
%46 = load i8*, i8** %6
%47 = call i32 @free(i8* %46)
%48 = load i8*, i8** %5
%49 = call i32 @free(i8* %48)
%50 = load i32, i32* %7
%51 = call i32 @close(i32 %50)
store i32 0, i32* %3
br label %67
52: ; preds = %28
%53 = load i8*, i8** %5
%54 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%55 = load i64, i64* %54
%56 = getelementptr inbounds i8, i8* %53, i64 %55
store i8 0, i8* %56
%57 = load i32, i32* @LOG_DEBUG
%58 = load i8*, i8** %5
%59 = call i32 @syslog(i32 %57, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %58)
br label %60
60: ; preds = %52, %23
br label %61
61: ; preds = %60, %19
%62 = load i32, i32* %7
%63 = call i32 @close(i32 %62)
br label %64
64: ; preds = %61
%65 = load i8*, i8** %6
%66 = call i32 @free(i8* %65)
store i32 1, i32* %3
br label %67
67: ; preds = %64, %42, %16
%68 = load i32, i32* %3
ret i32 %68
}

LLVM IR (-O2):
%struct.stat = type { i64, i32 }
@.str = [8 x i8] c"%s.mime\00"
@O_RDONLY = global i32 0
@LOG_ERR = global i32 0
@.str.1 = [18 x i8] c"failed to read %s\00"
@LOG_DEBUG = global i32 0
@.str.2 = [28 x i8] c"mime type set from file: %s\00"
define i32 @read_mimefile(i8* %0, i8* nocapture readnone %1) {
%3 = alloca i8*
%4 = alloca %struct.stat
%5 = bitcast i8** %3 to i8*
%6 = bitcast %struct.stat* %4 to i8*
%7 = call i32 @asprintf(i8** %3, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %0)
%8 = load i8*, i8** %3
%9 = load i32, i32* @O_RDONLY
%10 = call i32 @open(i8* %8, i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = load i8*, i8** %3
%14 = call i32 @free(i8* %13)
br label %47
15: ; preds = %2
%16 = call i64 @fstat(i32 %10, %struct.stat* %4)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %43
18: ; preds = %15
%19 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 1
%20 = load i32, i32* %19
%21 = call i64 @S_ISREG(i32 %20)
%22 = icmp eq i64 %21, 0
br i1 %22, label %43, label %23
23: ; preds = %18
%24 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%25 = load i64, i64* %24
%26 = trunc i64 %25 to i32
%27 = add i32 %26, 1
%28 = call i8* @malloc(i32 %27)
%29 = call i64 @read(i32 %10, i8* %28, i64 %25)
%30 = icmp eq i64 %29, %25
br i1 %30, label %39, label %31
31: ; preds = %23
%32 = load i32, i32* @LOG_ERR
%33 = load i8*, i8** %3
%34 = call i32 @syslog(i32 %32, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %33)
%35 = load i8*, i8** %3
%36 = call i32 @free(i8* %35)
%37 = call i32 @free(i8* %28)
%38 = call i32 @close(i32 %10)
br label %47
39: ; preds = %23
%40 = getelementptr inbounds i8, i8* %28, i64 %25
store i8 0, i8* %40
%41 = load i32, i32* @LOG_DEBUG
%42 = call i32 @syslog(i32 %41, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %28)
br label %43
43: ; preds = %18, %39, %15
%44 = call i32 @close(i32 %10)
%45 = load i8*, i8** %3
%46 = call i32 @free(i8* %45)
br label %47
47: ; preds = %43, %31, %12
%48 = phi i32 [ 0, %12 ], [ 0, %31 ], [ 1, %43 ]
ret i32 %48
}