#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*/ command;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *);
char *strchr(char *, char);
void guess_file_extension(const char *file, char *extension,
                          int extension_size) {
  memset(extension, 0, extension_size);
  char command[255] = {'\0'};
  snprintf(command, sizeof(command), "file -b --extension %s", file);
  FILE *f = popen(command, "r");
  if (f != NULL) {
    fgets(extension, extension_size, f);
    char *end = strchr(extension, '\n');
    if (end) {
      *end = '\0';
    }
    end = strchr(extension, '/');
    if (end) {
      *end = '\0';
    }
    pclose(f);
  }
}


x86 assembly (clang, -O0):
<guess_file_extension>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
xor    %esi,%esi
call   1060 <memset@plt>
lea    -0x120(%rbp),%rdi
xor    %esi,%esi
mov    $0xff,%edx
call   1060 <memset@plt>
lea    -0x120(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0xff,%esi
lea    0xe52(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x120(%rbp),%rdi
lea    0xe56(%rip),%rsi
call   1080 <popen@plt>
mov    %rax,-0x128(%rbp)
cmpq   $0x0,-0x128(%rbp)
je     1254 <guess_file_extension+0xf4>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x128(%rbp),%rdx
call   1070 <fgets@plt>
mov    -0x10(%rbp),%rdi
mov    $0xa,%esi
call   1030 <strchr@plt>
mov    %rax,-0x130(%rbp)
cmpq   $0x0,-0x130(%rbp)
je     121b <guess_file_extension+0xbb>
mov    -0x130(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1030 <strchr@plt>
mov    %rax,-0x130(%rbp)
cmpq   $0x0,-0x130(%rbp)
je     1248 <guess_file_extension+0xe8>
mov    -0x130(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x128(%rbp),%rdi
call   1040 <pclose@plt>
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<guess_file_extension>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x108,%rsp
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %rdi,%r15
mov    %rsi,%rdi
xor    %esi,%esi
call   1060 <memset@plt>
xorps  %xmm0,%xmm0
movups %xmm0,0xef(%rsp)
movaps %xmm0,0xe0(%rsp)
movaps %xmm0,0xd0(%rsp)
movaps %xmm0,0xc0(%rsp)
movaps %xmm0,0xb0(%rsp)
movaps %xmm0,0xa0(%rsp)
movaps %xmm0,0x90(%rsp)
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
lea    0xe0f(%rip),%rdx
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0xff,%esi
mov    %r15,%rcx
call   1050 <snprintf@plt>
lea    0xe0c(%rip),%rsi
mov    %rbp,%rdi
call   1080 <popen@plt>
test   %rax,%rax
je     125b <guess_file_extension+0xfb>
mov    %rax,%rbp
mov    %rbx,%rdi
mov    %r14d,%esi
mov    %rax,%rdx
call   1070 <fgets@plt>
mov    %rbx,%rdi
mov    $0xa,%esi
call   1030 <strchr@plt>
test   %rax,%rax
je     123e <guess_file_extension+0xde>
movb   $0x0,(%rax)
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1030 <strchr@plt>
test   %rax,%rax
je     1253 <guess_file_extension+0xf3>
movb   $0x0,(%rax)
mov    %rbp,%rdi
call   1040 <pclose@plt>
add    $0x108,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [23 x i8] c"file -b --extension %s\00"
@.str.1 = [2 x i8] c"r\00"
define void @guess_file_extension(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca [255 x i8]
%8 = alloca i32*
%9 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%10 = load i8*, i8** %5
%11 = load i32, i32* %6
%12 = call i32 @memset(i8* %10, i32 0, i32 %11)
%13 = bitcast [255 x i8]* %7 to i8*
%14 = getelementptr inbounds [255 x i8], [255 x i8]* %7, i64 0, i64 0
%15 = load i8*, i8** %4
%16 = call i32 @snprintf(i8* %14, i32 255, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = getelementptr inbounds [255 x i8], [255 x i8]* %7, i64 0, i64 0
%18 = call i32* @popen(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %18, i32** %8
%19 = load i32*, i32** %8
%20 = icmp ne i32* %19, null
br i1 %20, label %21, label %42
21: ; preds = %3
%22 = load i8*, i8** %5
%23 = load i32, i32* %6
%24 = load i32*, i32** %8
%25 = call i32 @fgets(i8* %22, i32 %23, i32* %24)
%26 = load i8*, i8** %5
%27 = call i8* @strchr(i8* %26, i8 signext 10)
store i8* %27, i8** %9
%28 = load i8*, i8** %9
%29 = icmp ne i8* %28, null
br i1 %29, label %30, label %32
30: ; preds = %21
%31 = load i8*, i8** %9
store i8 0, i8* %31
br label %32
32: ; preds = %30, %21
%33 = load i8*, i8** %5
%34 = call i8* @strchr(i8* %33, i8 signext 47)
store i8* %34, i8** %9
%35 = load i8*, i8** %9
%36 = icmp ne i8* %35, null
br i1 %36, label %37, label %39
37: ; preds = %32
%38 = load i8*, i8** %9
store i8 0, i8* %38
br label %39
39: ; preds = %37, %32
%40 = load i32*, i32** %8
%41 = call i32 @pclose(i32* %40)
br label %42
42: ; preds = %39, %3
ret void
}

LLVM IR (-O2):
@.str = [23 x i8] c"file -b --extension %s\00"
@.str.1 = [2 x i8] c"r\00"
define void @guess_file_extension(i8* %0, i8* %1, i32 %2) {
%4 = alloca [255 x i8]
%5 = tail call i32 @memset(i8* %1, i32 0, i32 %2)
%6 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 0
%7 = call i32 @snprintf(i8* %6, i32 255, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %0)
%8 = call i32* @popen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %21, label %10
10: ; preds = %3
%11 = call i32 @fgets(i8* %1, i32 %2, i32* %8)
%12 = call i8* @strchr(i8* dereferenceable(1) %1, i8 signext 10)
%13 = icmp eq i8* %12, null
br i1 %13, label %15, label %14
14: ; preds = %10
store i8 0, i8* %12
br label %15
15: ; preds = %14, %10
%16 = call i8* @strchr(i8* dereferenceable(1) %1, i8 signext 47)
%17 = icmp eq i8* %16, null
br i1 %17, label %19, label %18
18: ; preds = %15
store i8 0, i8* %16
br label %19
19: ; preds = %18, %15
%20 = call i32 @pclose(i32* %8)
br label %21
21: ; preds = %19, %3
ret void
}