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

/* Variables and functions */
char *SIGUSR1;
int atoi(char *);
scalar_t__ calloc(int, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ getpid();
int pcps_sigthreadinit(char *);
int producerinit(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
char *strerror(int);
int main(int argc, char *argv[]) {
  int produ_num = 0;
  int consu_num = 0;
  int pcps_err = 0;
  int i = 0;
  pthread_t *produ_tid = NULL;
  pthread_t *consu_tid = NULL;
  if (argc != 3) {
    fprintf(stderr, "Usage: %s producers consumers\n", argv[0]);
    return (-1);
  }
  produ_num = atoi(argv[1]);
  consu_num = atoi(argv[2]);
  if ((produ_num <= 0) || (consu_num) <= 0) {
    fprintf(stderr, "Type correct numbers of threads\n");
    return (-1);
  }
  if (pcps_err = pcps_sigthreadinit(SIGUSR1)) {
    fprintf(stderr, "Failed to init signal thread\n");
    return (-1);
  }
  fprintf(stdout, "Process %ld will run until SIGUSR1(%d) catched\n",
          (long)getpid(), SIGUSR1);
  if ((produ_tid = (pthread_t *)calloc(produ_num, sizeof(pthread_t))) == NULL)
    return (-1);
  if ((consu_tid = (pthread_t *)calloc(consu_num, sizeof(pthread_t))) == NULL)
    return (-1);
  for (i = 0; i < produ_num; i++) {
    if (pcps_err = producerinit(produ_tid + i)) {
      fprintf(stderr, "Failed to init consumer %d: %s\n", i,
              strerror(pcps_err));
      return (-1);
    }
  }
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11e6 <main+0x76>
mov    0x2e15(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe2d(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1384 <main+0x214>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    121a <main+0xaa>
cmpl   $0x0,-0x18(%rbp)
jg     123d <main+0xcd>
mov    0x2db7(%rip),%rax
mov    (%rax),%edi
lea    0xdf5(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1384 <main+0x214>
mov    0x2da4(%rip),%rax
mov    (%rax),%rdi
call   1070 <pcps_sigthreadinit@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
je     127b <main+0x10b>
mov    0x2d79(%rip),%rax
mov    (%rax),%edi
lea    0xdd8(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1384 <main+0x214>
mov    0x2d4e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x34(%rbp),%edi
mov    %rax,%rdx
mov    0x2d4d(%rip),%rax
mov    (%rax),%rcx
lea    0xdb9(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x14(%rbp),%edi
mov    $0x4,%esi
call   1050 <calloc@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    12d3 <main+0x163>
movl   $0xffffffff,-0x4(%rbp)
jmp    1384 <main+0x214>
mov    -0x18(%rbp),%edi
mov    $0x4,%esi
call   1050 <calloc@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    12fa <main+0x18a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1384 <main+0x214>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    137d <main+0x20d>
mov    -0x28(%rbp),%rdi
movslq -0x20(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
call   1030 <producerinit@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
je     136a <main+0x1fa>
mov    0x2ca4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x1c(%rbp),%edi
call   1090 <strerror@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%edx
mov    %rax,%rcx
lea    0xd37(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1384 <main+0x214>
jmp    136f <main+0x1ff>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1301 <main+0x191>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbp
cmp    $0x3,%edi
jne    11c4 <main+0x54>
mov    0x8(%rbp),%rdi
call   1080 <atoi@plt>
mov    %eax,%ebx
mov    0x10(%rbp),%rdi
call   1080 <atoi@plt>
test   %ebx,%ebx
jle    11e1 <main+0x71>
mov    %eax,%r14d
test   %eax,%eax
jle    11e1 <main+0x71>
mov    0x2e43(%rip),%rbp
mov    0x0(%rbp),%rdi
call   1070 <pcps_sigthreadinit@plt>
test   %eax,%eax
je     120c <main+0x9c>
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
lea    0xe7e(%rip),%rsi
jmp    11f1 <main+0x81>
mov    0x2e0d(%rip),%rax
mov    (%rax),%edi
mov    0x0(%rbp),%rdx
lea    0xe28(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    11f8 <main+0x88>
mov    0x2df0(%rip),%rax
mov    (%rax),%edi
lea    0xe2e(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0xffffffff,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dbd(%rip),%rax
mov    (%rax),%r15d
xor    %eax,%eax
call   1040 <getpid@plt>
mov    0x0(%rbp),%rcx
lea    0xe36(%rip),%rsi
mov    %r15d,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %ebx,%edi
mov    $0x4,%esi
call   1050 <calloc@plt>
mov    $0xffffffff,%r15d
test   %rax,%rax
je     11fe <main+0x8e>
mov    %rax,%rbp
mov    %r14d,%edi
mov    $0x4,%esi
call   1050 <calloc@plt>
test   %rax,%rax
je     11fe <main+0x8e>
mov    %ebx,%r14d
xor    %ebx,%ebx
xor    %r15d,%r15d
nopl   0x0(%rax)
mov    %rbp,%rdi
call   1030 <producerinit@plt>
test   %eax,%eax
jne    128e <main+0x11e>
add    $0x1,%rbx
add    $0x4,%rbp
cmp    %rbx,%r14
jne    1270 <main+0x100>
jmp    11fe <main+0x8e>
mov    0x2d43(%rip),%rcx
mov    (%rcx),%ebp
mov    %eax,%edi
call   1090 <strerror@plt>
lea    0xde9(%rip),%rsi
mov    %ebp,%edi
mov    %ebx,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    11f8 <main+0x88>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [31 x i8] c"Usage: %s producers consumers\0A\00"
@.str.1 = [33 x i8] c"Type correct numbers of threads\0A\00"
@SIGUSR1 = global i8* null
@.str.2 = [30 x i8] c"Failed to init signal thread\0A\00"
@stdout = global i32 0
@.str.3 = [48 x i8] c"Process %ld will run until SIGUSR1(%d) catched\0A\00"
@.str.4 = [32 x i8] c"Failed to init consumer %d: %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32*
%11 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
store i32 0, i32* %7
store i32 0, i32* %8
store i32 0, i32* %9
store i32* null, i32** %10
store i32* null, i32** %11
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 3
br i1 %13, label %14, label %20
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 0
%18 = load i8*, i8** %17
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %18)
store i32 -1, i32* %3
br label %83
20: ; preds = %2
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = call i32 @atoi(i8* %23)
store i32 %24, i32* %6
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 2
%27 = load i8*, i8** %26
%28 = call i32 @atoi(i8* %27)
store i32 %28, i32* %7
%29 = load i32, i32* %6
%30 = icmp sle i32 %29, 0
br i1 %30, label %34, label %31
31: ; preds = %20
%32 = load i32, i32* %7
%33 = icmp sle i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %31, %20
%35 = load i32, i32* @stderr
%36 = call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %83
37: ; preds = %31
%38 = load i8*, i8** @SIGUSR1
%39 = call i32 @pcps_sigthreadinit(i8* %38)
store i32 %39, i32* %8
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %37
%42 = load i32, i32* @stderr
%43 = call i32 (i32, i8*, ...) @fprintf(i32 %42, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %83
44: ; preds = %37
%45 = load i32, i32* @stdout
%46 = call i64 (...) @getpid()
%47 = load i8*, i8** @SIGUSR1
%48 = call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.3, i64 0, i64 0), i64 %46, i8* %47)
%49 = load i32, i32* %6
%50 = call i64 @calloc(i32 %49, i32 4)
%51 = inttoptr i64 %50 to i32*
store i32* %51, i32** %10
%52 = icmp eq i32* %51, null
br i1 %52, label %53, label %54
53: ; preds = %44
store i32 -1, i32* %3
br label %83
54: ; preds = %44
%55 = load i32, i32* %7
%56 = call i64 @calloc(i32 %55, i32 4)
%57 = inttoptr i64 %56 to i32*
store i32* %57, i32** %11
%58 = icmp eq i32* %57, null
br i1 %58, label %59, label %60
59: ; preds = %54
store i32 -1, i32* %3
br label %83
60: ; preds = %54
store i32 0, i32* %9
br label %61
61: ; preds = %79, %60
%62 = load i32, i32* %9
%63 = load i32, i32* %6
%64 = icmp slt i32 %62, %63
br i1 %64, label %65, label %82
65: ; preds = %61
%66 = load i32*, i32** %10
%67 = load i32, i32* %9
%68 = sext i32 %67 to i64
%69 = getelementptr inbounds i32, i32* %66, i64 %68
%70 = call i32 @producerinit(i32* %69)
store i32 %70, i32* %8
%71 = icmp ne i32 %70, 0
br i1 %71, label %72, label %78
72: ; preds = %65
%73 = load i32, i32* @stderr
%74 = load i32, i32* %9
%75 = load i32, i32* %8
%76 = call i8* @strerror(i32 %75)
%77 = call i32 (i32, i8*, ...) @fprintf(i32 %73, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i32 %74, i8* %76)
store i32 -1, i32* %3
br label %83
78: ; preds = %65
br label %79
79: ; preds = %78
%80 = load i32, i32* %9
%81 = add nsw i32 %80, 1
store i32 %81, i32* %9
br label %61
82: ; preds = %61
store i32 0, i32* %3
br label %83
83: ; preds = %82, %72, %59, %53, %41, %34, %14
%84 = load i32, i32* %3
ret i32 %84
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [31 x i8] c"Usage: %s producers consumers\0A\00"
@.str.1 = [33 x i8] c"Type correct numbers of threads\0A\00"
@SIGUSR1 = global i8* null
@.str.2 = [30 x i8] c"Failed to init signal thread\0A\00"
@stdout = global i32 0
@.str.3 = [48 x i8] c"Process %ld will run until SIGUSR1(%d) catched\0A\00"
@.str.4 = [32 x i8] c"Failed to init consumer %d: %s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 3
br i1 %3, label %8, label %4
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = load i8*, i8** %1
%7 = tail call i32 (i32, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %6)
br label %54
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call i32 @atoi(i8* %10)
%12 = getelementptr inbounds i8*, i8** %1, i64 2
%13 = load i8*, i8** %12
%14 = tail call i32 @atoi(i8* %13)
%15 = icmp slt i32 %11, 1
%16 = icmp slt i32 %14, 1
%17 = select i1 %15, i1 true, i1 %16
br i1 %17, label %18, label %21
18: ; preds = %8
%19 = load i32, i32* @stderr
%20 = tail call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
br label %54
21: ; preds = %8
%22 = load i8*, i8** @SIGUSR1
%23 = tail call i32 @pcps_sigthreadinit(i8* %22)
%24 = icmp eq i32 %23, 0
br i1 %24, label %28, label %25
25: ; preds = %21
%26 = load i32, i32* @stderr
%27 = tail call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
br label %54
28: ; preds = %21
%29 = load i32, i32* @stdout
%30 = tail call i64 (...) @getpid()
%31 = load i8*, i8** @SIGUSR1
%32 = tail call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.3, i64 0, i64 0), i64 %30, i8* %31)
%33 = tail call i64 @calloc(i32 %11, i32 4)
%34 = inttoptr i64 %33 to i32*
%35 = icmp eq i64 %33, 0
br i1 %35, label %54, label %36
36: ; preds = %28
%37 = tail call i64 @calloc(i32 %14, i32 4)
%38 = icmp eq i64 %37, 0
br i1 %38, label %54, label %39
39: ; preds = %36
%40 = zext i32 %11 to i64
br label %41
41: ; preds = %39, %51
%42 = phi i64 [ 0, %39 ], [ %52, %51 ]
%43 = getelementptr inbounds i32, i32* %34, i64 %42
%44 = tail call i32 @producerinit(i32* %43)
%45 = icmp eq i32 %44, 0
br i1 %45, label %51, label %46
46: ; preds = %41
%47 = trunc i64 %42 to i32
%48 = load i32, i32* @stderr
%49 = tail call i8* @strerror(i32 %44)
%50 = tail call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i32 %47, i8* %49)
br label %54
51: ; preds = %41
%52 = add nuw nsw i64 %42, 1
%53 = icmp eq i64 %52, %40
br i1 %53, label %54, label %41
54: ; preds = %51, %36, %28, %46, %25, %18, %4
%55 = phi i32 [ -1, %4 ], [ -1, %18 ], [ -1, %25 ], [ -1, %46 ], [ -1, %28 ], [ -1, %36 ], [ 0, %51 ]
ret i32 %55
}