#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 */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, char const *);
char *strchr(char *, char);
char *strdup(char *);
int strlen(char *);
char *get_mimetype_by_name(const char *name) {
  char *mime_type;
  char *ptr;
  char buf[1024];
  sprintf(buf, "mimetype '%s'", name);
  FILE *fp = popen(buf, "re");
  if (fp == NULL)
    do {
      perror("popen");
      exit(EXIT_FAILURE);
    } while (0);
  fgets(buf, 1024, fp);
  buf[strlen(buf) - 1] = '\0';
  if ((ptr = strchr(buf, ':')) == NULL)
    do {
      perror("mimetype_strchr");
      exit(EXIT_FAILURE);
    } while (0);
  if ((mime_type = strdup(ptr + 2)) == NULL)
    do {
      perror("strdup_mimetype");
      exit(EXIT_FAILURE);
    } while (0);
  pclose(fp);
  return mime_type;
}


x86 assembly (clang, -O0):
<get_mimetype_by_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x420(%rbp),%rdi
mov    -0x8(%rbp),%rdx
lea    0xe4f(%rip),%rsi
call   1090 <sprintf@plt>
lea    -0x420(%rbp),%rdi
lea    0xe4a(%rip),%rsi
call   1070 <popen@plt>
mov    %rax,-0x428(%rbp)
cmpq   $0x0,-0x428(%rbp)
jne    11fd <get_mimetype_by_name+0x6d>
jmp    11e3 <get_mimetype_by_name+0x53>
lea    0xe27(%rip),%rdi
call   1080 <perror@plt>
mov    0x2dea(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
lea    -0x420(%rbp),%rdi
mov    -0x428(%rbp),%rdx
mov    $0x400,%esi
call   1060 <fgets@plt>
lea    -0x420(%rbp),%rdi
call   1030 <strlen@plt>
sub    $0x1,%eax
cltq
movb   $0x0,-0x420(%rbp,%rax,1)
lea    -0x420(%rbp),%rdi
mov    $0x3a,%esi
call   1040 <strchr@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    126c <get_mimetype_by_name+0xdc>
jmp    1252 <get_mimetype_by_name+0xc2>
lea    0xdbe(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d7b(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x18(%rbp),%rdi
add    $0x2,%rdi
call   10b0 <strdup@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    12a6 <get_mimetype_by_name+0x116>
jmp    128c <get_mimetype_by_name+0xfc>
lea    0xd94(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d41(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x428(%rbp),%rdi
call   1050 <pclose@plt>
mov    -0x10(%rbp),%rax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_mimetype_by_name>:
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %rdi,%rdx
lea    0xe5c(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1090 <sprintf@plt>
lea    0xe58(%rip),%rsi
mov    %rbx,%rdi
call   1070 <popen@plt>
test   %rax,%rax
je     1221 <get_mimetype_by_name+0x91>
mov    %rax,%r14
mov    %rbx,%rdi
mov    $0x400,%esi
mov    %rax,%rdx
call   1060 <fgets@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
mov    %rbx,%rdi
mov    $0x3a,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     122a <get_mimetype_by_name+0x9a>
add    $0x2,%rax
mov    %rax,%rdi
call   10b0 <strdup@plt>
test   %rax,%rax
je     1233 <get_mimetype_by_name+0xa3>
mov    %rax,%rbx
mov    %r14,%rdi
call   1050 <pclose@plt>
mov    %rbx,%rax
add    $0x408,%rsp
pop    %rbx
pop    %r14
ret
lea    0xde9(%rip),%rdi
jmp    123a <get_mimetype_by_name+0xaa>
lea    0xde6(%rip),%rdi
jmp    123a <get_mimetype_by_name+0xaa>
lea    0xded(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d9a(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [14 x i8] c"mimetype '%s'\00"
@.str.1 = [3 x i8] c"re\00"
@.str.2 = [6 x i8] c"popen\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [16 x i8] c"mimetype_strchr\00"
@.str.4 = [16 x i8] c"strdup_mimetype\00"
define i8* @get_mimetype_by_name(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca [1024 x i8]
%6 = alloca i32*
store i8* %0, i8** %2
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%8 = load i8*, i8** %2
%9 = call i32 @sprintf(i8* %7, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %8)
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%11 = call i32* @popen(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %11, i32** %6
%12 = load i32*, i32** %6
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %20
14: ; preds = %1
br label %15
15: ; preds = %14
%16 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%17 = load i32, i32* @EXIT_FAILURE
%18 = call i32 @exit(i32 %17)
unreachable
19: ; No predecessors!
br label %20
20: ; preds = %19, %1
%21 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%22 = load i32*, i32** %6
%23 = call i32 @fgets(i8* %21, i32 1024, i32* %22)
%24 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%25 = call i32 @strlen(i8* %24)
%26 = sub nsw i32 %25, 1
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 %27
store i8 0, i8* %28
%29 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%30 = call i8* @strchr(i8* %29, i8 signext 58)
store i8* %30, i8** %4
%31 = icmp eq i8* %30, null
br i1 %31, label %32, label %38
32: ; preds = %20
br label %33
33: ; preds = %32
%34 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%35 = load i32, i32* @EXIT_FAILURE
%36 = call i32 @exit(i32 %35)
unreachable
37: ; No predecessors!
br label %38
38: ; preds = %37, %20
%39 = load i8*, i8** %4
%40 = getelementptr inbounds i8, i8* %39, i64 2
%41 = call i8* @strdup(i8* %40)
store i8* %41, i8** %3
%42 = icmp eq i8* %41, null
br i1 %42, label %43, label %49
43: ; preds = %38
br label %44
44: ; preds = %43
%45 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
%46 = load i32, i32* @EXIT_FAILURE
%47 = call i32 @exit(i32 %46)
unreachable
48: ; No predecessors!
br label %49
49: ; preds = %48, %38
%50 = load i32*, i32** %6
%51 = call i32 @pclose(i32* %50)
%52 = load i8*, i8** %3
ret i8* %52
}

LLVM IR (-O2):
@.str = [14 x i8] c"mimetype '%s'\00"
@.str.1 = [3 x i8] c"re\00"
@.str.2 = [6 x i8] c"popen\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [16 x i8] c"mimetype_strchr\00"
@.str.4 = [16 x i8] c"strdup_mimetype\00"
define noalias i8* @get_mimetype_by_name(i8* %0) {
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%4 = call i32 @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %0)
%5 = call i32* @popen(i8* %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %11
7: ; preds = %1
%8 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%9 = load i32, i32* @EXIT_FAILURE
%10 = tail call i32 @exit(i32 %9)
unreachable
11: ; preds = %1
%12 = call i32 @fgets(i8* %3, i32 1024, i32* %5)
%13 = call i32 @strlen(i8* %3)
%14 = add nsw i32 %13, -1
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 %15
store i8 0, i8* %16
%17 = call i8* @strchr(i8* %3, i8 signext 58)
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %23
19: ; preds = %11
%20 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%21 = load i32, i32* @EXIT_FAILURE
%22 = call i32 @exit(i32 %21)
unreachable
23: ; preds = %11
%24 = getelementptr inbounds i8, i8* %17, i64 2
%25 = call i8* @strdup(i8* %24)
%26 = icmp eq i8* %25, null
br i1 %26, label %27, label %31
27: ; preds = %23
%28 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
%29 = load i32, i32* @EXIT_FAILURE
%30 = call i32 @exit(i32 %29)
unreachable
31: ; preds = %23
%32 = call i32 @pclose(i32* %5)
ret i8* %25
}