#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 dummy;
};
typedef int key_t;

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC;
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ memcpy(char *, struct timespec *, size_t);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ pthread_exit(void *);
char *shmat(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int shmget(int, size_t, int);
void *pthread_create_fn(void *arg) {
  struct timespec child_end;
  clock_gettime(CLOCK_MONOTONIC, &child_end);
  size_t sz = sizeof(struct timespec);
  int shmid;
  key_t key;
  char *shm;
  key = 5678;
  shm = NULL;
  if ((shmid = shmget(key, sz, 0666)) < 0) {
    perror("shmget");
  }
  if ((shm = shmat(shmid, NULL, 0)) == (char *)-1) {
    perror("shmat");
  }
  memcpy(shm, &child_end, sz);
  pthread_exit((void *)0);
}


x86 assembly (clang, -O0):
<pthread_create_fn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e75(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
call   1030 <clock_gettime@plt>
movq   $0x4,-0x20(%rbp)
movl   $0x162e,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x28(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    $0x1b6,%edx
call   1080 <shmget@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    11be <pthread_create_fn+0x5e>
lea    0xe47(%rip),%rdi
call   1060 <perror@plt>
mov    -0x24(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1070 <shmat@plt>
mov    %rax,-0x30(%rbp)
mov    $0xffffffffffffffff,%rcx
cmp    %rcx,%rax
jne    11ec <pthread_create_fn+0x8c>
lea    0xe20(%rip),%rdi
call   1060 <perror@plt>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    -0x18(%rbp),%rsi
call   1040 <memcpy@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1050 <pthread_exit@plt>
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pthread_create_fn>:
push   %rbx
sub    $0x10,%rsp
mov    0x2e7c(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
call   1030 <clock_gettime@plt>
mov    $0x4,%esi
mov    $0x162e,%edi
mov    $0x1b6,%edx
call   1080 <shmget@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11c5 <pthread_create_fn+0x65>
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1070 <shmat@plt>
mov    %rax,%rbx
cmp    $0xffffffffffffffff,%rax
je     11d3 <pthread_create_fn+0x73>
lea    0x8(%rsp),%rsi
mov    $0x4,%edx
mov    %rbx,%rdi
call   1040 <memcpy@plt>
xor    %edi,%edi
call   1050 <pthread_exit@plt>
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe34(%rip),%rdi
call   1060 <perror@plt>
jmp    1192 <pthread_create_fn+0x32>
lea    0xe2d(%rip),%rdi
call   1060 <perror@plt>
jmp    11a6 <pthread_create_fn+0x46>

LLVM IR (-O0):
%struct.timespec = type { i32 }
@CLOCK_MONOTONIC = global i32 0
@.str = [7 x i8] c"shmget\00"
@.str.1 = [6 x i8] c"shmat\00"
define i8* @pthread_create_fn(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca %struct.timespec
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
store i8* %0, i8** %3
%9 = load i32, i32* @CLOCK_MONOTONIC
%10 = call i32 @clock_gettime(i32 %9, %struct.timespec* %4)
store i64 4, i64* %5
store i32 5678, i32* %7
store i8* null, i8** %8
%11 = load i32, i32* %7
%12 = load i64, i64* %5
%13 = call i32 @shmget(i32 %11, i64 %12, i32 438)
store i32 %13, i32* %6
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %1
%16 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %15, %1
%18 = load i32, i32* %6
%19 = call i8* @shmat(i32 %18, i32* null, i32 0)
store i8* %19, i8** %8
%20 = icmp eq i8* %19, inttoptr (i64 -1 to i8*)
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %23
23: ; preds = %21, %17
%24 = load i8*, i8** %8
%25 = load i64, i64* %5
%26 = call i32 @memcpy(i8* %24, %struct.timespec* %4, i64 %25)
%27 = call i32 @pthread_exit(i8* null)
%28 = load i8*, i8** %2
ret i8* %28
}

LLVM IR (-O2):
%struct.timespec = type { i32 }
@CLOCK_MONOTONIC = global i32 0
@.str = [7 x i8] c"shmget\00"
@.str.1 = [6 x i8] c"shmat\00"
define noalias i8* @pthread_create_fn(i8* nocapture readnone %0) {
%2 = alloca %struct.timespec
%3 = bitcast %struct.timespec* %2 to i8*
%4 = load i32, i32* @CLOCK_MONOTONIC
%5 = call i32 @clock_gettime(i32 %4, %struct.timespec* %2)
%6 = call i32 @shmget(i32 5678, i64 4, i32 438)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %8, %1
%11 = call i8* @shmat(i32 %6, i32* null, i32 0)
%12 = icmp eq i8* %11, inttoptr (i64 -1 to i8*)
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %15
15: ; preds = %13, %10
%16 = call i32 @memcpy(i8* %11, %struct.timespec* %2, i64 4)
%17 = call i32 @pthread_exit(i8* null)
ret i8* undef
}