#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*/ SEEK_SET;
int atoi(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ pthread_exit(int /*<<< orphan*/ *);
void *thr_add(void *p) {
  FILE *fp;
  char linebuf[1024];
  fp = fopen("/tmp/out", "r+");
  if (NULL == fp) {
    perror("fopen()");
    exit(1);
  }
  fgets(linebuf, 1024, fp);
  fseek(fp, 0, SEEK_SET);
  fprintf(fp, "%d\n", atoi(linebuf) + 1);
  fclose(fp);
  pthread_exit(NULL);
}


x86 assembly (clang, -O0):
<thr_add>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xe5a(%rip),%rdi
lea    0xe5c(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
xor    %eax,%eax
cmp    -0x18(%rbp),%rax
jne    11d8 <thr_add+0x48>
lea    0xe43(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    -0x420(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x400,%esi
call   1040 <fgets@plt>
mov    -0x18(%rbp),%rdi
mov    0x2de8(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x428(%rbp)
lea    -0x420(%rbp),%rdi
call   10a0 <atoi@plt>
mov    -0x428(%rbp),%rdi
mov    %eax,%edx
add    $0x1,%edx
lea    0xde9(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <pthread_exit@plt>
mov    -0x8(%rbp),%rax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<thr_add>:
push   %r14
push   %rbx
sub    $0x408,%rsp
lea    0xe5f(%rip),%rdi
lea    0xe61(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     120f <thr_add+0x7f>
mov    %rax,%rbx
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x400,%esi
mov    %rax,%rdx
call   1040 <fgets@plt>
mov    0x2e11(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
mov    %r14,%rdi
call   10a0 <atoi@plt>
lea    0x1(%rax),%edx
lea    0xe27(%rip),%rsi
mov    %rbx,%rdi
call   1050 <fprintf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %edi,%edi
call   1060 <pthread_exit@plt>
add    $0x408,%rsp
pop    %rbx
pop    %r14
ret
lea    0xdf6(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"/tmp/out\00"
@.str.1 = [3 x i8] c"r+\00"
@.str.2 = [8 x i8] c"fopen()\00"
@SEEK_SET = global i32 0
@.str.3 = [4 x i8] c"%d\0A\00"
define i8* @thr_add(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca [1024 x i8]
store i8* %0, i8** %3
%6 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %6, i32** %4
%7 = load i32*, i32** %4
%8 = icmp eq i32* null, %7
br i1 %8, label %9, label %12
9: ; preds = %1
%10 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%11 = call i32 @exit(i32 1)
unreachable
12: ; preds = %1
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%14 = load i32*, i32** %4
%15 = call i32 @fgets(i8* %13, i32 1024, i32* %14)
%16 = load i32*, i32** %4
%17 = load i32, i32* @SEEK_SET
%18 = call i32 @fseek(i32* %16, i32 0, i32 %17)
%19 = load i32*, i32** %4
%20 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%21 = call i32 @atoi(i8* %20)
%22 = add nsw i32 %21, 1
%23 = call i32 @fprintf(i32* %19, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %22)
%24 = load i32*, i32** %4
%25 = call i32 @fclose(i32* %24)
%26 = call i32 @pthread_exit(i32* null)
%27 = load i8*, i8** %2
ret i8* %27
}

LLVM IR (-O2):
@.str = [9 x i8] c"/tmp/out\00"
@.str.1 = [3 x i8] c"r+\00"
@.str.2 = [8 x i8] c"fopen()\00"
@SEEK_SET = global i32 0
@.str.3 = [4 x i8] c"%d\0A\00"
define noalias i8* @thr_add(i8* nocapture readnone %0) {
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%4 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 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 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %1
%10 = call i32 @fgets(i8* %3, i32 1024, i32* %4)
%11 = load i32, i32* @SEEK_SET
%12 = call i32 @fseek(i32* %4, i32 0, i32 %11)
%13 = call i32 @atoi(i8* %3)
%14 = add nsw i32 %13, 1
%15 = call i32 @fprintf(i32* %4, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %14)
%16 = call i32 @fclose(i32* %4)
%17 = call i32 @pthread_exit(i32* null)
ret i8* undef
}