#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 */
typedef struct _msgbuf TYPE_1__;

/* Type definitions */
struct _msgbuf {
  long mtype;
  char *mtext;
};

/* Variables and functions */
int msgget(int, int);
int msgsnd(int, TYPE_1__ *, int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char, char);
int random();
int /*<<< orphan*/ srandom(int /*<<< orphan*/);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int main() {
  int msgid, result;
  struct _msgbuf {
    long mtype;
    char mtext[2];
  } msgbuf;
  msgid = msgget(0xABC, 0666);
  if (msgid == -1) {
    perror("Couldn't find msg queue:");
    return 1;
  }
  srandom(time(NULL));
  msgbuf.mtype = 1;
  msgbuf.mtext[0] = (char)(random() % 26 + 65);
  msgbuf.mtext[1] = (char)(random() % 10 + 48);
  printf("About to send message %c%c\n", msgbuf.mtext[0], msgbuf.mtext[1]);
  result = msgsnd(msgid, &msgbuf, sizeof(msgbuf.mtext), 0);
  if (result == -1) {
    perror("Couldn't send a message:");
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0xabc,%edi
mov    $0x1b6,%esi
call   1030 <msgget@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11b3 <main+0x43>
lea    0xe5e(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    124e <main+0xde>
xor    %eax,%eax
mov    %eax,%edi
call   1070 <time@plt>
mov    %eax,%edi
call   1040 <srandom@plt>
movq   $0x1,-0x20(%rbp)
mov    $0x0,%al
call   1080 <random@plt>
mov    $0x1a,%ecx
cltd
idiv   %ecx
add    $0x41,%edx
mov    %dl,%al
mov    %al,-0x18(%rbp)
mov    $0x0,%al
call   1080 <random@plt>
mov    $0xa,%ecx
cltd
idiv   %ecx
add    $0x30,%edx
mov    %dl,%al
mov    %al,-0x17(%rbp)
mov    -0x18(%rbp),%al
lea    0xe16(%rip),%rdi
movsbl %al,%esi
movsbl -0x17(%rbp),%edx
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x2,%edx
xor    %ecx,%ecx
call   1060 <msgsnd@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    1247 <main+0xd7>
lea    0xdff(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    124e <main+0xde>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    $0xabc,%edi
mov    $0x1b6,%esi
call   1030 <msgget@plt>
cmp    $0xffffffff,%eax
je     1238 <main+0xc8>
mov    %eax,%ebp
xor    %r14d,%r14d
xor    %edi,%edi
call   1070 <time@plt>
mov    %eax,%edi
call   1040 <srandom@plt>
movq   $0x1,(%rsp)
xor    %eax,%eax
call   1080 <random@plt>
movslq %eax,%rbx
imul   $0x4ec4ec4f,%rbx,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x23,%rax
add    %ecx,%eax
lea    (%rax,%rax,4),%ecx
lea    (%rcx,%rcx,4),%ecx
add    %eax,%ecx
sub    %ecx,%ebx
add    $0x41,%bl
mov    %bl,0x8(%rsp)
xor    %eax,%eax
call   1080 <random@plt>
cltq
imul   $0x66666667,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x22,%rcx
add    %edx,%ecx
add    %ecx,%ecx
lea    (%rcx,%rcx,4),%ecx
sub    %ecx,%eax
add    $0x30,%al
mov    %al,0x9(%rsp)
movsbl %bl,%esi
movsbl %al,%edx
lea    0xe08(%rip),%rdi
call   1050 <printf@plt>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x2,%edx
xor    %ecx,%ecx
call   1060 <msgsnd@plt>
cmp    $0xffffffff,%eax
je     1241 <main+0xd1>
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdc1(%rip),%rdi
jmp    1248 <main+0xd8>
lea    0xded(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%r14d
jmp    122c <main+0xbc>

LLVM IR (-O0):
%struct._msgbuf = type { i64, [2 x i8] }
%struct._msgbuf.0 = type { i64, i8* }
@.str = [25 x i8] c"Couldn't find msg queue:\00"
@.str.1 = [28 x i8] c"About to send message %c%c\0A\00"
@.str.2 = [25 x i8] c"Couldn't send a message:\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct._msgbuf
store i32 0, i32* %1
%5 = call i32 @msgget(i32 2748, i32 438)
store i32 %5, i32* %2
%6 = load i32, i32* %2
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %41
10: ; preds = %0
%11 = call i32 @time(i32* null)
%12 = call i32 @srandom(i32 %11)
%13 = getelementptr inbounds %struct._msgbuf, %struct._msgbuf* %4, i32 0, i32 0
store i64 1, i64* %13
%14 = call i32 (...) @random()
%15 = srem i32 %14, 26
%16 = add nsw i32 %15, 65
%17 = trunc i32 %16 to i8
%18 = getelementptr inbounds %struct._msgbuf, %struct._msgbuf* %4, i32 0, i32 1
%19 = getelementptr inbounds [2 x i8], [2 x i8]* %18, i64 0, i64 0
store i8 %17, i8* %19
%20 = call i32 (...) @random()
%21 = srem i32 %20, 10
%22 = add nsw i32 %21, 48
%23 = trunc i32 %22 to i8
%24 = getelementptr inbounds %struct._msgbuf, %struct._msgbuf* %4, i32 0, i32 1
%25 = getelementptr inbounds [2 x i8], [2 x i8]* %24, i64 0, i64 1
store i8 %23, i8* %25
%26 = getelementptr inbounds %struct._msgbuf, %struct._msgbuf* %4, i32 0, i32 1
%27 = getelementptr inbounds [2 x i8], [2 x i8]* %26, i64 0, i64 0
%28 = load i8, i8* %27
%29 = getelementptr inbounds %struct._msgbuf, %struct._msgbuf* %4, i32 0, i32 1
%30 = getelementptr inbounds [2 x i8], [2 x i8]* %29, i64 0, i64 1
%31 = load i8, i8* %30
%32 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8 signext %28, i8 signext %31)
%33 = load i32, i32* %2
%34 = bitcast %struct._msgbuf* %4 to %struct._msgbuf.0*
%35 = call i32 @msgsnd(i32 %33, %struct._msgbuf.0* %34, i32 2, i32 0)
store i32 %35, i32* %3
%36 = load i32, i32* %3
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %40
38: ; preds = %10
%39 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %41
40: ; preds = %10
store i32 0, i32* %1
br label %41
41: ; preds = %40, %38, %8
%42 = load i32, i32* %1
ret i32 %42
}

LLVM IR (-O2):
%struct._msgbuf = type { i64, [2 x i8] }
%struct._msgbuf.0 = type { i64, i8* }
@.str = [25 x i8] c"Couldn't find msg queue:\00"
@.str.1 = [28 x i8] c"About to send message %c%c\0A\00"
@.str.2 = [25 x i8] c"Couldn't send a message:\00"
define i32 @main() {
%1 = alloca %struct._msgbuf
%2 = bitcast %struct._msgbuf* %1 to i8*
%3 = tail call i32 @msgget(i32 2748, i32 438)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = tail call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
br label %27
7: ; preds = %0
%8 = tail call i32 @time(i32* null)
%9 = tail call i32 @srandom(i32 %8)
%10 = getelementptr inbounds %struct._msgbuf, %struct._msgbuf* %1, i64 0, i32 0
store i64 1, i64* %10
%11 = tail call i32 (...) @random()
%12 = srem i32 %11, 26
%13 = trunc i32 %12 to i8
%14 = add nsw i8 %13, 65
%15 = getelementptr inbounds %struct._msgbuf, %struct._msgbuf* %1, i64 0, i32 1, i64 0
store i8 %14, i8* %15
%16 = tail call i32 (...) @random()
%17 = srem i32 %16, 10
%18 = trunc i32 %17 to i8
%19 = add nsw i8 %18, 48
%20 = getelementptr inbounds %struct._msgbuf, %struct._msgbuf* %1, i64 0, i32 1, i64 1
store i8 %19, i8* %20
%21 = tail call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8 signext %14, i8 signext %19)
%22 = bitcast %struct._msgbuf* %1 to %struct._msgbuf.0*
%23 = call i32 @msgsnd(i32 %3, %struct._msgbuf.0* %22, i32 2, i32 0)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %27
25: ; preds = %7
%26 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
br label %27
27: ; preds = %7, %25, %5
%28 = phi i32 [ 1, %5 ], [ 1, %25 ], [ 0, %7 ]
ret i32 %28
}