#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*/ fd_set;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int read(int, char *, int);
int /*<<< orphan*/ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                          int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ write(int, char *, int);
void serve_gizmo(int gizmofd, int sockfd) {
  fd_set rset, allset;
  int maxfdp1;
  int shutdown;
  char buf[128];
  int rdsize;
  shutdown = 0;
  maxfdp1 = ((sockfd > gizmofd ? sockfd : gizmofd) + 1);
  FD_ZERO(&allset);
  FD_SET(gizmofd, &allset);
  FD_SET(sockfd, &allset);
  while (!shutdown) {
    rset = allset;
    select(maxfdp1, &rset, NULL, NULL, NULL);
    if (FD_ISSET(gizmofd, &rset)) {
      rdsize = read(gizmofd, buf, sizeof(buf));
      if (rdsize > 0) {
        write(sockfd, buf, rdsize);
      } else {
        shutdown = 1;
      }
    }
    if (FD_ISSET(sockfd, &rset)) {
      rdsize = read(sockfd, buf, sizeof(buf));
      if (rdsize > 0) {
        write(gizmofd, buf, rdsize);
      } else {
        shutdown = 1;
      }
    }
  }
  return;
}


x86 assembly (clang, -O0):
<serve_gizmo>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0x4(%rbp),%eax
jle    1192 <serve_gizmo+0x32>
mov    -0x8(%rbp),%eax
mov    %eax,-0xa8(%rbp)
jmp    119b <serve_gizmo+0x3b>
mov    -0x4(%rbp),%eax
mov    %eax,-0xa8(%rbp)
mov    -0xa8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
lea    -0x10(%rbp),%rdi
call   1060 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1080 <FD_SET@plt>
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1080 <FD_SET@plt>
cmpl   $0x0,-0x18(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11de <serve_gizmo+0x7e>
jmp    12c6 <serve_gizmo+0x166>
mov    -0x10(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x14(%rbp),%edi
lea    -0xc(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   1070 <select@plt>
mov    -0x4(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     125e <serve_gizmo+0xfe>
mov    -0x4(%rbp),%edi
lea    -0xa0(%rbp),%rsi
mov    $0x80,%edx
call   1050 <read@plt>
mov    %eax,-0xa4(%rbp)
cmpl   $0x0,-0xa4(%rbp)
jle    1252 <serve_gizmo+0xf2>
mov    -0x8(%rbp),%edi
lea    -0xa0(%rbp),%rsi
mov    -0xa4(%rbp),%edx
call   1030 <write@plt>
jmp    1259 <serve_gizmo+0xf9>
movl   $0x1,-0x18(%rbp)
jmp    125e <serve_gizmo+0xfe>
mov    -0x8(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     12c1 <serve_gizmo+0x161>
mov    -0x8(%rbp),%edi
lea    -0xa0(%rbp),%rsi
mov    $0x80,%edx
call   1050 <read@plt>
mov    %eax,-0xa4(%rbp)
cmpl   $0x0,-0xa4(%rbp)
jle    12b5 <serve_gizmo+0x155>
mov    -0x4(%rbp),%edi
lea    -0xa0(%rbp),%rsi
mov    -0xa4(%rbp),%edx
call   1030 <write@plt>
jmp    12bc <serve_gizmo+0x15c>
movl   $0x1,-0x18(%rbp)
jmp    12c1 <serve_gizmo+0x161>
jmp    11c8 <serve_gizmo+0x68>
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serve_gizmo>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x98,%rsp
mov    %esi,%r13d
mov    %edi,%ebp
cmp    %edi,%esi
mov    %edi,%r14d
cmovg  %esi,%r14d
add    $0x1,%r14d
lea    0x8(%rsp),%r15
mov    %r15,%rdi
call   1060 <FD_ZERO@plt>
mov    %ebp,%edi
mov    %r15,%rsi
call   1080 <FD_SET@plt>
mov    %r13d,%edi
mov    %r15,%rsi
call   1080 <FD_SET@plt>
lea    0xc(%rsp),%r15
lea    0x10(%rsp),%r12
jmp    11d0 <serve_gizmo+0x70>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %r12,%rsi
mov    %eax,%edx
call   1030 <write@plt>
test   %bl,%bl
je     124a <serve_gizmo+0xea>
mov    0x8(%rsp),%eax
mov    %eax,0xc(%rsp)
mov    %r14d,%edi
mov    %r15,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1070 <select@plt>
mov    %ebp,%edi
mov    %r15,%rsi
call   1040 <FD_ISSET@plt>
mov    $0x1,%bl
test   %rax,%rax
je     1222 <serve_gizmo+0xc2>
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x80,%edx
call   1050 <read@plt>
test   %eax,%eax
jle    1220 <serve_gizmo+0xc0>
mov    %r13d,%edi
mov    %r12,%rsi
mov    %eax,%edx
call   1030 <write@plt>
jmp    1222 <serve_gizmo+0xc2>
nopl   (%rax)
xor    %ebx,%ebx
mov    %r13d,%edi
mov    %r15,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     11cc <serve_gizmo+0x6c>
mov    %r13d,%edi
mov    %r12,%rsi
mov    $0x80,%edx
call   1050 <read@plt>
test   %eax,%eax
jg     11c0 <serve_gizmo+0x60>
add    $0x98,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @serve_gizmo(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [128 x i8]
%10 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
store i32 0, i32* %8
%11 = load i32, i32* %4
%12 = load i32, i32* %3
%13 = icmp sgt i32 %11, %12
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = load i32, i32* %4
br label %18
16: ; preds = %2
%17 = load i32, i32* %3
br label %18
18: ; preds = %16, %14
%19 = phi i32 [ %15, %14 ], [ %17, %16 ]
%20 = add nsw i32 %19, 1
store i32 %20, i32* %7
%21 = call i32 @FD_ZERO(i32* %6)
%22 = load i32, i32* %3
%23 = call i32 @FD_SET(i32 %22, i32* %6)
%24 = load i32, i32* %4
%25 = call i32 @FD_SET(i32 %24, i32* %6)
br label %26
26: ; preds = %67, %18
%27 = load i32, i32* %8
%28 = icmp ne i32 %27, 0
%29 = xor i1 %28, true
br i1 %29, label %30, label %68
30: ; preds = %26
%31 = load i32, i32* %6
store i32 %31, i32* %5
%32 = load i32, i32* %7
%33 = call i32 @select(i32 %32, i32* %5, i32* null, i32* null, i32* null)
%34 = load i32, i32* %3
%35 = call i64 @FD_ISSET(i32 %34, i32* %5)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %50
37: ; preds = %30
%38 = load i32, i32* %3
%39 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%40 = call i32 @read(i32 %38, i8* %39, i32 128)
store i32 %40, i32* %10
%41 = load i32, i32* %10
%42 = icmp sgt i32 %41, 0
br i1 %42, label %43, label %48
43: ; preds = %37
%44 = load i32, i32* %4
%45 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%46 = load i32, i32* %10
%47 = call i32 @write(i32 %44, i8* %45, i32 %46)
br label %49
48: ; preds = %37
store i32 1, i32* %8
br label %49
49: ; preds = %48, %43
br label %50
50: ; preds = %49, %30
%51 = load i32, i32* %4
%52 = call i64 @FD_ISSET(i32 %51, i32* %5)
%53 = icmp ne i64 %52, 0
br i1 %53, label %54, label %67
54: ; preds = %50
%55 = load i32, i32* %4
%56 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%57 = call i32 @read(i32 %55, i8* %56, i32 128)
store i32 %57, i32* %10
%58 = load i32, i32* %10
%59 = icmp sgt i32 %58, 0
br i1 %59, label %60, label %65
60: ; preds = %54
%61 = load i32, i32* %3
%62 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%63 = load i32, i32* %10
%64 = call i32 @write(i32 %61, i8* %62, i32 %63)
br label %66
65: ; preds = %54
store i32 1, i32* %8
br label %66
66: ; preds = %65, %60
br label %67
67: ; preds = %66, %50
br label %26
68: ; preds = %26
ret void
}

LLVM IR (-O2):
define void @serve_gizmo(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca [128 x i8]
%6 = bitcast i32* %3 to i8*
%7 = bitcast i32* %4 to i8*
%8 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%9 = icmp sgt i32 %1, %0
%10 = select i1 %9, i32 %1, i32 %0
%11 = add nsw i32 %10, 1
%12 = call i32 @FD_ZERO(i32* %4)
%13 = call i32 @FD_SET(i32 %0, i32* %4)
%14 = call i32 @FD_SET(i32 %1, i32* %4)
br label %15
15: ; preds = %2, %34
%16 = load i32, i32* %4
store i32 %16, i32* %3
%17 = call i32 @select(i32 %11, i32* %3, i32* null, i32* null, i32* null)
%18 = call i64 @FD_ISSET(i32 %0, i32* %3)
%19 = icmp eq i64 %18, 0
br i1 %19, label %25, label %20
20: ; preds = %15
%21 = call i32 @read(i32 %0, i8* %8, i32 128)
%22 = icmp sgt i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = call i32 @write(i32 %1, i8* %8, i32 %21)
br label %25
25: ; preds = %20, %23, %15
%26 = phi i1 [ false, %20 ], [ true, %23 ], [ true, %15 ]
%27 = call i64 @FD_ISSET(i32 %1, i32* %3)
%28 = icmp eq i64 %27, 0
br i1 %28, label %34, label %29
29: ; preds = %25
%30 = call i32 @read(i32 %1, i8* %8, i32 128)
%31 = icmp sgt i32 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %29
%33 = call i32 @write(i32 %0, i8* %8, i32 %30)
br label %34
34: ; preds = %32, %25
br i1 %26, label %15, label %35
35: ; preds = %29, %34
ret void
}