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

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, ...);
int getpid();
int /*<<< orphan*/ recv(int, int *, int, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int check_magic(int fd, int magic) {
  int val;
  char *msg;
  recv(fd, &val, sizeof(int), 0);
  fprintf(stderr, "val = %x\n", val);
  if (val == magic)
    return 1;
  else {
    switch (magic) {
    case 0x18fabe0d:
      msg = "should have been PLA";
      break;
    case 0xdc3bae7b:
      msg = "should have been BOM";
      break;
    case 0x582da3c1:
      msg = "should have been WAL";
      break;
    default:
      msg = "Unknown magic";
      break;
    }
    fprintf(stderr, "(pid %d) Magic check fail: %s", getpid(), msg);
    fprintf(stderr, " was %x\n", val);
    return 0;
  }
}


x86 assembly (clang, -O0):
<check_magic>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x4,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    0x2e88(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%edx
lea    0xe9c(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jne    1183 <check_magic+0x53>
movl   $0x1,-0x4(%rbp)
jmp    1245 <check_magic+0x115>
mov    -0xc(%rbp),%eax
mov    %eax,-0x1c(%rbp)
sub    $0xdc3bae7b,%eax
je     11cf <check_magic+0x9f>
jmp    1199 <check_magic+0x69>
mov    -0x1c(%rbp),%eax
sub    $0x18fabe0d,%eax
je     11bf <check_magic+0x8f>
jmp    11ac <check_magic+0x7c>
mov    -0x1c(%rbp),%eax
sub    $0x582da3c1,%eax
je     11df <check_magic+0xaf>
jmp    11ef <check_magic+0xbf>
lea    0xe44(%rip),%rax
mov    %rax,-0x18(%rbp)
jmp    11fa <check_magic+0xca>
lea    0xe49(%rip),%rax
mov    %rax,-0x18(%rbp)
jmp    11fa <check_magic+0xca>
lea    0xe4e(%rip),%rax
mov    %rax,-0x18(%rbp)
jmp    11fa <check_magic+0xca>
lea    0xe53(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    0x2ddf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x20(%rbp),%edi
mov    %eax,%edx
mov    -0x18(%rbp),%rcx
lea    0xe3a(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%edx
lea    0xe3e(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_magic>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebx
lea    0x4(%rsp),%rsi
mov    $0x4,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    0x2e8f(%rip),%r15
mov    (%r15),%edi
mov    0x4(%rsp),%edx
lea    0xea1(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%ebp
cmp    %ebx,0x4(%rsp)
je     11e1 <check_magic+0xb1>
cmp    $0x582da3c1,%ebx
je     1192 <check_magic+0x62>
cmp    $0x18fabe0d,%ebx
je     119b <check_magic+0x6b>
cmp    $0xdc3bae7b,%ebx
jne    11a4 <check_magic+0x74>
lea    0xe8f(%rip),%r14
jmp    11ab <check_magic+0x7b>
lea    0xe9b(%rip),%r14
jmp    11ab <check_magic+0x7b>
lea    0xe68(%rip),%r14
jmp    11ab <check_magic+0x7b>
lea    0xe9e(%rip),%r14
mov    (%r15),%ebx
xor    %ebp,%ebp
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe99(%rip),%rsi
mov    %ebx,%edi
mov    %eax,%edx
mov    %r14,%rcx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    (%r15),%edi
mov    0x4(%rsp),%edx
lea    0xe9b(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [10 x i8] c"val = %x\0A\00"
@.str.1 = [21 x i8] c"should have been PLA\00"
@.str.2 = [21 x i8] c"should have been BOM\00"
@.str.3 = [21 x i8] c"should have been WAL\00"
@.str.4 = [14 x i8] c"Unknown magic\00"
@.str.5 = [30 x i8] c"(pid %d) Magic check fail: %s\00"
@.str.6 = [9 x i8] c" was %x\0A\00"
define i32 @check_magic(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
store i32 %0, i32* %4
store i32 %1, i32* %5
%8 = load i32, i32* %4
%9 = call i32 @recv(i32 %8, i32* %6, i32 4, i32 0)
%10 = load i32, i32* @stderr
%11 = load i32, i32* %6
%12 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = load i32, i32* %6
%14 = load i32, i32* %5
%15 = icmp eq i32 %13, %14
br i1 %15, label %16, label %17
16: ; preds = %2
store i32 1, i32* %3
br label %31
17: ; preds = %2
%18 = load i32, i32* %5
switch i32 %18, label %22 [
i32 419085837, label %19
i32 -600068485, label %20
i32 1479386049, label %21
]
19: ; preds = %17
store i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8** %7
br label %23
20: ; preds = %17
store i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8** %7
br label %23
21: ; preds = %17
store i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8** %7
br label %23
22: ; preds = %17
store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i8** %7
br label %23
23: ; preds = %22, %21, %20, %19
%24 = load i32, i32* @stderr
%25 = call i32 (...) @getpid()
%26 = load i8*, i8** %7
%27 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0), i32 %25, i8* %26)
%28 = load i32, i32* @stderr
%29 = load i32, i32* %6
%30 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0), i32 %29)
store i32 0, i32* %3
br label %31
31: ; preds = %23, %16
%32 = load i32, i32* %3
ret i32 %32
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [10 x i8] c"val = %x\0A\00"
@.str.1 = [21 x i8] c"should have been PLA\00"
@.str.2 = [21 x i8] c"should have been BOM\00"
@.str.3 = [21 x i8] c"should have been WAL\00"
@.str.4 = [14 x i8] c"Unknown magic\00"
@.str.5 = [30 x i8] c"(pid %d) Magic check fail: %s\00"
@.str.6 = [9 x i8] c" was %x\0A\00"
define i32 @check_magic(i32 %0, i32 %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = call i32 @recv(i32 %0, i32* %3, i32 4, i32 0)
%6 = load i32, i32* @stderr
%7 = load i32, i32* %3
%8 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %6, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = load i32, i32* %3
%10 = icmp eq i32 %9, %1
br i1 %10, label %23, label %11
11: ; preds = %2
switch i32 %1, label %14 [
i32 419085837, label %15
i32 -600068485, label %12
i32 1479386049, label %13
]
12: ; preds = %11
br label %15
13: ; preds = %11
br label %15
14: ; preds = %11
br label %15
15: ; preds = %11, %14, %13, %12
%16 = phi i8* [ getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), %14 ], [ getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), %13 ], [ getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), %12 ], [ getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), %11 ]
%17 = load i32, i32* @stderr
%18 = call i32 (...) @getpid()
%19 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0), i32 %18, i8* %16)
%20 = load i32, i32* @stderr
%21 = load i32, i32* %3
%22 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0), i32 %21)
br label %23
23: ; preds = %2, %15
%24 = phi i32 [ 0, %15 ], [ 1, %2 ]
ret i32 %24
}