#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 timespec {
  int tv_sec;
  int /*<<< orphan*/ tv_nsec;
};
struct pollfd {
  int fd;
  int /*<<< orphan*/ events;
};

/* Variables and functions */
int /*<<< orphan*/ POLLIN;
int /*<<< orphan*/ POLLOUT;
int /*<<< orphan*/ perror(char *);
int pipe(int *);
int ppoll(struct pollfd *, int, struct timespec *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, int);
int strlen(char *);
size_t write(int, char *, size_t);
int main(void) {
  int ret;
  int fd[2];
  char string[] = "Hello, world!\n";
  struct timespec tv = {.tv_sec = 10, .tv_nsec = 0};
  ret = pipe(fd);
  if (ret < 0) {
    perror("pipe creation failed");
    return 1;
  }
  struct pollfd outfds[] = {
      {.fd = fd[1], .events = POLLOUT},
  };
  ret = ppoll(outfds, 1, &tv, NULL);
  if (ret <= 0) {
    perror("ppoll with POLLOUT failed");
    return 1;
  }
  printf("ppoll(POLLOUT) returned %d file descriptors\n", ret);
  struct pollfd infds[] = {
      {.fd = fd[0], .events = POLLIN},
  };
  size_t size = strlen(string) + 1;
  if (write(fd[1], string, size) != size) {
    perror("write error");
    return 1;
  }
  ret = ppoll(infds, 1, &tv, NULL);
  if (ret <= 0) {
    perror("ppoll with POLLIN failed");
    return 1;
  }
  printf("ppoll(POLLIN) returned %d file descriptors\n", ret);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe8a(%rip),%rax
mov    %rax,-0x1f(%rbp)
mov    0xe88(%rip),%eax
mov    %eax,-0x17(%rbp)
mov    0xe82(%rip),%ax
mov    %ax,-0x13(%rbp)
mov    0xe7a(%rip),%al
mov    %al,-0x11(%rbp)
mov    0xf1e(%rip),%rax
mov    %rax,-0x28(%rbp)
lea    -0x10(%rbp),%rdi
call   1060 <pipe@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11d0 <main+0x70>
lea    0xe50(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12d3 <main+0x173>
mov    -0xc(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    0x2e1b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
lea    -0x30(%rbp),%rdi
mov    $0x1,%esi
lea    -0x28(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   1070 <ppoll@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jg     121d <main+0xbd>
lea    0xe18(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12d3 <main+0x173>
mov    -0x8(%rbp),%esi
lea    0xe17(%rip),%rdi
call   1050 <printf@plt>
mov    -0x10(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    0x2da7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
lea    -0x1f(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x40(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x1f(%rbp),%rsi
mov    -0x40(%rbp),%rdx
call   1030 <write@plt>
cmp    -0x40(%rbp),%rax
je     1282 <main+0x122>
lea    0xdfa(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12d3 <main+0x173>
lea    -0x38(%rbp),%rdi
mov    $0x1,%esi
lea    -0x28(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   1070 <ppoll@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jg     12bd <main+0x15d>
lea    0xdcb(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12d3 <main+0x173>
mov    -0x8(%rbp),%esi
lea    0xdc9(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x38,%rsp
movabs $0xa21646c726f77,%rax
mov    %rax,0x2f(%rsp)
movabs $0x77202c6f6c6c6548,%rax
2c 20 77
mov    %rax,0x28(%rsp)
movq   $0xa,0x20(%rsp)
lea    0x8(%rsp),%rdi
call   1060 <pipe@plt>
test   %eax,%eax
js     125b <main+0xfb>
mov    0xc(%rsp),%eax
mov    %eax,0x18(%rsp)
mov    0x2e49(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
lea    0x18(%rsp),%rdi
mov    $0x1,%ebx
lea    0x20(%rsp),%rdx
mov    $0x1,%esi
xor    %ecx,%ecx
call   1070 <ppoll@plt>
test   %eax,%eax
jle    1264 <main+0x104>
lea    0xe5f(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
mov    0x8(%rsp),%eax
mov    %eax,0x10(%rsp)
mov    0x2deb(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
lea    0x28(%rsp),%r14
mov    %r14,%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
movslq %eax,%rbx
mov    0xc(%rsp),%edi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1030 <write@plt>
cmp    %rbx,%rax
jne    1272 <main+0x112>
lea    0x10(%rsp),%rdi
mov    $0x1,%ebx
lea    0x20(%rsp),%rdx
mov    $0x1,%esi
xor    %ecx,%ecx
call   1070 <ppoll@plt>
test   %eax,%eax
jle    1285 <main+0x125>
lea    0xe48(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
ret
lea    0xdad(%rip),%rdi
jmp    1279 <main+0x119>
lea    0xdb9(%rip),%rdi
call   1080 <perror@plt>
jmp    1251 <main+0xf1>
lea    0xdf2(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%ebx
jmp    1251 <main+0xf1>
lea    0xdeb(%rip),%rdi
call   1080 <perror@plt>
jmp    1251 <main+0xf1>

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
%struct.pollfd = type { i32, i32 }
@__const.main.string = [15 x i8] c"Hello, world!\0A\00"
@__const.main.tv = %struct.timespec { i32 10, i32 0 }
@.str = [21 x i8] c"pipe creation failed\00"
@POLLOUT = global i32 0
@.str.1 = [26 x i8] c"ppoll with POLLOUT failed\00"
@.str.2 = [45 x i8] c"ppoll(POLLOUT) returned %d file descriptors\0A\00"
@POLLIN = global i32 0
@.str.3 = [12 x i8] c"write error\00"
@.str.4 = [25 x i8] c"ppoll with POLLIN failed\00"
@.str.5 = [44 x i8] c"ppoll(POLLIN) returned %d file descriptors\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [2 x i32]
%4 = alloca [15 x i8]
%5 = alloca %struct.timespec
%6 = alloca [1 x %struct.pollfd]
%7 = alloca [1 x %struct.pollfd]
%8 = alloca i64
store i32 0, i32* %1
%9 = bitcast [15 x i8]* %4 to i8*
%10 = bitcast %struct.timespec* %5 to i8*
%11 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%12 = call i32 @pipe(i32* %11)
store i32 %12, i32* %2
%13 = load i32, i32* %2
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %0
%16 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %62
17: ; preds = %0
%18 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %6, i64 0, i64 0
%19 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %18, i32 0, i32 0
%20 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%21 = load i32, i32* %20
store i32 %21, i32* %19
%22 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %18, i32 0, i32 1
%23 = load i32, i32* @POLLOUT
store i32 %23, i32* %22
%24 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %6, i64 0, i64 0
%25 = call i32 @ppoll(%struct.pollfd* %24, i32 1, %struct.timespec* %5, i32* null)
store i32 %25, i32* %2
%26 = load i32, i32* %2
%27 = icmp sle i32 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %17
%29 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %62
30: ; preds = %17
%31 = load i32, i32* %2
%32 = call i32 @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0), i32 %31)
%33 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %7, i64 0, i64 0
%34 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %33, i32 0, i32 0
%35 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%36 = load i32, i32* %35
store i32 %36, i32* %34
%37 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %33, i32 0, i32 1
%38 = load i32, i32* @POLLIN
store i32 %38, i32* %37
%39 = getelementptr inbounds [15 x i8], [15 x i8]* %4, i64 0, i64 0
%40 = call i32 @strlen(i8* %39)
%41 = add nsw i32 %40, 1
%42 = sext i32 %41 to i64
store i64 %42, i64* %8
%43 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%44 = load i32, i32* %43
%45 = getelementptr inbounds [15 x i8], [15 x i8]* %4, i64 0, i64 0
%46 = load i64, i64* %8
%47 = call i64 @write(i32 %44, i8* %45, i64 %46)
%48 = load i64, i64* %8
%49 = icmp ne i64 %47, %48
br i1 %49, label %50, label %52
50: ; preds = %30
%51 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %1
br label %62
52: ; preds = %30
%53 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %7, i64 0, i64 0
%54 = call i32 @ppoll(%struct.pollfd* %53, i32 1, %struct.timespec* %5, i32* null)
store i32 %54, i32* %2
%55 = load i32, i32* %2
%56 = icmp sle i32 %55, 0
br i1 %56, label %57, label %59
57: ; preds = %52
%58 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %1
br label %62
59: ; preds = %52
%60 = load i32, i32* %2
%61 = call i32 @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.5, i64 0, i64 0), i32 %60)
store i32 0, i32* %1
br label %62
62: ; preds = %59, %57, %50, %28, %15
%63 = load i32, i32* %1
ret i32 %63
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
%struct.pollfd = type { i32, i32 }
@__const.main.string = [15 x i8] c"Hello, world!\0A\00"
@.str = [21 x i8] c"pipe creation failed\00"
@POLLOUT = global i32 0
@.str.1 = [26 x i8] c"ppoll with POLLOUT failed\00"
@.str.2 = [45 x i8] c"ppoll(POLLOUT) returned %d file descriptors\0A\00"
@POLLIN = global i32 0
@.str.3 = [12 x i8] c"write error\00"
@.str.4 = [25 x i8] c"ppoll with POLLIN failed\00"
@.str.5 = [44 x i8] c"ppoll(POLLIN) returned %d file descriptors\0A\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca [15 x i8]
%3 = alloca i64
%4 = bitcast i64* %3 to %struct.timespec*
%5 = alloca [1 x %struct.pollfd]
%6 = alloca [1 x %struct.pollfd]
%7 = bitcast [2 x i32]* %1 to i8*
%8 = getelementptr inbounds [15 x i8], [15 x i8]* %2, i64 0, i64 0
%9 = bitcast i64* %3 to i8*
store i64 10, i64* %3
%10 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%11 = call i32 @pipe(i32* %10)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %0
%14 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %54
15: ; preds = %0
%16 = bitcast [1 x %struct.pollfd]* %5 to i8*
%17 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %5, i64 0, i64 0
%18 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %5, i64 0, i64 0, i32 0
%19 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%20 = load i32, i32* %19
store i32 %20, i32* %18
%21 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %5, i64 0, i64 0, i32 1
%22 = load i32, i32* @POLLOUT
store i32 %22, i32* %21
%23 = call i32 @ppoll(%struct.pollfd* %17, i32 1, %struct.timespec* %4, i32* null)
%24 = icmp slt i32 %23, 1
br i1 %24, label %25, label %27
25: ; preds = %15
%26 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
br label %52
27: ; preds = %15
%28 = call i32 @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0), i32 %23)
%29 = bitcast [1 x %struct.pollfd]* %6 to i8*
%30 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %6, i64 0, i64 0, i32 0
%31 = load i32, i32* %10
store i32 %31, i32* %30
%32 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %6, i64 0, i64 0, i32 1
%33 = load i32, i32* @POLLIN
store i32 %33, i32* %32
%34 = call i32 @strlen(i8* %8)
%35 = add nsw i32 %34, 1
%36 = sext i32 %35 to i64
%37 = load i32, i32* %19
%38 = call i64 @write(i32 %37, i8* %8, i64 %36)
%39 = icmp eq i64 %38, %36
br i1 %39, label %42, label %40
40: ; preds = %27
%41 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
br label %50
42: ; preds = %27
%43 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %6, i64 0, i64 0
%44 = call i32 @ppoll(%struct.pollfd* %43, i32 1, %struct.timespec* %4, i32* null)
%45 = icmp slt i32 %44, 1
br i1 %45, label %46, label %48
46: ; preds = %42
%47 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
br label %50
48: ; preds = %42
%49 = call i32 @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.5, i64 0, i64 0), i32 %44)
br label %50
50: ; preds = %48, %46, %40
%51 = phi i32 [ 1, %40 ], [ 1, %46 ], [ 0, %48 ]
br label %52
52: ; preds = %50, %25
%53 = phi i32 [ 1, %25 ], [ %51, %50 ]
br label %54
54: ; preds = %52, %13
%55 = phi i32 [ 1, %13 ], [ %53, %52 ]
ret i32 %55
}