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

/* Variables and functions */
int /*<<< orphan*/ build_bulb(unsigned char *, int, int, int, int,
                              int /*<<< orphan*/, int, int);
int /*<<< orphan*/ flush_buffer(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ getok(int, int, int);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ usleep(int);
int /*<<< orphan*/ writebuf(int, unsigned char *, int);
int perform_custom(int fd, char *tag) {
  int string;
  int addr;
  int red;
  int i;
  unsigned char out[4];
  if (strcmp(tag, "hammer") == 0) {
    build_bulb(out, 0, 0, 0xcc, 0, 0, 13, 0);
    for (i = 0; i < 6000; i++) {
      writebuf(fd, out, 4);
      flush_buffer(fd);
      if (getok(fd, 1, 1000 * 1000) < 0)
        fprintf(stderr, "Error:  did not get closing ack\n");
    }
    return 0;
  }
  if (strcmp(tag, "one") == 0) {
    build_bulb(out, 0, 0, 0xcc, 15, 0, 0, 0);
    writebuf(fd, out, 4);
    for (red = 0; red < 500; red++) {
      flush_buffer(fd);
      usleep(1000);
    }
    return 0;
  }
  if (strcmp(tag, "red") == 0) {
    while (1) {
      for (red = 0; red <= 15; red++) {
        for (addr = 0; addr <= 34; addr++) {
          for (string = 0; string <= 5; string++) {
            build_bulb(out, string, addr, 0xcc, red, 0, 0, string == 5 ? 0 : 1);
            writebuf(fd, out, 4);
          }
        }
      }
    }
    return 0;
  }
  return -1;
}


x86 assembly (clang, -O0):
<perform_custom>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe76(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
jne    123f <perform_custom+0xcf>
lea    -0x24(%rbp),%rdi
xor    %r9d,%r9d
mov    $0xcc,%ecx
mov    %r9d,%esi
mov    %r9d,%edx
mov    %r9d,%r8d
movl   $0xd,(%rsp)
movl   $0x0,0x8(%rsp)
call   1080 <build_bulb@plt>
movl   $0x0,-0x20(%rbp)
cmpl   $0x1770,-0x20(%rbp)
jge    1233 <perform_custom+0xc3>
mov    -0x8(%rbp),%edi
lea    -0x24(%rbp),%rsi
mov    $0x4,%edx
call   1060 <writebuf@plt>
mov    -0x8(%rbp),%edi
call   1070 <flush_buffer@plt>
mov    -0x8(%rbp),%edi
mov    $0x1,%esi
mov    $0xf4240,%edx
call   1030 <getok@plt>
cmp    $0x0,%rax
jge    1220 <perform_custom+0xb0>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
lea    0xdec(%rip),%rsi
call   1050 <fprintf@plt>
jmp    1225 <perform_custom+0xb5>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11c9 <perform_custom+0x59>
movl   $0x0,-0x4(%rbp)
jmp    13b1 <perform_custom+0x241>
mov    -0x10(%rbp),%rdi
lea    0xdde(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
jne    12d6 <perform_custom+0x166>
lea    -0x24(%rbp),%rdi
xor    %r9d,%r9d
mov    $0xcc,%ecx
mov    $0xf,%r8d
mov    %r9d,%esi
mov    %r9d,%edx
movl   $0x0,(%rsp)
movl   $0x0,0x8(%rsp)
call   1080 <build_bulb@plt>
mov    -0x8(%rbp),%edi
lea    -0x24(%rbp),%rsi
mov    $0x4,%edx
call   1060 <writebuf@plt>
movl   $0x0,-0x1c(%rbp)
cmpl   $0x1f4,-0x1c(%rbp)
jge    12ca <perform_custom+0x15a>
mov    -0x8(%rbp),%edi
call   1070 <flush_buffer@plt>
mov    $0x3e8,%edi
call   1090 <usleep@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    129d <perform_custom+0x12d>
movl   $0x0,-0x4(%rbp)
jmp    13b1 <perform_custom+0x241>
mov    -0x10(%rbp),%rdi
lea    0xd4b(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
jne    13aa <perform_custom+0x23a>
jmp    12f5 <perform_custom+0x185>
movl   $0x0,-0x1c(%rbp)
cmpl   $0xf,-0x1c(%rbp)
jg     13a5 <perform_custom+0x235>
movl   $0x0,-0x18(%rbp)
cmpl   $0x22,-0x18(%rbp)
jg     1392 <perform_custom+0x222>
movl   $0x0,-0x14(%rbp)
cmpl   $0x5,-0x14(%rbp)
jg     137f <perform_custom+0x20f>
lea    -0x24(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
mov    -0x1c(%rbp),%r8d
mov    -0x14(%rbp),%r9d
mov    $0x1,%eax
xor    %ecx,%ecx
cmp    $0x5,%r9d
cmove  %ecx,%eax
mov    $0xcc,%ecx
xor    %r9d,%r9d
movl   $0x0,(%rsp)
mov    %eax,0x8(%rsp)
call   1080 <build_bulb@plt>
mov    -0x8(%rbp),%edi
lea    -0x24(%rbp),%rsi
mov    $0x4,%edx
call   1060 <writebuf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    131e <perform_custom+0x1ae>
jmp    1384 <perform_custom+0x214>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    130d <perform_custom+0x19d>
jmp    1397 <perform_custom+0x227>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    12fc <perform_custom+0x18c>
jmp    12f5 <perform_custom+0x185>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<perform_custom>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
mov    %edi,%r14d
lea    0xe77(%rip),%rsi
mov    %rbx,%rdi
call   1040 <strcmp@plt>
test   %rax,%rax
je     1346 <perform_custom+0x1d6>
lea    0xe87(%rip),%rsi
mov    %rbx,%rdi
call   1040 <strcmp@plt>
test   %rax,%rax
je     13c6 <perform_custom+0x256>
lea    0xe73(%rip),%rsi
mov    %rbx,%rdi
call   1040 <strcmp@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
jne    1427 <perform_custom+0x2b7>
lea    0xc(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r15,%rdi
xor    %esi,%esi
mov    %ebx,%edx
mov    $0xcc,%ecx
mov    %ebp,%r8d
xor    %r9d,%r9d
push   $0x1
push   $0x0
call   1080 <build_bulb@plt>
add    $0x10,%rsp
mov    %r14d,%edi
mov    %r15,%rsi
mov    $0x4,%edx
call   1060 <writebuf@plt>
mov    %r15,%rdi
mov    $0x1,%esi
mov    %ebx,%edx
mov    $0xcc,%ecx
mov    %ebp,%r8d
xor    %r9d,%r9d
push   $0x1
push   $0x0
call   1080 <build_bulb@plt>
add    $0x10,%rsp
mov    %r14d,%edi
mov    %r15,%rsi
mov    $0x4,%edx
call   1060 <writebuf@plt>
mov    %r15,%rdi
mov    $0x2,%esi
mov    %ebx,%edx
mov    $0xcc,%ecx
mov    %ebp,%r8d
xor    %r9d,%r9d
push   $0x1
push   $0x0
call   1080 <build_bulb@plt>
add    $0x10,%rsp
mov    %r14d,%edi
mov    %r15,%rsi
mov    $0x4,%edx
call   1060 <writebuf@plt>
mov    %r15,%rdi
mov    $0x3,%esi
mov    %ebx,%edx
mov    $0xcc,%ecx
mov    %ebp,%r8d
xor    %r9d,%r9d
push   $0x1
push   $0x0
call   1080 <build_bulb@plt>
add    $0x10,%rsp
mov    %r14d,%edi
mov    %r15,%rsi
mov    $0x4,%edx
call   1060 <writebuf@plt>
mov    %r15,%rdi
mov    $0x4,%esi
mov    %ebx,%edx
mov    $0xcc,%ecx
mov    %ebp,%r8d
xor    %r9d,%r9d
push   $0x1
push   $0x0
call   1080 <build_bulb@plt>
add    $0x10,%rsp
mov    %r14d,%edi
mov    %r15,%rsi
mov    $0x4,%edx
call   1060 <writebuf@plt>
mov    %r15,%rdi
mov    $0x5,%esi
mov    %ebx,%edx
mov    $0xcc,%ecx
mov    %ebp,%r8d
xor    %r9d,%r9d
push   $0x0
push   $0x0
call   1080 <build_bulb@plt>
add    $0x10,%rsp
mov    %r14d,%edi
mov    %r15,%rsi
mov    $0x4,%edx
call   1060 <writebuf@plt>
add    $0x1,%ebx
cmp    $0x23,%ebx
jne    1200 <perform_custom+0x90>
add    $0x1,%ebp
cmp    $0x10,%ebp
jne    11f0 <perform_custom+0x80>
jmp    11e0 <perform_custom+0x70>
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
xor    %edx,%edx
mov    $0xcc,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
push   $0x0
push   $0xd
call   1080 <build_bulb@plt>
add    $0x10,%rsp
mov    $0x1770,%ebp
mov    0x2c6a(%rip),%r12
lea    0xc8a(%rip),%r15
jmp    1385 <perform_custom+0x215>
nop
add    $0xffffffff,%ebp
je     13c2 <perform_custom+0x252>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1060 <writebuf@plt>
mov    %r14d,%edi
call   1070 <flush_buffer@plt>
mov    %r14d,%edi
mov    $0x1,%esi
mov    $0xf4240,%edx
call   1030 <getok@plt>
test   %rax,%rax
jns    1380 <perform_custom+0x210>
mov    (%r12),%edi
mov    %r15,%rsi
call   1050 <fprintf@plt>
jmp    1380 <perform_custom+0x210>
xor    %ebx,%ebx
jmp    1427 <perform_custom+0x2b7>
lea    0xc(%rsp),%rbp
xor    %ebx,%ebx
mov    %rbp,%rdi
xor    %esi,%esi
xor    %edx,%edx
mov    $0xcc,%ecx
mov    $0xf,%r8d
xor    %r9d,%r9d
push   $0x0
push   $0x0
call   1080 <build_bulb@plt>
add    $0x10,%rsp
mov    %r14d,%edi
mov    %rbp,%rsi
mov    $0x4,%edx
call   1060 <writebuf@plt>
mov    $0x1f4,%ebp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r14d,%edi
call   1070 <flush_buffer@plt>
mov    $0x3e8,%edi
call   1090 <usleep@plt>
add    $0xffffffff,%ebp
jne    1410 <perform_custom+0x2a0>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [7 x i8] c"hammer\00"
@stderr = global i32 0
@.str.1 = [33 x i8] c"Error: did not get closing ack\0A\00"
@.str.2 = [4 x i8] c"one\00"
@.str.3 = [4 x i8] c"red\00"
define i32 @perform_custom(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 [4 x i8]
store i32 %0, i32* %4
store i8* %1, i8** %5
%11 = load i8*, i8** %5
%12 = call i64 @strcmp(i8* %11, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %37
14: ; preds = %2
%15 = getelementptr inbounds [4 x i8], [4 x i8]* %10, i64 0, i64 0
%16 = call i32 @build_bulb(i8* %15, i32 0, i32 0, i32 204, i32 0, i32 0, i32 13, i32 0)
store i32 0, i32* %9
br label %17
17: ; preds = %33, %14
%18 = load i32, i32* %9
%19 = icmp slt i32 %18, 6000
br i1 %19, label %20, label %36
20: ; preds = %17
%21 = load i32, i32* %4
%22 = getelementptr inbounds [4 x i8], [4 x i8]* %10, i64 0, i64 0
%23 = call i32 @writebuf(i32 %21, i8* %22, i32 4)
%24 = load i32, i32* %4
%25 = call i32 @flush_buffer(i32 %24)
%26 = load i32, i32* %4
%27 = call i64 @getok(i32 %26, i32 1, i32 1000000)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %20
%30 = load i32, i32* @stderr
%31 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
br label %32
32: ; preds = %29, %20
br label %33
33: ; preds = %32
%34 = load i32, i32* %9
%35 = add nsw i32 %34, 1
store i32 %35, i32* %9
br label %17
36: ; preds = %17
store i32 0, i32* %3
br label %101
37: ; preds = %2
%38 = load i8*, i8** %5
%39 = call i64 @strcmp(i8* %38, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %58
41: ; preds = %37
%42 = getelementptr inbounds [4 x i8], [4 x i8]* %10, i64 0, i64 0
%43 = call i32 @build_bulb(i8* %42, i32 0, i32 0, i32 204, i32 15, i32 0, i32 0, i32 0)
%44 = load i32, i32* %4
%45 = getelementptr inbounds [4 x i8], [4 x i8]* %10, i64 0, i64 0
%46 = call i32 @writebuf(i32 %44, i8* %45, i32 4)
store i32 0, i32* %8
br label %47
47: ; preds = %54, %41
%48 = load i32, i32* %8
%49 = icmp slt i32 %48, 500
br i1 %49, label %50, label %57
50: ; preds = %47
%51 = load i32, i32* %4
%52 = call i32 @flush_buffer(i32 %51)
%53 = call i32 @usleep(i32 1000)
br label %54
54: ; preds = %50
%55 = load i32, i32* %8
%56 = add nsw i32 %55, 1
store i32 %56, i32* %8
br label %47
57: ; preds = %47
store i32 0, i32* %3
br label %101
58: ; preds = %37
%59 = load i8*, i8** %5
%60 = call i64 @strcmp(i8* %59, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%61 = icmp eq i64 %60, 0
br i1 %61, label %62, label %100
62: ; preds = %58
br label %63
63: ; preds = %62, %99
store i32 0, i32* %8
br label %64
64: ; preds = %96, %63
%65 = load i32, i32* %8
%66 = icmp sle i32 %65, 15
br i1 %66, label %67, label %99
67: ; preds = %64
store i32 0, i32* %7
br label %68
68: ; preds = %92, %67
%69 = load i32, i32* %7
%70 = icmp sle i32 %69, 34
br i1 %70, label %71, label %95
71: ; preds = %68
store i32 0, i32* %6
br label %72
72: ; preds = %88, %71
%73 = load i32, i32* %6
%74 = icmp sle i32 %73, 5
br i1 %74, label %75, label %91
75: ; preds = %72
%76 = getelementptr inbounds [4 x i8], [4 x i8]* %10, i64 0, i64 0
%77 = load i32, i32* %6
%78 = load i32, i32* %7
%79 = load i32, i32* %8
%80 = load i32, i32* %6
%81 = icmp eq i32 %80, 5
%82 = zext i1 %81 to i64
%83 = select i1 %81, i32 0, i32 1
%84 = call i32 @build_bulb(i8* %76, i32 %77, i32 %78, i32 204, i32 %79, i32 0, i32 0, i32 %83)
%85 = load i32, i32* %4
%86 = getelementptr inbounds [4 x i8], [4 x i8]* %10, i64 0, i64 0
%87 = call i32 @writebuf(i32 %85, i8* %86, i32 4)
br label %88
88: ; preds = %75
%89 = load i32, i32* %6
%90 = add nsw i32 %89, 1
store i32 %90, i32* %6
br label %72
91: ; preds = %72
br label %92
92: ; preds = %91
%93 = load i32, i32* %7
%94 = add nsw i32 %93, 1
store i32 %94, i32* %7
br label %68
95: ; preds = %68
br label %96
96: ; preds = %95
%97 = load i32, i32* %8
%98 = add nsw i32 %97, 1
store i32 %98, i32* %8
br label %64
99: ; preds = %64
br label %63
100: ; preds = %58
store i32 -1, i32* %3
br label %101
101: ; preds = %100, %57, %36
%102 = load i32, i32* %3
ret i32 %102
}

LLVM IR (-O2):
@.str = [7 x i8] c"hammer\00"
@stderr = global i32 0
@.str.1 = [33 x i8] c"Error: did not get closing ack\0A\00"
@.str.2 = [4 x i8] c"one\00"
@.str.3 = [4 x i8] c"red\00"
define i32 @perform_custom(i32 %0, i8* %1) {
%3 = alloca [4 x i8]
%4 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 0
%5 = tail call i64 @strcmp(i8* %1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %21
7: ; preds = %2
%8 = call i32 @build_bulb(i8* %4, i32 0, i32 0, i32 204, i32 0, i32 0, i32 13, i32 0)
br label %9
9: ; preds = %7, %18
%10 = phi i32 [ 0, %7 ], [ %19, %18 ]
%11 = call i32 @writebuf(i32 %0, i8* %4, i32 4)
%12 = call i32 @flush_buffer(i32 %0)
%13 = call i64 @getok(i32 %0, i32 1, i32 1000000)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %9
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
br label %18
18: ; preds = %9, %15
%19 = add nuw nsw i32 %10, 1
%20 = icmp eq i32 %19, 6000
br i1 %20, label %58, label %9
21: ; preds = %2
%22 = tail call i64 @strcmp(i8* %1, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %33
24: ; preds = %21
%25 = call i32 @build_bulb(i8* %4, i32 0, i32 0, i32 204, i32 15, i32 0, i32 0, i32 0)
%26 = call i32 @writebuf(i32 %0, i8* %4, i32 4)
br label %27
27: ; preds = %24, %27
%28 = phi i32 [ 0, %24 ], [ %31, %27 ]
%29 = call i32 @flush_buffer(i32 %0)
%30 = call i32 @usleep(i32 1000)
%31 = add nuw nsw i32 %28, 1
%32 = icmp eq i32 %31, 500
br i1 %32, label %58, label %27
33: ; preds = %21
%34 = tail call i64 @strcmp(i8* %1, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %58
36: ; preds = %55, %33
br label %37
37: ; preds = %36, %55
%38 = phi i32 [ 0, %36 ], [ %56, %55 ]
br label %39
39: ; preds = %37, %39
%40 = phi i32 [ 0, %37 ], [ %53, %39 ]
%41 = call i32 @build_bulb(i8* %4, i32 0, i32 %40, i32 204, i32 %38, i32 0, i32 0, i32 1)
%42 = call i32 @writebuf(i32 %0, i8* %4, i32 4)
%43 = call i32 @build_bulb(i8* %4, i32 1, i32 %40, i32 204, i32 %38, i32 0, i32 0, i32 1)
%44 = call i32 @writebuf(i32 %0, i8* %4, i32 4)
%45 = call i32 @build_bulb(i8* %4, i32 2, i32 %40, i32 204, i32 %38, i32 0, i32 0, i32 1)
%46 = call i32 @writebuf(i32 %0, i8* %4, i32 4)
%47 = call i32 @build_bulb(i8* %4, i32 3, i32 %40, i32 204, i32 %38, i32 0, i32 0, i32 1)
%48 = call i32 @writebuf(i32 %0, i8* %4, i32 4)
%49 = call i32 @build_bulb(i8* %4, i32 4, i32 %40, i32 204, i32 %38, i32 0, i32 0, i32 1)
%50 = call i32 @writebuf(i32 %0, i8* %4, i32 4)
%51 = call i32 @build_bulb(i8* %4, i32 5, i32 %40, i32 204, i32 %38, i32 0, i32 0, i32 0)
%52 = call i32 @writebuf(i32 %0, i8* %4, i32 4)
%53 = add nuw nsw i32 %40, 1
%54 = icmp eq i32 %53, 35
br i1 %54, label %55, label %39
55: ; preds = %39
%56 = add nuw nsw i32 %38, 1
%57 = icmp eq i32 %56, 16
br i1 %57, label %36, label %37
58: ; preds = %27, %18, %33
%59 = phi i32 [ -1, %33 ], [ 0, %18 ], [ 0, %27 ]
ret i32 %59
}