#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_mutex_t;

/* Variables and functions */
int /*<<< orphan*/ close(int);
int dup(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int pthread_mutex_destroy(int /*<<< orphan*/ *);
int pthread_mutex_init(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int main(void) {
  int i;
  int avail_fd = dup(2);
  close(avail_fd);
  for (i = 0; i < 10; i++) {
    pthread_mutex_t mutex;
    int error = pthread_mutex_init(&mutex, NULL);
    if (error != 0) {
      fprintf(stderr, "Unable to create mutex, error %d", error);
      return 1;
    }
    error = pthread_mutex_destroy(&mutex);
    if (error != 0) {
      fprintf(stderr, "Unable to destroy mutex, error %d", error);
      return 1;
    }
  }
  int next_fd = dup(2);
  if (next_fd != avail_fd) {
    fprintf(stderr, "Leaked %d descriptors!\n", next_fd - avail_fd);
    return 1;
  }
  close(next_fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x2,%edi
call   1040 <dup@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x8(%rbp)
cmpl   $0xa,-0x8(%rbp)
jge    1210 <main+0xc0>
lea    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1070 <pthread_mutex_init@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11c3 <main+0x73>
mov    0x2e3a(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
lea    0xe4e(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    125f <main+0x10f>
lea    -0x10(%rbp),%rdi
call   1030 <pthread_mutex_destroy@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11fd <main+0xad>
mov    0x2e00(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
lea    0xe35(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    125f <main+0x10f>
jmp    1202 <main+0xb2>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    117b <main+0x2b>
mov    $0x2,%edi
call   1040 <dup@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0xc(%rbp),%eax
je     1250 <main+0x100>
mov    0x2db0(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%edx
sub    -0xc(%rbp),%edx
lea    0xe04(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    125f <main+0x10f>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
mov    $0x2,%edi
call   1040 <dup@plt>
mov    %eax,%ebx
mov    %eax,%edi
call   1050 <close@plt>
lea    0xc(%rsp),%rdi
xor    %esi,%esi
call   1070 <pthread_mutex_init@plt>
test   %eax,%eax
je     118a <main+0x3a>
mov    0x2e61(%rip),%rcx
mov    (%rcx),%edi
lea    0xe78(%rip),%rsi
jmp    11a8 <main+0x58>
lea    0xc(%rsp),%rdi
call   1030 <pthread_mutex_destroy@plt>
test   %eax,%eax
je     11ba <main+0x6a>
mov    0x2e41(%rip),%rcx
mov    (%rcx),%edi
lea    0xe79(%rip),%rsi
mov    %eax,%edx
call   1060 <fprintf@plt>
mov    $0x1,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xc(%rsp),%rdi
xor    %esi,%esi
call   1070 <pthread_mutex_init@plt>
test   %eax,%eax
jne    1178 <main+0x28>
lea    0xc(%rsp),%rdi
call   1030 <pthread_mutex_destroy@plt>
test   %eax,%eax
jne    1198 <main+0x48>
lea    0xc(%rsp),%rdi
xor    %esi,%esi
call   1070 <pthread_mutex_init@plt>
test   %eax,%eax
jne    1178 <main+0x28>
lea    0xc(%rsp),%rdi
call   1030 <pthread_mutex_destroy@plt>
test   %eax,%eax
jne    1198 <main+0x48>
lea    0xc(%rsp),%rdi
xor    %esi,%esi
call   1070 <pthread_mutex_init@plt>
test   %eax,%eax
jne    1178 <main+0x28>
lea    0xc(%rsp),%rdi
call   1030 <pthread_mutex_destroy@plt>
test   %eax,%eax
jne    1198 <main+0x48>
lea    0xc(%rsp),%rdi
xor    %esi,%esi
call   1070 <pthread_mutex_init@plt>
test   %eax,%eax
jne    1178 <main+0x28>
lea    0xc(%rsp),%rdi
call   1030 <pthread_mutex_destroy@plt>
test   %eax,%eax
jne    1198 <main+0x48>
lea    0xc(%rsp),%rdi
xor    %esi,%esi
call   1070 <pthread_mutex_init@plt>
test   %eax,%eax
jne    1178 <main+0x28>
lea    0xc(%rsp),%rdi
call   1030 <pthread_mutex_destroy@plt>
test   %eax,%eax
jne    1198 <main+0x48>
lea    0xc(%rsp),%rdi
xor    %esi,%esi
call   1070 <pthread_mutex_init@plt>
test   %eax,%eax
jne    1178 <main+0x28>
lea    0xc(%rsp),%rdi
call   1030 <pthread_mutex_destroy@plt>
test   %eax,%eax
jne    1198 <main+0x48>
lea    0xc(%rsp),%rdi
xor    %esi,%esi
call   1070 <pthread_mutex_init@plt>
test   %eax,%eax
jne    1178 <main+0x28>
lea    0xc(%rsp),%rdi
call   1030 <pthread_mutex_destroy@plt>
test   %eax,%eax
jne    1198 <main+0x48>
lea    0xc(%rsp),%rdi
xor    %esi,%esi
call   1070 <pthread_mutex_init@plt>
test   %eax,%eax
jne    1178 <main+0x28>
lea    0xc(%rsp),%rdi
call   1030 <pthread_mutex_destroy@plt>
test   %eax,%eax
jne    1198 <main+0x48>
lea    0xc(%rsp),%rdi
xor    %esi,%esi
call   1070 <pthread_mutex_init@plt>
test   %eax,%eax
jne    1178 <main+0x28>
lea    0xc(%rsp),%rdi
call   1030 <pthread_mutex_destroy@plt>
test   %eax,%eax
jne    1198 <main+0x48>
mov    $0x2,%edi
call   1040 <dup@plt>
sub    %ebx,%eax
jne    1318 <main+0x1c8>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %eax,%eax
jmp    11b4 <main+0x64>
mov    0x2cc1(%rip),%rcx
mov    (%rcx),%edi
lea    0xd1b(%rip),%rsi
jmp    11a8 <main+0x58>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [33 x i8] c"Unable to create mutex, error %d\00"
@.str.1 = [34 x i8] c"Unable to destroy mutex, error %d\00"
@.str.2 = [24 x i8] c"Leaked %d descriptors!\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
%7 = call i32 @dup(i32 2)
store i32 %7, i32* %3
%8 = load i32, i32* %3
%9 = call i32 @close(i32 %8)
store i32 0, i32* %2
br label %10
10: ; preds = %30, %0
%11 = load i32, i32* %2
%12 = icmp slt i32 %11, 10
br i1 %12, label %13, label %33
13: ; preds = %10
%14 = call i32 @pthread_mutex_init(i32* %4, i32* null)
store i32 %14, i32* %5
%15 = load i32, i32* %5
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %13
%18 = load i32, i32* @stderr
%19 = load i32, i32* %5
%20 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %19)
store i32 1, i32* %1
br label %47
21: ; preds = %13
%22 = call i32 @pthread_mutex_destroy(i32* %4)
store i32 %22, i32* %5
%23 = load i32, i32* %5
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %21
%26 = load i32, i32* @stderr
%27 = load i32, i32* %5
%28 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i32 %27)
store i32 1, i32* %1
br label %47
29: ; preds = %21
br label %30
30: ; preds = %29
%31 = load i32, i32* %2
%32 = add nsw i32 %31, 1
store i32 %32, i32* %2
br label %10
33: ; preds = %10
%34 = call i32 @dup(i32 2)
store i32 %34, i32* %6
%35 = load i32, i32* %6
%36 = load i32, i32* %3
%37 = icmp ne i32 %35, %36
br i1 %37, label %38, label %44
38: ; preds = %33
%39 = load i32, i32* @stderr
%40 = load i32, i32* %6
%41 = load i32, i32* %3
%42 = sub nsw i32 %40, %41
%43 = call i32 @fprintf(i32 %39, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %42)
store i32 1, i32* %1
br label %47
44: ; preds = %33
%45 = load i32, i32* %6
%46 = call i32 @close(i32 %45)
store i32 0, i32* %1
br label %47
47: ; preds = %44, %38, %25, %17
%48 = load i32, i32* %1
ret i32 %48
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [33 x i8] c"Unable to create mutex, error %d\00"
@.str.1 = [34 x i8] c"Unable to destroy mutex, error %d\00"
@.str.2 = [24 x i8] c"Leaked %d descriptors!\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = tail call i32 @dup(i32 2)
%3 = tail call i32 @close(i32 %2)
%4 = bitcast i32* %1 to i8*
%5 = call i32 @pthread_mutex_init(i32* %1, i32* null)
%6 = icmp eq i32 %5, 0
br i1 %6, label %11, label %7
7: ; preds = %67, %61, %55, %49, %43, %37, %31, %25, %19, %0
%8 = phi i32 [ %5, %0 ], [ %20, %19 ], [ %26, %25 ], [ %32, %31 ], [ %38, %37 ], [ %44, %43 ], [ %50, %49 ], [ %56, %55 ], [ %62, %61 ], [ %68, %67 ]
%9 = load i32, i32* @stderr
%10 = call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %8)
br label %18
11: ; preds = %0
%12 = call i32 @pthread_mutex_destroy(i32* %1)
%13 = icmp eq i32 %12, 0
br i1 %13, label %19, label %14
14: ; preds = %70, %64, %58, %52, %46, %40, %34, %28, %22, %11
%15 = phi i32 [ %12, %11 ], [ %23, %22 ], [ %29, %28 ], [ %35, %34 ], [ %41, %40 ], [ %47, %46 ], [ %53, %52 ], [ %59, %58 ], [ %65, %64 ], [ %71, %70 ]
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i32 %15)
br label %18
18: ; preds = %14, %7
br label %82
19: ; preds = %11
%20 = call i32 @pthread_mutex_init(i32* %1, i32* null)
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %7
22: ; preds = %19
%23 = call i32 @pthread_mutex_destroy(i32* %1)
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %14
25: ; preds = %22
%26 = call i32 @pthread_mutex_init(i32* %1, i32* null)
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %7
28: ; preds = %25
%29 = call i32 @pthread_mutex_destroy(i32* %1)
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %14
31: ; preds = %28
%32 = call i32 @pthread_mutex_init(i32* %1, i32* null)
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %7
34: ; preds = %31
%35 = call i32 @pthread_mutex_destroy(i32* %1)
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %14
37: ; preds = %34
%38 = call i32 @pthread_mutex_init(i32* %1, i32* null)
%39 = icmp eq i32 %38, 0
br i1 %39, label %40, label %7
40: ; preds = %37
%41 = call i32 @pthread_mutex_destroy(i32* %1)
%42 = icmp eq i32 %41, 0
br i1 %42, label %43, label %14
43: ; preds = %40
%44 = call i32 @pthread_mutex_init(i32* %1, i32* null)
%45 = icmp eq i32 %44, 0
br i1 %45, label %46, label %7
46: ; preds = %43
%47 = call i32 @pthread_mutex_destroy(i32* %1)
%48 = icmp eq i32 %47, 0
br i1 %48, label %49, label %14
49: ; preds = %46
%50 = call i32 @pthread_mutex_init(i32* %1, i32* null)
%51 = icmp eq i32 %50, 0
br i1 %51, label %52, label %7
52: ; preds = %49
%53 = call i32 @pthread_mutex_destroy(i32* %1)
%54 = icmp eq i32 %53, 0
br i1 %54, label %55, label %14
55: ; preds = %52
%56 = call i32 @pthread_mutex_init(i32* %1, i32* null)
%57 = icmp eq i32 %56, 0
br i1 %57, label %58, label %7
58: ; preds = %55
%59 = call i32 @pthread_mutex_destroy(i32* %1)
%60 = icmp eq i32 %59, 0
br i1 %60, label %61, label %14
61: ; preds = %58
%62 = call i32 @pthread_mutex_init(i32* %1, i32* null)
%63 = icmp eq i32 %62, 0
br i1 %63, label %64, label %7
64: ; preds = %61
%65 = call i32 @pthread_mutex_destroy(i32* %1)
%66 = icmp eq i32 %65, 0
br i1 %66, label %67, label %14
67: ; preds = %64
%68 = call i32 @pthread_mutex_init(i32* %1, i32* null)
%69 = icmp eq i32 %68, 0
br i1 %69, label %70, label %7
70: ; preds = %67
%71 = call i32 @pthread_mutex_destroy(i32* %1)
%72 = icmp eq i32 %71, 0
br i1 %72, label %73, label %14
73: ; preds = %70
%74 = call i32 @dup(i32 2)
%75 = icmp eq i32 %74, %2
br i1 %75, label %80, label %76
76: ; preds = %73
%77 = load i32, i32* @stderr
%78 = sub nsw i32 %74, %2
%79 = call i32 @fprintf(i32 %77, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %78)
br label %82
80: ; preds = %73
%81 = call i32 @close(i32 %2)
br label %82
82: ; preds = %18, %76, %80
%83 = phi i32 [ 1, %76 ], [ 0, %80 ], [ 1, %18 ]
ret i32 %83
}