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

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, char);
int /*<<< orphan*/ fread(char *, int, int, void *);
int /*<<< orphan*/ pthread_exit(void *);
int /*<<< orphan*/ puts(char *);
void *func(void *p) {
  char c = '0';
  fread(&c, 1, 1, p);
  FILE *fp_target = NULL;
  fp_target = fopen("/tmp/target.txt", "a");
  if (NULL == fp_target) {
    puts("fopen() failed");
    exit(3);
  }
  fprintf(fp_target, "%c\n", c);
  fclose(fp_target);
  pthread_exit(p);
}


x86 assembly (clang, -O0):
<func>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movb   $0x30,-0x11(%rbp)
mov    -0x10(%rbp),%rcx
lea    -0x11(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1040 <fread@plt>
movq   $0x0,-0x20(%rbp)
lea    0xe5d(%rip),%rdi
lea    0xe66(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
xor    %eax,%eax
cmp    -0x20(%rbp),%rax
jne    11d5 <func+0x65>
lea    0xe4c(%rip),%rdi
call   1030 <puts@plt>
mov    $0x3,%edi
call   1090 <exit@plt>
mov    -0x20(%rbp),%rdi
lea    0xe41(%rip),%rsi
movsbl -0x11(%rbp),%edx
call   1060 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x10(%rbp),%rdi
call   1070 <pthread_exit@plt>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<func>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
movb   $0x30,0x7(%rsp)
lea    0x7(%rsp),%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1040 <fread@plt>
lea    0xe66(%rip),%rdi
lea    0xe6f(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11d9 <func+0x69>
mov    %rax,%rbx
movsbl 0x7(%rsp),%edx
lea    0xe67(%rip),%rsi
mov    %rax,%rdi
call   1060 <fprintf@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %r14,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1070 <pthread_exit@plt>
lea    0xe32(%rip),%rdi
call   1030 <puts@plt>
mov    $0x3,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [16 x i8] c"/tmp/target.txt\00"
@.str.1 = [2 x i8] c"a\00"
@.str.2 = [15 x i8] c"fopen() failed\00"
@.str.3 = [4 x i8] c"%c\0A\00"
define i8* @func(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8
%5 = alloca i32*
store i8* %0, i8** %3
store i8 48, i8* %4
%6 = load i8*, i8** %3
%7 = call i32 @fread(i8* %4, i32 1, i32 1, i8* %6)
store i32* null, i32** %5
%8 = call i32* @fopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %8, i32** %5
%9 = load i32*, i32** %5
%10 = icmp eq i32* null, %9
br i1 %10, label %11, label %14
11: ; preds = %1
%12 = call i32 @puts(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%13 = call i32 @exit(i32 3)
unreachable
14: ; preds = %1
%15 = load i32*, i32** %5
%16 = load i8, i8* %4
%17 = call i32 @fprintf(i32* %15, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8 signext %16)
%18 = load i32*, i32** %5
%19 = call i32 @fclose(i32* %18)
%20 = load i8*, i8** %3
%21 = call i32 @pthread_exit(i8* %20)
%22 = load i8*, i8** %2
ret i8* %22
}

LLVM IR (-O2):
@.str = [16 x i8] c"/tmp/target.txt\00"
@.str.1 = [2 x i8] c"a\00"
@.str.2 = [15 x i8] c"fopen() failed\00"
@.str.3 = [4 x i8] c"%c\0A\00"
define noalias i8* @func(i8* %0) {
%2 = alloca i8
store i8 48, i8* %2
%3 = call i32 @fread(i8* %2, i32 1, i32 1, i8* %0)
%4 = tail call i32* @fopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %9
6: ; preds = %1
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%8 = tail call i32 @exit(i32 3)
unreachable
9: ; preds = %1
%10 = load i8, i8* %2
%11 = tail call i32 @fprintf(i32* %4, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8 signext %10)
%12 = tail call i32 @fclose(i32* %4)
%13 = tail call i32 @pthread_exit(i8* %0)
ret i8* undef
}