#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 pid_t;
typedef int /*<<< orphan*/ key_t;

/* Variables and functions */
int IPC_CREAT;
int IPC_NOWAIT;
int MSG_NOERROR;
int /*<<< orphan*/ SIGTERM;
int /*<<< orphan*/ WNOHANG;
int /*<<< orphan*/ exit(int);
int fork();
int getpid();
int /*<<< orphan*/ kill(int, int /*<<< orphan*/);
int msgget(int /*<<< orphan*/, int);
int msgrcv(int, char *, int, int /*<<< orphan*/, int);
int msgsnd(int, char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int waitpid(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
void mp_example5(void) {
  pid_t pid;
  pid_t tid;
  int ret;
  int msgid;
  msgid = msgget((key_t)1234, 0666 | IPC_CREAT);
  if (msgid == -1) {
    printf("msgget fail ");
    exit(-1);
  }
  tid = fork();
  if (tid == 0) {
    printf("i'm child %d\n", getpid());
    char *histr = "hello\n";
    ret = msgsnd(msgid, histr, 6, MSG_NOERROR | IPC_NOWAIT);
    if (ret) {
      perror("send fail ");
      exit(-1);
    }
    exit(0);
  } else if (tid == -1) {
    perror("fork fail ");
  } else {
    pid = getpid();
    printf("i'm father %d\n", pid);
    sleep(2);
    char buf[100];
    ret = msgrcv(msgid, buf, 10, 0, MSG_NOERROR | IPC_NOWAIT);
    if (ret < 0) {
      perror("msg recv fail ");
      exit(-3);
    }
    buf[ret] = '\0';
    printf("recv %s\n", buf);
    ret = waitpid(tid, NULL, WNOHANG);
    if (!ret)
      kill(tid, SIGTERM);
    printf("all done\n");
  }
}


x86 assembly (clang, -O0):
<mp_example5>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
or     $0x1b6,%esi
mov    $0x4d2,%edi
call   1030 <msgget@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    11f9 <mp_example5+0x49>
lea    0xe18(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    127c <mp_example5+0xcc>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xdf0(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xdf0(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    0x2d7b(%rip),%rax
mov    (%rax),%ecx
mov    0x2daa(%rip),%rax
or     (%rax),%ecx
mov    $0x6,%edx
call   1060 <msgsnd@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     1275 <mp_example5+0xc5>
lea    0xdbc(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
cmpl   $0xffffffff,-0x8(%rbp)
jne    1297 <mp_example5+0xe7>
lea    0xda0(%rip),%rdi
call   1090 <perror@plt>
jmp    1362 <mp_example5+0x1b2>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%esi
lea    0xd8d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x2,%edi
call   10b0 <sleep@plt>
mov    -0x10(%rbp),%edi
lea    -0x80(%rbp),%rsi
mov    0x2cee(%rip),%rax
mov    (%rax),%r8d
mov    0x2d1c(%rip),%rax
or     (%rax),%r8d
mov    $0xa,%edx
xor    %ecx,%ecx
call   10c0 <msgrcv@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1306 <mp_example5+0x156>
lea    0xd50(%rip),%rdi
call   1090 <perror@plt>
mov    $0xfffffffd,%edi
call   10a0 <exit@plt>
movslq -0xc(%rbp),%rax
movb   $0x0,-0x80(%rbp,%rax,1)
lea    -0x80(%rbp),%rsi
lea    0xd3c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
mov    0x2cad(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1080 <waitpid@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    1354 <mp_example5+0x1a4>
mov    -0x8(%rbp),%edi
mov    0x2c93(%rip),%rax
mov    (%rax),%esi
call   1070 <kill@plt>
lea    0xd04(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1367 <mp_example5+0x1b7>
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mp_example5>:
push   %rbp
push   %rbx
sub    $0x68,%rsp
mov    0x2dfb(%rip),%rax
mov    $0x1b6,%esi
or     (%rax),%esi
mov    $0x4d2,%edi
call   1030 <msgget@plt>
cmp    $0xffffffff,%eax
je     12b3 <mp_example5+0xf3>
mov    %eax,%ebp
xor    %eax,%eax
call   10e0 <fork@plt>
cmp    $0xffffffff,%eax
je     12a1 <mp_example5+0xe1>
mov    %eax,%ebx
xor    %eax,%eax
test   %ebx,%ebx
je     12cb <mp_example5+0x10b>
call   1050 <getpid@plt>
lea    0xe27(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x2,%edi
call   10c0 <sleep@plt>
mov    0x2d8d(%rip),%rax
mov    0x2dbe(%rip),%rcx
mov    (%rcx),%r8d
or     (%rax),%r8d
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0xa,%edx
xor    %ecx,%ecx
call   10d0 <msgrcv@plt>
test   %eax,%eax
js     1310 <mp_example5+0x150>
mov    %eax,%eax
movb   $0x0,(%rsp,%rax,1)
lea    0xdf8(%rip),%rdi
mov    %rsp,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2d69(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1090 <waitpid@plt>
test   %eax,%eax
jne    128e <mp_example5+0xce>
mov    0x2d5b(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1080 <kill@plt>
lea    0xdca(%rip),%rdi
call   1040 <puts@plt>
add    $0x68,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xd85(%rip),%rdi
add    $0x68,%rsp
pop    %rbx
pop    %rbp
jmp    10a0 <perror@plt>
lea    0xd46(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
call   1050 <getpid@plt>
lea    0xd36(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2cd1(%rip),%rax
mov    0x2d02(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
lea    0xd22(%rip),%rsi
mov    %ebp,%edi
mov    $0x6,%edx
call   1070 <msgsnd@plt>
test   %eax,%eax
jne    1326 <mp_example5+0x166>
xor    %edi,%edi
call   10b0 <exit@plt>
lea    0xd30(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xfffffffd,%edi
call   10b0 <exit@plt>
lea    0xcf5(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@IPC_CREAT = global i32 0
@.str = [13 x i8] c"msgget fail \00"
@.str.1 = [14 x i8] c"i'm child %d\0A\00"
@.str.2 = [7 x i8] c"hello\0A\00"
@MSG_NOERROR = global i32 0
@IPC_NOWAIT = global i32 0
@.str.3 = [11 x i8] c"send fail \00"
@.str.4 = [11 x i8] c"fork fail \00"
@.str.5 = [15 x i8] c"i'm father %d\0A\00"
@.str.6 = [15 x i8] c"msg recv fail \00"
@.str.7 = [9 x i8] c"recv %s\0A\00"
@WNOHANG = global i32 0
@SIGTERM = global i32 0
@.str.8 = [10 x i8] c"all done\0A\00"
define void @mp_example5() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca [100 x i8]
%7 = load i32, i32* @IPC_CREAT
%8 = or i32 438, %7
%9 = call i32 @msgget(i32 1234, i32 %8)
store i32 %9, i32* %4
%10 = load i32, i32* %4
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %0
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 -1)
unreachable
15: ; preds = %0
%16 = call i32 (...) @fork()
store i32 %16, i32* %2
%17 = load i32, i32* %2
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %35
19: ; preds = %15
%20 = call i32 (...) @getpid()
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %20)
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8** %5
%22 = load i32, i32* %4
%23 = load i8*, i8** %5
%24 = load i32, i32* @MSG_NOERROR
%25 = load i32, i32* @IPC_NOWAIT
%26 = or i32 %24, %25
%27 = call i32 @msgsnd(i32 %22, i8* %23, i32 6, i32 %26)
store i32 %27, i32* %3
%28 = load i32, i32* %3
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %19
%31 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%32 = call i32 @exit(i32 -1)
unreachable
33: ; preds = %19
%34 = call i32 @exit(i32 0)
unreachable
35: ; preds = %15
%36 = load i32, i32* %2
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %40
38: ; preds = %35
%39 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
br label %73
40: ; preds = %35
%41 = call i32 (...) @getpid()
store i32 %41, i32* %1
%42 = load i32, i32* %1
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i32 %42)
%44 = call i32 @sleep(i32 2)
%45 = load i32, i32* %4
%46 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%47 = load i32, i32* @MSG_NOERROR
%48 = load i32, i32* @IPC_NOWAIT
%49 = or i32 %47, %48
%50 = call i32 @msgrcv(i32 %45, i8* %46, i32 10, i32 0, i32 %49)
store i32 %50, i32* %3
%51 = load i32, i32* %3
%52 = icmp slt i32 %51, 0
br i1 %52, label %53, label %56
53: ; preds = %40
%54 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
%55 = call i32 @exit(i32 -3)
unreachable
56: ; preds = %40
%57 = load i32, i32* %3
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 %58
store i8 0, i8* %59
%60 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0), i8* %60)
%62 = load i32, i32* %2
%63 = load i32, i32* @WNOHANG
%64 = call i32 @waitpid(i32 %62, i32* null, i32 %63)
store i32 %64, i32* %3
%65 = load i32, i32* %3
%66 = icmp ne i32 %65, 0
br i1 %66, label %71, label %67
67: ; preds = %56
%68 = load i32, i32* %2
%69 = load i32, i32* @SIGTERM
%70 = call i32 @kill(i32 %68, i32 %69)
br label %71
71: ; preds = %67, %56
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.8, i64 0, i64 0))
br label %73
73: ; preds = %71, %38
br label %74
74: ; preds = %73
ret void
}

LLVM IR (-O2):
@IPC_CREAT = global i32 0
@.str = [13 x i8] c"msgget fail \00"
@.str.1 = [14 x i8] c"i'm child %d\0A\00"
@.str.2 = [7 x i8] c"hello\0A\00"
@MSG_NOERROR = global i32 0
@IPC_NOWAIT = global i32 0
@.str.3 = [11 x i8] c"send fail \00"
@.str.4 = [11 x i8] c"fork fail \00"
@.str.5 = [15 x i8] c"i'm father %d\0A\00"
@.str.6 = [15 x i8] c"msg recv fail \00"
@.str.7 = [9 x i8] c"recv %s\0A\00"
@WNOHANG = global i32 0
@SIGTERM = global i32 0
@str = [9 x i8] c"all done\00"
define void @mp_example5() {
%1 = alloca [100 x i8]
%2 = load i32, i32* @IPC_CREAT
%3 = or i32 %2, 438
%4 = tail call i32 @msgget(i32 1234, i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 @exit(i32 -1)
unreachable
9: ; preds = %0
%10 = tail call i32 (...) @fork()
switch i32 %10, label %26 [
i32 0, label %11
i32 -1, label %24
]
11: ; preds = %9
%12 = tail call i32 (...) @getpid()
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %12)
%14 = load i32, i32* @MSG_NOERROR
%15 = load i32, i32* @IPC_NOWAIT
%16 = or i32 %15, %14
%17 = tail call i32 @msgsnd(i32 %4, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 6, i32 %16)
%18 = icmp eq i32 %17, 0
br i1 %18, label %22, label %19
19: ; preds = %11
%20 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%21 = tail call i32 @exit(i32 -1)
unreachable
22: ; preds = %11
%23 = tail call i32 @exit(i32 0)
unreachable
24: ; preds = %9
%25 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
br label %51
26: ; preds = %9
%27 = tail call i32 (...) @getpid()
%28 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i32 %27)
%29 = tail call i32 @sleep(i32 2)
%30 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%31 = load i32, i32* @MSG_NOERROR
%32 = load i32, i32* @IPC_NOWAIT
%33 = or i32 %32, %31
%34 = call i32 @msgrcv(i32 %4, i8* %30, i32 10, i32 0, i32 %33)
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %26
%37 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
%38 = call i32 @exit(i32 -3)
unreachable
39: ; preds = %26
%40 = zext i32 %34 to i64
%41 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 %40
store i8 0, i8* %41
%42 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0), i8* %30)
%43 = load i32, i32* @WNOHANG
%44 = call i32 @waitpid(i32 %10, i32* null, i32 %43)
%45 = icmp eq i32 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %39
%47 = load i32, i32* @SIGTERM
%48 = call i32 @kill(i32 %10, i32 %47)
br label %49
49: ; preds = %46, %39
%50 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str, i64 0, i64 0))
br label %51
51: ; preds = %24, %49
ret void
}