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

/* Variables and functions */
int /*<<< orphan*/ FD_CLR(int, int /*<<< orphan*/ *);
scalar_t__ atoi(char *);
int /*<<< orphan*/ close(int);
int read(int, char *, int);
scalar_t__ strlen(char *);
int read_fifo(int fd, int *sum, fd_set *read_set) {
  char buf[100];
  int bytes = read(fd, buf, 100);
  buf[bytes] = '\0';
  if (bytes == 0) {
    FD_CLR(fd, read_set);
    close(fd);
    return 0;
  }
  if (strlen(buf)) {
    *sum += atoi(buf);
  } else {
    close(fd);
  }
  return bytes;
}


x86 assembly (clang, -O0):
<read_fifo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x80(%rbp),%rsi
mov    $0x64,%edx
call   1050 <read@plt>
mov    %eax,-0x84(%rbp)
movslq -0x84(%rbp),%rax
movb   $0x0,-0x80(%rbp,%rax,1)
cmpl   $0x0,-0x84(%rbp)
jne    11b6 <read_fifo+0x66>
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1070 <FD_CLR@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    11f7 <read_fifo+0xa7>
lea    -0x80(%rbp),%rdi
call   1030 <strlen@plt>
cmp    $0x0,%rax
je     11e6 <read_fifo+0x96>
lea    -0x80(%rbp),%rdi
call   1060 <atoi@plt>
mov    %rax,%rdx
mov    -0x10(%rbp),%rax
movslq (%rax),%rcx
add    %rdx,%rcx
mov    %ecx,(%rax)
jmp    11ee <read_fifo+0x9e>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
mov    -0x84(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_fifo>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x68,%rsp
mov    %rdx,%r15
mov    %rsi,%r14
mov    %edi,%ebp
mov    %rsp,%rsi
mov    $0x64,%edx
call   1040 <read@plt>
mov    %eax,%ebx
cltq
movb   $0x0,(%rsp,%rax,1)
test   %eax,%eax
je     117e <read_fifo+0x3e>
cmpb   $0x0,(%rsp)
je     1188 <read_fifo+0x48>
mov    %rsp,%rdi
call   1050 <atoi@plt>
add    %eax,(%r14)
jmp    118f <read_fifo+0x4f>
mov    %ebp,%edi
mov    %r15,%rsi
call   1060 <FD_CLR@plt>
mov    %ebp,%edi
call   1030 <close@plt>
mov    %ebx,%eax
add    $0x68,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @read_fifo(i32 %0, i32* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca [100 x i8]
%9 = alloca i32
store i32 %0, i32* %5
store i32* %1, i32** %6
store i32* %2, i32** %7
%10 = load i32, i32* %5
%11 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%12 = call i32 @read(i32 %10, i8* %11, i32 100)
store i32 %12, i32* %9
%13 = load i32, i32* %9
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 %14
store i8 0, i8* %15
%16 = load i32, i32* %9
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %24
18: ; preds = %3
%19 = load i32, i32* %5
%20 = load i32*, i32** %7
%21 = call i32 @FD_CLR(i32 %19, i32* %20)
%22 = load i32, i32* %5
%23 = call i32 @close(i32 %22)
store i32 0, i32* %4
br label %41
24: ; preds = %3
%25 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%26 = call i64 @strlen(i8* %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %36
28: ; preds = %24
%29 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%30 = call i64 @atoi(i8* %29)
%31 = load i32*, i32** %6
%32 = load i32, i32* %31
%33 = sext i32 %32 to i64
%34 = add nsw i64 %33, %30
%35 = trunc i64 %34 to i32
store i32 %35, i32* %31
br label %39
36: ; preds = %24
%37 = load i32, i32* %5
%38 = call i32 @close(i32 %37)
br label %39
39: ; preds = %36, %28
%40 = load i32, i32* %9
store i32 %40, i32* %4
br label %41
41: ; preds = %39, %18
%42 = load i32, i32* %4
ret i32 %42
}

LLVM IR (-O2):
define i32 @read_fifo(i32 %0, i32* nocapture %1, i32* %2) {
%4 = alloca [100 x i8]
%5 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%6 = call i32 @read(i32 %0, i8* %5, i32 100)
%7 = sext i32 %6 to i64
%8 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 %7
store i8 0, i8* %8
%9 = icmp eq i32 %6, 0
br i1 %9, label %10, label %13
10: ; preds = %3
%11 = tail call i32 @FD_CLR(i32 %0, i32* %2)
%12 = tail call i32 @close(i32 %0)
br label %23
13: ; preds = %3
%14 = load i8, i8* %5
%15 = icmp eq i8 %14, 0
br i1 %15, label %21, label %16
16: ; preds = %13
%17 = call i64 @atoi(i8* %5)
%18 = load i32, i32* %1
%19 = trunc i64 %17 to i32
%20 = add i32 %18, %19
store i32 %20, i32* %1
br label %23
21: ; preds = %13
%22 = tail call i32 @close(i32 %0)
br label %23
23: ; preds = %16, %21, %10
ret i32 %6
}