#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 */
struct semaphore {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ down(struct semaphore *);
void triple_down(struct semaphore *s1, struct semaphore *s2,
                 struct semaphore *s3) {
  if (s1 != s2) {
    if ((unsigned long)s1 < (unsigned long)s2) {
      if ((unsigned long)s1 < (unsigned long)s3) {
        struct semaphore *tmp = s3;
        s3 = s1;
        s1 = tmp;
      }
      if ((unsigned long)s1 < (unsigned long)s2) {
        struct semaphore *tmp = s2;
        s2 = s1;
        s1 = tmp;
      }
    } else {
      if ((unsigned long)s1 < (unsigned long)s3) {
        struct semaphore *tmp = s3;
        s3 = s1;
        s1 = tmp;
      }
      if ((unsigned long)s2 < (unsigned long)s3) {
        struct semaphore *tmp = s3;
        s3 = s2;
        s2 = tmp;
      }
    }
    down(s1);
  } else if ((unsigned long)s2 < (unsigned long)s3) {
    struct semaphore *tmp = s3;
    s3 = s2;
    s2 = tmp;
  }
  down(s2);
  down(s3);
}


x86 assembly (clang, -O0):
<triple_down>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rax
cmp    -0x10(%rbp),%rax
je     11ff <triple_down+0xef>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
cmp    %rcx,%rax
jae    119a <triple_down+0x8a>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
cmp    %rcx,%rax
jae    116c <triple_down+0x5c>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
cmp    %rcx,%rax
jae    1195 <triple_down+0x85>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11f1 <triple_down+0xe1>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
cmp    %rcx,%rax
jae    11c3 <triple_down+0xb3>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
cmp    %rcx,%rax
jae    11ec <triple_down+0xdc>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x10(%rbp)
jmp    11f1 <triple_down+0xe1>
mov    -0x8(%rbp),%rdi
call   1030 <down@plt>
jmp    122d <triple_down+0x11d>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
cmp    %rcx,%rax
jae    1228 <triple_down+0x118>
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x10(%rbp)
jmp    122d <triple_down+0x11d>
mov    -0x10(%rbp),%rdi
call   1030 <down@plt>
mov    -0x18(%rbp),%rdi
call   1030 <down@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<triple_down>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
cmp    %rsi,%rdi
je     113f <triple_down+0x2f>
cmp    %rdx,%rbx
mov    %rdx,%r14
cmovb  %rbx,%r14
cmovae %rbx,%rdx
cmp    %rsi,%rbx
jae    114f <triple_down+0x3f>
cmp    %rsi,%rdx
mov    %rsi,%rbx
cmovb  %rdx,%rbx
cmovb  %rsi,%rdx
jmp    1163 <triple_down+0x53>
cmp    %rdx,%rbx
mov    %rdx,%r14
cmovb  %rbx,%r14
cmovb  %rdx,%rbx
jmp    116b <triple_down+0x5b>
cmp    %rsi,%r14
mov    %r14,%rax
cmova  %rsi,%rax
cmova  %r14,%rsi
mov    %rax,%r14
mov    %rsi,%rbx
mov    %rdx,%rdi
call   1030 <down@plt>
mov    %rbx,%rdi
call   1030 <down@plt>
mov    %r14,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1030 <down@plt>

LLVM IR (-O0):
%struct.semaphore = type { i32 }
define void @triple_down(%struct.semaphore* %0, %struct.semaphore* %1, %struct.semaphore* %2) {
%4 = alloca %struct.semaphore*
%5 = alloca %struct.semaphore*
%6 = alloca %struct.semaphore*
%7 = alloca %struct.semaphore*
%8 = alloca %struct.semaphore*
%9 = alloca %struct.semaphore*
%10 = alloca %struct.semaphore*
%11 = alloca %struct.semaphore*
store %struct.semaphore* %0, %struct.semaphore** %4
store %struct.semaphore* %1, %struct.semaphore** %5
store %struct.semaphore* %2, %struct.semaphore** %6
%12 = load %struct.semaphore*, %struct.semaphore** %4
%13 = load %struct.semaphore*, %struct.semaphore** %5
%14 = icmp ne %struct.semaphore* %12, %13
br i1 %14, label %15, label %66
15: ; preds = %3
%16 = load %struct.semaphore*, %struct.semaphore** %4
%17 = ptrtoint %struct.semaphore* %16 to i64
%18 = load %struct.semaphore*, %struct.semaphore** %5
%19 = ptrtoint %struct.semaphore* %18 to i64
%20 = icmp ult i64 %17, %19
br i1 %20, label %21, label %42
21: ; preds = %15
%22 = load %struct.semaphore*, %struct.semaphore** %4
%23 = ptrtoint %struct.semaphore* %22 to i64
%24 = load %struct.semaphore*, %struct.semaphore** %6
%25 = ptrtoint %struct.semaphore* %24 to i64
%26 = icmp ult i64 %23, %25
br i1 %26, label %27, label %31
27: ; preds = %21
%28 = load %struct.semaphore*, %struct.semaphore** %6
store %struct.semaphore* %28, %struct.semaphore** %7
%29 = load %struct.semaphore*, %struct.semaphore** %4
store %struct.semaphore* %29, %struct.semaphore** %6
%30 = load %struct.semaphore*, %struct.semaphore** %7
store %struct.semaphore* %30, %struct.semaphore** %4
br label %31
31: ; preds = %27, %21
%32 = load %struct.semaphore*, %struct.semaphore** %4
%33 = ptrtoint %struct.semaphore* %32 to i64
%34 = load %struct.semaphore*, %struct.semaphore** %5
%35 = ptrtoint %struct.semaphore* %34 to i64
%36 = icmp ult i64 %33, %35
br i1 %36, label %37, label %41
37: ; preds = %31
%38 = load %struct.semaphore*, %struct.semaphore** %5
store %struct.semaphore* %38, %struct.semaphore** %8
%39 = load %struct.semaphore*, %struct.semaphore** %4
store %struct.semaphore* %39, %struct.semaphore** %5
%40 = load %struct.semaphore*, %struct.semaphore** %8
store %struct.semaphore* %40, %struct.semaphore** %4
br label %41
41: ; preds = %37, %31
br label %63
42: ; preds = %15
%43 = load %struct.semaphore*, %struct.semaphore** %4
%44 = ptrtoint %struct.semaphore* %43 to i64
%45 = load %struct.semaphore*, %struct.semaphore** %6
%46 = ptrtoint %struct.semaphore* %45 to i64
%47 = icmp ult i64 %44, %46
br i1 %47, label %48, label %52
48: ; preds = %42
%49 = load %struct.semaphore*, %struct.semaphore** %6
store %struct.semaphore* %49, %struct.semaphore** %9
%50 = load %struct.semaphore*, %struct.semaphore** %4
store %struct.semaphore* %50, %struct.semaphore** %6
%51 = load %struct.semaphore*, %struct.semaphore** %9
store %struct.semaphore* %51, %struct.semaphore** %4
br label %52
52: ; preds = %48, %42
%53 = load %struct.semaphore*, %struct.semaphore** %5
%54 = ptrtoint %struct.semaphore* %53 to i64
%55 = load %struct.semaphore*, %struct.semaphore** %6
%56 = ptrtoint %struct.semaphore* %55 to i64
%57 = icmp ult i64 %54, %56
br i1 %57, label %58, label %62
58: ; preds = %52
%59 = load %struct.semaphore*, %struct.semaphore** %6
store %struct.semaphore* %59, %struct.semaphore** %10
%60 = load %struct.semaphore*, %struct.semaphore** %5
store %struct.semaphore* %60, %struct.semaphore** %6
%61 = load %struct.semaphore*, %struct.semaphore** %10
store %struct.semaphore* %61, %struct.semaphore** %5
br label %62
62: ; preds = %58, %52
br label %63
63: ; preds = %62, %41
%64 = load %struct.semaphore*, %struct.semaphore** %4
%65 = call i32 @down(%struct.semaphore* %64)
br label %77
66: ; preds = %3
%67 = load %struct.semaphore*, %struct.semaphore** %5
%68 = ptrtoint %struct.semaphore* %67 to i64
%69 = load %struct.semaphore*, %struct.semaphore** %6
%70 = ptrtoint %struct.semaphore* %69 to i64
%71 = icmp ult i64 %68, %70
br i1 %71, label %72, label %76
72: ; preds = %66
%73 = load %struct.semaphore*, %struct.semaphore** %6
store %struct.semaphore* %73, %struct.semaphore** %11
%74 = load %struct.semaphore*, %struct.semaphore** %5
store %struct.semaphore* %74, %struct.semaphore** %6
%75 = load %struct.semaphore*, %struct.semaphore** %11
store %struct.semaphore* %75, %struct.semaphore** %5
br label %76
76: ; preds = %72, %66
br label %77
77: ; preds = %76, %63
%78 = load %struct.semaphore*, %struct.semaphore** %5
%79 = call i32 @down(%struct.semaphore* %78)
%80 = load %struct.semaphore*, %struct.semaphore** %6
%81 = call i32 @down(%struct.semaphore* %80)
ret void
}

LLVM IR (-O2):
%struct.semaphore = type { i32 }
define void @triple_down(%struct.semaphore* %0, %struct.semaphore* %1, %struct.semaphore* %2) {
%4 = icmp eq %struct.semaphore* %0, %1
br i1 %4, label %23, label %5
5: ; preds = %3
%6 = icmp ult %struct.semaphore* %0, %1
%7 = icmp ult %struct.semaphore* %0, %2
%8 = select i1 %7, %struct.semaphore* %0, %struct.semaphore* %2
%9 = select i1 %7, %struct.semaphore* %2, %struct.semaphore* %0
br i1 %6, label %10, label %14
10: ; preds = %5
%11 = icmp ult %struct.semaphore* %9, %1
%12 = select i1 %11, %struct.semaphore* %9, %struct.semaphore* %1
%13 = select i1 %11, %struct.semaphore* %1, %struct.semaphore* %9
br label %18
14: ; preds = %5
%15 = icmp ugt %struct.semaphore* %8, %1
%16 = select i1 %15, %struct.semaphore* %1, %struct.semaphore* %8
%17 = select i1 %15, %struct.semaphore* %8, %struct.semaphore* %1
br label %18
18: ; preds = %14, %10
%19 = phi %struct.semaphore* [ %8, %10 ], [ %16, %14 ]
%20 = phi %struct.semaphore* [ %12, %10 ], [ %17, %14 ]
%21 = phi %struct.semaphore* [ %13, %10 ], [ %9, %14 ]
%22 = tail call i32 @down(%struct.semaphore* %21)
br label %27
23: ; preds = %3
%24 = icmp ult %struct.semaphore* %0, %2
%25 = select i1 %24, %struct.semaphore* %0, %struct.semaphore* %2
%26 = select i1 %24, %struct.semaphore* %2, %struct.semaphore* %0
br label %27
27: ; preds = %23, %18
%28 = phi %struct.semaphore* [ %19, %18 ], [ %25, %23 ]
%29 = phi %struct.semaphore* [ %20, %18 ], [ %26, %23 ]
%30 = tail call i32 @down(%struct.semaphore* %29)
%31 = tail call i32 @down(%struct.semaphore* %28)
ret void
}