#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 */
long MM_CONSOLE;
int MM_NOCON;
int MM_NOTOK;
int MM_OK;
long MM_PRINT;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, char *);
int /*<<< orphan*/ free(char *);
char *getenv(char *);
char *printfmt(char *, long, char const *, int, char const *, char const *,
               char const *);
int /*<<< orphan*/ *stderr;
char *strdup(char *);
scalar_t__ strlen(char *);
scalar_t__ validmsgverb(char *);
int fmtmsg(long class, const char *label, int sev, const char *text,
           const char *action, const char *tag) {
  FILE *fp;
  char *env, *msgverb, *output;
  if (class & MM_PRINT) {
    if ((env = getenv("MSGVERB")) != NULL && *env != '\0' &&
        strlen(env) <= strlen("label:severity:text:action:tag")) {
      if ((msgverb = strdup(env)) == NULL)
        return (MM_NOTOK);
      else if (validmsgverb(msgverb) == 0) {
        free(msgverb);
        goto def;
      }
    } else {
    def:
      if ((msgverb = strdup("label:severity:text:action:tag")) == NULL)
        return (MM_NOTOK);
    }
    output = printfmt(msgverb, class, label, sev, text, action, tag);
    if (output == NULL) {
      free(msgverb);
      return (MM_NOTOK);
    }
    if (*output != '\0')
      fprintf(stderr, "%s", output);
    free(msgverb);
    free(output);
  }
  if (class & MM_CONSOLE) {
    output = printfmt("label:severity:text:action:tag", class, label, sev, text,
                      action, tag);
    if (output == NULL)
      return (MM_NOCON);
    if (*output != '\0') {
      if ((fp = fopen("/dev/console", "a")) == NULL) {
        free(output);
        return (MM_NOCON);
      }
      fprintf(fp, "%s", output);
      fclose(fp);
    }
    free(output);
  }
  return (MM_OK);
}


x86 assembly (clang, -O0):
<fmtmsg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    %r9,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2e26(%rip),%rcx
and    (%rcx),%rax
cmp    $0x0,%rax
je     132b <fmtmsg+0x19b>
lea    0xe32(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x48(%rbp)
cmp    $0x0,%rax
je     126d <fmtmsg+0xdd>
mov    -0x48(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     126d <fmtmsg+0xdd>
mov    -0x48(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,-0x60(%rbp)
lea    0xe03(%rip),%rdi
call   1060 <strlen@plt>
mov    %rax,%rcx
mov    -0x60(%rbp),%rax
cmp    %rcx,%rax
jg     126d <fmtmsg+0xdd>
mov    -0x48(%rbp),%rdi
call   10b0 <strdup@plt>
mov    %rax,-0x50(%rbp)
cmp    $0x0,%rax
jne    1242 <fmtmsg+0xb2>
mov    0x2d88(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1407 <fmtmsg+0x277>
mov    -0x50(%rbp),%rdi
call   1090 <validmsgverb@plt>
cmp    $0x0,%rax
jne    1263 <fmtmsg+0xd3>
mov    -0x50(%rbp),%rdi
call   1040 <free@plt>
jmp    1272 <fmtmsg+0xe2>
jmp    1268 <fmtmsg+0xd8>
jmp    12a2 <fmtmsg+0x112>
jmp    1272 <fmtmsg+0xe2>
lea    0xd8f(%rip),%rdi
call   10b0 <strdup@plt>
mov    %rax,-0x50(%rbp)
cmp    $0x0,%rax
jne    129d <fmtmsg+0x10d>
mov    0x2d2d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1407 <fmtmsg+0x277>
jmp    12a2 <fmtmsg+0x112>
mov    -0x50(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
mov    -0x28(%rbp),%r8
mov    -0x30(%rbp),%r9
mov    -0x38(%rbp),%rax
mov    %rax,(%rsp)
call   1080 <printfmt@plt>
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
jne    12ef <fmtmsg+0x15f>
mov    -0x50(%rbp),%rdi
call   1040 <free@plt>
mov    0x2cdb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1407 <fmtmsg+0x277>
mov    -0x58(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1319 <fmtmsg+0x189>
mov    0x2cc2(%rip),%rax
mov    (%rax),%rdi
mov    -0x58(%rbp),%rdx
lea    0xd13(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x50(%rbp),%rdi
call   1040 <free@plt>
mov    -0x58(%rbp),%rdi
call   1040 <free@plt>
mov    -0x10(%rbp),%rax
mov    0x2ca2(%rip),%rcx
and    (%rcx),%rax
cmp    $0x0,%rax
je     13fb <fmtmsg+0x26b>
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
mov    -0x28(%rbp),%r8
mov    -0x30(%rbp),%r9
mov    -0x38(%rbp),%rax
lea    0xca7(%rip),%rdi
mov    %rax,(%rsp)
call   1080 <printfmt@plt>
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
jne    138a <fmtmsg+0x1fa>
mov    0x2c78(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1407 <fmtmsg+0x277>
mov    -0x58(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     13f2 <fmtmsg+0x262>
lea    0xc89(%rip),%rdi
lea    0xc8f(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
jne    13d5 <fmtmsg+0x245>
mov    -0x58(%rbp),%rdi
call   1040 <free@plt>
mov    0x2c2d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1407 <fmtmsg+0x277>
mov    -0x40(%rbp),%rdi
mov    -0x58(%rbp),%rdx
lea    0xc43(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x40(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x58(%rbp),%rdi
call   1040 <free@plt>
mov    0x2bae(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fmtmsg>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r9,%r12
mov    %r8,%r15
mov    %rcx,%r8
mov    %edx,%ecx
mov    %rsi,%r13
mov    %rdi,%rbx
mov    0x2e2a(%rip),%rax
test   %rdi,(%rax)
je     1290 <fmtmsg+0x100>
mov    %ecx,0xc(%rsp)
mov    %r8,0x10(%rsp)
lea    0xe31(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1218 <fmtmsg+0x88>
mov    %rax,%rbp
cmpb   $0x0,(%rax)
je     1218 <fmtmsg+0x88>
mov    %rbp,%rdi
call   1060 <strlen@plt>
cmp    $0x1e,%rax
jg     1218 <fmtmsg+0x88>
mov    %rbp,%rdi
call   10b0 <strdup@plt>
test   %rax,%rax
je     12f9 <fmtmsg+0x169>
mov    %rax,%r14
mov    %rax,%rdi
call   1090 <validmsgverb@plt>
test   %rax,%rax
jne    1230 <fmtmsg+0xa0>
mov    %r14,%rdi
call   1040 <free@plt>
lea    0xde9(%rip),%rdi
call   10b0 <strdup@plt>
mov    %rax,%r14
test   %rax,%rax
je     12f9 <fmtmsg+0x169>
mov    %r12,(%rsp)
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %r13,%rdx
mov    0xc(%rsp),%ecx
mov    0x10(%rsp),%r8
mov    %r15,%rbp
mov    %r15,%r9
call   1080 <printfmt@plt>
test   %rax,%rax
je     1314 <fmtmsg+0x184>
mov    %rax,%r15
cmpb   $0x0,(%rax)
je     1274 <fmtmsg+0xe4>
mov    0x2d5f(%rip),%rax
mov    (%rax),%rsi
mov    %r15,%rdi
call   1070 <fputs@plt>
mov    %r14,%rdi
call   1040 <free@plt>
mov    %r15,%rdi
call   1040 <free@plt>
mov    %rbp,%r15
mov    0x10(%rsp),%r8
mov    0xc(%rsp),%ecx
mov    0x2d41(%rip),%rax
test   %rbx,(%rax)
je     1302 <fmtmsg+0x172>
mov    %r12,(%rsp)
lea    0xd61(%rip),%rdi
mov    %rbx,%rsi
mov    %r13,%rdx
mov    %r15,%r9
call   1080 <printfmt@plt>
test   %rax,%rax
je     130b <fmtmsg+0x17b>
mov    %rax,%r14
mov    0x2cec(%rip),%rbp
cmpb   $0x0,(%rax)
je     1324 <fmtmsg+0x194>
lea    0xd57(%rip),%rdi
lea    0xd5d(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     131d <fmtmsg+0x18d>
mov    %rax,%rbx
mov    %r14,%rdi
mov    %rax,%rsi
call   1070 <fputs@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
jmp    1324 <fmtmsg+0x194>
mov    0x2cc0(%rip),%rbp
jmp    132c <fmtmsg+0x19c>
mov    0x2ca7(%rip),%rbp
jmp    132c <fmtmsg+0x19c>
mov    0x2ce6(%rip),%rbp
jmp    132c <fmtmsg+0x19c>
mov    0x2ca5(%rip),%rbp
jmp    1324 <fmtmsg+0x194>
mov    0x2cd4(%rip),%rbp
mov    %r14,%rdi
call   1040 <free@plt>
mov    0x0(%rbp),%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@MM_PRINT = global i64 0
@.str = [8 x i8] c"MSGVERB\00"
@.str.1 = [31 x i8] c"label:severity:text:action:tag\00"
@MM_NOTOK = global i32 0
@stderr = global i32* null
@.str.2 = [3 x i8] c"%s\00"
@MM_CONSOLE = global i64 0
@MM_NOCON = global i32 0
@.str.3 = [13 x i8] c"/dev/console\00"
@.str.4 = [2 x i8] c"a\00"
@MM_OK = global i32 0
define i32 @fmtmsg(i64 %0, i8* %1, i32 %2, i8* %3, i8* %4, i8* %5) {
%7 = alloca i32
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca i32*
%15 = alloca i8*
%16 = alloca i8*
%17 = alloca i8*
store i64 %0, i64* %8
store i8* %1, i8** %9
store i32 %2, i32* %10
store i8* %3, i8** %11
store i8* %4, i8** %12
store i8* %5, i8** %13
%18 = load i64, i64* %8
%19 = load i64, i64* @MM_PRINT
%20 = and i64 %18, %19
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %86
22: ; preds = %6
%23 = call i8* @getenv(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
store i8* %23, i8** %15
%24 = icmp ne i8* %23, null
br i1 %24, label %25, label %50
25: ; preds = %22
%26 = load i8*, i8** %15
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %50
30: ; preds = %25
%31 = load i8*, i8** %15
%32 = call i64 @strlen(i8* %31)
%33 = call i64 @strlen(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
%34 = icmp sle i64 %32, %33
br i1 %34, label %35, label %50
35: ; preds = %30
%36 = load i8*, i8** %15
%37 = call i8* @strdup(i8* %36)
store i8* %37, i8** %16
%38 = icmp eq i8* %37, null
br i1 %38, label %39, label %41
39: ; preds = %35
%40 = load i32, i32* @MM_NOTOK
store i32 %40, i32* %7
br label %126
41: ; preds = %35
%42 = load i8*, i8** %16
%43 = call i64 @validmsgverb(i8* %42)
%44 = icmp eq i64 %43, 0
br i1 %44, label %45, label %48
45: ; preds = %41
%46 = load i8*, i8** %16
%47 = call i32 @free(i8* %46)
br label %51
48: ; preds = %41
br label %49
49: ; preds = %48
br label %57
50: ; preds = %30, %25, %22
br label %51
51: ; preds = %50, %45
%52 = call i8* @strdup(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
store i8* %52, i8** %16
%53 = icmp eq i8* %52, null
br i1 %53, label %54, label %56
54: ; preds = %51
%55 = load i32, i32* @MM_NOTOK
store i32 %55, i32* %7
br label %126
56: ; preds = %51
br label %57
57: ; preds = %56, %49
%58 = load i8*, i8** %16
%59 = load i64, i64* %8
%60 = load i8*, i8** %9
%61 = load i32, i32* %10
%62 = load i8*, i8** %11
%63 = load i8*, i8** %12
%64 = load i8*, i8** %13
%65 = call i8* @printfmt(i8* %58, i64 %59, i8* %60, i32 %61, i8* %62, i8* %63, i8* %64)
store i8* %65, i8** %17
%66 = load i8*, i8** %17
%67 = icmp eq i8* %66, null
br i1 %67, label %68, label %72
68: ; preds = %57
%69 = load i8*, i8** %16
%70 = call i32 @free(i8* %69)
%71 = load i32, i32* @MM_NOTOK
store i32 %71, i32* %7
br label %126
72: ; preds = %57
%73 = load i8*, i8** %17
%74 = load i8, i8* %73
%75 = sext i8 %74 to i32
%76 = icmp ne i32 %75, 0
br i1 %76, label %77, label %81
77: ; preds = %72
%78 = load i32*, i32** @stderr
%79 = load i8*, i8** %17
%80 = call i32 @fprintf(i32* %78, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %79)
br label %81
81: ; preds = %77, %72
%82 = load i8*, i8** %16
%83 = call i32 @free(i8* %82)
%84 = load i8*, i8** %17
%85 = call i32 @free(i8* %84)
br label %86
86: ; preds = %81, %6
%87 = load i64, i64* %8
%88 = load i64, i64* @MM_CONSOLE
%89 = and i64 %87, %88
%90 = icmp ne i64 %89, 0
br i1 %90, label %91, label %124
91: ; preds = %86
%92 = load i64, i64* %8
%93 = load i8*, i8** %9
%94 = load i32, i32* %10
%95 = load i8*, i8** %11
%96 = load i8*, i8** %12
%97 = load i8*, i8** %13
%98 = call i8* @printfmt(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i64 %92, i8* %93, i32 %94, i8* %95, i8* %96, i8* %97)
store i8* %98, i8** %17
%99 = load i8*, i8** %17
%100 = icmp eq i8* %99, null
br i1 %100, label %101, label %103
101: ; preds = %91
%102 = load i32, i32* @MM_NOCON
store i32 %102, i32* %7
br label %126
103: ; preds = %91
%104 = load i8*, i8** %17
%105 = load i8, i8* %104
%106 = sext i8 %105 to i32
%107 = icmp ne i32 %106, 0
br i1 %107, label %108, label %121
108: ; preds = %103
%109 = call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %109, i32** %14
%110 = icmp eq i32* %109, null
br i1 %110, label %111, label %115
111: ; preds = %108
%112 = load i8*, i8** %17
%113 = call i32 @free(i8* %112)
%114 = load i32, i32* @MM_NOCON
store i32 %114, i32* %7
br label %126
115: ; preds = %108
%116 = load i32*, i32** %14
%117 = load i8*, i8** %17
%118 = call i32 @fprintf(i32* %116, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %117)
%119 = load i32*, i32** %14
%120 = call i32 @fclose(i32* %119)
br label %121
121: ; preds = %115, %103
%122 = load i8*, i8** %17
%123 = call i32 @free(i8* %122)
br label %124
124: ; preds = %121, %86
%125 = load i32, i32* @MM_OK
store i32 %125, i32* %7
br label %126
126: ; preds = %124, %111, %101, %68, %54, %39
%127 = load i32, i32* %7
ret i32 %127
}

LLVM IR (-O2):
@MM_PRINT = global i64 0
@.str = [8 x i8] c"MSGVERB\00"
@.str.1 = [31 x i8] c"label:severity:text:action:tag\00"
@MM_NOTOK = global i32 0
@stderr = global i32* null
@MM_CONSOLE = global i64 0
@MM_NOCON = global i32 0
@.str.3 = [13 x i8] c"/dev/console\00"
@.str.4 = [2 x i8] c"a\00"
@MM_OK = global i32 0
define i32 @fmtmsg(i64 %0, i8* %1, i32 %2, i8* %3, i8* %4, i8* %5) {
%7 = load i64, i64* @MM_PRINT
%8 = and i64 %7, %0
%9 = icmp eq i64 %8, 0
br i1 %9, label %43, label %10
10: ; preds = %6
%11 = tail call i8* @getenv(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%12 = icmp eq i8* %11, null
br i1 %12, label %27, label %13
13: ; preds = %10
%14 = load i8, i8* %11
%15 = icmp eq i8 %14, 0
br i1 %15, label %27, label %16
16: ; preds = %13
%17 = tail call i64 @strlen(i8* %11)
%18 = icmp slt i64 %17, 31
br i1 %18, label %19, label %27
19: ; preds = %16
%20 = tail call i8* @strdup(i8* %11)
%21 = icmp eq i8* %20, null
br i1 %21, label %63, label %22
22: ; preds = %19
%23 = tail call i64 @validmsgverb(i8* %20)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %30
25: ; preds = %22
%26 = tail call i32 @free(i8* %20)
br label %27
27: ; preds = %10, %13, %16, %25
%28 = tail call dereferenceable_or_null(31) i8* @strdup(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
%29 = icmp eq i8* %28, null
br i1 %29, label %63, label %30
30: ; preds = %27, %22
%31 = phi i8* [ %28, %27 ], [ %20, %22 ]
%32 = tail call i8* @printfmt(i8* %31, i64 %0, i8* %1, i32 %2, i8* %3, i8* %4, i8* %5)
%33 = icmp eq i8* %32, null
br i1 %33, label %59, label %34
34: ; preds = %30
%35 = load i8, i8* %32
%36 = icmp eq i8 %35, 0
br i1 %36, label %40, label %37
37: ; preds = %34
%38 = load i32*, i32** @stderr
%39 = tail call i32 @fputs(i8* %32, i32* %38)
br label %40
40: ; preds = %37, %34
%41 = tail call i32 @free(i8* %31)
%42 = tail call i32 @free(i8* %32)
br label %43
43: ; preds = %40, %6
%44 = load i64, i64* @MM_CONSOLE
%45 = and i64 %44, %0
%46 = icmp eq i64 %45, 0
br i1 %46, label %63, label %47
47: ; preds = %43
%48 = tail call i8* @printfmt(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i64 %0, i8* %1, i32 %2, i8* %3, i8* %4, i8* %5)
%49 = icmp eq i8* %48, null
br i1 %49, label %63, label %50
50: ; preds = %47
%51 = load i8, i8* %48
%52 = icmp eq i8 %51, 0
br i1 %52, label %59, label %53
53: ; preds = %50
%54 = tail call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%55 = icmp eq i32* %54, null
br i1 %55, label %59, label %56
56: ; preds = %53
%57 = tail call i32 @fputs(i8* %48, i32* %54)
%58 = tail call i32 @fclose(i32* %54)
br label %59
59: ; preds = %50, %56, %53, %30
%60 = phi i8* [ %31, %30 ], [ %48, %53 ], [ %48, %56 ], [ %48, %50 ]
%61 = phi i32* [ @MM_NOTOK, %30 ], [ @MM_NOCON, %53 ], [ @MM_OK, %56 ], [ @MM_OK, %50 ]
%62 = tail call i32 @free(i8* %60)
br label %63
63: ; preds = %59, %43, %47, %27, %19
%64 = phi i32* [ @MM_NOTOK, %19 ], [ @MM_NOTOK, %27 ], [ @MM_NOCON, %47 ], [ @MM_OK, %43 ], [ %61, %59 ]
%65 = load i32, i32* %64
ret i32 %65
}