#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*/ buff;

/* Variables and functions */
int /*<<< orphan*/ com_sprintf(char *, int, char *, int, char *, char *);
scalar_t__ errormsgshow(char *, char *);
scalar_t__ geterrormsg(char *, char *, int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, char *);
int strlen(char *);
int printerrormsg(char *name, int line, char *code, char *filename) {
  char buff[1024];
  int ilen;
  char *p;
  if (code == NULL || line <= 0 || name == NULL)
    return 0;
  ilen = sizeof(buff);
  memset(buff, 0, ilen);
  com_sprintf(buff, sizeof(buff), "%06d:%4s:%s", line, code, name);
  p = buff + strlen(buff);
  ilen -= strlen(buff) + 1;
  if (geterrormsg(code, p, ilen) == 0) {
    printf("printerrmsg: no error message for '%4s'\n", code);
    return 0;
  }
  if (errormsgshow(filename, buff) == 0) {
    return 0;
  }
  return 1;
}


x86 assembly (clang, -O0):
<printerrormsg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     119a <printerrormsg+0x3a>
cmpl   $0x0,-0x14(%rbp)
jle    119a <printerrormsg+0x3a>
cmpq   $0x0,-0x10(%rbp)
jne    11a6 <printerrormsg+0x46>
movl   $0x0,-0x4(%rbp)
jmp    129f <printerrormsg+0x13f>
movl   $0x400,-0x434(%rbp)
lea    -0x430(%rbp),%rdi
mov    -0x434(%rbp),%edx
xor    %esi,%esi
call   1050 <memset@plt>
lea    -0x430(%rbp),%rdi
mov    -0x14(%rbp),%ecx
mov    -0x20(%rbp),%r8
mov    -0x10(%rbp),%r9
mov    $0x400,%esi
lea    0xe1e(%rip),%rdx
call   1070 <com_sprintf@plt>
lea    -0x430(%rbp),%rax
mov    %rax,-0x448(%rbp)
lea    -0x430(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x448(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x440(%rbp)
lea    -0x430(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
add    $0x1,%ecx
mov    -0x434(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x434(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x440(%rbp),%rsi
mov    -0x434(%rbp),%edx
call   1080 <geterrormsg@plt>
cmp    $0x0,%rax
jne    1272 <printerrormsg+0x112>
mov    -0x20(%rbp),%rsi
lea    0xdab(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    129f <printerrormsg+0x13f>
mov    -0x28(%rbp),%rdi
lea    -0x430(%rbp),%rsi
call   1060 <errormsgshow@plt>
cmp    $0x0,%rax
jne    1298 <printerrormsg+0x138>
movl   $0x0,-0x4(%rbp)
jmp    129f <printerrormsg+0x13f>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<printerrormsg>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
xor    %r12d,%r12d
test   %rdi,%rdi
je     121d <printerrormsg+0xbd>
mov    %esi,%ebp
test   %esi,%esi
jle    121d <printerrormsg+0xbd>
mov    %rdx,%r14
test   %rdx,%rdx
je     121d <printerrormsg+0xbd>
mov    %rcx,%r15
mov    %rdi,%rbx
mov    %rsp,%r13
xor    %r12d,%r12d
mov    %r13,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
lea    0xe4b(%rip),%rdx
mov    %r13,%rdi
mov    $0x400,%esi
mov    %ebp,%ecx
mov    %r14,%r8
mov    %rbx,%r9
call   1070 <com_sprintf@plt>
mov    %r13,%rdi
call   1030 <strlen@plt>
cltq
lea    (%rsp,%rax,1),%rbx
mov    %r13,%rdi
call   1030 <strlen@plt>
mov    $0x3ff,%edx
sub    %eax,%edx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1080 <geterrormsg@plt>
test   %rax,%rax
je     120e <printerrormsg+0xae>
mov    %rsp,%rsi
mov    %r15,%rdi
call   1060 <errormsgshow@plt>
xor    %r12d,%r12d
test   %rax,%rax
setne  %r12b
jmp    121d <printerrormsg+0xbd>
lea    0xdf7(%rip),%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
mov    %r12d,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [12 x i8] c"%06d:%4s:%s\00"
@.str.1 = [41 x i8] c"printerrmsg: no error message for '%4s'\0A\00"
define i32 @printerrormsg(i8* %0, i32 %1, i8* %2, i8* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca [1024 x i8]
%11 = alloca i32
%12 = alloca i8*
store i8* %0, i8** %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i8* %3, i8** %9
%13 = load i8*, i8** %8
%14 = icmp eq i8* %13, null
br i1 %14, label %21, label %15
15: ; preds = %4
%16 = load i32, i32* %7
%17 = icmp sle i32 %16, 0
br i1 %17, label %21, label %18
18: ; preds = %15
%19 = load i8*, i8** %6
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %22
21: ; preds = %18, %15, %4
store i32 0, i32* %5
br label %56
22: ; preds = %18
store i32 1024, i32* %11
%23 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%24 = load i32, i32* %11
%25 = call i32 @memset(i8* %23, i32 0, i32 %24)
%26 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%27 = load i32, i32* %7
%28 = load i8*, i8** %8
%29 = load i8*, i8** %6
%30 = call i32 @com_sprintf(i8* %26, i32 1024, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %27, i8* %28, i8* %29)
%31 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%32 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%33 = call i32 @strlen(i8* %32)
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %31, i64 %34
store i8* %35, i8** %12
%36 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%37 = call i32 @strlen(i8* %36)
%38 = add nsw i32 %37, 1
%39 = load i32, i32* %11
%40 = sub nsw i32 %39, %38
store i32 %40, i32* %11
%41 = load i8*, i8** %8
%42 = load i8*, i8** %12
%43 = load i32, i32* %11
%44 = call i64 @geterrormsg(i8* %41, i8* %42, i32 %43)
%45 = icmp eq i64 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %22
%47 = load i8*, i8** %8
%48 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i8* %47)
store i32 0, i32* %5
br label %56
49: ; preds = %22
%50 = load i8*, i8** %9
%51 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%52 = call i64 @errormsgshow(i8* %50, i8* %51)
%53 = icmp eq i64 %52, 0
br i1 %53, label %54, label %55
54: ; preds = %49
store i32 0, i32* %5
br label %56
55: ; preds = %49
store i32 1, i32* %5
br label %56
56: ; preds = %55, %54, %46, %21
%57 = load i32, i32* %5
ret i32 %57
}

LLVM IR (-O2):
@.str = [12 x i8] c"%06d:%4s:%s\00"
@.str.1 = [41 x i8] c"printerrmsg: no error message for '%4s'\0A\00"
define i32 @printerrormsg(i8* %0, i32 %1, i8* %2, i8* %3) {
%5 = alloca [1024 x i8]
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%7 = icmp eq i8* %2, null
%8 = icmp slt i32 %1, 1
%9 = or i1 %8, %7
%10 = icmp eq i8* %0, null
%11 = or i1 %10, %9
br i1 %11, label %28, label %12
12: ; preds = %4
%13 = call i32 @memset(i8* %6, i32 0, i32 1024)
%14 = call i32 @com_sprintf(i8* %6, i32 1024, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %1, i8* %2, i8* %0)
%15 = call i32 @strlen(i8* %6)
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 %16
%18 = call i32 @strlen(i8* %6)
%19 = sub i32 1023, %18
%20 = call i64 @geterrormsg(i8* %2, i8* %17, i32 %19)
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %12
%23 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i8* %2)
br label %28
24: ; preds = %12
%25 = call i64 @errormsgshow(i8* %3, i8* %6)
%26 = icmp ne i64 %25, 0
%27 = zext i1 %26 to i32
br label %28
28: ; preds = %24, %4, %22
%29 = phi i32 [ 0, %22 ], [ 0, %4 ], [ %27, %24 ]
ret i32 %29
}