#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*/ O_RDONLY;
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ ult_exit(int /*<<< orphan*/);
scalar_t__ ult_read(int, char *, int);
int /*<<< orphan*/ ult_yield();
void threadA() {
  char out[129];
  int handle = open("/dev/random", O_RDONLY);
  if (ult_read(handle, out, 10)) {
    printf("gelesen:%s\n", out);
  }
  ult_yield();
  printf("works!\n");
  ult_exit(0);
}


x86 assembly (clang, -O0):
<threadA>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    0x2e86(%rip),%rax
mov    (%rax),%esi
lea    0xe95(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x94(%rbp)
mov    -0x94(%rbp),%edi
lea    -0x90(%rbp),%rsi
mov    $0xa,%edx
call   1070 <ult_read@plt>
cmp    $0x0,%rax
je     11ac <threadA+0x5c>
lea    -0x90(%rbp),%rsi
lea    0xe67(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x0,%al
call   1060 <ult_yield@plt>
lea    0xe5e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %edi,%edi
call   1040 <ult_exit@plt>
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<threadA>:
sub    $0x88,%rsp
mov    0x2e7a(%rip),%rax
mov    (%rax),%esi
lea    0xe89(%rip),%rdi
call   1060 <open@plt>
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0xa,%edx
call   1080 <ult_read@plt>
test   %rax,%rax
je     11a1 <threadA+0x41>
lea    0xe75(%rip),%rdi
mov    %rsp,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   1070 <ult_yield@plt>
lea    0xe69(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1050 <ult_exit@plt>
add    $0x88,%rsp
ret

LLVM IR (-O0):
@.str = [12 x i8] c"/dev/random\00"
@O_RDONLY = global i32 0
@.str.1 = [12 x i8] c"gelesen:%s\0A\00"
@.str.2 = [8 x i8] c"works!\0A\00"
define void @threadA() {
%1 = alloca [129 x i8]
%2 = alloca i32
%3 = load i32, i32* @O_RDONLY
%4 = call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %3)
store i32 %4, i32* %2
%5 = load i32, i32* %2
%6 = getelementptr inbounds [129 x i8], [129 x i8]* %1, i64 0, i64 0
%7 = call i64 @ult_read(i32 %5, i8* %6, i32 10)
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = getelementptr inbounds [129 x i8], [129 x i8]* %1, i64 0, i64 0
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %10)
br label %12
12: ; preds = %9, %0
%13 = call i32 (...) @ult_yield()
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%15 = call i32 @ult_exit(i32 0)
ret void
}

LLVM IR (-O2):
@.str = [12 x i8] c"/dev/random\00"
@O_RDONLY = global i32 0
@.str.1 = [12 x i8] c"gelesen:%s\0A\00"
@str = [7 x i8] c"works!\00"
define void @threadA() {
%1 = alloca [129 x i8]
%2 = getelementptr inbounds [129 x i8], [129 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = call i64 @ult_read(i32 %4, i8* %2, i32 10)
%6 = icmp eq i64 %5, 0
br i1 %6, label %9, label %7
7: ; preds = %0
%8 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %2)
br label %9
9: ; preds = %7, %0
%10 = call i32 (...) @ult_yield()
%11 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str, i64 0, i64 0))
%12 = call i32 @ult_exit(i32 0)
ret void
}