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

/* Variables and functions */
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
char *get_exec_cmd(char *);
char *get_expression(int, char **);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char **argv) {
  if (argc < 2) {
    printf("Usage - `hexcalc {expression}`\n");
    return 0;
  }
  FILE *fp;
  int status;
  char path[1024];
  char *expression = get_expression(argc, argv);
  printf("evaling: %s\n", expression);
  char *exec_cmd = get_exec_cmd(expression);
  fp = popen(exec_cmd, "r");
  if (fp == NULL) {
    printf("Failed to run command\n");
    return 1;
  }
  while (fgets(path, sizeof(path) - 1, fp) != NULL) {
    printf("result : %s", path);
  }
  free(expression);
  free(exec_cmd);
  pclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11ad <main+0x3d>
lea    0xe66(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    128a <main+0x11a>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1090 <get_expression@plt>
mov    %rax,-0x428(%rbp)
mov    -0x428(%rbp),%rsi
lea    0xe52(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x428(%rbp),%rdi
call   1080 <get_exec_cmd@plt>
mov    %rax,-0x430(%rbp)
mov    -0x430(%rbp),%rdi
lea    0xe37(%rip),%rsi
call   1070 <popen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1224 <main+0xb4>
lea    0xe1e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    128a <main+0x11a>
jmp    1229 <main+0xb9>
lea    -0x420(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x3ff,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     1262 <main+0xf2>
lea    -0x420(%rbp),%rsi
lea    0xdf0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1229 <main+0xb9>
mov    -0x428(%rbp),%rdi
call   1030 <free@plt>
mov    -0x430(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1050 <pclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x400,%rsp
cmp    $0x1,%edi
jg     11a6 <main+0x26>
lea    0xe95(%rip),%rdi
call   1040 <puts@plt>
jmp    123a <main+0xba>
call   10a0 <get_expression@plt>
mov    %rax,%r15
lea    0xe4b(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r15,%rdi
call   1090 <get_exec_cmd@plt>
mov    %rax,%r14
lea    0xe3c(%rip),%rsi
mov    %rax,%rdi
call   1080 <popen@plt>
test   %rax,%rax
je     123e <main+0xbe>
mov    %rax,%r13
mov    %rsp,%rdi
mov    $0x3ff,%esi
mov    %rax,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     1222 <main+0xa2>
lea    0xe12(%rip),%r12
mov    %rsp,%rbx
mov    %r12,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
mov    $0x3ff,%esi
mov    %r13,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
jne    1200 <main+0x80>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r13,%rdi
call   1060 <pclose@plt>
xor    %eax,%eax
jmp    124f <main+0xcf>
lea    0xdd6(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [32 x i8] c"Usage - `hexcalc {expression}`\0A\00"
@.str.1 = [13 x i8] c"evaling: %s\0A\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [23 x i8] c"Failed to run command\0A\00"
@.str.4 = [12 x i8] c"result : %s\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32
%8 = alloca [1024 x i8]
%9 = alloca i8*
%10 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 2
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %3
br label %45
15: ; preds = %2
%16 = load i32, i32* %4
%17 = load i8**, i8*** %5
%18 = call i8* @get_expression(i32 %16, i8** %17)
store i8* %18, i8** %9
%19 = load i8*, i8** %9
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %19)
%21 = load i8*, i8** %9
%22 = call i8* @get_exec_cmd(i8* %21)
store i8* %22, i8** %10
%23 = load i8*, i8** %10
%24 = call i32* @popen(i8* %23, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %24, i32** %6
%25 = load i32*, i32** %6
%26 = icmp eq i32* %25, null
br i1 %26, label %27, label %29
27: ; preds = %15
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %45
29: ; preds = %15
br label %30
30: ; preds = %35, %29
%31 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%32 = load i32*, i32** %6
%33 = call i32* @fgets(i8* %31, i32 1023, i32* %32)
%34 = icmp ne i32* %33, null
br i1 %34, label %35, label %38
35: ; preds = %30
%36 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i8* %36)
br label %30
38: ; preds = %30
%39 = load i8*, i8** %9
%40 = call i32 @free(i8* %39)
%41 = load i8*, i8** %10
%42 = call i32 @free(i8* %41)
%43 = load i32*, i32** %6
%44 = call i32 @pclose(i32* %43)
store i32 0, i32* %3
br label %45
45: ; preds = %38, %27, %13
%46 = load i32, i32* %3
ret i32 %46
}

LLVM IR (-O2):
@.str.1 = [13 x i8] c"evaling: %s\0A\00"
@.str.2 = [2 x i8] c"r\00"
@.str.4 = [12 x i8] c"result : %s\00"
@str = [22 x i8] c"Failed to run command\00"
@str.5 = [31 x i8] c"Usage - `hexcalc {expression}`\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca [1024 x i8]
%4 = icmp slt i32 %0, 2
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.5, i64 0, i64 0))
br label %29
7: ; preds = %2
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%9 = tail call i8* @get_expression(i32 %0, i8** %1)
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %9)
%11 = tail call i8* @get_exec_cmd(i8* %9)
%12 = tail call i32* @popen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %17, label %14
14: ; preds = %7
%15 = call i32* @fgets(i8* %8, i32 1023, i32* %12)
%16 = icmp eq i32* %15, null
br i1 %16, label %23, label %19
17: ; preds = %7
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
br label %27
19: ; preds = %14, %19
%20 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i8* %8)
%21 = call i32* @fgets(i8* %8, i32 1023, i32* %12)
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %19
23: ; preds = %19, %14
%24 = call i32 @free(i8* %9)
%25 = call i32 @free(i8* %11)
%26 = call i32 @pclose(i32* %12)
br label %27
27: ; preds = %23, %17
%28 = phi i32 [ 1, %17 ], [ 0, %23 ]
br label %29
29: ; preds = %27, %5
%30 = phi i32 [ 0, %5 ], [ %28, %27 ]
ret i32 %30
}