#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 user_desc {
  int entry_number;
  int base_addr;
  int limit;
};

/* Variables and functions */
unsigned long get_fs();
int /*<<< orphan*/ get_thread_area(struct user_desc *);
int /*<<< orphan*/ memset(struct user_desc *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, int, ...);
int /*<<< orphan*/ set_thread_area(struct user_desc *);
int /*<<< orphan*/ usleep(int);
int prog(void) {
  unsigned long fs;
  struct user_desc ta;
  fs = get_fs();
  printf("FS: %lu\n", fs);
  ta.entry_number = 0;
  get_thread_area(&ta);
  printf("TA: entry=%d, base=%lu, length=%d\n", ta.entry_number, ta.base_addr,
         ta.limit);
  ta.entry_number = 1;
  get_thread_area(&ta);
  printf("TA: entry=%d, base=%lu, length=%d\n", ta.entry_number, ta.base_addr,
         ta.limit);
  ta.entry_number = 2;
  get_thread_area(&ta);
  printf("TA: entry=%d, base=%lu, length=%d\n", ta.entry_number, ta.base_addr,
         ta.limit);
  ta.entry_number = 3;
  get_thread_area(&ta);
  printf("TA: entry=%d, base=%lu, length=%d\n", ta.entry_number, ta.base_addr,
         ta.limit);
  ta.entry_number = 3;
  ta.base_addr = 0x10000;
  ta.limit = 100;
  set_thread_area(&ta);
  memset(&ta, 0, sizeof(struct user_desc));
  ta.entry_number = 3;
  get_thread_area(&ta);
  printf("TA: entry=%d, base=%lu, length=%d\n", ta.entry_number, ta.base_addr,
         ta.limit);
  ta.entry_number = 2;
  get_thread_area(&ta);
  printf("TA: entry=%d, base=%lu, length=%d\n", ta.entry_number, ta.base_addr,
         ta.limit);
  usleep(500000);
}


x86 assembly (clang, -O0):
<prog>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x0,%al
call   1060 <get_fs@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %eax,%esi
lea    0xe80(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x20(%rbp)
lea    -0x20(%rbp),%rdi
call   1030 <get_thread_area@plt>
mov    -0x20(%rbp),%esi
mov    -0x1c(%rbp),%edx
mov    -0x18(%rbp),%ecx
lea    0xe62(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x20(%rbp)
lea    -0x20(%rbp),%rdi
call   1030 <get_thread_area@plt>
mov    -0x20(%rbp),%esi
mov    -0x1c(%rbp),%edx
mov    -0x18(%rbp),%ecx
lea    0xe3b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x2,-0x20(%rbp)
lea    -0x20(%rbp),%rdi
call   1030 <get_thread_area@plt>
mov    -0x20(%rbp),%esi
mov    -0x1c(%rbp),%edx
mov    -0x18(%rbp),%ecx
lea    0xe14(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x3,-0x20(%rbp)
lea    -0x20(%rbp),%rdi
call   1030 <get_thread_area@plt>
mov    -0x20(%rbp),%esi
mov    -0x1c(%rbp),%edx
mov    -0x18(%rbp),%ecx
lea    0xded(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x3,-0x20(%rbp)
movl   $0x10000,-0x1c(%rbp)
movl   $0x64,-0x18(%rbp)
lea    -0x20(%rbp),%rdi
call   1070 <set_thread_area@plt>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
movl   $0x3,-0x20(%rbp)
lea    -0x20(%rbp),%rdi
call   1030 <get_thread_area@plt>
mov    -0x20(%rbp),%esi
mov    -0x1c(%rbp),%edx
mov    -0x18(%rbp),%ecx
lea    0xd98(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x2,-0x20(%rbp)
lea    -0x20(%rbp),%rdi
call   1030 <get_thread_area@plt>
mov    -0x20(%rbp),%esi
mov    -0x1c(%rbp),%edx
mov    -0x18(%rbp),%ecx
lea    0xd71(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x7a120,%edi
call   1080 <usleep@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<prog>:
push   %r14
push   %rbx
sub    $0x18,%rsp
xor    %eax,%eax
call   1060 <get_fs@plt>
lea    0xe8b(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
movl   $0x0,0x8(%rsp)
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
call   1030 <get_thread_area@plt>
mov    0x8(%rsp),%esi
mov    0xc(%rsp),%edx
mov    0x10(%rsp),%ecx
lea    0xe63(%rip),%r14
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
movl   $0x1,0x8(%rsp)
mov    %rbx,%rdi
call   1030 <get_thread_area@plt>
mov    0x8(%rsp),%esi
mov    0xc(%rsp),%edx
mov    0x10(%rsp),%ecx
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
movl   $0x2,0x8(%rsp)
mov    %rbx,%rdi
call   1030 <get_thread_area@plt>
mov    0x8(%rsp),%esi
mov    0xc(%rsp),%edx
mov    0x10(%rsp),%ecx
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
movl   $0x3,0x8(%rsp)
mov    %rbx,%rdi
call   1030 <get_thread_area@plt>
mov    0x8(%rsp),%esi
mov    0xc(%rsp),%edx
mov    0x10(%rsp),%ecx
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
movabs $0x1000000000003,%rax
mov    %rax,0x8(%rsp)
movl   $0x64,0x10(%rsp)
mov    %rbx,%rdi
call   1070 <set_thread_area@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
movl   $0x3,0x8(%rsp)
mov    %rbx,%rdi
call   1030 <get_thread_area@plt>
mov    0x8(%rsp),%esi
mov    0xc(%rsp),%edx
mov    0x10(%rsp),%ecx
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
movl   $0x2,0x8(%rsp)
mov    %rbx,%rdi
call   1030 <get_thread_area@plt>
mov    0x8(%rsp),%esi
mov    0xc(%rsp),%edx
mov    0x10(%rsp),%ecx
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x7a120,%edi
call   1080 <usleep@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.user_desc = type { i32, i32, i32 }
@.str = [9 x i8] c"FS: %lu\0A\00"
@.str.1 = [35 x i8] c"TA: entry=%d, base=%lu, length=%d\0A\00"
define i32 @prog() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca %struct.user_desc
%4 = call i64 (...) @get_fs()
store i64 %4, i64* %2
%5 = load i64, i64* %2
%6 = trunc i64 %5 to i32
%7 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = getelementptr inbounds %struct.user_desc, %struct.user_desc* %3, i32 0, i32 0
store i32 0, i32* %8
%9 = call i32 @get_thread_area(%struct.user_desc* %3)
%10 = getelementptr inbounds %struct.user_desc, %struct.user_desc* %3, i32 0, i32 0
%11 = load i32, i32* %10
%12 = getelementptr inbounds %struct.user_desc, %struct.user_desc* %3, i32 0, i32 1
%13 = load i32, i32* %12
%14 = getelementptr inbounds %struct.user_desc, %struct.user_desc* %3, i32 0, i32 2
%15 = load i32, i32* %14
%16 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i32 %11, i32 %13, i32 %15)
%17 = getelementptr inbounds %struct.user_desc, %struct.user_desc* %3, i32 0, i32 0
store i32 1, i32* %17
%18 = call i32 @get_thread_area(%struct.user_desc* %3)
%19 = getelementptr inbounds %struct.user_desc, %struct.user_desc* %3, i32 0, i32 0
%20 = load i32, i32* %19
%21 = getelementptr inbounds %struct.user_desc, %struct.user_desc* %3, i32 0, i32 1
%22 = load i32, i32* %21
%23 = getelementptr inbounds %struct.user_desc, %struct.user_desc* %3, i32 0, i32 2
%24 = load i32, i32* %23
%25 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i32 %20, i32 %22, i32 %24)
%26 = getelementptr inbounds %struct.user_desc, %struct.user_desc* %3, i32 0, i32 0
store i32 2, i32* %26
%27 = call i32 @get_thread_area(%struct.user_desc* %3)
%28 = getelementptr inbounds %struct.user_desc, %struct.user_desc* %3, i32 0, i32 0
%29 = load i32, i32* %28
%30 = getelementptr inbounds %struct.user_desc, %struct.user_desc* %3, i32 0, i32 1
%31 = load i32, i32* %30
%32 = getelementptr inbounds %struct.user_desc, %struct.user_desc* %3, i32 0, i32 2
%33 = load i32, i32* %32
%34 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i32 %29, i32 %31, i32 %33)
%35 = getelementptr inbounds %struct.user_desc, %struct.user_desc* %3, i32 0, i32 0
store i32 3, i32* %35
%36 = call i32 @get_thread_area(%struct.user_desc* %3)
%37 = getelementptr inbounds %struct.user_desc, %struct.user_desc* %3, i32 0, i32 0
%38 = load i32, i32* %37
%39 = getelementptr inbounds %struct.user_desc, %struct.user_desc* %3, i32 0, i32 1
%40 = load i32, i32* %39
%41 = getelementptr inbounds %struct.user_desc, %struct.user_desc* %3, i32 0, i32 2
%42 = load i32, i32* %41
%43 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i32 %38, i32 %40, i32 %42)
%44 = getelementptr inbounds %struct.user_desc, %struct.user_desc* %3, i32 0, i32 0
store i32 3, i32* %44
%45 = getelementptr inbounds %struct.user_desc, %struct.user_desc* %3, i32 0, i32 1
store i32 65536, i32* %45
%46 = getelementptr inbounds %struct.user_desc, %struct.user_desc* %3, i32 0, i32 2
store i32 100, i32* %46
%47 = call i32 @set_thread_area(%struct.user_desc* %3)
%48 = call i32 @memset(%struct.user_desc* %3, i32 0, i32 12)
%49 = getelementptr inbounds %struct.user_desc, %struct.user_desc* %3, i32 0, i32 0
store i32 3, i32* %49
%50 = call i32 @get_thread_area(%struct.user_desc* %3)
%51 = getelementptr inbounds %struct.user_desc, %struct.user_desc* %3, i32 0, i32 0
%52 = load i32, i32* %51
%53 = getelementptr inbounds %struct.user_desc, %struct.user_desc* %3, i32 0, i32 1
%54 = load i32, i32* %53
%55 = getelementptr inbounds %struct.user_desc, %struct.user_desc* %3, i32 0, i32 2
%56 = load i32, i32* %55
%57 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i32 %52, i32 %54, i32 %56)
%58 = getelementptr inbounds %struct.user_desc, %struct.user_desc* %3, i32 0, i32 0
store i32 2, i32* %58
%59 = call i32 @get_thread_area(%struct.user_desc* %3)
%60 = getelementptr inbounds %struct.user_desc, %struct.user_desc* %3, i32 0, i32 0
%61 = load i32, i32* %60
%62 = getelementptr inbounds %struct.user_desc, %struct.user_desc* %3, i32 0, i32 1
%63 = load i32, i32* %62
%64 = getelementptr inbounds %struct.user_desc, %struct.user_desc* %3, i32 0, i32 2
%65 = load i32, i32* %64
%66 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i32 %61, i32 %63, i32 %65)
%67 = call i32 @usleep(i32 500000)
%68 = load i32, i32* %1
ret i32 %68
}

LLVM IR (-O2):
%struct.user_desc = type { i32, i32, i32 }
@.str = [9 x i8] c"FS: %lu\0A\00"
@.str.1 = [35 x i8] c"TA: entry=%d, base=%lu, length=%d\0A\00"
define i32 @prog() {
%1 = alloca %struct.user_desc
%2 = bitcast %struct.user_desc* %1 to i8*
%3 = tail call i64 (...) @get_fs()
%4 = trunc i64 %3 to i32
%5 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = getelementptr inbounds %struct.user_desc, %struct.user_desc* %1, i64 0, i32 0
store i32 0, i32* %6
%7 = call i32 @get_thread_area(%struct.user_desc* %1)
%8 = load i32, i32* %6
%9 = getelementptr inbounds %struct.user_desc, %struct.user_desc* %1, i64 0, i32 1
%10 = load i32, i32* %9
%11 = getelementptr inbounds %struct.user_desc, %struct.user_desc* %1, i64 0, i32 2
%12 = load i32, i32* %11
%13 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i32 %8, i32 %10, i32 %12)
store i32 1, i32* %6
%14 = call i32 @get_thread_area(%struct.user_desc* %1)
%15 = load i32, i32* %6
%16 = load i32, i32* %9
%17 = load i32, i32* %11
%18 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i32 %15, i32 %16, i32 %17)
store i32 2, i32* %6
%19 = call i32 @get_thread_area(%struct.user_desc* %1)
%20 = load i32, i32* %6
%21 = load i32, i32* %9
%22 = load i32, i32* %11
%23 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i32 %20, i32 %21, i32 %22)
store i32 3, i32* %6
%24 = call i32 @get_thread_area(%struct.user_desc* %1)
%25 = load i32, i32* %6
%26 = load i32, i32* %9
%27 = load i32, i32* %11
%28 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i32 %25, i32 %26, i32 %27)
store i32 3, i32* %6
store i32 65536, i32* %9
store i32 100, i32* %11
%29 = call i32 @set_thread_area(%struct.user_desc* %1)
%30 = call i32 @memset(%struct.user_desc* %1, i32 0, i32 12)
store i32 3, i32* %6
%31 = call i32 @get_thread_area(%struct.user_desc* %1)
%32 = load i32, i32* %6
%33 = load i32, i32* %9
%34 = load i32, i32* %11
%35 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i32 %32, i32 %33, i32 %34)
store i32 2, i32* %6
%36 = call i32 @get_thread_area(%struct.user_desc* %1)
%37 = load i32, i32* %6
%38 = load i32, i32* %9
%39 = load i32, i32* %11
%40 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i32 %37, i32 %38, i32 %39)
%41 = call i32 @usleep(i32 500000)
ret i32 undef
}